@@ -16,6 +16,7 @@ import (
1616 M "github.com/sagernet/sing/common/metadata"
1717 N "github.com/sagernet/sing/common/network"
1818 "github.com/sagernet/sing/common/udpnat"
19+ "github.com/sagernet/sing/service/pause"
1920)
2021
2122type System struct {
@@ -40,6 +41,7 @@ type System struct {
4041 udpNat * udpnat.Service [netip.AddrPort ]
4142 bindInterface bool
4243 interfaceFinder control.InterfaceFinder
44+ pauseManager pause.Manager
4345}
4446
4547type Session struct {
@@ -62,6 +64,7 @@ func NewSystem(options StackOptions) (Stack, error) {
6264 inet6Prefixes : options .Inet6Address ,
6365 bindInterface : options .ForwarderBindInterface ,
6466 interfaceFinder : options .InterfaceFinder ,
67+ pauseManager : pause .ManagerFromContext (options .Context ),
6568 }
6669 if len (options .Inet4Address ) > 0 {
6770 if options .Inet4Address [0 ].Bits () == 32 {
@@ -197,6 +200,11 @@ func (s *System) acceptLoop(listener net.Listener) {
197200 if err != nil {
198201 return
199202 }
203+ if s .pauseManager != nil && pause .IsPaused (s .pauseManager .NetworkPauseChan ()) {
204+ conn .(* net.TCPConn ).SetLinger (0 )
205+ conn .Close ()
206+ continue
207+ }
200208 connPort := M .SocksaddrFromNet (conn .RemoteAddr ()).Port
201209 session := s .tcpNat .LookupBack (connPort )
202210 if session == nil {
@@ -330,11 +338,14 @@ func (s *System) processIPv4UDP(packet clashtcpip.IPv4Packet, header clashtcpip.
330338 Source : M .SocksaddrFromNetIP (source ),
331339 Destination : M .SocksaddrFromNetIP (destination ),
332340 }
333- s .udpNat .NewPacket (s .ctx , source , data .ToOwned (), metadata , func (natConn N.PacketConn ) N.PacketWriter {
341+ s .udpNat .NewContextPacket1 (s .ctx , source , data .ToOwned (), metadata , func (natConn N.PacketConn ) (context.Context , N.PacketWriter , error ) {
342+ if s .pauseManager != nil && pause .IsPaused (s .pauseManager .NetworkPauseChan ()) {
343+ return nil , nil , pause .ErrPaused
344+ }
334345 headerLen := packet .HeaderLen () + clashtcpip .UDPHeaderSize
335346 headerCopy := make ([]byte , headerLen )
336347 copy (headerCopy , packet [:headerLen ])
337- return & systemUDPPacketWriter4 {s .tun , headerCopy , source }
348+ return s . ctx , & systemUDPPacketWriter4 {s .tun , headerCopy , source }, nil
338349 })
339350 return nil
340351}
@@ -353,11 +364,14 @@ func (s *System) processIPv6UDP(packet clashtcpip.IPv6Packet, header clashtcpip.
353364 Source : M .SocksaddrFromNetIP (source ),
354365 Destination : M .SocksaddrFromNetIP (destination ),
355366 }
356- s .udpNat .NewPacket (s .ctx , source , data .ToOwned (), metadata , func (natConn N.PacketConn ) N.PacketWriter {
367+ s .udpNat .NewContextPacket1 (s .ctx , source , data .ToOwned (), metadata , func (natConn N.PacketConn ) (context.Context , N.PacketWriter , error ) {
368+ if s .pauseManager != nil && pause .IsPaused (s .pauseManager .NetworkPauseChan ()) {
369+ return nil , nil , pause .ErrPaused
370+ }
357371 headerLen := len (packet ) - int (header .Length ()) + clashtcpip .UDPHeaderSize
358372 headerCopy := make ([]byte , headerLen )
359373 copy (headerCopy , packet [:headerLen ])
360- return & systemUDPPacketWriter6 {s .tun , headerCopy , source }
374+ return s . ctx , & systemUDPPacketWriter6 {s .tun , headerCopy , source }, nil
361375 })
362376 return nil
363377}
0 commit comments