Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tcpproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ type Target interface {
HandleConn(net.Conn)
}

type srcAddrContextKey struct{}

// SourceAddrContextKey is the context key used by DialProxy.HandleConn to
// pass the incoming connection's remote address (net.Addr) to DialContext.
var SourceAddrContextKey srcAddrContextKey

// To is shorthand way of writing &tcpproxy.DialProxy{Addr: addr}.
func To(addr string) *DialProxy {
return &DialProxy{Addr: addr}
Expand Down Expand Up @@ -374,7 +380,7 @@ func closeWrite(c net.Conn) {

// HandleConn implements the Target interface.
func (dp *DialProxy) HandleConn(src net.Conn) {
ctx := context.Background()
ctx := context.WithValue(context.Background(), SourceAddrContextKey, src.RemoteAddr())
var cancel context.CancelFunc
if dp.DialTimeout >= 0 {
ctx, cancel = context.WithTimeout(ctx, dp.dialTimeout())
Expand Down