From c95c9b6cf80ee71f8f08d8872032554d91b22ee3 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: Tue, 10 Jun 2025 17:24:53 +0000 Subject: [PATCH 01/13] Freedom: Cache UDP resolve result --- proxy/freedom/freedom.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 779992f7..14a16afe 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -346,6 +346,12 @@ type PacketWriter struct { *Handler context.Context UDPOverride net.Destination + + // Dest of udp packets might be a domain, we will resolve them to IP + // But resolver will return a random one if the domain has many IPs + // Resulting in these packets being sent to many different IPs randomly + // So, cache and keep the resolve result + resolvedUDPAddr map[string]net.Address } func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { @@ -365,9 +371,14 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { b.UDP.Port = w.UDPOverride.Port } if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() { + if ip := w.resolvedUDPAddr[b.UDP.Address.Domain()]; ip != nil { + b.UDP.Address = ip + } + } else { ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) if ip != nil { b.UDP.Address = ip + w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip } } destAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) From 980503cba4c93fc7d66f375e58e17312bc0ef5c5 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: Tue, 10 Jun 2025 17:29:13 +0000 Subject: [PATCH 02/13] map init --- proxy/freedom/freedom.go | 1 + 1 file changed, 1 insertion(+) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 14a16afe..c69fca1a 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -334,6 +334,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride Handler: h, Context: ctx, UDPOverride: UDPOverride, + resolvedUDPAddr: make(map[string]net.Address), } } From 8b8dcc20b37f9390f86ca978fac5d0e23f555b76 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: Tue, 10 Jun 2025 17:49:21 +0000 Subject: [PATCH 03/13] Fix --- proxy/freedom/freedom.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index c69fca1a..7cab2dc5 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -374,12 +374,12 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() { if ip := w.resolvedUDPAddr[b.UDP.Address.Domain()]; ip != nil { b.UDP.Address = ip - } - } else { - ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) - if ip != nil { - b.UDP.Address = ip - w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip + } else { + ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) + if ip != nil { + b.UDP.Address = ip + w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip + } } } destAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) From fc97436c2427aec01297fe91d55672c3e32e3f23 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: Tue, 10 Jun 2025 20:02:57 +0000 Subject: [PATCH 04/13] Add init check --- proxy/freedom/freedom.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 7cab2dc5..0399c3d1 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -328,13 +328,22 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride counter = statConn.WriteCounter } if c, ok := iConn.(*internet.PacketConnWrapper); ok { + // If target is a domain, it will be resolved in dialer + // check this behavior and add it to map + outbounds := session.OutboundsFromContext(ctx) + targetAddr := outbounds[len(outbounds)-1].Target.Address + resolvedUDPAddr := make(map[string]net.Address) + if targetAddr.Family().IsDomain() { + RemoteAddress, _, _ := net.SplitHostPort(conn.RemoteAddr().String()) + resolvedUDPAddr[targetAddr.String()] = net.ParseAddress(RemoteAddress) + } return &PacketWriter{ PacketConnWrapper: c, Counter: counter, Handler: h, Context: ctx, UDPOverride: UDPOverride, - resolvedUDPAddr: make(map[string]net.Address), + resolvedUDPAddr: resolvedUDPAddr, } } From 6d5ef88d96be36cd2d808f482ec8687854aea5ef 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: Fri, 13 Jun 2025 08:30:58 +0000 Subject: [PATCH 05/13] Add and use new TypedSyncMap --- common/utils/TypedSyncMap.go | 59 ++++++++++++++++++++++++++++++++++++ proxy/freedom/freedom.go | 9 +++--- 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 common/utils/TypedSyncMap.go diff --git a/common/utils/TypedSyncMap.go b/common/utils/TypedSyncMap.go new file mode 100644 index 00000000..7512c0fb --- /dev/null +++ b/common/utils/TypedSyncMap.go @@ -0,0 +1,59 @@ +package util + +import ( + "sync" +) + +// TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values. +// No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa +type TypedSyncMap[K, V any] struct { + syncMap *sync.Map +} + +func NewTypedSyncMap[K any, V any]() *TypedSyncMap[K, V] { + return &TypedSyncMap[K, V]{ + syncMap: &sync.Map{}, + } +} + +func (m *TypedSyncMap[K, V]) Clear() { + m.syncMap.Clear() +} + +func (m *TypedSyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) { + return m.syncMap.CompareAndDelete(key, old) +} + +func (m *TypedSyncMap[K, V]) CompareAndSwap(key K, old V, new V) (swapped bool) { + return m.syncMap.CompareAndSwap(key, old, new) +} + +func (m *TypedSyncMap[K, V]) Delete(key K) { + m.syncMap.Delete(key) +} + +func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) { + anyValue, ok := m.syncMap.Load(key) + return anyValue.(V), ok +} + +func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { + anyValue, loaded := m.syncMap.LoadAndDelete(key) + return anyValue.(V), loaded +} +func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { + anyActual, loaded := m.syncMap.LoadOrStore(key, value) + return anyActual.(V), loaded +} +func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) { + m.syncMap.Range(func(key, value any) bool { + return f(key.(K), value.(V)) + }) +} +func (m *TypedSyncMap[K, V]) Store(key K, value V) { + m.syncMap.Store(key, value) +} +func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) { + anyPrevious, loaded := m.syncMap.Swap(key, value) + return anyPrevious.(V), loaded +} \ No newline at end of file diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 0399c3d1..e4f49d9d 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -27,6 +27,7 @@ import ( "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/tls" + "github.com/xtls/xray-core/common/utils" ) var useSplice bool @@ -343,7 +344,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride Handler: h, Context: ctx, UDPOverride: UDPOverride, - resolvedUDPAddr: resolvedUDPAddr, + resolvedUDPAddr: util.NewTypedSyncMap[string, net.Address](), } } @@ -361,7 +362,7 @@ type PacketWriter struct { // But resolver will return a random one if the domain has many IPs // Resulting in these packets being sent to many different IPs randomly // So, cache and keep the resolve result - resolvedUDPAddr map[string]net.Address + resolvedUDPAddr *util.TypedSyncMap[string, net.Address] } func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { @@ -381,13 +382,13 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { b.UDP.Port = w.UDPOverride.Port } if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() { - if ip := w.resolvedUDPAddr[b.UDP.Address.Domain()]; ip != nil { + if ip, ok := w.resolvedUDPAddr.Load(b.UDP.Address.Domain()); ok { b.UDP.Address = ip } else { ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) if ip != nil { b.UDP.Address = ip - w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip + w.resolvedUDPAddr.Store(b.UDP.Address.Domain(), ip) } } } From f241edc679a09ed5b6720af1b03bf5ed2abbdc57 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: Fri, 13 Jun 2025 09:19:35 +0000 Subject: [PATCH 06/13] Refine --- common/utils/TypedSyncMap.go | 28 +++++++++++++++++++++++----- proxy/freedom/freedom.go | 4 ++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/common/utils/TypedSyncMap.go b/common/utils/TypedSyncMap.go index 7512c0fb..0cddc8eb 100644 --- a/common/utils/TypedSyncMap.go +++ b/common/utils/TypedSyncMap.go @@ -1,4 +1,4 @@ -package util +package utils import ( "sync" @@ -6,6 +6,7 @@ import ( // TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values. // No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa +// If sync.Map returned nil, it will return the zero value of the type V. type TypedSyncMap[K, V any] struct { syncMap *sync.Map } @@ -34,26 +35,43 @@ func (m *TypedSyncMap[K, V]) Delete(key K) { func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) { anyValue, ok := m.syncMap.Load(key) - return anyValue.(V), ok + // anyValue might be nil + if anyValue != nil { + value = anyValue.(V) + } + return value, ok } func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { anyValue, loaded := m.syncMap.LoadAndDelete(key) - return anyValue.(V), loaded + if anyValue != nil { + value = anyValue.(V) + } + return value, loaded } + func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { anyActual, loaded := m.syncMap.LoadOrStore(key, value) - return anyActual.(V), loaded + if anyActual != nil { + actual = anyActual.(V) + } + return actual, loaded } + func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) { m.syncMap.Range(func(key, value any) bool { return f(key.(K), value.(V)) }) } + func (m *TypedSyncMap[K, V]) Store(key K, value V) { m.syncMap.Store(key, value) } + func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) { anyPrevious, loaded := m.syncMap.Swap(key, value) - return anyPrevious.(V), loaded + if anyPrevious != nil { + previous = anyPrevious.(V) + } + return previous, loaded } \ No newline at end of file diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index e4f49d9d..cb5c7ead 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -344,7 +344,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride Handler: h, Context: ctx, UDPOverride: UDPOverride, - resolvedUDPAddr: util.NewTypedSyncMap[string, net.Address](), + resolvedUDPAddr: utils.NewTypedSyncMap[string, net.Address](), } } @@ -362,7 +362,7 @@ type PacketWriter struct { // But resolver will return a random one if the domain has many IPs // Resulting in these packets being sent to many different IPs randomly // So, cache and keep the resolve result - resolvedUDPAddr *util.TypedSyncMap[string, net.Address] + resolvedUDPAddr *utils.TypedSyncMap[string, net.Address] } func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { From 2c79f675dac9d767adb77ac0b2e78cc18cd28b7c 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, 14 Jun 2025 08:29:48 +0000 Subject: [PATCH 07/13] Use LoadOrStore --- proxy/freedom/freedom.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index cb5c7ead..8584791c 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -387,8 +387,7 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { } else { ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) if ip != nil { - b.UDP.Address = ip - w.resolvedUDPAddr.Store(b.UDP.Address.Domain(), ip) + b.UDP.Address, _ = w.resolvedUDPAddr.LoadOrStore(b.UDP.Address.Domain(), ip) } } } From d38529b016cba0ca4bf85cd6254eaf685585131f 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, 28 Jun 2025 11:57:12 +0000 Subject: [PATCH 08/13] Also handle ASIS and apply force strategy --- proxy/freedom/freedom.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 8584791c..2893cc2e 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -18,6 +18,7 @@ import ( "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/common/signal" "github.com/xtls/xray-core/common/task" + "github.com/xtls/xray-core/common/utils" "github.com/xtls/xray-core/core" "github.com/xtls/xray-core/features/dns" "github.com/xtls/xray-core/features/policy" @@ -27,7 +28,6 @@ import ( "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/stat" "github.com/xtls/xray-core/transport/internet/tls" - "github.com/xtls/xray-core/common/utils" ) var useSplice bool @@ -381,11 +381,26 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.UDPOverride.Port != 0 { b.UDP.Port = w.UDPOverride.Port } - if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() { + if b.UDP.Address.Family().IsDomain() { if ip, ok := w.resolvedUDPAddr.Load(b.UDP.Address.Domain()); ok { b.UDP.Address = ip } else { - ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) + ShouldUseSystemResolver := true + if w.Handler.config.hasStrategy() { + ip = w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil) + if ip != nil { + ShouldUseSystemResolver = false + } + // drop packet if resolve failed when forceIP + if ip == nil && w.Handler.config.forceIP() { + b.Release() + continue + } + } + if ShouldUseSystemResolver { + udpAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) + ip = net.IPAddress(udpAddr.IP) + } if ip != nil { b.UDP.Address, _ = w.resolvedUDPAddr.LoadOrStore(b.UDP.Address.Domain(), ip) } From b44a5ab894f7d9a1700a1e7b90d684f8028fedaa 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, 28 Jun 2025 12:44:29 +0000 Subject: [PATCH 09/13] ensure udpAddr is not nil --- proxy/freedom/freedom.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 2893cc2e..1e4e077d 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -398,8 +398,10 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { } } if ShouldUseSystemResolver { - udpAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) - ip = net.IPAddress(udpAddr.IP) + udpAddr, err := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) + if err == nil { + ip = net.IPAddress(udpAddr.IP) + } } if ip != nil { b.UDP.Address, _ = w.resolvedUDPAddr.LoadOrStore(b.UDP.Address.Domain(), ip) From bc8f92d8f1afea878a3fc0572d3fbbd185c653bf 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, 28 Jun 2025 16:49:25 +0000 Subject: [PATCH 10/13] ensure udpAddr is not nil --- proxy/freedom/freedom.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index 1e4e077d..fd364c69 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -399,7 +399,10 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { } if ShouldUseSystemResolver { udpAddr, err := net.ResolveUDPAddr("udp", b.UDP.NetAddr()) - if err == nil { + if err != nil { + b.Release() + continue + } else { ip = net.IPAddress(udpAddr.IP) } } From 1a860bbf52c195fc23438a0abccef44025001255 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: Sun, 29 Jun 2025 16:36:52 +0000 Subject: [PATCH 11/13] Fix init map --- proxy/freedom/freedom.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index fd364c69..df3efabe 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -333,10 +333,10 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride // check this behavior and add it to map outbounds := session.OutboundsFromContext(ctx) targetAddr := outbounds[len(outbounds)-1].Target.Address - resolvedUDPAddr := make(map[string]net.Address) + resolvedUDPAddr := utils.NewTypedSyncMap[string, net.Address]() if targetAddr.Family().IsDomain() { RemoteAddress, _, _ := net.SplitHostPort(conn.RemoteAddr().String()) - resolvedUDPAddr[targetAddr.String()] = net.ParseAddress(RemoteAddress) + resolvedUDPAddr.Store(targetAddr.String(), net.ParseAddress(RemoteAddress)) } return &PacketWriter{ PacketConnWrapper: c, @@ -344,7 +344,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride Handler: h, Context: ctx, UDPOverride: UDPOverride, - resolvedUDPAddr: utils.NewTypedSyncMap[string, net.Address](), + resolvedUDPAddr: resolvedUDPAddr, } } From 8ddd9085c16feac76f3d687324217043ecb76290 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: Wed, 16 Jul 2025 03:44:04 +0000 Subject: [PATCH 12/13] Fix 0 len outbounds --- proxy/freedom/freedom.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/proxy/freedom/freedom.go b/proxy/freedom/freedom.go index df3efabe..6d8664a8 100644 --- a/proxy/freedom/freedom.go +++ b/proxy/freedom/freedom.go @@ -203,7 +203,7 @@ func (h *Handler) Process(ctx context.Context, link *transport.Link, dialer inte writer = buf.NewWriter(conn) } } else { - writer = NewPacketWriter(conn, h, ctx, UDPOverride) + writer = NewPacketWriter(conn, h, ctx, UDPOverride, destination) if h.config.Noises != nil { errors.LogDebug(ctx, "NOISE", h.config.Noises) writer = &NoisePacketWriter{ @@ -318,7 +318,8 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return buf.MultiBuffer{b}, nil } -func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride net.Destination) buf.Writer { +// DialDest means the dial target used in the dialer when creating conn +func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride net.Destination, DialDest net.Destination) buf.Writer { iConn := conn statConn, ok := iConn.(*stat.CounterConnection) if ok { @@ -329,14 +330,12 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride counter = statConn.WriteCounter } if c, ok := iConn.(*internet.PacketConnWrapper); ok { - // If target is a domain, it will be resolved in dialer + // If DialDest is a domain, it will be resolved in dialer // check this behavior and add it to map - outbounds := session.OutboundsFromContext(ctx) - targetAddr := outbounds[len(outbounds)-1].Target.Address resolvedUDPAddr := utils.NewTypedSyncMap[string, net.Address]() - if targetAddr.Family().IsDomain() { + if DialDest.Address.Family().IsDomain() { RemoteAddress, _, _ := net.SplitHostPort(conn.RemoteAddr().String()) - resolvedUDPAddr.Store(targetAddr.String(), net.ParseAddress(RemoteAddress)) + resolvedUDPAddr.Store(DialDest.Address.String(), net.ParseAddress(RemoteAddress)) } return &PacketWriter{ PacketConnWrapper: c, From a1960c7ccbd9fa96ece8285c780de277f2601f07 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, 19 Jul 2025 16:25:07 +0000 Subject: [PATCH 13/13] Rename and copy all comments from sync.Map --- common/utils/TypedSyncMap.go | 77 ----------------------- common/utils/typed_sync_map.go | 112 +++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 77 deletions(-) delete mode 100644 common/utils/TypedSyncMap.go create mode 100644 common/utils/typed_sync_map.go diff --git a/common/utils/TypedSyncMap.go b/common/utils/TypedSyncMap.go deleted file mode 100644 index 0cddc8eb..00000000 --- a/common/utils/TypedSyncMap.go +++ /dev/null @@ -1,77 +0,0 @@ -package utils - -import ( - "sync" -) - -// TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values. -// No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa -// If sync.Map returned nil, it will return the zero value of the type V. -type TypedSyncMap[K, V any] struct { - syncMap *sync.Map -} - -func NewTypedSyncMap[K any, V any]() *TypedSyncMap[K, V] { - return &TypedSyncMap[K, V]{ - syncMap: &sync.Map{}, - } -} - -func (m *TypedSyncMap[K, V]) Clear() { - m.syncMap.Clear() -} - -func (m *TypedSyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) { - return m.syncMap.CompareAndDelete(key, old) -} - -func (m *TypedSyncMap[K, V]) CompareAndSwap(key K, old V, new V) (swapped bool) { - return m.syncMap.CompareAndSwap(key, old, new) -} - -func (m *TypedSyncMap[K, V]) Delete(key K) { - m.syncMap.Delete(key) -} - -func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) { - anyValue, ok := m.syncMap.Load(key) - // anyValue might be nil - if anyValue != nil { - value = anyValue.(V) - } - return value, ok -} - -func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { - anyValue, loaded := m.syncMap.LoadAndDelete(key) - if anyValue != nil { - value = anyValue.(V) - } - return value, loaded -} - -func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { - anyActual, loaded := m.syncMap.LoadOrStore(key, value) - if anyActual != nil { - actual = anyActual.(V) - } - return actual, loaded -} - -func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) { - m.syncMap.Range(func(key, value any) bool { - return f(key.(K), value.(V)) - }) -} - -func (m *TypedSyncMap[K, V]) Store(key K, value V) { - m.syncMap.Store(key, value) -} - -func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) { - anyPrevious, loaded := m.syncMap.Swap(key, value) - if anyPrevious != nil { - previous = anyPrevious.(V) - } - return previous, loaded -} \ No newline at end of file diff --git a/common/utils/typed_sync_map.go b/common/utils/typed_sync_map.go new file mode 100644 index 00000000..39524a6f --- /dev/null +++ b/common/utils/typed_sync_map.go @@ -0,0 +1,112 @@ +package utils + +import ( + "sync" +) + +// TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values. +// No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa +// If sync.Map methods returned nil, it will return the zero value of the type V. +type TypedSyncMap[K, V any] struct { + syncMap *sync.Map +} + +// NewTypedSyncMap creates a new TypedSyncMap +// K is key type, V is value type +// It is recommended to use pointer types for V because sync.Map might return nil +// If sync.Map methods really returned nil, it will return the zero value of the type V +func NewTypedSyncMap[K any, V any]() *TypedSyncMap[K, V] { + return &TypedSyncMap[K, V]{ + syncMap: &sync.Map{}, + } +} + +// Clear deletes all the entries, resulting in an empty Map. +func (m *TypedSyncMap[K, V]) Clear() { + m.syncMap.Clear() +} + +// CompareAndDelete deletes the entry for key if its value is equal to old. +// The old value must be of a comparable type. +// +// If there is no current value for key in the map, CompareAndDelete +// returns false (even if the old value is the nil interface value). +func (m *TypedSyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) { + return m.syncMap.CompareAndDelete(key, old) +} + +// CompareAndSwap swaps the old and new values for key +// if the value stored in the map is equal to old. +// The old value must be of a comparable type. +func (m *TypedSyncMap[K, V]) CompareAndSwap(key K, old V, new V) (swapped bool) { + return m.syncMap.CompareAndSwap(key, old, new) +} + +// Delete deletes the value for a key. +func (m *TypedSyncMap[K, V]) Delete(key K) { + m.syncMap.Delete(key) +} + +// Load returns the value stored in the map for a key, or nil if no +// value is present. +// The ok result indicates whether value was found in the map. +func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) { + anyValue, ok := m.syncMap.Load(key) + // anyValue might be nil + if anyValue != nil { + value = anyValue.(V) + } + return value, ok +} + +// LoadAndDelete deletes the value for a key, returning the previous value if any. +// The loaded result reports whether the key was present. +func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { + anyValue, loaded := m.syncMap.LoadAndDelete(key) + if anyValue != nil { + value = anyValue.(V) + } + return value, loaded +} + +// LoadOrStore returns the existing value for the key if present. +// Otherwise, it stores and returns the given value. +// The loaded result is true if the value was loaded, false if stored. +func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { + anyActual, loaded := m.syncMap.LoadOrStore(key, value) + if anyActual != nil { + actual = anyActual.(V) + } + return actual, loaded +} + +// Range calls f sequentially for each key and value present in the map. +// If f returns false, range stops the iteration. +// +// Range does not necessarily correspond to any consistent snapshot of the Map's +// contents: no key will be visited more than once, but if the value for any key +// is stored or deleted concurrently (including by f), Range may reflect any +// mapping for that key from any point during the Range call. Range does not +// block other methods on the receiver; even f itself may call any method on m. +// +// Range may be O(N) with the number of elements in the map even if f returns +// false after a constant number of calls. +func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) { + m.syncMap.Range(func(key, value any) bool { + return f(key.(K), value.(V)) + }) +} + +// Store sets the value for a key. +func (m *TypedSyncMap[K, V]) Store(key K, value V) { + m.syncMap.Store(key, value) +} + +// Swap swaps the value for a key and returns the previous value if any. The loaded result reports whether the key was present. +func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) { + anyPrevious, loaded := m.syncMap.Swap(key, value) + if anyPrevious != nil { + previous = anyPrevious.(V) + } + return previous, loaded +} \ No newline at end of file