Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. 'use strict';
  2.  
  3. // ----- global
  4. let settings = {};
  5.  
  6. browser.runtime.onMessage.addListener(s => settings = s);
  7.  
  8. function logToUI(log) { browser.runtime.sendMessage(log); }
  9.  
  10.  
  11. function FindProxyForURL(url, host) { // The URL being accessed. The path and query components of https:// URLs are stripped.
  12.  
  13. switch (settings.mode) {
  14. // not supported at the moment
  15. case 'random':
  16. case 'roundrobin':
  17. return [{type: 'direct'}];
  18.  
  19. case 'patterns':
  20.  
  21. const proxyMatch = findProxyMatch(url); // |url| contains port, if any, but |host| does not.
  22.  
  23. if (proxyMatch) {
  24. return [prepareSetting(url, proxyMatch.proxy, proxyMatch.pattern)];
  25. }
  26. else {
  27. // logToUI({type: 'log', url, timestamp: Date.now()});
  28. return [{type: 'direct'}]; // default
  29. }
  30.  
  31. default:
  32. // Use proxy "xxxx" for all URLs // const USE_PROXY_FOR_ALL_URLS = 2;
  33. return [prepareSetting(url, settings.proxySettings[0], 'all')]; // the first proxy
  34. }
  35. }
  36.  
  37. const schemeSet = { // converting to meaningful terms
  38. all : 1,
  39. http: 2,
  40. https: 4
  41. };
  42.  
  43. function findProxyMatch(url) {
  44. // note: we've already thrown out inactive settings and inactive patterns in background.js.
  45. // we're not iterating over them
  46.  
  47. const [scheme, hostPort] = url.split('://');
  48. for (const proxy of settings.proxySettings) {
  49.  
  50. // Check black patterns first
  51. const blackMatch = proxy.blackPatterns.find(item =>
  52. (item.protocols === schemeSet.all || item.protocols === schemeSet[scheme]) &&
  53. new RegExp(item['regExp'], 'i').test(hostPort));
  54.  
  55. //if (blackMatch) { return null; } // found a blacklist match, end here, use direct, no proxy
  56. if (blackMatch) { continue; } // if blacklist matched move to the next proxy
  57.  
  58. const whiteMatch = proxy.whitePatterns.find(item =>
  59. (item.protocols === schemeSet.all || item.protocols === schemeSet[scheme]) &&
  60. new RegExp(item['regExp'], 'i').test(hostPort));
  61.  
  62. if (whiteMatch) { return {proxy, pattern: whiteMatch}; } // found a whitelist match, end here
  63. }
  64.  
  65. return null; // no black or white matches
  66. }
  67.  
  68. const typeSet = {
  69. 1: 'http', // PROXY_TYPE_HTTP
  70. 2: 'https', // PROXY_TYPE_HTTPS
  71. 3: 'socks', // PROXY_TYPE_SOCKS5
  72. 4: 'socks4', // PROXY_TYPE_SOCKS4
  73. 5: 'direct' // PROXY_TYPE_NONE
  74. };
  75.  
  76. function prepareSetting(url, proxy, matchedPattern) {
  77.  
  78. const ret = {
  79. type: typeSet[proxy.type] || null,
  80. host: proxy.address,
  81. port: proxy.port
  82. };
  83. proxy.username && (ret.username = proxy.username);
  84. proxy.password && (ret.password = proxy.password);
  85. proxy.proxyDNS && (ret.proxyDNS = proxy.proxyDNS);
  86.  
  87. // trim the log data to what is needed
  88. const log = {
  89. type: 'log',
  90. url,
  91. title: proxy.title,
  92. color: proxy.color,
  93. address: proxy.address,
  94. matchedPattern,
  95. timestamp: Date.now()
  96. };
  97. logToUI(log);
  98. return ret;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement