chore: a better approach for http proxy error log

This commit is contained in:
Haruue Icymoon 2022-08-25 00:53:52 +08:00
parent 2fb70bdb58
commit f7de18fd43
No known key found for this signature in database
GPG key ID: F6083B28CBCBC148

View file

@ -27,7 +27,12 @@ func NewProxyHTTPServer(hyClient *core.Client, transport *transport.ClientTransp
proxy.Logger = &nopLogger{} proxy.Logger = &nopLogger{}
proxy.NonproxyHandler = http.NotFoundHandler() proxy.NonproxyHandler = http.NotFoundHandler()
proxy.Tr = &http.Transport{ proxy.Tr = &http.Transport{
Dial: func(network, addr string) (net.Conn, error) { Dial: func(network, addr string) (conn net.Conn, err error) {
defer func() {
if err != nil {
proxyErrorFunc(addr, err)
}
}()
// Parse addr string // Parse addr string
host, port, err := utils.SplitHostPort(addr) host, port, err := utils.SplitHostPort(addr)
if err != nil { if err != nil {
@ -48,44 +53,27 @@ func NewProxyHTTPServer(hyClient *core.Client, transport *transport.ClientTransp
if resErr != nil { if resErr != nil {
return nil, resErr return nil, resErr
} }
conn, err := transport.DialTCP(&net.TCPAddr{ return transport.DialTCP(&net.TCPAddr{
IP: ipAddr.IP, IP: ipAddr.IP,
Port: int(port), Port: int(port),
Zone: ipAddr.Zone, Zone: ipAddr.Zone,
}) })
if err != nil {
proxyErrorFunc(addr, err)
}
return conn, err
case acl.ActionProxy: case acl.ActionProxy:
conn, err := hyClient.DialTCP(addr) return hyClient.DialTCP(addr)
if err != nil {
proxyErrorFunc(addr, err)
}
return conn, err
case acl.ActionBlock: case acl.ActionBlock:
err := errors.New("blocked by ACL") return nil, errors.New("blocked by ACL")
proxyErrorFunc(addr, err)
return nil, err
case acl.ActionHijack: case acl.ActionHijack:
hijackIPAddr, err := transport.ResolveIPAddr(arg) hijackIPAddr, err := transport.ResolveIPAddr(arg)
if err != nil { if err != nil {
proxyErrorFunc(addr, err)
return nil, err return nil, err
} }
conn, err := transport.DialTCP(&net.TCPAddr{ return transport.DialTCP(&net.TCPAddr{
IP: hijackIPAddr.IP, IP: hijackIPAddr.IP,
Port: int(port), Port: int(port),
Zone: hijackIPAddr.Zone, Zone: hijackIPAddr.Zone,
}) })
if err != nil {
proxyErrorFunc(addr, err)
}
return conn, err
default: default:
err := fmt.Errorf("unknown action %d", action) return nil, fmt.Errorf("unknown action %d", action)
proxyErrorFunc(addr, err)
return nil, err
} }
}, },
IdleConnTimeout: idleTimeout, IdleConnTimeout: idleTimeout,