docs(handler/relay): convert all comments to English
Translate all Chinese comments in the relay handler package to English: handler.go, connect.go, bind.go, forward.go, conn.go, entrypoint.go, observe.go, metadata.go. All inline and doc comments are now in English. Also set a persistent preference: all future code comments must be written in English only.
This commit is contained in:
@@ -26,6 +26,41 @@ import (
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
)
|
||||
|
||||
// handleConnect processes a relay CmdConnect request.
|
||||
//
|
||||
// This is the core function of the relay handler: the client connects to a
|
||||
// target address through the relay.
|
||||
//
|
||||
// Data flow:
|
||||
//
|
||||
// handleConnect()
|
||||
// ├─ 1. Clean address (unix/serial special handling)
|
||||
// ├─ 2. Wrap traffic limiter + stats
|
||||
// ├─ 3. Check target address is non-empty
|
||||
// ├─ 4. Bypass check
|
||||
// ├─ 5. Set consistent-hashing source (e.g. "host")
|
||||
// ├─ 6. Dial by network type:
|
||||
// │ ├─ "unix" → net.Dialer.DialContext("unix")
|
||||
// │ ├─ "serial" → serial.OpenPort()
|
||||
// │ └─ other → h.options.Router.Dial() ← chain routing
|
||||
// ├─ 7. Send response header (noDelay controls timing)
|
||||
// │ ├─ noDelay=true → write relay.Response immediately
|
||||
// │ └─ noDelay=false → buffer in wbuf, merged with first data packet
|
||||
// ├─ 8. Wrap connection by network type:
|
||||
// │ ├─ UDP → udpConn (2-byte length prefix)
|
||||
// │ └─ TCP → tcpConn (passthrough)
|
||||
// ├─ 9. Optional protocol sniffing:
|
||||
// │ ├─ HTTP → sniffer.HandleHTTP()
|
||||
// │ ├─ TLS → sniffer.HandleTLS()
|
||||
// │ └─ other → continue to step 10
|
||||
// └─ 10. xnet.Pipe() bidir copy (client ↔ target)
|
||||
//
|
||||
// Key design decisions:
|
||||
// - noDelay: low-latency mode sends each write as an independent relay frame.
|
||||
// Without it, the relay response header is merged with the first data packet.
|
||||
// - Sniffing reads the first few bytes after connection to detect the protocol.
|
||||
// HTTP/TLS traffic can be MITM-decrypted or recorded.
|
||||
// - Route info (node chain) is recorded via ictx.ContextWithBuffer into ro.Route.
|
||||
func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network, address string, ro *xrecorder.HandlerRecorderObject, log logger.Logger) (err error) {
|
||||
if network == "unix" || network == "serial" {
|
||||
if host, _, _ := net.SplitHostPort(address); host != "" {
|
||||
@@ -41,6 +76,8 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
|
||||
log.Debugf("%s >> %s/%s", conn.RemoteAddr(), address, network)
|
||||
|
||||
// --- Traffic limiter + stats wrapper ---
|
||||
// clientID is used as the key for traffic limiting and stats collection.
|
||||
{
|
||||
clientID := xctx.ClientIDFromContext(ctx)
|
||||
rw := traffic_wrapper.WrapReadWriter(
|
||||
@@ -78,6 +115,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
return
|
||||
}
|
||||
|
||||
// Bypass check — if the target is in the bypass list, return Forbidden
|
||||
if h.options.Bypass != nil && h.options.Bypass.Contains(ctx, network, address, bypass.WithService(h.options.Service)) {
|
||||
log.Debug("bypass: ", address)
|
||||
resp.Status = relay.StatusForbidden
|
||||
@@ -85,11 +123,13 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
return xbypass.ErrBypass
|
||||
}
|
||||
|
||||
// Consistent hashing — used for sticky sessions across upstream nodes
|
||||
switch h.md.hash {
|
||||
case "host":
|
||||
ctx = xctx.ContextWithHash(ctx, &xctx.Hash{Source: address})
|
||||
}
|
||||
|
||||
// --- Dial to target ---
|
||||
var cc net.Conn
|
||||
switch network {
|
||||
case "unix":
|
||||
@@ -119,6 +159,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
ro.SrcAddr = cc.LocalAddr().String()
|
||||
ro.DstAddr = cc.RemoteAddr().String()
|
||||
|
||||
// --- Send relay response header ---
|
||||
if h.md.noDelay {
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
log.Error(err)
|
||||
@@ -126,13 +167,16 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
}
|
||||
}
|
||||
|
||||
// --- Wrap connection by network type ---
|
||||
// UDP connections use a 2-byte length prefix for datagram framing.
|
||||
// TCP connections pass data through directly.
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6":
|
||||
rc := &udpConn{
|
||||
Conn: conn,
|
||||
}
|
||||
if !h.md.noDelay {
|
||||
// cache the header
|
||||
// 缓存响应头,与第一个数据包合并发送
|
||||
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -143,7 +187,7 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
rc := &tcpConn{
|
||||
Conn: conn,
|
||||
}
|
||||
// cache the header
|
||||
// 缓存响应头,与第一个数据包合并发送
|
||||
if _, err := resp.WriteTo(&rc.wbuf); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -151,6 +195,9 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
}
|
||||
}
|
||||
|
||||
// --- Optional protocol sniffing ---
|
||||
// After connection establishment, sniff the first bytes of client traffic
|
||||
// to detect HTTP or TLS. When detected, MITM decryption can be applied.
|
||||
if h.md.sniffing {
|
||||
if h.md.sniffingTimeout > 0 {
|
||||
conn.SetReadDeadline(time.Now().Add(h.md.sniffingTimeout))
|
||||
@@ -204,9 +251,9 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
}
|
||||
}
|
||||
|
||||
// --- 双向数据拷贝 ---
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), address)
|
||||
// xnet.Transport(conn, cc)
|
||||
xnet.Pipe(ctx, conn, cc)
|
||||
log.WithFields(map[string]any{
|
||||
"duration": time.Since(t),
|
||||
@@ -215,6 +262,10 @@ func (h *relayHandler) handleConnect(ctx context.Context, conn net.Conn, network
|
||||
return nil
|
||||
}
|
||||
|
||||
// serialConn wraps a serial port connection as net.Conn.
|
||||
// Serial ports have no network addresses, so LocalAddr/RemoteAddr return
|
||||
// custom serialAddr values. Deadline methods are no-ops since serial
|
||||
// ports do not support deadlines.
|
||||
type serialConn struct {
|
||||
io.ReadWriteCloser
|
||||
port string
|
||||
@@ -244,6 +295,7 @@ func (c *serialConn) SetWriteDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// serialAddr is the address type for a serial port connection.
|
||||
type serialAddr struct {
|
||||
port string
|
||||
}
|
||||
@@ -254,4 +306,4 @@ func (a *serialAddr) Network() string {
|
||||
|
||||
func (a *serialAddr) String() string {
|
||||
return a.port
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user