tor-browser

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

ProxyAutoConfig.h (5360B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef ProxyAutoConfig_h__
      8 #define ProxyAutoConfig_h__
      9 
     10 #include <functional>
     11 #include "nsString.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsTArray.h"
     14 
     15 class nsIEventTarget;
     16 class nsITimer;
     17 class nsIThread;
     18 namespace JS {
     19 class CallArgs;
     20 }  // namespace JS
     21 
     22 namespace mozilla {
     23 namespace net {
     24 
     25 class JSContextWrapper;
     26 class ProxyAutoConfigParent;
     27 union NetAddr;
     28 
     29 class ProxyAutoConfigBase {
     30 public:
     31  virtual ~ProxyAutoConfigBase() = default;
     32  virtual nsresult Init(nsIThread* aPACThread) { return NS_OK; }
     33  virtual nsresult ConfigurePAC(const nsACString& aPACURI,
     34                                const nsACString& aPACScriptData,
     35                                bool aIncludePath, uint32_t aExtraHeapSize,
     36                                nsISerialEventTarget* aEventTarget) = 0;
     37  virtual void SetThreadLocalIndex(uint32_t index) {}
     38  virtual void Shutdown() = 0;
     39  virtual void GC() = 0;
     40  virtual void GetProxyForURIWithCallback(
     41      const nsACString& aTestURI, const nsACString& aTestHost,
     42      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
     43          aCallback) = 0;
     44 };
     45 
     46 // The ProxyAutoConfig class is meant to be created and run on a
     47 // non main thread. It synchronously resolves PAC files by blocking that
     48 // thread and running nested event loops. GetProxyForURI is not re-entrant.
     49 
     50 class ProxyAutoConfig : public ProxyAutoConfigBase {
     51 public:
     52  ProxyAutoConfig();
     53  virtual ~ProxyAutoConfig();
     54 
     55  nsresult ConfigurePAC(const nsACString& aPACURI,
     56                        const nsACString& aPACScriptData, bool aIncludePath,
     57                        uint32_t aExtraHeapSize,
     58                        nsISerialEventTarget* aEventTarget) override;
     59  void SetThreadLocalIndex(uint32_t index) override;
     60  void Shutdown() override;
     61  void GC() override;
     62  bool MyIPAddress(const JS::CallArgs& aArgs);
     63  bool ResolveAddress(const nsACString& aHostName, NetAddr* aNetAddr,
     64                      unsigned int aTimeout);
     65 
     66  /**
     67   * Get the proxy string for the specified URI.  The proxy string is
     68   * given by the following:
     69   *
     70   *   result      = proxy-spec *( proxy-sep proxy-spec )
     71   *   proxy-spec  = direct-type | proxy-type LWS proxy-host [":" proxy-port]
     72   *   direct-type = "DIRECT"
     73   *   proxy-type  = "PROXY" | "HTTP" | "HTTPS" | "SOCKS" | "SOCKS4" | "SOCKS5"
     74   *   proxy-sep   = ";" LWS
     75   *   proxy-host  = hostname | ipv4-address-literal
     76   *   proxy-port  = <any 16-bit unsigned integer>
     77   *   LWS         = *( SP | HT )
     78   *   SP          = <US-ASCII SP, space (32)>
     79   *   HT          = <US-ASCII HT, horizontal-tab (9)>
     80   *
     81   * NOTE: direct-type and proxy-type are case insensitive
     82   * NOTE: SOCKS implies SOCKS4
     83   *
     84   * Examples:
     85   *   "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT"
     86   *   "SOCKS socksproxy"
     87   *   "DIRECT"
     88   *
     89   * XXX add support for IPv6 address literals.
     90   * XXX quote whatever the official standard is for PAC.
     91   *
     92   * @param aTestURI
     93   *        The URI as an ASCII string to test.
     94   * @param aTestHost
     95   *        The ASCII hostname to test.
     96   *
     97   * @param result
     98   *        result string as defined above.
     99   */
    100  nsresult GetProxyForURI(const nsACString& aTestURI,
    101                          const nsACString& aTestHost, nsACString& result);
    102 
    103  void GetProxyForURIWithCallback(
    104      const nsACString& aTestURI, const nsACString& aTestHost,
    105      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
    106          aCallback) override;
    107 
    108 private:
    109  // allow 665ms for myipaddress dns queries. That's 95th percentile.
    110  const static unsigned int kTimeout = 665;
    111 
    112  // used to compile the PAC file and setup the execution context
    113  nsresult SetupJS();
    114 
    115  bool SrcAddress(const NetAddr* remoteAddress, nsCString& localAddress);
    116  bool MyIPAddressTryHost(const nsACString& hostName, unsigned int timeout,
    117                          const JS::CallArgs& aArgs, bool* aResult);
    118 
    119  JSContextWrapper* mJSContext{nullptr};
    120  bool mJSNeedsSetup{false};
    121  bool mShutdown{true};
    122  nsCString mConcatenatedPACData;
    123  nsCString mPACURI;
    124  bool mIncludePath{false};
    125  uint32_t mExtraHeapSize{0};
    126  nsCString mRunningHost;
    127  nsCOMPtr<nsITimer> mTimer;
    128  nsCOMPtr<nsISerialEventTarget> mMainThreadEventTarget;
    129 };
    130 
    131 class RemoteProxyAutoConfig : public ProxyAutoConfigBase {
    132 public:
    133  RemoteProxyAutoConfig();
    134  virtual ~RemoteProxyAutoConfig();
    135 
    136  nsresult Init(nsIThread* aPACThread) override;
    137  nsresult ConfigurePAC(const nsACString& aPACURI,
    138                        const nsACString& aPACScriptData, bool aIncludePath,
    139                        uint32_t aExtraHeapSize,
    140                        nsISerialEventTarget* aEventTarget) override;
    141  void Shutdown() override;
    142  void GC() override;
    143  void GetProxyForURIWithCallback(
    144      const nsACString& aTestURI, const nsACString& aTestHost,
    145      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
    146          aCallback) override;
    147 
    148 private:
    149  RefPtr<ProxyAutoConfigParent> mProxyAutoConfigParent;
    150 };
    151 
    152 }  // namespace net
    153 }  // namespace mozilla
    154 
    155 #endif  // ProxyAutoConfig_h__