Skip to content

Commit

Permalink
fix: detect proxy from environment on go 1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
wadahiro committed Aug 1, 2022
1 parent 227747c commit d202426
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
21 changes: 20 additions & 1 deletion proxy.go
Expand Up @@ -76,7 +76,26 @@ type EnvProxy struct {

// Find proxy URL from starndard environment variables.
func (p *EnvProxy) Find(req *http.Request) (*url.URL, error) {
return http.ProxyFromEnvironment(req)
var schema string
if req.Method == http.MethodConnect {
schema = "https"
} else {
schema = req.URL.Scheme
}

u, err := url.Parse(schema + "://" + req.Host)
if err != nil {
log.Printf("error: Invalid request URL for EnvProxy. err: %v", err)
return nil, err
}

r := &http.Request{
URL: u,
}

log.Printf("debug: Find proxy from environment for: %s", r.URL.String())

return http.ProxyFromEnvironment(r)
}

// PACProxy is a Proxy implementation using pac file.
Expand Down
7 changes: 6 additions & 1 deletion verify.go
Expand Up @@ -63,7 +63,12 @@ func connect(r *http.Request, p Proxy) (*tls.Conn, error) {
// VerifyPeerCertificate: verify,
}

u, _ := p.Find(r)
u, err := p.Find(r)
if err != nil {
log.Printf("error: Find upstream proxy error: %s, %v", target.Host, err)
return nil, err
}

if u == nil {
// DIRECT
return tls.Dial("tcp", target.Host, tlsConfig)
Expand Down

0 comments on commit d202426

Please sign in to comment.