From 3df585cdee79bd231191575e57af1f32673f2ebb Mon Sep 17 00:00:00 2001 From: youngifif Date: Wed, 11 Dec 2024 11:44:38 +0800 Subject: [PATCH] ProxyHttpServer.Tr.Dial() Deprecated, use DialContext() instead. --- examples/goproxy-sokeepalive/sokeepalive.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/goproxy-sokeepalive/sokeepalive.go b/examples/goproxy-sokeepalive/sokeepalive.go index 9135e575..a50398e5 100644 --- a/examples/goproxy-sokeepalive/sokeepalive.go +++ b/examples/goproxy-sokeepalive/sokeepalive.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "github.com/elazarl/goproxy" "log" @@ -13,10 +14,19 @@ func main() { addr := flag.String("addr", ":8080", "proxy listen address") flag.Parse() proxy := goproxy.NewProxyHttpServer() - proxy.Tr.Dial = func(network, addr string) (c net.Conn, err error) { - c, err = net.Dial(network, addr) + proxy.Tr.DialContext = func(ctx context.Context, network, addr string) (c net.Conn, err error) { + var d net.Dialer + c, err = d.DialContext(ctx, network, addr) if c, ok := c.(*net.TCPConn); err == nil && ok { c.SetKeepAlive(true) + go func() { + select { + case <-ctx.Done(): + { + c.Close() + } + } + }() } return }