add src field for logger and recorder
This commit is contained in:
@@ -121,10 +121,11 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"client": ro.ClientIP,
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"client": ro.ClientIP,
|
||||
"network": ro.Network,
|
||||
})
|
||||
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
|
||||
@@ -163,6 +164,11 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
return err
|
||||
}
|
||||
|
||||
if userid := string(req.Userid); userid != "" {
|
||||
log = log.WithFields(map[string]any{"user": userid})
|
||||
ro.ClientID = userid
|
||||
}
|
||||
|
||||
ro.Host = req.Addr.String()
|
||||
log.Trace(req)
|
||||
|
||||
@@ -177,6 +183,7 @@ func (h *socks4Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
}
|
||||
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
|
||||
ro.ClientID = clientID
|
||||
log = log.WithFields(map[string]any{"clientID": clientID})
|
||||
}
|
||||
|
||||
switch req.Cmd {
|
||||
@@ -254,6 +261,10 @@ 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()
|
||||
|
||||
resp := gosocks4.NewReply(gosocks4.Granted, nil)
|
||||
log.Trace(resp)
|
||||
if err := resp.Write(conn); err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package v5
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
@@ -14,11 +13,12 @@ import (
|
||||
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"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
)
|
||||
|
||||
func (h *socks5Handler) handleBind(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) error {
|
||||
func (h *socks5Handler) handleBind(ctx context.Context, conn net.Conn, network, address string, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error {
|
||||
log = log.WithFields(map[string]any{
|
||||
"dst": fmt.Sprintf("%s/%s", address, network),
|
||||
"dst": address,
|
||||
"cmd": "bind",
|
||||
})
|
||||
|
||||
@@ -56,10 +56,10 @@ func (h *socks5Handler) handleBind(ctx context.Context, conn net.Conn, network,
|
||||
}
|
||||
|
||||
// BIND does not support chain.
|
||||
return h.bindLocal(ctx, conn, network, address, log)
|
||||
return h.bindLocal(ctx, conn, network, address, ro, log)
|
||||
}
|
||||
|
||||
func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) error {
|
||||
func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, network, address string, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error {
|
||||
lc := xnet.ListenConfig{
|
||||
Netns: h.options.Netns,
|
||||
}
|
||||
@@ -74,6 +74,12 @@ func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, network, a
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"src": ln.Addr().String(),
|
||||
"bind": ln.Addr().String(),
|
||||
})
|
||||
ro.Src = ln.Addr().String()
|
||||
|
||||
socksAddr := gosocks5.Addr{}
|
||||
if err := socksAddr.ParseFrom(ln.Addr().String()); err != nil {
|
||||
log.Warn(err)
|
||||
@@ -90,10 +96,6 @@ func (h *socks5Handler) bindLocal(ctx context.Context, conn net.Conn, network, a
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"bind": fmt.Sprintf("%s/%s", ln.Addr(), ln.Addr().Network()),
|
||||
})
|
||||
|
||||
log.Debugf("bind on %s OK", ln.Addr())
|
||||
|
||||
h.serveBind(ctx, conn, ln, log)
|
||||
|
||||
@@ -76,6 +76,10 @@ 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()
|
||||
|
||||
resp := gosocks5.NewReply(gosocks5.Succeeded, nil)
|
||||
log.Trace(resp)
|
||||
if err := resp.Write(conn); err != nil {
|
||||
|
||||
@@ -122,10 +122,11 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
}
|
||||
|
||||
log := h.options.Logger.WithFields(map[string]any{
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"client": ro.ClientIP,
|
||||
"remote": conn.RemoteAddr().String(),
|
||||
"local": conn.LocalAddr().String(),
|
||||
"sid": ctxvalue.SidFromContext(ctx),
|
||||
"client": ro.ClientIP,
|
||||
"network": ro.Network,
|
||||
})
|
||||
log.Infof("%s <> %s", conn.RemoteAddr(), conn.LocalAddr())
|
||||
|
||||
@@ -166,7 +167,7 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
|
||||
if clientID := sc.ID(); clientID != "" {
|
||||
ctx = ctxvalue.ContextWithClientID(ctx, ctxvalue.ClientID(clientID))
|
||||
log = log.WithFields(map[string]any{"user": clientID})
|
||||
log = log.WithFields(map[string]any{"user": clientID, "clientID": clientID})
|
||||
ro.ClientID = clientID
|
||||
}
|
||||
|
||||
@@ -180,9 +181,9 @@ func (h *socks5Handler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
||||
case gosocks5.CmdConnect:
|
||||
return h.handleConnect(ctx, conn, "tcp", address, ro, log)
|
||||
case gosocks5.CmdBind:
|
||||
return h.handleBind(ctx, conn, "tcp", address, log)
|
||||
return h.handleBind(ctx, conn, "tcp", address, ro, log)
|
||||
case socks.CmdMuxBind:
|
||||
return h.handleMuxBind(ctx, conn, "tcp", address, log)
|
||||
return h.handleMuxBind(ctx, conn, "tcp", address, ro, log)
|
||||
case gosocks5.CmdUdp:
|
||||
ro.Network = "udp"
|
||||
return h.handleUDP(ctx, conn, ro, log)
|
||||
|
||||
@@ -2,7 +2,6 @@ package v5
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
@@ -15,11 +14,12 @@ import (
|
||||
"github.com/go-gost/x/internal/util/mux"
|
||||
traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper"
|
||||
stats_wrapper "github.com/go-gost/x/observer/stats/wrapper"
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
)
|
||||
|
||||
func (h *socks5Handler) handleMuxBind(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) error {
|
||||
func (h *socks5Handler) handleMuxBind(ctx context.Context, conn net.Conn, network, address string, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error {
|
||||
log = log.WithFields(map[string]any{
|
||||
"dst": fmt.Sprintf("%s/%s", address, network),
|
||||
"dst": address,
|
||||
"cmd": "mbind",
|
||||
})
|
||||
|
||||
@@ -56,10 +56,10 @@ func (h *socks5Handler) handleMuxBind(ctx context.Context, conn net.Conn, networ
|
||||
conn = xnet.NewReadWriteConn(rw, rw, conn)
|
||||
}
|
||||
|
||||
return h.muxBindLocal(ctx, conn, network, address, log)
|
||||
return h.muxBindLocal(ctx, conn, network, address, ro, log)
|
||||
}
|
||||
|
||||
func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network, address string, log logger.Logger) error {
|
||||
func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network, address string, ro *xrecorder.HandlerRecorderObject, log logger.Logger) error {
|
||||
lc := xnet.ListenConfig{
|
||||
Netns: h.options.Netns,
|
||||
}
|
||||
@@ -74,6 +74,12 @@ func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"src": ln.Addr().String(),
|
||||
"bind": ln.Addr().String(),
|
||||
})
|
||||
|
||||
ro.Src = ln.Addr().String()
|
||||
socksAddr := gosocks5.Addr{}
|
||||
err = socksAddr.ParseFrom(ln.Addr().String())
|
||||
if err != nil {
|
||||
@@ -91,10 +97,6 @@ func (h *socks5Handler) muxBindLocal(ctx context.Context, conn net.Conn, network
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"bind": fmt.Sprintf("%s/%s", ln.Addr(), ln.Addr().Network()),
|
||||
})
|
||||
|
||||
log.Debugf("bind on %s OK", ln.Addr())
|
||||
|
||||
return h.serveMuxBind(ctx, conn, ln, log)
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
@@ -49,6 +48,12 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, ro *xrecor
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"src": cc.LocalAddr().String(),
|
||||
"bind": cc.LocalAddr().String(),
|
||||
})
|
||||
ro.Src = cc.LocalAddr().String()
|
||||
|
||||
saddr := gosocks5.Addr{}
|
||||
saddr.ParseFrom(cc.LocalAddr().String())
|
||||
reply := gosocks5.NewReply(gosocks5.Succeeded, &saddr)
|
||||
@@ -58,9 +63,6 @@ func (h *socks5Handler) handleUDP(ctx context.Context, conn net.Conn, ro *xrecor
|
||||
return err
|
||||
}
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"bind": fmt.Sprintf("%s/%s", cc.LocalAddr(), cc.LocalAddr().Network()),
|
||||
})
|
||||
log.Debugf("bind on %s OK", cc.LocalAddr())
|
||||
|
||||
// obtain a udp connection
|
||||
|
||||
@@ -102,9 +102,16 @@ func (h *socks5Handler) handleUDPTun(ctx context.Context, conn net.Conn, network
|
||||
reply.Write(conn)
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
defer pc.Close()
|
||||
|
||||
log = log.WithFields(map[string]any{
|
||||
"src": pc.LocalAddr().String(),
|
||||
"bind": pc.LocalAddr().String(),
|
||||
})
|
||||
ro.Src = pc.LocalAddr().String()
|
||||
|
||||
saddr := gosocks5.Addr{}
|
||||
saddr.ParseFrom(pc.LocalAddr().String())
|
||||
reply := gosocks5.NewReply(gosocks5.Succeeded, &saddr)
|
||||
|
||||
Reference in New Issue
Block a user