From d4c1594f6fa5f8a41a89f51ac3ffeec9d67f307c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Sat, 25 Jan 2025 15:18:09 +0000 Subject: [PATCH] UDS: Keep valid source addr --- app/proxyman/inbound/worker.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/proxyman/inbound/worker.go b/app/proxyman/inbound/worker.go index 14df725d..6ef8cca4 100644 --- a/app/proxyman/inbound/worker.go +++ b/app/proxyman/inbound/worker.go @@ -2,6 +2,7 @@ package inbound import ( "context" + "strings" "sync" "sync/atomic" "time" @@ -463,9 +464,19 @@ func (w *dsWorker) callback(conn stat.Connection) { WriteCounter: w.downlinkCounter, } } + // For most of time, unix obviously have no source addr. But if we leave it empty, it will cause panic. + // So we use gateway as source for log. + // However, there are some special situations where a valid source address might be available. + // Such as the source address parsed from X-Forwarded-For in websocket. + // In that case, we keep it. + var source net.Destination + if !strings.Contains(conn.RemoteAddr().String(), "unix") { + source = net.DestinationFromAddr(conn.RemoteAddr()) + } else { + source = net.UnixDestination(w.address) + } ctx = session.ContextWithInbound(ctx, &session.Inbound{ - // Unix have no source addr, so we use gateway as source for log. - Source: net.UnixDestination(w.address), + Source: source, Gateway: net.UnixDestination(w.address), Tag: w.tag, Conn: conn,