Add UDPTimeout option for flow
This commit is contained in:
parent
c17af6ee8c
commit
7c92d5e53e
2 changed files with 16 additions and 4 deletions
6
flow.go
6
flow.go
|
|
@ -1,11 +1,15 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import "net/netip"
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type FlowVerdict struct {
|
type FlowVerdict struct {
|
||||||
Action FlowAction
|
Action FlowAction
|
||||||
Port Port
|
Port Port
|
||||||
Destination netip.AddrPort
|
Destination netip.AddrPort
|
||||||
|
UDPTimeout time.Duration
|
||||||
NewTracker func() FlowTracker
|
NewTracker func() FlowTracker
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"maps"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -47,6 +48,7 @@ type forwardFlow struct {
|
||||||
reverseRule rewriteRule
|
reverseRule rewriteRule
|
||||||
effectiveMTU uint32
|
effectiveMTU uint32
|
||||||
protocol uint8
|
protocol uint8
|
||||||
|
udpTimeout time.Duration
|
||||||
tracker FlowTracker
|
tracker FlowTracker
|
||||||
|
|
||||||
clientAddress netip.Addr
|
clientAddress netip.Addr
|
||||||
|
|
@ -293,6 +295,9 @@ func (d *ForwardDispatcher) flowIdle(flow *forwardFlow) time.Duration {
|
||||||
if flow.protocol == uint8(header.TCPProtocolNumber) && flow.finForward && flow.finReverse.Load() {
|
if flow.protocol == uint8(header.TCPProtocolNumber) && flow.finForward && flow.finReverse.Load() {
|
||||||
return tcpClosingTimeout
|
return tcpClosingTimeout
|
||||||
}
|
}
|
||||||
|
if flow.udpTimeout > 0 {
|
||||||
|
return flow.udpTimeout
|
||||||
|
}
|
||||||
established := flow.established.Load() && !flow.finForward && !flow.finReverse.Load()
|
established := flow.established.Load() && !flow.finForward && !flow.finReverse.Load()
|
||||||
return d.idleTimeout(flow.protocol, established)
|
return d.idleTimeout(flow.protocol, established)
|
||||||
}
|
}
|
||||||
|
|
@ -331,11 +336,16 @@ func (d *ForwardDispatcher) createFlow(packet *forwardPacket, verdict FlowVerdic
|
||||||
if !allocated {
|
if !allocated {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
var udpTimeout time.Duration
|
||||||
|
if packet.protocol == uint8(header.UDPProtocolNumber) {
|
||||||
|
udpTimeout = verdict.UDPTimeout
|
||||||
|
}
|
||||||
flow := &forwardFlow{
|
flow := &forwardFlow{
|
||||||
nat: nat,
|
nat: nat,
|
||||||
reverseKey: reverseKey,
|
reverseKey: reverseKey,
|
||||||
effectiveMTU: effectiveMTU,
|
effectiveMTU: effectiveMTU,
|
||||||
protocol: packet.protocol,
|
protocol: packet.protocol,
|
||||||
|
udpTimeout: udpTimeout,
|
||||||
clientAddress: packet.source.Addr(),
|
clientAddress: packet.source.Addr(),
|
||||||
clientSelector: packet.source.Port(),
|
clientSelector: packet.source.Port(),
|
||||||
clientDestinationAddress: clientDestinationAddress,
|
clientDestinationAddress: clientDestinationAddress,
|
||||||
|
|
@ -399,9 +409,7 @@ func (d *ForwardDispatcher) natFor(port Port) *portNAT {
|
||||||
d.natList.Store(&natList)
|
d.natList.Store(&natList)
|
||||||
revMap := make(map[netip.Addr]*portNAT)
|
revMap := make(map[netip.Addr]*portNAT)
|
||||||
if currentRev := d.revNAT.Load(); currentRev != nil {
|
if currentRev := d.revNAT.Load(); currentRev != nil {
|
||||||
for addr, existing := range *currentRev {
|
maps.Copy(revMap, *currentRev)
|
||||||
revMap[addr] = existing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
v4Address, v6Address := port.PortAddresses()
|
v4Address, v6Address := port.PortAddresses()
|
||||||
if v4Address.IsValid() {
|
if v4Address.IsValid() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue