proxy.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package httpproxy provides support for HTTP proxy determination
  5. // based on environment variables, as provided by net/http's
  6. // ProxyFromEnvironment function.
  7. //
  8. // The API is not subject to the Go 1 compatibility promise and may change at
  9. // any time.
  10. package httpproxy
  11. import (
  12. "errors"
  13. "fmt"
  14. "net"
  15. "net/netip"
  16. "net/url"
  17. "os"
  18. "strings"
  19. "unicode/utf8"
  20. "golang.org/x/net/idna"
  21. )
  22. // Config holds configuration for HTTP proxy settings. See
  23. // FromEnvironment for details.
  24. type Config struct {
  25. // HTTPProxy represents the value of the HTTP_PROXY or
  26. // http_proxy environment variable. It will be used as the proxy
  27. // URL for HTTP requests unless overridden by NoProxy.
  28. HTTPProxy string
  29. // HTTPSProxy represents the HTTPS_PROXY or https_proxy
  30. // environment variable. It will be used as the proxy URL for
  31. // HTTPS requests unless overridden by NoProxy.
  32. HTTPSProxy string
  33. // NoProxy represents the NO_PROXY or no_proxy environment
  34. // variable. It specifies a string that contains comma-separated values
  35. // specifying hosts that should be excluded from proxying. Each value is
  36. // represented by an IP address prefix (1.2.3.4), an IP address prefix in
  37. // CIDR notation (1.2.3.4/8), a domain name, or a special DNS label (*).
  38. // An IP address prefix and domain name can also include a literal port
  39. // number (1.2.3.4:80).
  40. // A domain name matches that name and all subdomains. A domain name with
  41. // a leading "." matches subdomains only. For example "foo.com" matches
  42. // "foo.com" and "bar.foo.com"; ".y.com" matches "x.y.com" but not "y.com".
  43. // A single asterisk (*) indicates that no proxying should be done.
  44. // A best effort is made to parse the string and errors are
  45. // ignored.
  46. NoProxy string
  47. // CGI holds whether the current process is running
  48. // as a CGI handler (FromEnvironment infers this from the
  49. // presence of a REQUEST_METHOD environment variable).
  50. // When this is set, ProxyForURL will return an error
  51. // when HTTPProxy applies, because a client could be
  52. // setting HTTP_PROXY maliciously. See https://golang.org/s/cgihttpproxy.
  53. CGI bool
  54. }
  55. // config holds the parsed configuration for HTTP proxy settings.
  56. type config struct {
  57. // Config represents the original configuration as defined above.
  58. Config
  59. // httpsProxy is the parsed URL of the HTTPSProxy if defined.
  60. httpsProxy *url.URL
  61. // httpProxy is the parsed URL of the HTTPProxy if defined.
  62. httpProxy *url.URL
  63. // ipMatchers represent all values in the NoProxy that are IP address
  64. // prefixes or an IP address in CIDR notation.
  65. ipMatchers []matcher
  66. // domainMatchers represent all values in the NoProxy that are a domain
  67. // name or hostname & domain name
  68. domainMatchers []matcher
  69. }
  70. // FromEnvironment returns a Config instance populated from the
  71. // environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the
  72. // lowercase versions thereof).
  73. //
  74. // The environment values may be either a complete URL or a
  75. // "host[:port]", in which case the "http" scheme is assumed. An error
  76. // is returned if the value is a different form.
  77. func FromEnvironment() *Config {
  78. return &Config{
  79. HTTPProxy: getEnvAny("HTTP_PROXY", "http_proxy"),
  80. HTTPSProxy: getEnvAny("HTTPS_PROXY", "https_proxy"),
  81. NoProxy: getEnvAny("NO_PROXY", "no_proxy"),
  82. CGI: os.Getenv("REQUEST_METHOD") != "",
  83. }
  84. }
  85. func getEnvAny(names ...string) string {
  86. for _, n := range names {
  87. if val := os.Getenv(n); val != "" {
  88. return val
  89. }
  90. }
  91. return ""
  92. }
  93. // ProxyFunc returns a function that determines the proxy URL to use for
  94. // a given request URL. Changing the contents of cfg will not affect
  95. // proxy functions created earlier.
  96. //
  97. // A nil URL and nil error are returned if no proxy is defined in the
  98. // environment, or a proxy should not be used for the given request, as
  99. // defined by NO_PROXY.
  100. //
  101. // As a special case, if req.URL.Host is "localhost" or a loopback address
  102. // (with or without a port number), then a nil URL and nil error will be returned.
  103. func (cfg *Config) ProxyFunc() func(reqURL *url.URL) (*url.URL, error) {
  104. // Preprocess the Config settings for more efficient evaluation.
  105. cfg1 := &config{
  106. Config: *cfg,
  107. }
  108. cfg1.init()
  109. return cfg1.proxyForURL
  110. }
  111. func (cfg *config) proxyForURL(reqURL *url.URL) (*url.URL, error) {
  112. var proxy *url.URL
  113. if reqURL.Scheme == "https" {
  114. proxy = cfg.httpsProxy
  115. } else if reqURL.Scheme == "http" {
  116. proxy = cfg.httpProxy
  117. if proxy != nil && cfg.CGI {
  118. return nil, errors.New("refusing to use HTTP_PROXY value in CGI environment; see golang.org/s/cgihttpproxy")
  119. }
  120. }
  121. if proxy == nil {
  122. return nil, nil
  123. }
  124. if !cfg.useProxy(canonicalAddr(reqURL)) {
  125. return nil, nil
  126. }
  127. return proxy, nil
  128. }
  129. func parseProxy(proxy string) (*url.URL, error) {
  130. if proxy == "" {
  131. return nil, nil
  132. }
  133. proxyURL, err := url.Parse(proxy)
  134. if err != nil || proxyURL.Scheme == "" || proxyURL.Host == "" {
  135. // proxy was bogus. Try prepending "http://" to it and
  136. // see if that parses correctly. If not, we fall
  137. // through and complain about the original one.
  138. if proxyURL, err := url.Parse("http://" + proxy); err == nil {
  139. return proxyURL, nil
  140. }
  141. }
  142. if err != nil {
  143. return nil, fmt.Errorf("invalid proxy address %q: %v", proxy, err)
  144. }
  145. return proxyURL, nil
  146. }
  147. // useProxy reports whether requests to addr should use a proxy,
  148. // according to the NO_PROXY or no_proxy environment variable.
  149. // addr is always a canonicalAddr with a host and port.
  150. func (cfg *config) useProxy(addr string) bool {
  151. if len(addr) == 0 {
  152. return true
  153. }
  154. host, port, err := net.SplitHostPort(addr)
  155. if err != nil {
  156. return false
  157. }
  158. if host == "localhost" {
  159. return false
  160. }
  161. nip, err := netip.ParseAddr(host)
  162. var ip net.IP
  163. if err == nil {
  164. ip = net.IP(nip.AsSlice())
  165. if ip.IsLoopback() {
  166. return false
  167. }
  168. }
  169. addr = strings.ToLower(strings.TrimSpace(host))
  170. if ip != nil {
  171. for _, m := range cfg.ipMatchers {
  172. if m.match(addr, port, ip) {
  173. return false
  174. }
  175. }
  176. }
  177. for _, m := range cfg.domainMatchers {
  178. if m.match(addr, port, ip) {
  179. return false
  180. }
  181. }
  182. return true
  183. }
  184. func (c *config) init() {
  185. if parsed, err := parseProxy(c.HTTPProxy); err == nil {
  186. c.httpProxy = parsed
  187. }
  188. if parsed, err := parseProxy(c.HTTPSProxy); err == nil {
  189. c.httpsProxy = parsed
  190. }
  191. for _, p := range strings.Split(c.NoProxy, ",") {
  192. p = strings.ToLower(strings.TrimSpace(p))
  193. if len(p) == 0 {
  194. continue
  195. }
  196. if p == "*" {
  197. c.ipMatchers = []matcher{allMatch{}}
  198. c.domainMatchers = []matcher{allMatch{}}
  199. return
  200. }
  201. // IPv4/CIDR, IPv6/CIDR
  202. if _, pnet, err := net.ParseCIDR(p); err == nil {
  203. c.ipMatchers = append(c.ipMatchers, cidrMatch{cidr: pnet})
  204. continue
  205. }
  206. // IPv4:port, [IPv6]:port
  207. phost, pport, err := net.SplitHostPort(p)
  208. if err == nil {
  209. if len(phost) == 0 {
  210. // There is no host part, likely the entry is malformed; ignore.
  211. continue
  212. }
  213. if phost[0] == '[' && phost[len(phost)-1] == ']' {
  214. phost = phost[1 : len(phost)-1]
  215. }
  216. } else {
  217. phost = p
  218. }
  219. // IPv4, IPv6
  220. if pip := net.ParseIP(phost); pip != nil {
  221. c.ipMatchers = append(c.ipMatchers, ipMatch{ip: pip, port: pport})
  222. continue
  223. }
  224. if len(phost) == 0 {
  225. // There is no host part, likely the entry is malformed; ignore.
  226. continue
  227. }
  228. // domain.com or domain.com:80
  229. // foo.com matches bar.foo.com
  230. // .domain.com or .domain.com:port
  231. // *.domain.com or *.domain.com:port
  232. if strings.HasPrefix(phost, "*.") {
  233. phost = phost[1:]
  234. }
  235. matchHost := false
  236. if phost[0] != '.' {
  237. matchHost = true
  238. phost = "." + phost
  239. }
  240. if v, err := idnaASCII(phost); err == nil {
  241. phost = v
  242. }
  243. c.domainMatchers = append(c.domainMatchers, domainMatch{host: phost, port: pport, matchHost: matchHost})
  244. }
  245. }
  246. var portMap = map[string]string{
  247. "http": "80",
  248. "https": "443",
  249. "socks5": "1080",
  250. }
  251. // canonicalAddr returns url.Host but always with a ":port" suffix
  252. func canonicalAddr(url *url.URL) string {
  253. addr := url.Hostname()
  254. if v, err := idnaASCII(addr); err == nil {
  255. addr = v
  256. }
  257. port := url.Port()
  258. if port == "" {
  259. port = portMap[url.Scheme]
  260. }
  261. return net.JoinHostPort(addr, port)
  262. }
  263. // Given a string of the form "host", "host:port", or "[ipv6::address]:port",
  264. // return true if the string includes a port.
  265. func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
  266. func idnaASCII(v string) (string, error) {
  267. // TODO: Consider removing this check after verifying performance is okay.
  268. // Right now punycode verification, length checks, context checks, and the
  269. // permissible character tests are all omitted. It also prevents the ToASCII
  270. // call from salvaging an invalid IDN, when possible. As a result it may be
  271. // possible to have two IDNs that appear identical to the user where the
  272. // ASCII-only version causes an error downstream whereas the non-ASCII
  273. // version does not.
  274. // Note that for correct ASCII IDNs ToASCII will only do considerably more
  275. // work, but it will not cause an allocation.
  276. if isASCII(v) {
  277. return v, nil
  278. }
  279. return idna.Lookup.ToASCII(v)
  280. }
  281. func isASCII(s string) bool {
  282. for i := 0; i < len(s); i++ {
  283. if s[i] >= utf8.RuneSelf {
  284. return false
  285. }
  286. }
  287. return true
  288. }
  289. // matcher represents the matching rule for a given value in the NO_PROXY list
  290. type matcher interface {
  291. // match returns true if the host and optional port or ip and optional port
  292. // are allowed
  293. match(host, port string, ip net.IP) bool
  294. }
  295. // allMatch matches on all possible inputs
  296. type allMatch struct{}
  297. func (a allMatch) match(host, port string, ip net.IP) bool {
  298. return true
  299. }
  300. type cidrMatch struct {
  301. cidr *net.IPNet
  302. }
  303. func (m cidrMatch) match(host, port string, ip net.IP) bool {
  304. return m.cidr.Contains(ip)
  305. }
  306. type ipMatch struct {
  307. ip net.IP
  308. port string
  309. }
  310. func (m ipMatch) match(host, port string, ip net.IP) bool {
  311. if m.ip.Equal(ip) {
  312. return m.port == "" || m.port == port
  313. }
  314. return false
  315. }
  316. type domainMatch struct {
  317. host string
  318. port string
  319. matchHost bool
  320. }
  321. func (m domainMatch) match(host, port string, ip net.IP) bool {
  322. if ip != nil {
  323. return false
  324. }
  325. if strings.HasSuffix(host, m.host) || (m.matchHost && host == m.host[1:]) {
  326. return m.port == "" || m.port == port
  327. }
  328. return false
  329. }