| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- FindProxyForURL = (function () {
- var OmegaPac = require('omega-pac');
- var options = {};
- var state = {};
- var activeProfile = null;
- var fallbackResult = 'DIRECT';
- var pacCache = {};
- init();
- return FindProxyForURL;
- function FindProxyForURL(url, host, details) {
- if (!activeProfile) {
- warn('Warning: Proxy script not initialized on handling: ' + url);
- return fallbackResult;
- }
- // Moz: Neither path or query is included url regardless of scheme for now.
- // This is even more strict than Chromium restricting HTTPS URLs.
- // Therefore, it leads to different behavior than the icon and badge.
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1337001
- var request = OmegaPac.Conditions.requestFromUrl(url);
- var profile = activeProfile;
- var matchResult, next;
- while (profile) {
- matchResult = OmegaPac.Profiles.match(profile, request)
- if (!matchResult) {
- if (profile.profileType === 'DirectProfile') {
- return 'DIRECT';
- } else {
- warn('Warning: Unsupported profile: ' + profile.profileType);
- return fallbackResult;
- }
- }
- if (Array.isArray(matchResult)) {
- next = matchResult[0];
- // TODO: Maybe also return user/pass if Mozilla supports it or it ends
- // up standardized in WebExtensions in the future.
- // MOZ: Mozilla has a bug tracked for user/pass in PAC return value.
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1319641
- if (next.charCodeAt(0) !== 43) {
- // MOZ: SOCKS5 proxies are supported under the prefix SOCKS.
- // https://dxr.mozilla.org/mozilla-central/source/toolkit/components/extensions/ProxyScriptContext.jsm#178
- // Note: We have to replace this because MOZ won't process the rest of
- // the list if the syntax of the first item is not recognized.
- next = next.replace(/SOCKS5 /g, 'SOCKS ');
- // MOZ: HTTP proxies are wrongly treated as HTTPS proxies!
- // There is no workaround now. Therefore, they won't work.
- // MOZ: HTTPS proxies are (wrongly) supported under the prefix PROXY.
- // XXX: Remove the following hack once they are properly supported.
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1359417
- next = next.replace(/HTTPS /g, 'PROXY ');
- return next;
- }
- } else if (matchResult.profileName) {
- next = OmegaPac.Profiles.nameAsKey(matchResult.profileName)
- } else {
- return fallbackResult;
- }
- profile = OmegaPac.Profiles.byKey(next, options)
- }
- warn('Warning: Cannot find profile: ' + next);
- return fallbackResult;
- }
- function warn(message, error) {
- // We don't have console here and alert is not implemented.
- // Throwing and messaging seems to be the only ways to communicate.
- // MOZ: alert(): https://bugzilla.mozilla.org/show_bug.cgi?id=1353510
- browser.runtime.sendMessage({
- event: 'proxyScriptLog',
- message: message,
- error: error,
- level: 'warn',
- });
- }
- function init() {
- browser.runtime.sendMessage({event: 'proxyScriptLoaded'});
- browser.runtime.onMessage.addListener(function(message) {
- if (message.event === 'proxyScriptStateChanged') {
- state = message.state;
- options = message.options;
- if (!state.currentProfileName) {
- activeProfile = state.tempProfile;
- } else {
- activeProfile = OmegaPac.Profiles.byName(state.currentProfileName,
- options);
- }
- }
- });
- }
- })();
|