Firefox Addon
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

179 linhas
4.9KB

  1. const DEBUG = 0;
  2. const DIRECT_PROXY = {
  3. type: 'direct'
  4. };
  5. const DEFAULT_PROXY_SETTINGS = {
  6. type: 'direct',
  7. host: '',
  8. port: 0,
  9. username: '',
  10. password: '',
  11. proxyDNS: false
  12. };
  13. let proxies = [ DIRECT_PROXY, DIRECT_PROXY ];
  14. var skipLocal = 1;
  15. var pendingRequests = [];
  16. var currentProxy = 0;
  17. function buttonClicked() {
  18. currentProxy = (currentProxy ? 0 : 1);
  19. if ( proxies[1].type == 'direct' ){
  20. currentProxy = 0;
  21. }
  22. browser.storage.local.set({ currentProxy: currentProxy });
  23. updateState();
  24. }
  25. function updateState() {
  26. if(currentProxy==0)
  27. {
  28. browser.browserAction.setIcon({path: "icons/unlock48.png"});
  29. browser.browserAction.setTitle({title: "Local IP"});
  30. }
  31. else
  32. {
  33. browser.browserAction.setIcon({path: "icons/lock48.png"});
  34. browser.browserAction.setTitle({title: "Biukop Secure Internet"});
  35. }
  36. }
  37. function decodepass(pass) {
  38. var result = atob(pass);
  39. if (result.length <=3)
  40. return pass;
  41. if ("#!" != result.substring(result.length-3,result.length-1))
  42. return pass;
  43. var tail = Number(result.substring(2,3));
  44. result = result.substring(5, result.length - tail);
  45. return result;
  46. }
  47. function settingsChanged(settings) {
  48. if ("proxySettings" in settings){
  49. proxies[1] = settings.proxySettings.newValue;
  50. proxies[1].password = decodepass(proxies[1].password);
  51. }
  52. if ("skipLocal" in settings){
  53. skipLocal = settings.skipLocal.newValue;
  54. }
  55. if ("currentProxy" in settings) {
  56. currentProxy = settings.currentProxy.newValue;
  57. updateState();
  58. }
  59. }
  60. function completed(requestDetails) {
  61. if (DEBUG) {
  62. console.log("completed request: " + requestDetails.requestId);
  63. }
  64. var index = pendingRequests.indexOf(requestDetails.requestId);
  65. if (index > -1) {
  66. pendingRequests.splice(index, 1);
  67. }
  68. console.log("completed");
  69. }
  70. function provideCredentialsSync(requestDetails) {
  71. console.log("check credential");
  72. if (!requestDetails.isProxy)
  73. return;
  74. if (!currentProxy == 1)
  75. return;
  76. if (pendingRequests.indexOf(requestDetails.requestId) != -1) {
  77. //if we've seen the request before, assume bad credentials and give up
  78. console.log("Bad proxy credentials for request: " + requestDetails.requestId);
  79. return {cancel:true};
  80. }
  81. var credentials = {
  82. username: proxies[1].username,
  83. password: proxies[1].password
  84. }
  85. pendingRequests.push(requestDetails.requestId);
  86. if (DEBUG) {
  87. console.log(`Providing proxy credentials for request: ${requestDetails.requestId} username: ${credentials.username}`);
  88. }
  89. return {authCredentials: credentials};
  90. }
  91. function isLocalIPv4(host)
  92. {
  93. var octets = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(host);
  94. if(!octets)
  95. return false;
  96. if(octets[1]>255||octets[2]>255||octets[3]>255||octets[4]>255)
  97. return false;
  98. if(octets[1]==10||octets[1]==127) //class A or local loopback
  99. return true;
  100. if(octets[1]==172&&octets[2]>=16&&octets[2]<=31) //class B
  101. return true;
  102. if(octets[1]==192&&octets[2]==168) //class C
  103. return true;
  104. return false;
  105. }
  106. function isLocal(host)
  107. {
  108. if(host.indexOf('biukop') != -1)
  109. return true;
  110. if(host.indexOf('.') == -1)
  111. return true;
  112. if(host.endsWith(".local"))
  113. return true;
  114. if(host=="::1")
  115. return true;
  116. return(isLocalIPv4(host));
  117. }
  118. function handleProxyRequest(requestInfo) {
  119. const url = new URL(requestInfo.url);
  120. var host = url.hostname;
  121. var proxyNum = currentProxy;
  122. if (skipLocal) {
  123. if(isLocal(host)) {
  124. if (DEBUG)
  125. console.log(`Local host detected: ${host}`);
  126. proxyNum = 0;
  127. }
  128. }
  129. if (DEBUG) {
  130. console.log(`Proxying: ${url.hostname}`);
  131. console.log(proxies[proxyNum]);
  132. }
  133. return(proxies[proxyNum]);
  134. }
  135. browser.storage.local.get({ currentProxy: 0, skipLocal: true, proxySettings: DEFAULT_PROXY_SETTINGS }, items=>{
  136. currentProxy = items.currentProxy;
  137. skipLocal = items.skipLocal;
  138. proxies[1] = items.proxySettings;
  139. proxies[1].password = decodepass(proxies[1].password);
  140. updateState();
  141. });
  142. browser.storage.onChanged.addListener(settingsChanged);
  143. browser.browserAction.onClicked.addListener(buttonClicked);
  144. browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]});
  145. browser.proxy.onError.addListener(error => {
  146. console.error(`Proxy error: ${error.message}`);
  147. });
  148. browser.webRequest.onAuthRequired.addListener(
  149. provideCredentialsSync,
  150. {urls: ["<all_urls>"]},
  151. ["blocking"]
  152. );
  153. browser.webRequest.onCompleted.addListener(
  154. completed,
  155. {urls: ["<all_urls>"]}
  156. );
  157. browser.webRequest.onErrorOccurred.addListener(
  158. completed,
  159. {urls: ["<all_urls>"]}
  160. );