stack: Add icmp timeout

This commit is contained in:
世界 2026-06-01 17:40:28 +08:00
parent 568357315c
commit bebc8d67f3
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
3 changed files with 7 additions and 2 deletions

View file

@ -28,6 +28,7 @@ type StackOptions struct {
Tun Tun Tun Tun
TunOptions Options TunOptions Options
UDPTimeout time.Duration UDPTimeout time.Duration
ICMPTimeout time.Duration
Handler Handler Handler Handler
Logger logger.Logger Logger logger.Logger
ForwarderBindInterface bool ForwarderBindInterface bool

View file

@ -34,6 +34,7 @@ type GVisor struct {
inet4LoopbackAddress []netip.Addr inet4LoopbackAddress []netip.Addr
inet6LoopbackAddress []netip.Addr inet6LoopbackAddress []netip.Addr
udpTimeout time.Duration udpTimeout time.Duration
icmpTimeout time.Duration
broadcastAddr netip.Addr broadcastAddr netip.Addr
handler Handler handler Handler
logger logger.Logger logger logger.Logger
@ -74,6 +75,7 @@ func NewGVisor(
inet4LoopbackAddress: options.TunOptions.Inet4LoopbackAddress, inet4LoopbackAddress: options.TunOptions.Inet4LoopbackAddress,
inet6LoopbackAddress: options.TunOptions.Inet6LoopbackAddress, inet6LoopbackAddress: options.TunOptions.Inet6LoopbackAddress,
udpTimeout: options.UDPTimeout, udpTimeout: options.UDPTimeout,
icmpTimeout: options.ICMPTimeout,
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address), broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
handler: options.Handler, handler: options.Handler,
logger: options.Logger, logger: options.Logger,
@ -93,7 +95,7 @@ func (t *GVisor) Start() error {
} }
ipStack.SetTransportProtocolHandler(tcp.ProtocolNumber, NewTCPForwarderWithLoopback(t.ctx, ipStack, t.handler, t.inet4LoopbackAddress, t.inet6LoopbackAddress, t.tun).HandlePacket) ipStack.SetTransportProtocolHandler(tcp.ProtocolNumber, NewTCPForwarderWithLoopback(t.ctx, ipStack, t.handler, t.inet4LoopbackAddress, t.inet6LoopbackAddress, t.tun).HandlePacket)
ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout).HandlePacket) ipStack.SetTransportProtocolHandler(udp.ProtocolNumber, NewUDPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout).HandlePacket)
icmpForwarder := NewICMPForwarder(t.ctx, ipStack, t.handler, t.udpTimeout) icmpForwarder := NewICMPForwarder(t.ctx, ipStack, t.handler, t.icmpTimeout)
icmpForwarder.SetLocalAddresses(t.inet4Address, t.inet6Address) icmpForwarder.SetLocalAddresses(t.inet4Address, t.inet6Address)
ipStack.SetTransportProtocolHandler(icmp.ProtocolNumber4, icmpForwarder.HandlePacket) ipStack.SetTransportProtocolHandler(icmp.ProtocolNumber4, icmpForwarder.HandlePacket)
ipStack.SetTransportProtocolHandler(icmp.ProtocolNumber6, icmpForwarder.HandlePacket) ipStack.SetTransportProtocolHandler(icmp.ProtocolNumber6, icmpForwarder.HandlePacket)

View file

@ -39,6 +39,7 @@ type System struct {
inet4LoopbackAddress []netip.Addr inet4LoopbackAddress []netip.Addr
inet6LoopbackAddress []netip.Addr inet6LoopbackAddress []netip.Addr
udpTimeout time.Duration udpTimeout time.Duration
icmpTimeout time.Duration
tcpListener net.Listener tcpListener net.Listener
tcpListener6 net.Listener tcpListener6 net.Listener
tcpPort uint16 tcpPort uint16
@ -69,6 +70,7 @@ func NewSystem(options StackOptions) (Stack, error) {
inet4LoopbackAddress: options.TunOptions.Inet4LoopbackAddress, inet4LoopbackAddress: options.TunOptions.Inet4LoopbackAddress,
inet6LoopbackAddress: options.TunOptions.Inet6LoopbackAddress, inet6LoopbackAddress: options.TunOptions.Inet6LoopbackAddress,
udpTimeout: options.UDPTimeout, udpTimeout: options.UDPTimeout,
icmpTimeout: options.ICMPTimeout,
handler: options.Handler, handler: options.Handler,
logger: options.Logger, logger: options.Logger,
inet4Prefixes: options.TunOptions.Inet4Address, inet4Prefixes: options.TunOptions.Inet4Address,
@ -160,7 +162,7 @@ func (s *System) start() error {
} }
s.tcpNat = NewNat(s.ctx, s.udpTimeout) s.tcpNat = NewNat(s.ctx, s.udpTimeout)
s.udpNat = udpnat.New(s.handler, s.preparePacketConnection, s.udpTimeout, false) s.udpNat = udpnat.New(s.handler, s.preparePacketConnection, s.udpTimeout, false)
s.directNat = NewDirectRouteMapping(s.udpTimeout) s.directNat = NewDirectRouteMapping(s.icmpTimeout)
return nil return nil
} }