Skip to content

Commit

Permalink
optimize: avoid conflict with potential local dns server (daeuniverse…
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Jan 22, 2024
1 parent 41d5de1 commit b0f6205
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
27 changes: 16 additions & 11 deletions control/anyfrom_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (a *Anyfrom) afterWrite(err error) {
a.RefreshTtl()
}
func (a *Anyfrom) RefreshTtl() {
a.deadlineTimer.Reset(a.ttl)
if a.deadlineTimer != nil {
a.deadlineTimer.Reset(a.ttl)
}
}
func (a *Anyfrom) SupportGso(size int) bool {
if size > math.MaxUint16 {
Expand Down Expand Up @@ -202,16 +204,19 @@ func (p *AnyfromPool) GetOrCreate(lAddr string, ttl time.Duration) (conn *Anyfro
gotGSOError: false,
gso: isGSOSupported(uConn),
}
af.deadlineTimer = time.AfterFunc(ttl, func() {
p.mu.Lock()
defer p.mu.Unlock()
_af := p.pool[lAddr]
if _af == af {
delete(p.pool, lAddr)
af.Close()
}
})
p.pool[lAddr] = af

if ttl > 0 {
af.deadlineTimer = time.AfterFunc(ttl, func() {
p.mu.Lock()
defer p.mu.Unlock()
_af := p.pool[lAddr]
if _af == af {
delete(p.pool, lAddr)
af.Close()
}
})
p.pool[lAddr] = af
}
return af, true, nil
} else {
af.RefreshTtl()
Expand Down
7 changes: 6 additions & 1 deletion control/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ func sendPkt(data []byte, from netip.AddrPort, realTo, to netip.AddrPort, lConn
return sendPktWithHdrWithFlag(data, from, lConn, to, lanWanFlag)
}

uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), AnyfromTimeout)
transparentTimeout := AnyfromTimeout
if from.Port() == 53 {
// Add port 53 (udp) to whitelist to avoid conflicts with the potential local dns server.
transparentTimeout = 0
}
uConn, _, err := DefaultAnyfromPool.GetOrCreate(from.String(), transparentTimeout)
if err != nil && errors.Is(err, syscall.EADDRINUSE) {
logrus.WithField("from", from).
WithField("to", to).
Expand Down

0 comments on commit b0f6205

Please sign in to comment.