add context for conn

This commit is contained in:
ginuerzh
2025-08-04 19:29:38 +08:00
parent ad5cf6fd61
commit b597467858
90 changed files with 889 additions and 917 deletions
+2 -2
View File
@@ -101,7 +101,7 @@ func (h *relayHandler) bindTCP(ctx context.Context, conn net.Conn, network, addr
"bind": fmt.Sprintf("%s/%s", ln.Addr(), ln.Addr().Network()),
"src": ln.Addr().String(),
})
ro.Src = ln.Addr().String()
ro.SrcAddr = ln.Addr().String()
af := &relay.AddrFeature{}
if err := af.ParseFrom(ln.Addr().String()); err != nil {
@@ -183,7 +183,7 @@ func (h *relayHandler) bindUDP(ctx context.Context, conn net.Conn, network, addr
"bind": pc.LocalAddr().String(),
"src": pc.LocalAddr().String(),
})
ro.Src = pc.LocalAddr().String()
ro.SrcAddr = pc.LocalAddr().String()
pc = metrics.WrapPacketConn(serviceName, pc)
// pc = admission.WrapPacketConn(l.options.Admission, pc)
+7 -6
View File
@@ -15,7 +15,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/relay"
xbypass "github.com/go-gost/x/bypass"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
xnet "github.com/go-gost/x/internal/net"
serial "github.com/go-gost/x/internal/util/serial"
"github.com/go-gost/x/internal/util/sniffing"
@@ -40,7 +41,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
log.Debugf("%s >> %s/%s", conn.RemoteAddr(), address, network)
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -85,7 +86,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: address})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: address})
}
var cc net.Conn
@@ -103,7 +104,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
}
default:
var buf bytes.Buffer
cc, err = h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, address)
cc, err = h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, address)
ro.Route = buf.String()
}
if err != nil {
@@ -114,8 +115,8 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
defer cc.Close()
log = log.WithFields(map[string]any{"src": cc.LocalAddr().String(), "dst": cc.RemoteAddr().String()})
ro.Src = cc.LocalAddr().String()
ro.Dst = cc.RemoteAddr().String()
ro.SrcAddr = cc.LocalAddr().String()
ro.DstAddr = cc.RemoteAddr().String()
if h.md.noDelay {
if _, err := resp.WriteTo(conn); err != nil {
+4 -3
View File
@@ -12,6 +12,7 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/relay"
ctxvalue "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
xnet "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/limiter/traffic/wrapper"
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
@@ -63,7 +64,7 @@ func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, target.Addr)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, target.Addr)
ro.Route = buf.String()
if err != nil {
// TODO: the router itself may be failed due to the failed node in the router,
@@ -81,8 +82,8 @@ func (h *relayHandler) handleForward(ctx context.Context, conn net.Conn, network
defer cc.Close()
log = log.WithFields(map[string]any{"src": cc.LocalAddr().String(), "dst": cc.RemoteAddr().String()})
ro.Src = cc.LocalAddr().String()
ro.Dst = cc.RemoteAddr().String()
ro.SrcAddr = cc.LocalAddr().String()
ro.DstAddr = cc.RemoteAddr().String()
if marker := target.Marker(); marker != nil {
marker.Reset()
+11 -9
View File
@@ -16,7 +16,7 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
"github.com/go-gost/relay"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
stats_util "github.com/go-gost/x/internal/util/stats"
tls_util "github.com/go-gost/x/internal/util/tls"
rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -105,22 +105,24 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Network: "tcp",
Service: h.options.Service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
SID: xctx.SidFromContext(ctx).String(),
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientIP = srcAddr.String()
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := h.options.Logger.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -206,7 +208,7 @@ func (h *relayHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
}
log = log.WithFields(map[string]any{"clientID": clientID})
ro.ClientID = clientID
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
}
network := networkID.String()