2019-10-11 20:11:18 +08:00
|
|
|
package nat
|
|
|
|
|
|
|
|
import (
|
2023-02-17 16:31:15 +08:00
|
|
|
"net"
|
2019-10-11 20:11:18 +08:00
|
|
|
"sync"
|
2020-02-17 17:34:19 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2023-09-02 16:54:35 +08:00
|
|
|
|
|
|
|
"github.com/puzpuzpuz/xsync/v2"
|
2019-10-11 20:11:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Table struct {
|
2023-09-02 16:54:35 +08:00
|
|
|
mapping *xsync.MapOf[string, *Entry]
|
|
|
|
lockMap *xsync.MapOf[string, *sync.Cond]
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
2023-02-17 16:31:15 +08:00
|
|
|
type Entry struct {
|
|
|
|
PacketConn C.PacketConn
|
2023-06-03 21:40:09 +08:00
|
|
|
WriteBackProxy C.WriteBackProxy
|
2023-09-02 16:54:35 +08:00
|
|
|
LocalUDPConnMap *xsync.MapOf[string, *net.UDPConn]
|
|
|
|
LocalLockMap *xsync.MapOf[string, *sync.Cond]
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:40:09 +08:00
|
|
|
func (t *Table) Set(key string, e C.PacketConn, w C.WriteBackProxy) {
|
2023-02-17 16:31:15 +08:00
|
|
|
t.mapping.Store(key, &Entry{
|
|
|
|
PacketConn: e,
|
2023-06-03 21:40:09 +08:00
|
|
|
WriteBackProxy: w,
|
2023-09-02 16:54:35 +08:00
|
|
|
LocalUDPConnMap: xsync.NewMapOf[*net.UDPConn](),
|
|
|
|
LocalLockMap: xsync.NewMapOf[*sync.Cond](),
|
2023-02-17 16:31:15 +08:00
|
|
|
})
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
2023-06-03 21:40:09 +08:00
|
|
|
func (t *Table) Get(key string) (C.PacketConn, C.WriteBackProxy) {
|
2023-02-17 16:31:15 +08:00
|
|
|
entry, exist := t.getEntry(key)
|
2019-10-11 20:11:18 +08:00
|
|
|
if !exist {
|
2023-06-03 21:40:09 +08:00
|
|
|
return nil, nil
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
2023-06-03 21:40:09 +08:00
|
|
|
return entry.PacketConn, entry.WriteBackProxy
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 21:26:50 +08:00
|
|
|
func (t *Table) GetOrCreateLock(key string) (*sync.Cond, bool) {
|
2023-09-02 16:54:35 +08:00
|
|
|
item, loaded := t.lockMap.LoadOrCompute(key, makeLock)
|
|
|
|
return item, loaded
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) Delete(key string) {
|
|
|
|
t.mapping.Delete(key)
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
func (t *Table) DeleteLock(lockKey string) {
|
|
|
|
t.lockMap.Delete(lockKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) GetForLocalConn(lAddr, rAddr string) *net.UDPConn {
|
2023-02-17 16:31:15 +08:00
|
|
|
entry, exist := t.getEntry(lAddr)
|
|
|
|
if !exist {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
item, exist := entry.LocalUDPConnMap.Load(rAddr)
|
|
|
|
if !exist {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-02 16:54:35 +08:00
|
|
|
return item
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
func (t *Table) AddForLocalConn(lAddr, rAddr string, conn *net.UDPConn) bool {
|
2023-02-17 16:31:15 +08:00
|
|
|
entry, exist := t.getEntry(lAddr)
|
|
|
|
if !exist {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
entry.LocalUDPConnMap.Store(rAddr, conn)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
func (t *Table) RangeForLocalConn(lAddr string, f func(key string, value *net.UDPConn) bool) {
|
2023-02-17 16:31:15 +08:00
|
|
|
entry, exist := t.getEntry(lAddr)
|
|
|
|
if !exist {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.LocalUDPConnMap.Range(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) GetOrCreateLockForLocalConn(lAddr, key string) (*sync.Cond, bool) {
|
|
|
|
entry, loaded := t.getEntry(lAddr)
|
|
|
|
if !loaded {
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-09-02 16:54:35 +08:00
|
|
|
item, loaded := entry.LocalLockMap.LoadOrCompute(key, makeLock)
|
|
|
|
return item, loaded
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
func (t *Table) DeleteForLocalConn(lAddr, key string) {
|
2023-02-17 16:31:15 +08:00
|
|
|
entry, loaded := t.getEntry(lAddr)
|
|
|
|
if !loaded {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
entry.LocalUDPConnMap.Delete(key)
|
|
|
|
}
|
|
|
|
|
2023-09-02 16:54:35 +08:00
|
|
|
func (t *Table) DeleteLockForLocalConn(lAddr, key string) {
|
|
|
|
entry, loaded := t.getEntry(lAddr)
|
|
|
|
if !loaded {
|
|
|
|
return
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|
2023-09-02 16:54:35 +08:00
|
|
|
entry.LocalLockMap.Delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Table) getEntry(key string) (*Entry, bool) {
|
|
|
|
return t.mapping.Load(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeLock() *sync.Cond {
|
|
|
|
return sync.NewCond(&sync.Mutex{})
|
2023-02-17 16:31:15 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 20:11:18 +08:00
|
|
|
// New return *Cache
|
|
|
|
func New() *Table {
|
2023-09-02 16:54:35 +08:00
|
|
|
return &Table{
|
|
|
|
mapping: xsync.NewMapOf[*Entry](),
|
|
|
|
lockMap: xsync.NewMapOf[*sync.Cond](),
|
|
|
|
}
|
2019-10-11 20:11:18 +08:00
|
|
|
}
|