2018-09-06 21:42:41 +00:00
|
|
|
package lfshttp
|
2016-12-20 17:29:26 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2021-08-10 06:18:38 +00:00
|
|
|
"github.com/git-lfs/git-lfs/v2/config"
|
2020-01-07 15:45:31 +00:00
|
|
|
"golang.org/x/net/http/httpproxy"
|
2016-12-20 17:29:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Logic is copied, with small changes, from "net/http".ProxyFromEnvironment in the go std lib.
|
2017-01-06 21:36:34 +00:00
|
|
|
func proxyFromClient(c *Client) func(req *http.Request) (*url.URL, error) {
|
2016-12-20 17:29:26 +00:00
|
|
|
return func(req *http.Request) (*url.URL, error) {
|
2017-10-05 14:35:49 +00:00
|
|
|
httpsProxy, httpProxy, noProxy := getProxyServers(req.URL, c.uc, c.osEnv)
|
|
|
|
|
2016-12-20 17:29:26 +00:00
|
|
|
var proxy string
|
|
|
|
if req.URL.Scheme == "https" {
|
|
|
|
proxy = httpsProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxy) == 0 {
|
|
|
|
proxy = httpProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxy) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-22 17:01:07 +00:00
|
|
|
if strings.HasPrefix(proxy, "socks5h://") {
|
|
|
|
proxy = strings.Replace(proxy, "socks5h://", "socks5://", 1)
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:45:31 +00:00
|
|
|
cfg := &httpproxy.Config{
|
|
|
|
HTTPProxy: proxy,
|
|
|
|
HTTPSProxy: proxy,
|
|
|
|
NoProxy: noProxy,
|
|
|
|
CGI: false,
|
2016-12-20 17:29:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 15:45:31 +00:00
|
|
|
// We want to use the standard logic except that we want to
|
|
|
|
// allow proxies for localhost, which the standard library does
|
|
|
|
// not. Since the proxy code looks only at the URL, we
|
|
|
|
// synthesize a fake URL except that we rewrite "localhost" to
|
|
|
|
// "127.0.0.1" for purposes of looking up the proxy.
|
|
|
|
u := *(req.URL)
|
|
|
|
if u.Host == "localhost" {
|
|
|
|
u.Host = "127.0.0.1"
|
2016-12-20 17:29:26 +00:00
|
|
|
}
|
2020-01-07 15:45:31 +00:00
|
|
|
return cfg.ProxyFunc()(&u)
|
2016-12-20 17:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 21:33:20 +00:00
|
|
|
func getProxyServers(u *url.URL, urlCfg *config.URLConfig, osEnv config.Environment) (httpsProxy string, httpProxy string, noProxy string) {
|
2017-10-05 14:35:49 +00:00
|
|
|
if osEnv == nil {
|
|
|
|
return
|
2016-12-20 22:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(httpsProxy) == 0 {
|
|
|
|
httpsProxy, _ = osEnv.Get("HTTPS_PROXY")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(httpsProxy) == 0 {
|
|
|
|
httpsProxy, _ = osEnv.Get("https_proxy")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(httpProxy) == 0 {
|
|
|
|
httpProxy, _ = osEnv.Get("HTTP_PROXY")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(httpProxy) == 0 {
|
|
|
|
httpProxy, _ = osEnv.Get("http_proxy")
|
|
|
|
}
|
|
|
|
|
2018-06-13 17:24:15 +00:00
|
|
|
if urlCfg != nil {
|
|
|
|
gitProxy, ok := urlCfg.Get("http", u.String(), "proxy")
|
|
|
|
if len(gitProxy) > 0 && ok {
|
2021-02-03 20:27:39 +00:00
|
|
|
if u.Scheme == "https" {
|
2018-06-13 17:24:15 +00:00
|
|
|
httpsProxy = gitProxy
|
|
|
|
}
|
|
|
|
httpProxy = gitProxy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 14:35:49 +00:00
|
|
|
noProxy, _ = osEnv.Get("NO_PROXY")
|
2016-12-20 22:06:15 +00:00
|
|
|
if len(noProxy) == 0 {
|
|
|
|
noProxy, _ = osEnv.Get("no_proxy")
|
|
|
|
}
|
|
|
|
|
2017-10-05 14:35:49 +00:00
|
|
|
return
|
2016-12-20 22:06:15 +00:00
|
|
|
}
|