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
+11 -9
View File
@@ -18,8 +18,9 @@ import (
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xhop "github.com/go-gost/x/hop"
ictx "github.com/go-gost/x/internal/ctx"
resolver_util "github.com/go-gost/x/internal/util/resolver"
rate_limiter "github.com/go-gost/x/limiter/rate"
xstats "github.com/go-gost/x/observer/stats"
@@ -145,18 +146,19 @@ func (h *dnsHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
Host: conn.LocalAddr().String(),
Proto: "dns",
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
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())
@@ -310,7 +312,7 @@ func (h *dnsHandler) request(ctx context.Context, msg []byte, ro *xrecorder.Hand
log.Debugf("exchange message %d: %s", mq.Id, mq.Question[0].String())
var buf bytes.Buffer
mr, err := h.exchange(ctxvalue.ContextWithBuffer(ctx, &buf), ex, &mq)
mr, err := h.exchange(ictx.ContextWithBuffer(ctx, &buf), ex, &mq)
ro.Route = buf.String()
if err != nil {
return nil, err
+9 -2
View File
@@ -4,13 +4,15 @@ import (
"context"
"net"
"net/http"
"net/http/httputil"
"sync"
"time"
"github.com/go-gost/core/handler"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/recorder"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xrecorder "github.com/go-gost/x/recorder"
"github.com/go-gost/x/registry"
)
@@ -67,7 +69,7 @@ func (h *fileHandler) Init(md md.Metadata) (err error) {
func (h *fileHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) error {
var clientAddr string
if srcAddr := ctxvalue.SrcAddrFromContext(ctx); srcAddr != nil {
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
clientAddr = srcAddr.String()
}
@@ -113,6 +115,11 @@ func (h *fileHandler) handleFunc(w http.ResponseWriter, r *http.Request) {
"remote": r.RemoteAddr,
})
if log.IsLevelEnabled(logger.TraceLevel) {
dump, _ := httputil.DumpRequest(r, false)
log.Trace(string(dump))
}
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
defer func() {
+22 -21
View File
@@ -14,7 +14,8 @@ import (
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/net/proxyproto"
"github.com/go-gost/x/internal/util/forwarder"
@@ -87,28 +88,28 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
LocalAddr: conn.LocalAddr().String(),
Network: "tcp",
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
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{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ro.SID,
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
network := "tcp"
if _, ok := conn.(net.PacketConn); ok {
network = "udp"
}
ro.Network = network
log := h.options.Logger.WithFields(map[string]any{
"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())
pStats := xstats.Stats{}
conn = stats_wrapper.WrapConn(conn, &pStats)
@@ -150,13 +151,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
dial := func(ctx context.Context, network, address string) (net.Conn, error) {
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", address)
ro.Route = buf.String()
cc = proxyproto.WrapClientConn(
h.md.proxyProtocol,
ctxvalue.SrcAddrFromContext(ctx),
ctxvalue.DstAddrFromContext(ctx),
xctx.SrcAddrFromContext(ctx),
xctx.DstAddrFromContext(ctx),
cc)
return cc, err
@@ -232,7 +233,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
log.Debugf("%s >> %s", conn.RemoteAddr(), addr)
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, addr)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, addr)
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -250,13 +251,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
cc = proxyproto.WrapClientConn(
h.md.proxyProtocol,
ctxvalue.SrcAddrFromContext(ctx),
ctxvalue.DstAddrFromContext(ctx),
xctx.SrcAddrFromContext(ctx),
xctx.DstAddrFromContext(ctx),
cc)
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()
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
+23 -28
View File
@@ -14,7 +14,8 @@ import (
mdata "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/net/proxyproto"
"github.com/go-gost/x/internal/util/forwarder"
@@ -87,27 +88,28 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
LocalAddr: conn.LocalAddr().String(),
Network: "tcp",
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
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": ro.SID,
"client": ro.ClientIP,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
network := "tcp"
if _, ok := conn.(net.PacketConn); ok {
network = "udp"
}
ro.Network = network
log := h.options.Logger.WithFields(map[string]any{
"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())
pStats := xstats.Stats{}
conn = stats_wrapper.WrapConn(conn, &pStats)
@@ -133,13 +135,6 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
return rate_limiter.ErrRateLimit
}
var host string
if md, ok := conn.(mdata.Metadatable); ok {
if v := mdutil.GetString(md.Metadata(), "host"); v != "" {
host = v
}
}
var proto string
if network == "tcp" && h.md.sniffing {
if h.md.sniffingTimeout > 0 {
@@ -156,13 +151,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
dial := func(ctx context.Context, network, address string) (net.Conn, error) {
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", address)
ro.Route = buf.String()
cc = proxyproto.WrapClientConn(
h.md.proxyProtocol,
ctxvalue.SrcAddrFromContext(ctx),
ctxvalue.DstAddrFromContext(ctx),
xctx.SrcAddrFromContext(ctx),
xctx.DstAddrFromContext(ctx),
cc)
return cc, err
@@ -203,7 +198,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
var target *chain.Node
if host != "" {
if host := mdutil.GetString(ictx.MetadataFromContext(ctx), "host"); host != "" {
target = &chain.Node{
Addr: host,
}
@@ -239,7 +234,7 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
log.Debugf("%s >> %s", conn.RemoteAddr(), target.Addr)
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 {
log.Error(err)
@@ -256,13 +251,13 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
}
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()
cc = proxyproto.WrapClientConn(
h.md.proxyProtocol,
ctxvalue.SrcAddrFromContext(ctx),
ctxvalue.DstAddrFromContext(ctx),
xctx.SrcAddrFromContext(ctx),
xctx.DstAddrFromContext(ctx),
cc)
t := time.Now()
+23 -22
View File
@@ -30,8 +30,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
xbypass "github.com/go-gost/x/bypass"
ctxvalue "github.com/go-gost/x/ctx"
ctx_internal "github.com/go-gost/x/internal/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
@@ -130,18 +130,19 @@ func (h *httpHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler
LocalAddr: conn.LocalAddr().String(),
Proto: "http",
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
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())
@@ -292,7 +293,7 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
resp.Header.Set("Proxy-Agent", h.md.proxyAgent)
}
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
if h.options.Bypass != nil &&
h.options.Bypass.Contains(ctx, network, addr) {
@@ -338,8 +339,8 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
return h.handleProxy(ctx, conn, req, ro, log)
}
ctx = ctx_internal.ContextWithRecorderObject(ctx, ro)
ctx = ctxvalue.ContextWithLogger(ctx, log)
ctx = ictx.ContextWithRecorderObject(ctx, ro)
ctx = ictx.ContextWithLogger(ctx, log)
cc, err := h.dial(ctx, "tcp", addr)
if err != nil {
@@ -355,8 +356,8 @@ func (h *httpHandler) handleRequest(ctx context.Context, conn net.Conn, req *htt
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()
b := []byte("HTTP/1.1 200 Connection established\r\n" +
"Proxy-Agent: " + h.md.proxyAgent + "\r\n\r\n")
@@ -565,8 +566,8 @@ func (h *httpHandler) proxyRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
}
}
ctx = ctx_internal.ContextWithRecorderObject(ctx, ro)
ctx = ctxvalue.ContextWithLogger(ctx, log)
ctx = ictx.ContextWithRecorderObject(ctx, ro)
ctx = ictx.ContextWithLogger(ctx, log)
resp, err := h.transport.RoundTrip(req.WithContext(ctx))
@@ -639,21 +640,21 @@ func (h *httpHandler) proxyRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
func (h *httpHandler) dial(ctx context.Context, network, addr string) (conn net.Conn, err error) {
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: addr})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: addr})
}
if log := ctxvalue.LoggerFromContext(ctx); log != nil {
if log := ictx.LoggerFromContext(ctx); log != nil {
log.Debugf("dial: new connection to host %s", addr)
}
var buf bytes.Buffer
conn, err = h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, addr)
if ro := ctx_internal.RecorderObjectFromContext(ctx); ro != nil {
conn, err = h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, addr)
if ro := ictx.RecorderObjectFromContext(ctx); ro != nil {
ro.Route = buf.String()
if conn != nil {
ro.Src = conn.LocalAddr().String()
ro.Dst = conn.RemoteAddr().String()
ro.SrcAddr = conn.LocalAddr().String()
ro.DstAddr = conn.RemoteAddr().String()
}
}
+3 -3
View File
@@ -10,7 +10,7 @@ import (
"time"
"github.com/go-gost/core/logger"
ctxvalue "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
"github.com/go-gost/x/internal/net/udp"
"github.com/go-gost/x/internal/util/socks"
xrecorder "github.com/go-gost/x/recorder"
@@ -55,7 +55,7 @@ func (h *httpHandler) handleUDP(ctx context.Context, conn net.Conn, ro *xrecorde
// obtain a udp connection
var buf bytes.Buffer
c, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "udp", "") // UDP association
c, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "udp", "") // UDP association
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -64,7 +64,7 @@ func (h *httpHandler) handleUDP(ctx context.Context, conn net.Conn, ro *xrecorde
defer c.Close()
log.WithFields(map[string]any{"src": c.LocalAddr().String()})
ro.Src = c.LocalAddr().String()
ro.SrcAddr = c.LocalAddr().String()
pc, ok := c.(net.PacketConn)
if !ok {
+24 -22
View File
@@ -27,7 +27,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
@@ -106,18 +107,18 @@ func (h *http2Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
LocalAddr: conn.LocalAddr().String(),
Network: "tcp",
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
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{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
defer func() {
@@ -136,20 +137,17 @@ func (h *http2Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
return rate_limiter.ErrRateLimit
}
v, ok := conn.(md.Metadatable)
if !ok || v == nil {
err = errors.New("wrong connection type")
md := ictx.MetadataFromContext(ctx)
if md == nil {
err = errors.New("http2: wrong connection type")
log.Error(err)
return err
}
md := v.Metadata()
return h.roundTrip(ctx,
md.Get("w").(http.ResponseWriter),
md.Get("r").(*http.Request),
ro,
log,
)
w, _ := md.Get("w").(http.ResponseWriter)
r, _ := md.Get("r").(*http.Request)
return h.roundTrip(ctx, w, r, ro, log)
}
func (h *http2Handler) Close() error {
@@ -163,6 +161,10 @@ func (h *http2Handler) Close() error {
// when server returns an non-200 status code,
// May be fixed in go1.18.
func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req *http.Request, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error {
if w == nil || req == nil {
return nil
}
// Try to get the actual host.
// Compatible with GOST 2.x.
if v := req.Header.Get("Gost-Target"); v != "" {
@@ -237,7 +239,7 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
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))
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, "tcp", host) {
resp.StatusCode = http.StatusForbidden
@@ -254,11 +256,11 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: host})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: host})
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", host)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", host)
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -269,8 +271,8 @@ func (h *http2Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
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 req.Method == http.MethodConnect {
resp.StatusCode = http.StatusOK
+17 -13
View File
@@ -14,7 +14,8 @@ import (
"github.com/go-gost/core/hop"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
"github.com/go-gost/x/registry"
)
@@ -57,10 +58,10 @@ func (h *http3Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
start := time.Now()
log := h.options.Logger.WithFields(map[string]any{
"network": "udp",
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"network": "udp",
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
defer func() {
@@ -73,21 +74,24 @@ func (h *http3Handler) Handle(ctx context.Context, conn net.Conn, opts ...handle
return nil
}
v, ok := conn.(md.Metadatable)
if !ok || v == nil {
err := errors.New("wrong connection type")
md := ictx.MetadataFromContext(ctx)
if md == nil {
err := errors.New("http3: wrong connection type")
log.Error(err)
return err
}
md := v.Metadata()
return h.roundTrip(ctx,
md.Get("w").(http.ResponseWriter),
md.Get("r").(*http.Request),
log,
)
w, _ := md.Get("w").(http.ResponseWriter)
r, _ := md.Get("r").(*http.Request)
return h.roundTrip(ctx, w, r, log)
}
func (h *http3Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req *http.Request, log logger.Logger) error {
if w == nil || req == nil {
return nil
}
addr := req.Host
if _, port, _ := net.SplitHostPort(addr); port == "" {
addr = net.JoinHostPort(strings.Trim(addr, "[]"), "80")
@@ -110,7 +114,7 @@ func (h *http3Handler) roundTrip(ctx context.Context, w http.ResponseWriter, req
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: addr})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: addr})
}
var target *chain.Node
+14 -13
View File
@@ -14,7 +14,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/util/sniffing"
tls_util "github.com/go-gost/x/internal/util/tls"
@@ -74,24 +75,24 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
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{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -133,7 +134,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
}
ro.Host = dstAddr.String()
ro.Dst = dstAddr.String()
ro.DstAddr = dstAddr.String()
log = log.WithFields(map[string]any{
"dst": dstAddr.String(),
@@ -166,7 +167,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
ro.Host = address
var buf bytes.Buffer
cc, err = h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
cc, err = h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", address)
ro.Route = buf.String()
if err != nil && !h.md.sniffingFallback {
return nil, err
@@ -178,7 +179,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
return nil, xbypass.ErrBypass
}
var buf bytes.Buffer
cc, err = h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
cc, err = h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
ro.Route = buf.String()
ro.Host = dstAddr.String()
}
@@ -232,7 +233,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), dstAddr.Network(), dstAddr.String())
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), dstAddr.Network(), dstAddr.String())
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -241,8 +242,8 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
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()
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), dstAddr)
+14 -9
View File
@@ -11,7 +11,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
rate_limiter "github.com/go-gost/x/limiter/rate"
xstats "github.com/go-gost/x/observer/stats"
@@ -62,20 +63,24 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Network: "udp",
Service: h.options.Service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
Network: "udp",
SID: xctx.SidFromContext(ctx).String(),
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := h.options.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -123,7 +128,7 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), dstAddr.Network(), dstAddr.String())
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), dstAddr.Network(), dstAddr.String())
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -132,8 +137,8 @@ func (h *redirectHandler) Handle(ctx context.Context, conn net.Conn, opts ...han
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()
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), dstAddr)
+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()
+6 -5
View File
@@ -15,7 +15,7 @@ import (
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer"
"github.com/go-gost/relay"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/internal/util/cache"
stats_util "github.com/go-gost/x/internal/util/stats"
@@ -136,9 +136,10 @@ func (h *routerHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
start := time.Now()
log := h.log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"network": conn.LocalAddr().Network(),
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -216,7 +217,7 @@ func (h *routerHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
resp.WriteTo(conn)
return ErrUnauthorized
}
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
}
switch req.Cmd & relay.CmdMask {
+3 -2
View File
@@ -13,7 +13,7 @@ import (
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/recorder"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
serial "github.com/go-gost/x/internal/util/serial"
xrecorder "github.com/go-gost/x/recorder"
@@ -68,9 +68,10 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
log := h.options.Logger
log = log.WithFields(map[string]any{
"network": "serial",
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"sid": xctx.SidFromContext(ctx).String(),
})
conn = &recorderConn{
+14 -9
View File
@@ -13,7 +13,8 @@ import (
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/util/sniffing"
tls_util "github.com/go-gost/x/internal/util/tls"
@@ -73,24 +74,24 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Network: "tcp",
Service: h.options.Service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
Network: "tcp",
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{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -125,9 +126,13 @@ func (h *sniHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
dial := func(ctx context.Context, network, address string) (net.Conn, error) {
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", address)
ro.Route = buf.String()
if cc != nil {
ro.SrcAddr = cc.LocalAddr().String()
ro.DstAddr = cc.RemoteAddr().String()
}
return cc, err
}
dialTLS := func(ctx context.Context, network, address string, cfg *tls.Config) (net.Conn, error) {
+14 -17
View File
@@ -18,7 +18,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
"github.com/go-gost/gosocks4"
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"
"github.com/go-gost/x/internal/util/sniffing"
stats_util "github.com/go-gost/x/internal/util/stats"
@@ -104,27 +105,23 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
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)),
}
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := h.options.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -181,7 +178,7 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
log.Trace(resp)
return resp.Write(conn)
}
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
ro.ClientID = clientID
log = log.WithFields(map[string]any{"clientID": clientID})
}
@@ -215,7 +212,7 @@ func (h *socks4Handler) handleConnect(ctx context.Context, conn net.Conn, req *g
log.Debugf("%s >> %s", conn.RemoteAddr(), addr)
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -247,11 +244,11 @@ func (h *socks4Handler) handleConnect(ctx context.Context, conn net.Conn, req *g
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: addr})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: addr})
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", addr)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", addr)
ro.Route = buf.String()
if err != nil {
resp := gosocks4.NewReply(gosocks4.Failed, nil)
@@ -262,8 +259,8 @@ func (h *socks4Handler) handleConnect(ctx context.Context, conn net.Conn, req *g
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()
resp := gosocks4.NewReply(gosocks4.Granted, nil)
log.Trace(resp)
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/gosocks5"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
@@ -32,7 +32,7 @@ func (h *socks5Handler) handleBind(ctx context.Context, conn net.Conn, network,
}
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -78,7 +78,7 @@ func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, network, a
"src": ln.Addr().String(),
"bind": ln.Addr().String(),
})
ro.Src = ln.Addr().String()
ro.SrcAddr = ln.Addr().String()
socksAddr := gosocks5.Addr{}
if err := socksAddr.ParseFrom(ln.Addr().String()); err != nil {
+7 -6
View File
@@ -13,7 +13,8 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/gosocks5"
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"
"github.com/go-gost/x/internal/util/sniffing"
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
@@ -30,7 +31,7 @@ func (h *socks5Handler) handleConnect(ctx context.Context, conn net.Conn, networ
log.Debugf("%s >> %s", conn.RemoteAddr(), address)
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -62,11 +63,11 @@ func (h *socks5Handler) handleConnect(ctx context.Context, conn net.Conn, networ
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: address})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: address})
}
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 {
resp := gosocks5.NewReply(gosocks5.NetUnreachable, nil)
@@ -77,8 +78,8 @@ func (h *socks5Handler) handleConnect(ctx context.Context, conn net.Conn, networ
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()
resp := gosocks5.NewReply(gosocks5.Succeeded, nil)
log.Trace(resp)
+9 -13
View File
@@ -14,7 +14,7 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
"github.com/go-gost/gosocks5"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
"github.com/go-gost/x/internal/util/socks"
stats_util "github.com/go-gost/x/internal/util/stats"
tls_util "github.com/go-gost/x/internal/util/tls"
@@ -105,28 +105,24 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
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)),
}
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := h.options.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -167,7 +163,7 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
log.Trace(req)
if clientID := sc.ID(); clientID != "" {
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
log = log.WithFields(map[string]any{"user": clientID, "clientID": clientID})
ro.ClientID = clientID
}
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/gosocks5"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
"github.com/go-gost/x/internal/util/mux"
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
@@ -33,7 +33,7 @@ func (h *socks5Handler) handleMuxBind(ctx context.Context, conn net.Conn, networ
}
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -79,7 +79,7 @@ func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network
"bind": ln.Addr().String(),
})
ro.Src = ln.Addr().String()
ro.SrcAddr = ln.Addr().String()
socksAddr := gosocks5.Addr{}
err = socksAddr.ParseFrom(ln.Addr().String())
if err != nil {
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"github.com/go-gost/core/auth"
"github.com/go-gost/core/logger"
"github.com/go-gost/gosocks5"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
"github.com/go-gost/x/internal/util/socks"
)
@@ -70,7 +70,7 @@ func (s *serverSelector) OnSelected(method uint8, conn net.Conn) (string, net.Co
var id string
if s.Authenticator != nil {
var ok bool
ctx := ctxvalue.ContextWithClientAddr(context.Background(), ctxvalue.ClientAddr(conn.RemoteAddr().String()))
ctx := xctx.ContextWithSrcAddr(context.Background(), conn.RemoteAddr())
id, ok = s.Authenticator.Authenticate(ctx, req.Username, req.Password)
if !ok {
resp := gosocks5.NewUserPassResponse(gosocks5.UserPassVer, gosocks5.Failure)
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/gosocks5"
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/internal/net/udp"
"github.com/go-gost/x/internal/util/socks"
@@ -54,7 +55,7 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, network st
"src": cc.LocalAddr().String(),
"bind": cc.LocalAddr().String(),
})
ro.Src = cc.LocalAddr().String()
ro.SrcAddr = cc.LocalAddr().String()
saddr := gosocks5.Addr{}
saddr.ParseFrom(cc.LocalAddr().String())
@@ -75,7 +76,7 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, network st
// obtain a udp connection
var buf bytes.Buffer
c, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, "") // UDP association
c, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, "") // UDP association
ro.Route = buf.String()
if err != nil {
log.Error(err)
+6 -5
View File
@@ -11,7 +11,8 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/gosocks5"
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"
"github.com/go-gost/x/internal/net/udp"
"github.com/go-gost/x/internal/util/socks"
@@ -27,7 +28,7 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network
})
{
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
rw := traffic_wrapper.WrapReadWriter(
h.limiter,
conn,
@@ -67,7 +68,7 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network
// obtain a udp connection
var buf bytes.Buffer
c, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), network, "") // UDP association
c, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), network, "") // UDP association
ro.Route = buf.String()
if err != nil {
log.Error(err)
@@ -111,7 +112,7 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network
"src": pc.LocalAddr().String(),
"bind": pc.LocalAddr().String(),
})
ro.Src = pc.LocalAddr().String()
ro.SrcAddr = pc.LocalAddr().String()
saddr := gosocks5.Addr{}
saddr.ParseFrom(pc.LocalAddr().String())
@@ -123,7 +124,7 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network
}
log.Debugf("bind on %s OK", pc.LocalAddr())
clientID := ctxvalue.ClientIDFromContext(ctx)
clientID := xctx.ClientIDFromContext(ctx)
if h.options.Observer != nil {
pstats := h.stats.Stats(string(clientID))
pstats.Add(stats.KindTotalConns, 1)
+12 -15
View File
@@ -14,7 +14,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
"github.com/go-gost/gosocks5"
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"
"github.com/go-gost/x/internal/util/sniffing"
"github.com/go-gost/x/internal/util/ss"
@@ -83,28 +84,24 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
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)),
}
ro.ClientIP = conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
ro.ClientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
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,
"client": ro.ClientAddr,
"network": ro.Network,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -163,11 +160,11 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
switch h.md.hash {
case "host":
ctx = ctxvalue.ContextWithHash(ctx, &ctxvalue.Hash{Source: addr.String()})
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: addr.String()})
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", addr.String())
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", addr.String())
ro.Route = buf.String()
if err != nil {
return err
@@ -175,8 +172,8 @@ func (h *ssHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.H
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.sniffing {
if h.md.sniffingTimeout > 0 {
+12 -8
View File
@@ -12,7 +12,8 @@ import (
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/recorder"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
"github.com/go-gost/x/internal/util/relay"
"github.com/go-gost/x/internal/util/ss"
rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -73,21 +74,24 @@ func (h *ssuHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
Network: "udp",
Service: h.options.Service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
SID: xctx.SidFromContext(ctx).String(),
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := h.options.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -124,7 +128,7 @@ func (h *ssuHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
// obtain a udp connection
var buf bytes.Buffer
c, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "udp", "") // UDP association
c, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "udp", "") // UDP association
ro.Route = buf.String()
if err != nil {
log.Error(err)
+15 -11
View File
@@ -18,7 +18,8 @@ import (
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/util/sniffing"
sshd_util "github.com/go-gost/x/internal/util/sshd"
@@ -83,21 +84,24 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
start := time.Now()
ro := &xrecorder.HandlerRecorderObject{
Service: h.options.Service,
Network: "tcp",
Service: h.options.Service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
Time: start,
SID: string(ctxvalue.SidFromContext(ctx)),
SID: xctx.SidFromContext(ctx).String(),
}
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
log := h.options.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": ro.ClientIP,
"network": ro.Network,
"client": ro.ClientAddr,
"sid": ro.SID,
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -156,7 +160,7 @@ func (h *forwardHandler) handleDirectForward(ctx context.Context, conn *sshd_uti
}
var buf bytes.Buffer
cc, err := h.options.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", targetAddr)
cc, err := h.options.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", targetAddr)
ro.Route = buf.String()
if err != nil {
return err
@@ -164,8 +168,8 @@ func (h *forwardHandler) handleDirectForward(ctx context.Context, conn *sshd_uti
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.sniffing {
if h.md.sniffingTimeout > 0 {
@@ -261,7 +265,7 @@ func (h *forwardHandler) handleRemoteForward(ctx context.Context, conn *sshd_uti
"src": ln.Addr().String(),
"bind": ln.Addr().String(),
})
ro.Src = ln.Addr().String()
ro.SrcAddr = ln.Addr().String()
log.Debugf("bind on %s OK", ln.Addr())
+9 -5
View File
@@ -15,7 +15,8 @@ import (
"github.com/go-gost/core/hop"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
ctxvalue "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
xctx "github.com/go-gost/x/ctx"
"github.com/go-gost/x/internal/util/ss"
tap_util "github.com/go-gost/x/internal/util/tap"
"github.com/go-gost/x/registry"
@@ -76,8 +77,12 @@ func (h *tapHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
defer conn.Close()
log := h.options.Logger
v, _ := conn.(md.Metadatable)
if v == nil {
var config *tap_util.Config
if md := ictx.MetadataFromContext(ctx); md != nil {
config, _ = md.Get("config").(*tap_util.Config)
}
if config == nil {
err := errors.New("tap: wrong connection type")
log.Error(err)
return err
@@ -87,7 +92,7 @@ func (h *tapHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
log = log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -117,7 +122,6 @@ func (h *tapHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
log.Debugf("%s >> %s", conn.RemoteAddr(), target.Addr)
}
config := v.Metadata().Get("config").(*tap_util.Config)
h.handleLoop(ctx, conn, raddr, config, log)
return nil
}
+5 -4
View File
@@ -12,7 +12,8 @@ import (
"github.com/go-gost/core/handler"
"github.com/go-gost/core/hop"
md "github.com/go-gost/core/metadata"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
tun_util "github.com/go-gost/x/internal/util/tun"
"github.com/go-gost/x/registry"
)
@@ -63,8 +64,8 @@ func (h *tunHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
log := h.options.Logger
var config *tun_util.Config
if v, _ := conn.(md.Metadatable); v != nil {
config = v.Metadata().Get("config").(*tun_util.Config)
if md := ictx.MetadataFromContext(ctx); md != nil {
config, _ = md.Get("config").(*tun_util.Config)
}
if config == nil {
err := errors.New("tun: wrong connection type")
@@ -76,7 +77,7 @@ func (h *tunHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.
log = log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
+5 -4
View File
@@ -12,7 +12,8 @@ import (
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/observer"
"github.com/go-gost/core/recorder"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
stats_util "github.com/go-gost/x/internal/util/stats"
tun_util "github.com/go-gost/x/internal/util/tun"
cache_limiter "github.com/go-gost/x/limiter/traffic/cache"
@@ -86,8 +87,8 @@ func (h *tungoHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
log := h.options.Logger
var config *tun_util.Config
if v, _ := conn.(md.Metadatable); v != nil {
config = v.Metadata().Get("config").(*tun_util.Config)
if md := ictx.MetadataFromContext(ctx); md != nil {
config, _ = md.Get("config").(*tun_util.Config)
}
if config == nil {
err := errors.New("tun: wrong connection type")
@@ -99,7 +100,7 @@ func (h *tungoHandler) Handle(ctx context.Context, conn net.Conn, opts ...handle
log = log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": string(ctxvalue.SidFromContext(ctx)),
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
+22 -27
View File
@@ -16,7 +16,8 @@ import (
"github.com/go-gost/core/handler"
"github.com/go-gost/core/observer/stats"
"github.com/go-gost/core/recorder"
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"
"github.com/go-gost/x/internal/util/sniffing"
stats_util "github.com/go-gost/x/internal/util/stats"
@@ -108,27 +109,24 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
start := time.Now()
sid := xid.New().String()
ctx := ctxvalue.ContextWithSid(context.Background(), ctxvalue.Sid(sid))
ctx := xctx.ContextWithSid(context.Background(), xctx.Sid(sid))
ro := &xrecorder.HandlerRecorderObject{
Service: h.opts.Service,
Network: "tcp",
RemoteAddr: remoteAddr.String(),
Dst: dstAddr.String(),
DstAddr: dstAddr.String(),
Host: dstAddr.String(),
ClientIP: remoteAddr.String(),
ClientAddr: remoteAddr.String(),
Time: start,
SID: sid,
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
}
log := h.opts.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": ro.RemoteAddr,
"dst": ro.Dst,
"client": ro.ClientIP,
"dst": ro.DstAddr,
"client": ro.ClientAddr,
"sid": ro.SID,
})
@@ -152,7 +150,7 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
}
log.WithFields(map[string]any{
"src": ro.Src,
"src": ro.SrcAddr,
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
@@ -193,7 +191,7 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
ro.Host = address
var buf bytes.Buffer
cc, err = h.opts.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", address)
cc, err = h.opts.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", address)
ro.Route = buf.String()
if err != nil && !h.sniffingFallback {
return nil, err
@@ -202,7 +200,7 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
if cc == nil {
var buf bytes.Buffer
cc, err = h.opts.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
cc, err = h.opts.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
ro.Route = buf.String()
ro.Host = dstAddr.String()
}
@@ -250,7 +248,7 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
}
var buf bytes.Buffer
cc, err := h.opts.Router.Dial(ctxvalue.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
cc, err := h.opts.Router.Dial(ictx.ContextWithBuffer(ctx, &buf), "tcp", dstAddr.String())
ro.Route = buf.String()
if err != nil {
log.Errorf("dial %s: %v", dstAddr.String(), err)
@@ -258,8 +256,8 @@ func (h *transportHandler) handleTCPConn(originConn adapter.TCPConn) {
}
defer cc.Close()
ro.Src = cc.LocalAddr().String()
log = log.WithFields(map[string]any{"src": ro.Src})
ro.SrcAddr = cc.LocalAddr().String()
log = log.WithFields(map[string]any{"src": ro.SrcAddr})
t := time.Now()
log.Infof("%s <-> %s", remoteAddr, dstAddr)
@@ -283,26 +281,23 @@ func (h *transportHandler) handleUDPConn(uc adapter.UDPConn) {
start := time.Now()
sid := xid.New().String()
ctx := ctxvalue.ContextWithSid(context.Background(), ctxvalue.Sid(sid))
ctx := xctx.ContextWithSid(context.Background(), xctx.Sid(sid))
ro := &xrecorder.HandlerRecorderObject{
Service: h.opts.Service,
Network: "udp",
Service: h.opts.Service,
RemoteAddr: remoteAddr.String(),
Dst: dstAddr.String(),
DstAddr: dstAddr.String(),
ClientAddr: remoteAddr.String(),
Host: dstAddr.String(),
ClientIP: remoteAddr.String(),
Time: start,
SID: sid,
}
if h, _, _ := net.SplitHostPort(ro.ClientIP); h != "" {
ro.ClientIP = h
Time: start,
}
log := h.opts.Logger.WithFields(map[string]any{
"network": ro.Network,
"remote": ro.RemoteAddr,
"dst": ro.Dst,
"dst": ro.DstAddr,
"sid": ro.SID,
})
@@ -326,7 +321,7 @@ func (h *transportHandler) handleUDPConn(uc adapter.UDPConn) {
}
log.WithFields(map[string]any{
"src": ro.Src,
"src": ro.SrcAddr,
"duration": time.Since(start),
"inputBytes": ro.InputBytes,
"outputBytes": ro.OutputBytes,
@@ -340,8 +335,8 @@ func (h *transportHandler) handleUDPConn(uc adapter.UDPConn) {
}
defer cc.Close()
ro.Src = cc.LocalAddr().String()
log = log.WithFields(map[string]any{"src": ro.Src})
ro.SrcAddr = cc.LocalAddr().String()
log = log.WithFields(map[string]any{"src": ro.SrcAddr})
t := time.Now()
log.Infof("%s <-> %s", remoteAddr, dstAddr)
+29 -28
View File
@@ -27,8 +27,8 @@ import (
"github.com/go-gost/relay"
dissector "github.com/go-gost/tls-dissector"
admission "github.com/go-gost/x/admission/wrapper"
ctxvalue "github.com/go-gost/x/ctx"
ctx_internal "github.com/go-gost/x/internal/ctx"
xctx "github.com/go-gost/x/ctx"
ictx "github.com/go-gost/x/internal/ctx"
xio "github.com/go-gost/x/internal/io"
xnet "github.com/go-gost/x/internal/net"
xhttp "github.com/go-gost/x/internal/net/http"
@@ -70,20 +70,25 @@ func (ep *entrypoint) Handle(ctx context.Context, conn net.Conn) (err error) {
defer conn.Close()
ro := &xrecorder.HandlerRecorderObject{
Network: "tcp",
Node: ep.node,
Service: ep.service,
RemoteAddr: conn.RemoteAddr().String(),
LocalAddr: conn.LocalAddr().String(),
Network: "tcp",
SID: xctx.SidFromContext(ctx).String(),
Time: time.Now(),
SID: string(ctxvalue.SidFromContext(ctx)),
}
ro.ClientIP, _, _ = net.SplitHostPort(conn.RemoteAddr().String())
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
ro.ClientAddr = srcAddr.String()
}
log := ep.log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ro.SID,
"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())
@@ -132,7 +137,7 @@ func (ep *entrypoint) dial(ctx context.Context, network, addr string) (conn net.
}
}
log := ctxvalue.LoggerFromContext(ctx)
log := ictx.LoggerFromContext(ctx)
if log == nil {
log = ep.log
}
@@ -142,7 +147,7 @@ func (ep *entrypoint) dial(ctx context.Context, network, addr string) (conn net.
return nil, fmt.Errorf("%w %s", ErrTunnelRoute, addr)
}
ro := ctx_internal.RecorderObjectFromContext(ctx)
ro := ictx.RecorderObjectFromContext(ctx)
ro.ClientID = tunnelID.String()
if tunnelID.IsPrivate() {
@@ -170,7 +175,10 @@ func (ep *entrypoint) dial(ctx context.Context, network, addr string) (conn net.
if node == ep.node {
ro.Redirect = ""
clientAddr := ctxvalue.ClientAddrFromContext(ctx)
var clientAddr string
if addr := xctx.SrcAddrFromContext(ctx); addr != nil {
clientAddr = addr.String()
}
var features []relay.Feature
af := &relay.AddrFeature{}
af.ParseFrom(string(clientAddr))
@@ -311,17 +319,6 @@ func (ep *entrypoint) httpRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
},
}
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
}
clientAddr := ro.RemoteAddr
if ro.ClientIP != "" {
if _, port, _ := net.SplitHostPort(ro.RemoteAddr); port != "" {
clientAddr = net.JoinHostPort(ro.ClientIP, port)
}
}
res := &http.Response{
ProtoMajor: req.ProtoMajor,
ProtoMinor: req.ProtoMinor,
@@ -345,9 +342,13 @@ func (ep *entrypoint) httpRoundTrip(ctx context.Context, rw io.ReadWriteCloser,
}
}
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(clientAddr))
ctx = ctx_internal.ContextWithRecorderObject(ctx, ro)
ctx = ctxvalue.ContextWithLogger(ctx, log)
if clientIP := xhttp.GetClientIP(req); clientIP != nil {
ro.ClientIP = clientIP.String()
ctx = xctx.ContextWithSrcAddr(ctx, (&net.TCPAddr{IP: clientIP}))
}
ctx = ictx.ContextWithRecorderObject(ctx, ro)
ctx = ictx.ContextWithLogger(ctx, log)
resp, err := ep.transport.RoundTrip(req.WithContext(ctx))
@@ -580,9 +581,9 @@ func (ep *entrypoint) HandleTLS(ctx context.Context, conn net.Conn, ro *xrecorde
ro.Host = host
}
ctx = ctxvalue.ContextWithClientAddr(ctx, ctxvalue.ClientAddr(ro.RemoteAddr))
ctx = ctx_internal.ContextWithRecorderObject(ctx, ro)
ctx = ctxvalue.ContextWithLogger(ctx, log)
// ctx = xctx.ContextWithClientAddr(ctx, xctx.ClientAddr(ro.RemoteAddr))
ctx = ictx.ContextWithRecorderObject(ctx, ro)
ctx = ictx.ContextWithLogger(ctx, log)
cc, err := ep.dial(ctx, "tcp", host)
if err != nil {
+7 -10
View File
@@ -18,7 +18,7 @@ import (
"github.com/go-gost/core/observer"
"github.com/go-gost/core/service"
"github.com/go-gost/relay"
ctxvalue "github.com/go-gost/x/ctx"
xctx "github.com/go-gost/x/ctx"
xnet "github.com/go-gost/x/internal/net"
stats_util "github.com/go-gost/x/internal/util/stats"
rate_limiter "github.com/go-gost/x/limiter/rate"
@@ -187,19 +187,16 @@ func (h *tunnelHandler) initEntrypoint() (err error) {
func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handler.HandleOption) (err error) {
start := time.Now()
clientIP := conn.RemoteAddr().String()
if clientAddr := ctxvalue.ClientAddrFromContext(ctx); clientAddr != "" {
clientIP = string(clientAddr)
}
if h, _, _ := net.SplitHostPort(clientIP); h != "" {
clientIP = h
var clientAddr string
if srcAddr := xctx.SrcAddrFromContext(ctx); srcAddr != nil {
clientAddr = srcAddr.String()
}
log := h.log.WithFields(map[string]any{
"remote": conn.RemoteAddr().String(),
"local": conn.LocalAddr().String(),
"sid": ctxvalue.SidFromContext(ctx),
"client": clientIP,
"client": clientAddr,
"sid": xctx.SidFromContext(ctx).String(),
})
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
@@ -286,7 +283,7 @@ func (h *tunnelHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
resp.WriteTo(conn)
return ErrUnauthorized
}
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
ctx = xctx.ContextWithClientID(ctx, xctx.ClientID(clientID))
}
switch req.Cmd & relay.CmdMask {