MCPcopy Create free account
hub / github.com/HyBuildNet/quic-relay / OnConnect

Method OnConnect

internal/handler/forwarder.go:34–84  ·  view source on GitHub ↗

OnConnect establishes a UDP session to the backend.

(ctx *Context)

Source from the content-addressed store, hash-verified

32
33// OnConnect establishes a UDP session to the backend.
34func (h *ForwarderHandler) OnConnect(ctx *Context) Result {
35 // Get backend from context (set by router handler)
36 backend := ctx.GetString("backend")
37 if backend == "" {
38 return Result{Action: Drop, Error: errors.New("no backend address")}
39 }
40
41 // Resolve backend address
42 backendAddr, err := net.ResolveUDPAddr("udp", backend)
43 if err != nil {
44 return Result{Action: Drop, Error: err}
45 }
46
47 // Create UDP connection to backend
48 backendConn, err := net.DialUDP("udp", nil, backendAddr)
49 if err != nil {
50 return Result{Action: Drop, Error: err}
51 }
52
53 // Create session
54 now := time.Now()
55 session := &Session{
56 ID: h.sessionCounter.Add(1),
57 BackendAddr: backendAddr,
58 BackendConn: backendConn,
59 CreatedAt: now,
60 }
61 session.SetClientAddr(ctx.ClientAddr)
62 session.LastActivity.Store(now.Unix())
63 ctx.Session = session
64
65 log.Printf("[forwarder] session=%d %s -> %s", session.ID, ctx.ClientAddr, backend)
66
67 // Forward the initial packet to backend
68 if len(ctx.InitialPacket) > 0 {
69 _, err := backendConn.Write(ctx.InitialPacket)
70 if err != nil {
71 log.Printf("[forwarder] failed to forward initial packet: %v", err)
72 backendConn.Close()
73 return Result{Action: Drop, Error: err}
74 }
75 }
76
77 // Clear InitialPacket to free memory (~1.4KB per session)
78 ctx.InitialPacket = nil
79
80 // Start goroutine to read from backend and send to client
81 go h.backendToClient(ctx, session)
82
83 return Result{Action: Handled}
84}
85
86// OnPacket forwards packets from client to backend.
87func (h *ForwarderHandler) OnPacket(ctx *Context, packet []byte, dir Direction) Result {

Callers

nothing calls this directly

Calls 4

SetClientAddrMethod · 0.95
backendToClientMethod · 0.95
GetStringMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected