feat: add stateless UDP forwarding mode (#853)
Add stateless=true metadata option to UDP listener/handler for raw datagram forwarding without per-client session tracking, similar to NGINX Stream Module behavior. - udp.NewListener: add Stateless field to ListenConfig; branch in NewListener/Accept to skip connPool/listenLoop when stateless - datagramConn: lightweight net.Conn+PacketConn wrapping a single UDP datagram — no channels, mutexes, or pool logic - forward handler: add handleRawDatagram for single request-response cycle; extract dialTarget to share hop selection/dial/proxyproto preamble with handleRawForwarding Usage: gost -L "udp://:10000/127.0.0.1:2000?stateless=true"
This commit is contained in:
@@ -16,10 +16,20 @@ import (
|
||||
xrecorder "github.com/go-gost/x/recorder"
|
||||
)
|
||||
|
||||
// handleRawForwarding performs node selection, dials the target through the
|
||||
// router, and pipes the raw connection. It is used when sniffing is disabled
|
||||
// or the protocol was not HTTP/TLS.
|
||||
func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn, ro *xrecorder.HandlerRecorderObject, log logger.Logger, network, proto string) error {
|
||||
// dialResult is returned by dialTarget, carrying the dialed connection
|
||||
// and associated state that callers need for subsequent I/O and logging.
|
||||
type dialResult struct {
|
||||
cc net.Conn
|
||||
log logger.Logger
|
||||
target *chain.Node
|
||||
}
|
||||
|
||||
// dialTarget performs node selection, Router.Dial, proxy protocol wrapping,
|
||||
// and recorder population — the shared preamble for both stream forwarding
|
||||
// (handleRawForwarding) and datagram forwarding (handleRawDatagram).
|
||||
//
|
||||
// The caller must close cc when done.
|
||||
func (h *forwardHandler) dialTarget(ctx context.Context, conn net.Conn, ro *xrecorder.HandlerRecorderObject, log logger.Logger, network, proto string) (*dialResult, error) {
|
||||
target := &chain.Node{}
|
||||
if curHop := h.getHop(); curHop != nil {
|
||||
target = curHop.Select(ctx,
|
||||
@@ -28,7 +38,7 @@ func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn,
|
||||
}
|
||||
if target == nil {
|
||||
log.Error(errNodeNotAvailable)
|
||||
return errNodeNotAvailable
|
||||
return nil, errNodeNotAvailable
|
||||
}
|
||||
addr := target.Addr
|
||||
if opts := target.Options(); opts != nil {
|
||||
@@ -63,12 +73,11 @@ func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn,
|
||||
if marker := target.Marker(); marker != nil {
|
||||
marker.Mark()
|
||||
}
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
if marker := target.Marker(); marker != nil {
|
||||
marker.Reset()
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
cc = proxyproto.WrapClientConn(
|
||||
h.md.proxyProtocol,
|
||||
@@ -80,14 +89,87 @@ func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn,
|
||||
ro.SrcAddr = cc.LocalAddr().String()
|
||||
ro.DstAddr = cc.RemoteAddr().String()
|
||||
|
||||
return &dialResult{
|
||||
cc: cc,
|
||||
log: log,
|
||||
target: target,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// handleRawForwarding performs node selection, dials the target through the
|
||||
// router, and pipes the raw connection. It is used when sniffing is disabled
|
||||
// or the protocol was not HTTP/TLS.
|
||||
func (h *forwardHandler) handleRawForwarding(ctx context.Context, conn net.Conn, ro *xrecorder.HandlerRecorderObject, log logger.Logger, network, proto string) error {
|
||||
dr, err := h.dialTarget(ctx, conn, ro, log, network, proto)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dr.cc.Close()
|
||||
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
||||
if err := xnet.Pipe(ctx, conn, cc, xnet.WithReadTimeout(h.md.idleTimeout)); err != nil {
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), dr.target.Addr)
|
||||
if err := xnet.Pipe(ctx, conn, dr.cc, xnet.WithReadTimeout(h.md.idleTimeout)); err != nil {
|
||||
log.Debugf("pipe: %v", err)
|
||||
}
|
||||
log.WithFields(map[string]any{
|
||||
"duration": time.Since(t),
|
||||
}).Infof("%s >-< %s", conn.RemoteAddr(), target.Addr)
|
||||
}).Infof("%s >-< %s", conn.RemoteAddr(), dr.target.Addr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleRawDatagram forwards a single UDP datagram to the selected node and
|
||||
// writes the response back. Unlike handleRawForwarding (which uses xnet.Pipe
|
||||
// for bidirectional stream copy), this performs a single request-response
|
||||
// cycle — appropriate for stateless UDP forwarding where each packet is
|
||||
// independent and no session is maintained.
|
||||
//
|
||||
// The method reuses dialTarget for the shared hop-selection, Router.Dial,
|
||||
// proxyproto, and recorder preamble.
|
||||
func (h *forwardHandler) handleRawDatagram(ctx context.Context, conn net.Conn, ro *xrecorder.HandlerRecorderObject, log logger.Logger, network, proto string) error {
|
||||
dr, err := h.dialTarget(ctx, conn, ro, log, network, proto)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dr.cc.Close()
|
||||
|
||||
t := time.Now()
|
||||
log.Infof("%s <-> %s", conn.RemoteAddr(), dr.target.Addr)
|
||||
|
||||
bufp := make([]byte, h.md.bufferSize)
|
||||
n, err := conn.Read(bufp)
|
||||
if err != nil {
|
||||
log.Debugf("conn read: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := dr.cc.Write(bufp[:n]); err != nil {
|
||||
log.Debugf("outbound write: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Read the response into the same buffer to avoid a second allocation.
|
||||
if err := dr.cc.SetReadDeadline(time.Now().Add(h.md.readTimeout)); err != nil {
|
||||
log.Debugf("set read deadline: %v", err)
|
||||
return err
|
||||
}
|
||||
n, err = dr.cc.Read(bufp)
|
||||
if err != nil {
|
||||
log.Debugf("outbound read: %v", err)
|
||||
return err
|
||||
}
|
||||
// Clear the deadline so it doesn't affect subsequent use of the
|
||||
// underlying connection (e.g., keep-alive pools).
|
||||
dr.cc.SetReadDeadline(time.Time{})
|
||||
|
||||
if _, err := conn.Write(bufp[:n]); err != nil {
|
||||
log.Debugf("conn write: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.WithFields(map[string]any{
|
||||
"duration": time.Since(t),
|
||||
}).Infof("%s >-< %s", conn.RemoteAddr(), dr.target.Addr)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -206,6 +206,10 @@ func (h *forwardHandler) Handle(ctx context.Context, conn net.Conn, opts ...hand
|
||||
return errRouterNotAvailable
|
||||
}
|
||||
|
||||
if h.md.stateless {
|
||||
return h.handleRawDatagram(ctx, conn, ro, log, network, "udp")
|
||||
}
|
||||
|
||||
var proto string
|
||||
if network == "tcp" && h.md.sniffing {
|
||||
if h.md.sniffingTimeout > 0 {
|
||||
|
||||
@@ -36,6 +36,9 @@ type metadata struct {
|
||||
privateKey crypto.PrivateKey
|
||||
alpn string
|
||||
mitmBypass bypass.Bypass
|
||||
|
||||
stateless bool
|
||||
bufferSize int
|
||||
}
|
||||
|
||||
func (h *forwardHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
@@ -70,5 +73,11 @@ func (h *forwardHandler) parseMetadata(md mdata.Metadata) (err error) {
|
||||
h.md.alpn = mdutil.GetString(md, "mitm.alpn")
|
||||
h.md.mitmBypass = registry.BypassRegistry().Get(mdutil.GetString(md, "mitm.bypass"))
|
||||
|
||||
h.md.stateless = mdutil.GetBool(md, "stateless")
|
||||
h.md.bufferSize = mdutil.GetInt(md, "bufferSize", "readBufferSize", "udp.bufferSize")
|
||||
if h.md.bufferSize <= 0 {
|
||||
h.md.bufferSize = 4096
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user