mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-06-28 23:02:03 +08:00
Compare commits
No commits in common. "4bc9f8ed3773f46fb153095a9cd9a3b3408eb439" and "f4246e9314a610596291061613c587aeafbbf3c2" have entirely different histories.
4bc9f8ed37
...
f4246e9314
@ -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
|
|
||||||
}
|
|
@ -27,7 +27,6 @@ import (
|
|||||||
"github.com/xtls/xray-core/transport/internet"
|
"github.com/xtls/xray-core/transport/internet"
|
||||||
"github.com/xtls/xray-core/transport/internet/stat"
|
"github.com/xtls/xray-core/transport/internet/stat"
|
||||||
"github.com/xtls/xray-core/transport/internet/tls"
|
"github.com/xtls/xray-core/transport/internet/tls"
|
||||||
"github.com/xtls/xray-core/common/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var useSplice bool
|
var useSplice bool
|
||||||
@ -344,7 +343,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride
|
|||||||
Handler: h,
|
Handler: h,
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
UDPOverride: UDPOverride,
|
UDPOverride: UDPOverride,
|
||||||
resolvedUDPAddr: utils.NewTypedSyncMap[string, net.Address](),
|
resolvedUDPAddr: resolvedUDPAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -362,7 +361,7 @@ type PacketWriter struct {
|
|||||||
// But resolver will return a random one if the domain has many IPs
|
// But resolver will return a random one if the domain has many IPs
|
||||||
// Resulting in these packets being sent to many different IPs randomly
|
// Resulting in these packets being sent to many different IPs randomly
|
||||||
// So, cache and keep the resolve result
|
// So, cache and keep the resolve result
|
||||||
resolvedUDPAddr *utils.TypedSyncMap[string, net.Address]
|
resolvedUDPAddr map[string]net.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
@ -382,13 +381,13 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||||||
b.UDP.Port = w.UDPOverride.Port
|
b.UDP.Port = w.UDPOverride.Port
|
||||||
}
|
}
|
||||||
if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() {
|
if w.Handler.config.hasStrategy() && b.UDP.Address.Family().IsDomain() {
|
||||||
if ip, ok := w.resolvedUDPAddr.Load(b.UDP.Address.Domain()); ok {
|
if ip := w.resolvedUDPAddr[b.UDP.Address.Domain()]; ip != nil {
|
||||||
b.UDP.Address = ip
|
b.UDP.Address = ip
|
||||||
} else {
|
} else {
|
||||||
ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil)
|
ip := w.Handler.resolveIP(w.Context, b.UDP.Address.Domain(), nil)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
b.UDP.Address = ip
|
b.UDP.Address = ip
|
||||||
w.resolvedUDPAddr.Store(b.UDP.Address.Domain(), ip)
|
w.resolvedUDPAddr[b.UDP.Address.Domain()] = ip
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user