This commit is contained in:
patterniha
2025-08-23 02:26:04 +03:30
parent b7b364b4af
commit 42d77f24dc
3 changed files with 20 additions and 10 deletions

View File

@@ -382,7 +382,7 @@ func (w *udpWorker) clean() error {
}
for addr, conn := range w.activeConn {
if nowSec-atomic.LoadInt64(&conn.lastActivityTime) > 2*60 {
if nowSec-atomic.LoadInt64(&conn.lastActivityTime) > 60 {
if !conn.inactive {
conn.setInactive()
delete(w.activeConn, addr)
@@ -409,7 +409,7 @@ func (w *udpWorker) Start() error {
w.cone = w.ctx.Value("cone").(bool)
w.checker = &task.Periodic{
Interval: time.Minute,
Interval: 30 * time.Second,
Execute: w.clean,
}

View File

@@ -73,7 +73,7 @@ func isValidAddress(addr *net.IPOrDomain) bool {
}
a := addr.AsAddress()
return a != net.AnyIP
return a != net.AnyIP && a != net.AnyIPv6
}
// Process implements proxy.Outbound.
@@ -418,7 +418,7 @@ func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
}
}
}
destAddr, _ := net.ResolveUDPAddr("udp", b.UDP.NetAddr())
destAddr := b.UDP.RawNetAddr()
if destAddr == nil {
b.Release()
continue

View File

@@ -22,17 +22,24 @@ type ResponseCallback func(ctx context.Context, packet *udp.Packet)
type connEntry struct {
link *transport.Link
timer signal.ActivityUpdater
timer *signal.ActivityTimer
cancel context.CancelFunc
closed bool
}
func (c *connEntry) Close() error {
c.timer.SetTimeout(0)
return nil
}
func (c *connEntry) terminate() {
if c.closed {
panic("terminate called more than once")
}
c.closed = true
c.cancel()
common.Interrupt(c.link.Reader)
common.Close(c.link.Writer)
return nil
}
type Dispatcher struct {
@@ -41,6 +48,7 @@ type Dispatcher struct {
dispatcher routing.Dispatcher
callback ResponseCallback
callClose func() error
closed bool
}
func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
@@ -53,6 +61,7 @@ func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Di
func (v *Dispatcher) RemoveRay() {
v.Lock()
defer v.Unlock()
v.closed = true
if v.conn != nil {
v.conn.Close()
v.conn = nil
@@ -63,6 +72,10 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*
v.Lock()
defer v.Unlock()
if v.closed {
return nil, errors.New("dispatcher is closed")
}
if v.conn != nil {
if v.conn.closed {
v.conn = nil
@@ -85,11 +98,8 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*
link: link,
cancel: cancel,
}
entryClose := func() {
entry.Close()
}
entry.timer = signal.CancelAfterInactivity(ctx, entryClose, time.Minute)
entry.timer = signal.CancelAfterInactivity(ctx, entry.terminate, 30*time.Second) // The UDP timeout is set to 30 seconds in most NAT configurations
v.conn = entry
go handleInput(ctx, entry, dest, v.callback, v.callClose)
return entry, nil