PageRenderTime 41ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1. // Copyright (c) 2010 Moxie Marlinspike <moxie@thoughtcrime.org>
  2. // This program is free software; you can redistribute it and/or
  3. // modify it under the terms of the GNU General Public License as
  4. // published by the Free Software Foundation; either version 3 of the
  5. // License, or (at your option) any later version.
  6. // This program is distributed in the hope that it will be useful, but
  7. // WITHOUT ANY WARRANTY; without even the implied warranty of
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. // General Public License for more details.
  10. // You should have received a copy of the GNU General Public License
  11. // along with this program; if not, write to the Free Software
  12. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  13. // USA
  14. function LocalProxy(remoteProxy) {
  15. this.remoteProxy = remoteProxy;
  16. this.serverSocket = null;
  17. this.proxyInfo = null;
  18. this.tunnelProxy = null;
  19. this.terminated = false;
  20. this.constructServerSocket();
  21. this.initializeProxyInfo();
  22. }
  23. LocalProxy.prototype.setProxyTunnel = function(tunnelProxy) {
  24. this.tunnelProxy = tunnelProxy;
  25. };
  26. LocalProxy.prototype.getInterfaceLanguage = function() {
  27. return this.remoteProxy.getInterfaceLanguage();
  28. };
  29. LocalProxy.prototype.getSearchLanguage = function() {
  30. return this.remoteProxy.getSearchLanguage();
  31. };
  32. LocalProxy.prototype.shutdown = function() {
  33. this.terminated = true;
  34. this.serverSocket.close();
  35. };
  36. LocalProxy.prototype.getProxyInfo = function() {
  37. return this.proxyInfo;
  38. };
  39. LocalProxy.prototype.matchesURL = function(url) {
  40. return this.remoteProxy.matchesURL(url);
  41. };
  42. LocalProxy.prototype.constructServerSocket = function() {
  43. try {
  44. this.serverSocket = Components.classes["@mozilla.org/network/server-socket;1"].createInstance(Components.interfaces.nsIServerSocket);
  45. this.serverSocket.init(-1,true,-1);
  46. this.serverSocket.asyncListen(this);
  47. } catch (e) {
  48. dump("Reconstruction failed: " + e + "\n");
  49. }
  50. };
  51. LocalProxy.prototype.initializeProxyInfo = function() {
  52. var proxyService = Components.classes["@mozilla.org/network/protocol-proxy-service;1"].getService(Components.interfaces.nsIProtocolProxyService);
  53. if (this.remoteProxy.isSSLEnabled()) {
  54. this.proxyInfo = proxyService.newProxyInfo("http", "localhost", this.serverSocket.port,
  55. 1, 0, null);
  56. } else {
  57. this.proxyInfo = proxyService.newProxyInfo("http", this.remoteProxy.getHost(),
  58. this.remoteProxy.getHTTPPort(),
  59. 1, 0, null);
  60. }
  61. };
  62. LocalProxy.prototype.connectToRemoteProxy = function() {
  63. var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  64. return transportService.createTransport(['ssl'],1,this.remoteProxy.getHost(), this.remoteProxy.getSSLPort(),this.tunnelProxy);
  65. };
  66. LocalProxy.prototype.onSocketAccepted = function(serverSocket, clientTransport) {
  67. var serverTransport = this.connectToRemoteProxy();
  68. var dataShuffler = new DataShuffler(clientTransport, serverTransport);
  69. dataShuffler.shuffle();
  70. };
  71. LocalProxy.prototype.onStopListening = function(serverSocket, status) {
  72. if (!this.terminated) {
  73. dump("onStopListening -- reconstructing socket...\n");
  74. // this.constructServerSocket();
  75. // this.initializeProxyInfo();
  76. }
  77. };