Fix HTTP server leak

This commit is contained in:
世界 2023-10-01 14:36:31 +08:00
parent e727641a98
commit e0ec961fb1
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -21,7 +21,6 @@ import (
type Handler = N.TCPConnectionHandler type Handler = N.TCPConnectionHandler
func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error { func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Reader, authenticator auth.Authenticator, handler Handler, metadata M.Metadata) error {
var httpClient *http.Client
for { for {
request, err := ReadRequest(reader) request, err := ReadRequest(reader)
if err != nil { if err != nil {
@ -95,28 +94,26 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
} }
var innerErr error var innerErr error
if httpClient == nil { httpClient := &http.Client{
httpClient = &http.Client{ Transport: &http.Transport{
Transport: &http.Transport{ DisableCompression: true,
DisableCompression: true, DialContext: func(context context.Context, network, address string) (net.Conn, error) {
DialContext: func(context context.Context, network, address string) (net.Conn, error) { metadata.Destination = M.ParseSocksaddr(address)
metadata.Destination = M.ParseSocksaddr(address) metadata.Protocol = "http"
metadata.Protocol = "http" input, output := net.Pipe()
input, output := net.Pipe() go func() {
go func() { hErr := handler.NewConnection(ctx, output, metadata)
hErr := handler.NewConnection(ctx, output, metadata) if hErr != nil {
if hErr != nil { innerErr = hErr
innerErr = hErr common.Close(input, output)
common.Close(input, output) }
} }()
}() return input, nil
return input, nil
},
}, },
CheckRedirect: func(req *http.Request, via []*http.Request) error { },
return http.ErrUseLastResponse CheckRedirect: func(req *http.Request, via []*http.Request) error {
}, return http.ErrUseLastResponse
} },
} }
response, err := httpClient.Do(request) response, err := httpClient.Do(request)
@ -139,6 +136,8 @@ func HandleConnection(ctx context.Context, conn net.Conn, reader *std_bufio.Read
return E.Errors(innerErr, err) return E.Errors(innerErr, err)
} }
httpClient.CloseIdleConnections()
if !keepAlive { if !keepAlive {
return conn.Close() return conn.Close()
} }