Skip to content

Commit

Permalink
add timeout to proxy connection reading and writing (#1791)
Browse files Browse the repository at this point in the history
Co-authored-by: kalmanzhao <kalmanzhao@tencent.com>
  • Loading branch information
Sniper91 and kalmanzhao authored Jun 19, 2024
1 parent b06f4e2 commit 21b235d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions fasthttpproxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.Dia
return func(addr string) (net.Conn, error) {
var conn net.Conn
var err error
start := time.Now()

if strings.HasPrefix(proxy, "[") {
// ipv6
Expand All @@ -63,13 +64,21 @@ func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.Dia
return nil, err
}

if timeout > 0 {
if err = conn.SetDeadline(start.Add(timeout)); err != nil {
conn.Close()
return nil, err
}
}

req := "CONNECT " + addr + " HTTP/1.1\r\nHost: " + addr + "\r\n"
if auth != "" {
req += "Proxy-Authorization: Basic " + auth + "\r\n"
}
req += "\r\n"

if _, err := conn.Write([]byte(req)); err != nil {
conn.Close()
return nil, err
}

Expand All @@ -86,6 +95,12 @@ func FasthttpHTTPDialerTimeout(proxy string, timeout time.Duration) fasthttp.Dia
conn.Close()
return nil, fmt.Errorf("could not connect to proxy: %s status code: %d", proxy, res.Header.StatusCode())
}
if timeout > 0 {
if err := conn.SetDeadline(time.Time{}); err != nil {
conn.Close()
return nil, err
}
}
return conn, nil
}
}
20 changes: 20 additions & 0 deletions fasthttpproxy/proxy_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
authHTTPSStorage := &atomic.Value{}

return func(addr string) (net.Conn, error) {
start := time.Now()
port, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, fmt.Errorf("unexpected addr format: %w", err)
Expand Down Expand Up @@ -78,6 +79,14 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
return nil, err
}

if timeout > 0 {
if err := conn.SetDeadline(start.Add(timeout)); err != nil {
if connErr := conn.Close(); connErr != nil {
return nil, fmt.Errorf("conn close err %v precede by set conn deadline %w", connErr, err)
}
}
}

req := "CONNECT " + addr + " HTTP/1.1\r\n"

if proxyURL.User != nil {
Expand All @@ -98,6 +107,9 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
req += "\r\n"

if _, err := conn.Write([]byte(req)); err != nil {
if connErr := conn.Close(); connErr != nil {
return nil, fmt.Errorf("conn close err %v precede by write conn err %w", connErr, err)
}
return nil, err
}

Expand All @@ -120,6 +132,14 @@ func FasthttpProxyHTTPDialerTimeout(timeout time.Duration) fasthttp.DialFunc {
}
return nil, fmt.Errorf("could not connect to proxy: code: %d body %q", res.StatusCode(), string(res.Body()))
}
if timeout > 0 {
if err := conn.SetDeadline(time.Time{}); err != nil {
if connErr := conn.Close(); connErr != nil {
return nil, fmt.Errorf("conn close err %v precede by clear conn deadline err %w", connErr, err)
}
return nil, err
}
}
return conn, nil
}
}

0 comments on commit 21b235d

Please sign in to comment.