omega_webext_proxy_script.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. return next;
  45. }
  46. } else if (matchResult.profileName) {
  47. next = OmegaPac.Profiles.nameAsKey(matchResult.profileName)
  48. } else {
  49. return fallbackResult;
  50. }
  51. profile = OmegaPac.Profiles.byKey(next, options)
  52. }
  53. warn('Warning: Cannot find profile: ' + next);
  54. return fallbackResult;
  55. }
  56. function warn(message, error) {
  57. // We don't have console here and alert is not implemented.
  58. // Throwing and messaging seems to be the only ways to communicate.
  59. // MOZ: alert(): https://bugzilla.mozilla.org/show_bug.cgi?id=1353510
  60. browser.runtime.sendMessage({
  61. event: 'proxyScriptLog',
  62. message: message,
  63. error: error,
  64. level: 'warn',
  65. });
  66. }
  67. function init() {
  68. browser.runtime.sendMessage({event: 'proxyScriptLoaded'});
  69. browser.runtime.onMessage.addListener(function(message) {
  70. if (message.event === 'proxyScriptStateChanged') {
  71. state = message.state;
  72. options = message.options;
  73. if (!state.currentProfileName) {
  74. activeProfile = state.tempProfile;
  75. } else {
  76. activeProfile = OmegaPac.Profiles.byName(state.currentProfileName,
  77. options);
  78. }
  79. }
  80. });
  81. }
  82. })();