omega_webext_proxy_script.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. FindProxyForURL = (function () {
  2. var OmegaPac = require('omega-pac');
  3. var options = {};
  4. var state = {};
  5. var activeProfile = null;
  6. var fallbackResult = 'DIRECT';
  7. var pacCache = {};
  8. init();
  9. return FindProxyForURL;
  10. function FindProxyForURL(url, host, details) {
  11. if (!activeProfile) {
  12. warn('Warning: Proxy script not initialized on handling: ' + url);
  13. return fallbackResult;
  14. }
  15. // Moz: Neither path or query is included url regardless of scheme for now.
  16. // This is even more strict than Chromium restricting HTTPS URLs.
  17. // Therefore, it leads to different behavior than the icon and badge.
  18. // https://bugzilla.mozilla.org/show_bug.cgi?id=1337001
  19. var request = OmegaPac.Conditions.requestFromUrl(url);
  20. var profile = activeProfile;
  21. var matchResult, next;
  22. while (profile) {
  23. matchResult = OmegaPac.Profiles.match(profile, request)
  24. if (!matchResult) {
  25. if (profile.profileType === 'DirectProfile') {
  26. return 'DIRECT';
  27. } else {
  28. warn('Warning: Unsupported profile: ' + profile.profileType);
  29. return fallbackResult;
  30. }
  31. }
  32. if (Array.isArray(matchResult)) {
  33. next = matchResult[0];
  34. // TODO: Maybe also return user/pass if Mozilla supports it or it ends
  35. // up standardized in WebExtensions in the future.
  36. // MOZ: Mozilla has a bug tracked for user/pass in PAC return value.
  37. // https://bugzilla.mozilla.org/show_bug.cgi?id=1319641
  38. if (next.charCodeAt(0) !== 43) {
  39. // MOZ: SOCKS5 proxies are supported under the prefix SOCKS.
  40. // https://dxr.mozilla.org/mozilla-central/source/toolkit/components/extensions/ProxyScriptContext.jsm#178
  41. // Note: We have to replace this because MOZ won't process the rest of
  42. // the list if the syntax of the first item is not recognized.
  43. next = next.replace(/SOCKS5 /g, 'SOCKS ');
  44. // MOZ: HTTP proxies are wrongly treated as HTTPS proxies!
  45. // There is no workaround now. Therefore, they won't work.
  46. // MOZ: HTTPS proxies are (wrongly) supported under the prefix PROXY.
  47. // XXX: Remove the following hack once they are properly supported.
  48. // https://bugzilla.mozilla.org/show_bug.cgi?id=1359417
  49. next = next.replace(/HTTPS /g, 'PROXY ');
  50. return next;
  51. }
  52. } else if (matchResult.profileName) {
  53. next = OmegaPac.Profiles.nameAsKey(matchResult.profileName)
  54. } else {
  55. return fallbackResult;
  56. }
  57. profile = OmegaPac.Profiles.byKey(next, options)
  58. }
  59. warn('Warning: Cannot find profile: ' + next);
  60. return fallbackResult;
  61. }
  62. function warn(message, error) {
  63. // We don't have console here and alert is not implemented.
  64. // Throwing and messaging seems to be the only ways to communicate.
  65. // MOZ: alert(): https://bugzilla.mozilla.org/show_bug.cgi?id=1353510
  66. browser.runtime.sendMessage({
  67. event: 'proxyScriptLog',
  68. message: message,
  69. error: error,
  70. level: 'warn',
  71. });
  72. }
  73. function init() {
  74. browser.runtime.sendMessage({event: 'proxyScriptLoaded'});
  75. browser.runtime.onMessage.addListener(function(message) {
  76. if (message.event === 'proxyScriptStateChanged') {
  77. state = message.state;
  78. options = message.options;
  79. if (!state.currentProfileName) {
  80. activeProfile = state.tempProfile;
  81. } else {
  82. activeProfile = OmegaPac.Profiles.byName(state.currentProfileName,
  83. options);
  84. }
  85. }
  86. });
  87. }
  88. })();