tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ProxyPolicies.sys.mjs (4121B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const PREF_LOGLEVEL = "browser.policies.loglevel";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineLazyGetter(lazy, "log", () => {
     10  let { ConsoleAPI } = ChromeUtils.importESModule(
     11    "resource://gre/modules/Console.sys.mjs"
     12  );
     13  return new ConsoleAPI({
     14    prefix: "ProxyPolicies",
     15    // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
     16    // messages during development. See LOG_LEVELS in Console.sys.mjs for details.
     17    maxLogLevel: "error",
     18    maxLogLevelPref: PREF_LOGLEVEL,
     19  });
     20 });
     21 
     22 // Don't use const here because this is acessed by
     23 // tests through the SystemGlobal object.
     24 export var PROXY_TYPES_MAP = new Map([
     25  ["none", Ci.nsIProtocolProxyService.PROXYCONFIG_DIRECT],
     26  ["system", Ci.nsIProtocolProxyService.PROXYCONFIG_SYSTEM],
     27  ["manual", Ci.nsIProtocolProxyService.PROXYCONFIG_MANUAL],
     28  ["autoDetect", Ci.nsIProtocolProxyService.PROXYCONFIG_WPAD],
     29  ["autoConfig", Ci.nsIProtocolProxyService.PROXYCONFIG_PAC],
     30 ]);
     31 
     32 let proxyPreferences = [
     33  "network.proxy.type",
     34  "network.proxy.autoconfig_url",
     35  "network.proxy.socks_remote_dns",
     36  "network.proxy.socks5_remote_dns",
     37  "signon.autologin.proxy",
     38  "network.proxy.socks_version",
     39  "network.proxy.no_proxies_on",
     40  "network.proxy.share_proxy_settings",
     41  "network.proxy.http",
     42  "network.proxy.http_port",
     43  "network.proxy.ssl",
     44  "network.proxy.ssl_port",
     45  "network.proxy.socks",
     46  "network.proxy.socks_port",
     47 ];
     48 
     49 export var ProxyPolicies = {
     50  configureProxySettings(param, setPref) {
     51    if (param.Mode) {
     52      setPref("network.proxy.type", PROXY_TYPES_MAP.get(param.Mode));
     53    }
     54 
     55    if (param.AutoConfigURL) {
     56      setPref("network.proxy.autoconfig_url", param.AutoConfigURL.href);
     57    }
     58 
     59    if (param.UseProxyForDNS !== undefined) {
     60      setPref("network.proxy.socks_remote_dns", param.UseProxyForDNS);
     61      setPref("network.proxy.socks5_remote_dns", param.UseProxyForDNS);
     62    }
     63 
     64    if (param.AutoLogin !== undefined) {
     65      setPref("signon.autologin.proxy", param.AutoLogin);
     66    }
     67 
     68    if (param.SOCKSVersion !== undefined) {
     69      if (param.SOCKSVersion != 4 && param.SOCKSVersion != 5) {
     70        lazy.log.error("Invalid SOCKS version");
     71      } else {
     72        setPref("network.proxy.socks_version", param.SOCKSVersion);
     73      }
     74    }
     75 
     76    if (param.Passthrough !== undefined) {
     77      setPref("network.proxy.no_proxies_on", param.Passthrough);
     78    }
     79 
     80    if (param.UseHTTPProxyForAllProtocols !== undefined) {
     81      setPref(
     82        "network.proxy.share_proxy_settings",
     83        param.UseHTTPProxyForAllProtocols
     84      );
     85    }
     86 
     87    if (param.FTPProxy) {
     88      lazy.log.warn("FTPProxy support was removed in bug 1574475");
     89    }
     90 
     91    function setProxyHostAndPort(type, address) {
     92      // Prepend https just so we can use the URL parser
     93      // instead of parsing manually.
     94      let url = URL.parse(`https://${address}`);
     95      if (!url) {
     96        lazy.log.error(`Invalid address for ${type} proxy: ${address}`);
     97        return;
     98      }
     99 
    100      setPref(`network.proxy.${type}`, url.hostname);
    101      if (url.port) {
    102        setPref(`network.proxy.${type}_port`, Number(url.port));
    103      }
    104    }
    105 
    106    if (param.HTTPProxy) {
    107      setProxyHostAndPort("http", param.HTTPProxy);
    108 
    109      // network.proxy.share_proxy_settings is a UI feature, not handled by the
    110      // network code. That pref only controls if the checkbox is checked, and
    111      // then we must manually set the other values.
    112      if (param.UseHTTPProxyForAllProtocols) {
    113        param.SSLProxy = param.HTTPProxy;
    114      }
    115    }
    116 
    117    if (param.SSLProxy) {
    118      setProxyHostAndPort("ssl", param.SSLProxy);
    119    }
    120 
    121    if (param.SOCKSProxy) {
    122      setProxyHostAndPort("socks", param.SOCKSProxy);
    123    }
    124 
    125    // All preferences should be locked regardless of whether or not a
    126    // specific value was set.
    127    if (param.Locked) {
    128      for (let preference of proxyPreferences) {
    129        Services.prefs.lockPref(preference);
    130      }
    131    }
    132  },
    133 };