chore: update proxy's udpConn when received a new packet
This commit is contained in:
26
component/nat/proxy.go
Normal file
26
component/nat/proxy.go
Normal file
@ -0,0 +1,26 @@
|
||||
package nat
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/atomic"
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
)
|
||||
|
||||
type writeBackProxy struct {
|
||||
wb atomic.TypedValue[C.WriteBack]
|
||||
}
|
||||
|
||||
func (w *writeBackProxy) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||
return w.wb.Load().WriteBack(b, addr)
|
||||
}
|
||||
|
||||
func (w *writeBackProxy) UpdateWriteBack(wb C.WriteBack) {
|
||||
w.wb.Store(wb)
|
||||
}
|
||||
|
||||
func NewWriteBackProxy(wb C.WriteBack) C.WriteBackProxy {
|
||||
w := &writeBackProxy{}
|
||||
w.UpdateWriteBack(wb)
|
||||
return w
|
||||
}
|
@ -13,22 +13,24 @@ type Table struct {
|
||||
|
||||
type Entry struct {
|
||||
PacketConn C.PacketConn
|
||||
WriteBackProxy C.WriteBackProxy
|
||||
LocalUDPConnMap sync.Map
|
||||
}
|
||||
|
||||
func (t *Table) Set(key string, e C.PacketConn) {
|
||||
func (t *Table) Set(key string, e C.PacketConn, w C.WriteBackProxy) {
|
||||
t.mapping.Store(key, &Entry{
|
||||
PacketConn: e,
|
||||
WriteBackProxy: w,
|
||||
LocalUDPConnMap: sync.Map{},
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Table) Get(key string) C.PacketConn {
|
||||
func (t *Table) Get(key string) (C.PacketConn, C.WriteBackProxy) {
|
||||
entry, exist := t.getEntry(key)
|
||||
if !exist {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
return entry.PacketConn
|
||||
return entry.PacketConn, entry.WriteBackProxy
|
||||
}
|
||||
|
||||
func (t *Table) GetOrCreateLock(key string) (*sync.Cond, bool) {
|
||||
|
Reference in New Issue
Block a user