File: proxy_middleman_windows.go

package info (click to toggle)
golang-github-mattn-go-ieproxy 0.0~git20191113.7c0f686-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 156 kB
  • sloc: makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ieproxy

import (
	"net/http"
	"net/url"

	"golang.org/x/net/http/httpproxy"
)

func proxyMiddleman() func(req *http.Request) (i *url.URL, e error) {
	// Get the proxy configuration
	conf := GetConf()
	envcfg := httpproxy.FromEnvironment()

	if envcfg.HTTPProxy != "" || envcfg.HTTPSProxy != "" {
		// If the user manually specifies environment variables, prefer those over the Windows config.
		return http.ProxyFromEnvironment
	}

	return func(req *http.Request) (i *url.URL, e error) {
		if conf.Automatic.Active {
			host := conf.Automatic.FindProxyForURL(req.URL.String())
			if host != "" {
				return &url.URL{Host: host}, nil
			}
		}
		if conf.Static.Active {
			return staticProxy(conf, req)
		}
		// Should return no proxy; fallthrough.
		return http.ProxyFromEnvironment(req)
	}
}

func staticProxy(conf ProxyConf, req *http.Request) (i *url.URL, e error) {
	// If static proxy obtaining is specified
	prox := httpproxy.Config{
		HTTPSProxy: mapFallback("https", "", conf.Static.Protocols),
		HTTPProxy:  mapFallback("http", "", conf.Static.Protocols),
		NoProxy:    conf.Static.NoProxy,
	}
	return prox.ProxyFunc()(req.URL)
}

// Return oKey or fbKey if oKey doesn't exist in the map.
func mapFallback(oKey, fbKey string, m map[string]string) string {
	if v, ok := m[oKey]; ok {
		return v
	} else {
		return m[fbKey]
	}
}