optimize: avoid conflict with potential local dns server (#422)

This commit is contained in:
mzz 2024-01-22 17:32:07 +08:00 committed by GitHub
parent 41d5de1056
commit b0f6205cc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 12 deletions

View File

@ -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 {
@ -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()

View File

@ -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).