/mozilla-googlesharing-0.18/components/LocalProxy.js
JavaScript | 94 lines | 62 code | 17 blank | 15 comment | 3 complexity | 9fa2f06d5c50ccd653996dd609bb7074 MD5 | raw file
Possible License(s): GPL-3.0
- // Copyright (c) 2010 Moxie Marlinspike <moxie@thoughtcrime.org>
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the GNU General Public License as
- // published by the Free Software Foundation; either version 3 of the
- // License, or (at your option) any later version.
- // This program is distributed in the hope that it will be useful, but
- // WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- // General Public License for more details.
- // You should have received a copy of the GNU General Public License
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- // USA
- function LocalProxy(remoteProxy) {
- this.remoteProxy = remoteProxy;
- this.serverSocket = null;
- this.proxyInfo = null;
- this.tunnelProxy = null;
- this.terminated = false;
- this.constructServerSocket();
- this.initializeProxyInfo();
- }
- LocalProxy.prototype.setProxyTunnel = function(tunnelProxy) {
- this.tunnelProxy = tunnelProxy;
- };
- LocalProxy.prototype.getInterfaceLanguage = function() {
- return this.remoteProxy.getInterfaceLanguage();
- };
- LocalProxy.prototype.getSearchLanguage = function() {
- return this.remoteProxy.getSearchLanguage();
- };
- LocalProxy.prototype.shutdown = function() {
- this.terminated = true;
- this.serverSocket.close();
- };
- LocalProxy.prototype.getProxyInfo = function() {
- return this.proxyInfo;
- };
- LocalProxy.prototype.matchesURL = function(url) {
- return this.remoteProxy.matchesURL(url);
- };
- LocalProxy.prototype.constructServerSocket = function() {
- try {
- this.serverSocket = Components.classes["@mozilla.org/network/server-socket;1"].createInstance(Components.interfaces.nsIServerSocket);
-
- this.serverSocket.init(-1,true,-1);
- this.serverSocket.asyncListen(this);
- } catch (e) {
- dump("Reconstruction failed: " + e + "\n");
- }
- };
- LocalProxy.prototype.initializeProxyInfo = function() {
- var proxyService = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].getService(Components.interfaces.nsIProtocolProxyService);
- if (this.remoteProxy.isSSLEnabled()) {
- this.proxyInfo = proxyService.newProxyInfo("http", "localhost", this.serverSocket.port,
- 1, 0, null);
- } else {
- this.proxyInfo = proxyService.newProxyInfo("http", this.remoteProxy.getHost(),
- this.remoteProxy.getHTTPPort(),
- 1, 0, null);
- }
- };
- LocalProxy.prototype.connectToRemoteProxy = function() {
- var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
- return transportService.createTransport(['ssl'],1,this.remoteProxy.getHost(), this.remoteProxy.getSSLPort(),this.tunnelProxy);
- };
- LocalProxy.prototype.onSocketAccepted = function(serverSocket, clientTransport) {
- var serverTransport = this.connectToRemoteProxy();
- var dataShuffler = new DataShuffler(clientTransport, serverTransport);
- dataShuffler.shuffle();
- };
- LocalProxy.prototype.onStopListening = function(serverSocket, status) {
- if (!this.terminated) {
- dump("onStopListening -- reconstructing socket...\n");
- // this.constructServerSocket();
- // this.initializeProxyInfo();
- }
- };