aboutsummaryrefslogtreecommitdiff
path: root/chrome
diff options
context:
space:
mode:
authorEinar Egilsson2011-08-28 23:31:04 +0200
committerEinar Egilsson2011-08-28 23:31:04 +0200
commit4fc435047afa02cf4be96a398c1a06c377901aba (patch)
treec206c0c6aa2a3f013e1dec99dd0cff238925bd23 /chrome
parent3ba13e5bcb4177abc3aaef2da37c2b99befd6c88 (diff)
Prototype of redirecting proxy server
Diffstat (limited to 'chrome')
-rw-r--r--chrome/code/proxyserver.js58
-rw-r--r--chrome/code/redirector.js54
-rw-r--r--chrome/code/redirectorprefs.js5
-rw-r--r--chrome/code/xpcom.js13
4 files changed, 84 insertions, 46 deletions
diff --git a/chrome/code/proxyserver.js b/chrome/code/proxyserver.js
new file mode 100644
index 0000000..5d4f1d6
--- /dev/null
+++ b/chrome/code/proxyserver.js
@@ -0,0 +1,58 @@
+Components.utils.import("chrome://redirector/content/code/xpcom.js");
+
+var EXPORTED_SYMBOLS = ['RedirectorProxy'];
+
+var RedirectorProxy = {
+
+ start : function(port, getUrl) {
+ dump('Opening Proxy Server Socket on port ' + port);
+ this.getUrl = getUrl;
+ this.serverSocket = new ServerSocket(port, true, -1);
+ this.serverSocket.asyncListen(this);
+ },
+
+ onSocketAccepted: function(serverSocket, clientSocket) {
+ dump("Accepted connection on "+clientSocket.host+":"+clientSocket.port);
+ var requestStream = clientSocket.openInputStream(0, 0, 0).QueryInterface(Ci.nsIAsyncInputStream);
+ var responseStream = clientSocket.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
+ var tm = Cc["@mozilla.org/thread-manager;1"].getService();
+ requestStream.asyncWait({
+ onInputStreamReady : function(inputStream) {
+ RedirectorProxy.processRequest(clientSocket, inputStream, responseStream);
+ }
+ },0,0,tm.mainThread);
+ },
+
+ processRequest : function(clientSocket, inputStream, responseStream) {
+ var requestStream = new ScriptableInputStream(inputStream);
+ requestStream.available();
+ var request = '';
+ while (requestStream.available()) {
+ request = request + requestStream.read(2048);
+ }
+ var parts = request.split(' ');
+ dump('\n\n\n' + request + '\n\n\n');
+ dump("\n" + parts[0] + " request for " + parts[1]);
+ var redirectUrl = 'http://einaregilsson.com';//Redirector.getRedirectUrl(parts[1]);
+ var outp = 'HTTP/1.1 302 Moved Temporarily';
+ outp += '\r\nContent-Length: <cl>';
+ outp += '\r\nLocation: ' + redirectUrl;
+ outp += '\r\nX-Redirected-By: Redirector Firefox Extension'
+ outp += '\r\n\r\n';
+ var cl = outp.length -4;
+ if (cl < 100) {
+ cl+=2;
+ } else if (cl < 1000) {
+ cl += 3;
+ } else if (cl < 10000) {
+ cl += 4;
+ } else if (cl < 100000) {
+ cl += 5;
+ }
+ outp = outp.replace('<cl>', cl);
+ dump(outp);
+ responseStream.write(outp, outp.length);
+ responseStream.close();
+ inputStream.close();
+ }
+} \ No newline at end of file
diff --git a/chrome/code/redirector.js b/chrome/code/redirector.js
index 19b0f0c..5c6fe81 100644
--- a/chrome/code/redirector.js
+++ b/chrome/code/redirector.js
@@ -1,15 +1,9 @@
-var EXPORTED_SYMBOLS = ['Redirector', 'rdump'];
-Ci = Components.interfaces;
-Cc = Components.classes;
-Ci = Components.interfaces;
-Cr = Components.results;
-
-const FileInputStream = Components.Constructor("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init");
-const ConverterInputStream = Components.Constructor("@mozilla.org/intl/converter-input-stream;1", "nsIConverterInputStream", "init");
-const LocalFile = Components.Constructor("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");
-
+Components.utils.import("chrome://redirector/content/code/xpcom.js");
Components.utils.import("chrome://redirector/content/code/redirect.js");
Components.utils.import("chrome://redirector/content/code/redirectorprefs.js");
+Components.utils.import("chrome://redirector/content/code/proxyserver.js");
+
+var EXPORTED_SYMBOLS = ['Redirector', 'rdump'];
function rdump(msg) {
//dump(msg + '\n');
@@ -156,36 +150,6 @@ Redirector = {
this._prefs.version = currentVersion;
},
- importOldRedirects : function(file) {
- var fileStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
- fileStream.init(file, 0x01, 0444, 0); //TODO: Find the actual constants for these magic numbers
-
- var stream = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(Ci.nsIConverterInputStream);
- stream.init(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
- stream = stream.QueryInterface(Ci.nsIUnicharLineInputStream);
-
-
-
- var importCount = 0, existsCount = 0;
- var lines = [];
- var line = {value: null};
- stream.readLine(line);
- while (line.value) {
- var redirect = new Redirect();
- redirect.deserialize(line.value.replace('\n', ''));
- if (this._containsRedirect(redirect)) {
- existsCount++;
- } else {
- this._list.push(redirect);
- importCount++;
- }
- stream.readLine(line);
- }
- stream.close();
- this.save();
- return importCount | (existsCount << 16);
- },
-
importRedirects : function(file) {
var fileStream = new FileInputStream(file, 0x01, 0444, 0);
var stream = new ConverterInputStream(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
@@ -351,6 +315,11 @@ Redirector = {
if (redirectsFile.exists()) {
this.importRedirects(redirectsFile);
}
+
+ RedirectorProxy.start(this._prefs.proxyServerPort);
+ rdump('Registering as Proxy Filter');
+ //var pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService(Ci.nsIProtocolProxyService);
+ //pps.registerFilter(this, 0);
},
_loadStrings : function() {
@@ -360,11 +329,6 @@ Redirector = {
var stringBundleService = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
this._strings = stringBundleService.createBundle(src, appLocale);
},
-
- _redirectsAsString : function(seperator) {
- return [r.serialize() for each (r in this._list)].join(seperator);
- },
-
_containsRedirect : function(redirect) {
for each (var existing in this._list) {
diff --git a/chrome/code/redirectorprefs.js b/chrome/code/redirectorprefs.js
index 3920539..40e8737 100644
--- a/chrome/code/redirectorprefs.js
+++ b/chrome/code/redirectorprefs.js
@@ -14,7 +14,7 @@ RedirectorPrefs.prototype = {
_debugEnabled : null,
_defaultDir : null,
_redirects : null,
-
+ _proxyServerPort : null,
_prefBranch : null,
_listeners : null,
@@ -36,6 +36,9 @@ RedirectorPrefs.prototype = {
get debugEnabled() { return this._debugEnabled; },
set debugEnabled(value) { this._prefBranch.setBoolPref('debugEnabled', value); },
+ get proxyServerPort() { return this._proxyServerPort; },
+ set proxyServerPort(value) { this._prefBranch.setIntPref('proxyServerPort', value); },
+
get defaultDir() { return this._defaultDir; },
set defaultDir(value) { this._prefBranch.setCharPref('defaultDir', value); },
diff --git a/chrome/code/xpcom.js b/chrome/code/xpcom.js
new file mode 100644
index 0000000..ec2232f
--- /dev/null
+++ b/chrome/code/xpcom.js
@@ -0,0 +1,13 @@
+
+var EXPORTED_SYMBOLS = [
+'Ci', 'Cc', 'Cr', 'ServerSocket', 'ScriptableInputStream', 'FileInputStream', 'ConverterInputStream', 'LocalFile'];
+
+Ci = Components.interfaces;
+Cc = Components.classes;
+Cr = Components.results;
+
+const ServerSocket = Components.Constructor("@mozilla.org/network/server-socket;1", "nsIServerSocket", "init");
+const ScriptableInputStream = Components.Constructor("@mozilla.org/scriptableinputstream;1", "nsIScriptableInputStream", "init");
+const FileInputStream = Components.Constructor("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init");
+const ConverterInputStream = Components.Constructor("@mozilla.org/intl/converter-input-stream;1", "nsIConverterInputStream", "init");
+const LocalFile = Components.Constructor("@mozilla.org/file/local;1", "nsILocalFile", "initWithPath");