Class UpstreamProxyManager

java.lang.Object
org.microproxy.UpstreamProxyManager
All Implemented Interfaces:
ChainedProxyManager

public final class UpstreamProxyManager extends Object implements ChainedProxyManager
A ChainedProxyManager configured like command-line HTTP clients: an upstream proxy URL for plain HTTP, one for HTTPS (CONNECT), and NoProxyRules for hosts reached directly.

Proxy URLs take the forms http://[user:password@]host[:port], https://... (TLS to the proxy itself), h2://... (TLS, offering HTTP/2 with ALPN), h2c://... (HTTP/2 with prior knowledge over plain TCP), socks4://..., socks4a://..., socks5://... and socks5h://.... An HTTP/2 proxy carries tunnels and plain-HTTP requests as streams of shared connections (ChainedProxy.http2()); an h2:// proxy that chooses HTTP/1.1 is spoken to in HTTP/1.1. SOCKS proxies always receive host names, so the a/h variants behave like the plain ones. Default ports are 80 (http, h2c), 443 (https, h2) and 1080 (SOCKS).


 bootstrap.withChainProxyManager(UpstreamProxyManager.fromEnvironment(System.getenv()));
 bootstrap.withChainProxyManager(new UpstreamProxyManager(
         "http://user:secret@proxy.corp:3128", null, NoProxyRules.parse("localhost,.corp,10.0.0.0/8")));
 
  • Constructor Details

    • UpstreamProxyManager

      public UpstreamProxyManager(String httpProxyUrl, String httpsProxyUrl, NoProxyRules noProxy)
      Creates upstream routing with protocol-specific proxies and bypass rules.
      Parameters:
      httpProxyUrl - proxy for plain HTTP requests, or null for direct
      httpsProxyUrl - proxy for CONNECT / HTTPS, or null to use httpProxyUrl
      noProxy - the rules identifying destinations that bypass upstream proxies
  • Method Details

    • fromEnvironment

      public static UpstreamProxyManager fromEnvironment(Map<String,String> env)
      Reads http_proxy, https_proxy/HTTPS_PROXY, all_proxy/ ALL_PROXY and no_proxy/NO_PROXY, like curl. Upper-case HTTP_PROXY is ignored, as curl does, because CGI servers set it from the request's Proxy header.
      Parameters:
      env - the environment variable map
      Returns:
      the manager, or null when no proxy variable is set
    • withFallbackToDirect

      public UpstreamProxyManager withFallbackToDirect(boolean fallbackToDirect)
      Also try a direct connection when the upstream proxy cannot be reached.
      Parameters:
      fallbackToDirect - whether to try connecting directly after upstream proxy failure
      Returns:
      this manager
    • lookupChainedProxies

      public void lookupChainedProxies(HttpRequest request, Queue<ChainedProxy> chainedProxies, ClientDetails clientDetails)
      Description copied from interface: ChainedProxyManager
      Adds the proxies to try, in order, to chainedProxies. Leave the queue empty to reject the request with 502, or add ChainedProxyAdapter.FALLBACK_TO_DIRECT_CONNECTION to connect directly.
      Specified by:
      lookupChainedProxies in interface ChainedProxyManager
      Parameters:
      request - the request head
      chainedProxies - the queue to populate with proxies to try, in order
      clientDetails - the client address and authentication details
    • usesHttp2

      public boolean usesHttp2()
      Returns whether any of the upstream proxies is spoken to in HTTP/2 (h2:// or h2c://).
      Returns:
      whether any of the upstream proxies is spoken to in HTTP/2 (h2:// or h2c://)
    • toString

      public String toString()
      Returns the upstream proxies, their credentials masked (http://***@host:port), and whether direct is a fallback.
      Overrides:
      toString in class Object
      Returns:
      the upstream proxies, their credentials masked (http://***@host:port), and whether direct is a fallback
    • proxy

      public static ChainedProxy proxy(String url)
      Parses a proxy URL into a ChainedProxy.
      Parameters:
      url - the upstream proxy URL
      Returns:
      the parsed upstream proxy