Pass timeout to PrepareConnection
This commit is contained in:
parent
9532c7f1f6
commit
ff4941daa4
7 changed files with 24 additions and 12 deletions
|
|
@ -25,6 +25,7 @@ type DirectRouteSession struct {
|
||||||
|
|
||||||
type DirectRouteMapping struct {
|
type DirectRouteMapping struct {
|
||||||
mapping freelru.Cache[DirectRouteSession, DirectRouteDestination]
|
mapping freelru.Cache[DirectRouteSession, DirectRouteDestination]
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDirectRouteMapping(timeout time.Duration) *DirectRouteMapping {
|
func NewDirectRouteMapping(timeout time.Duration) *DirectRouteMapping {
|
||||||
|
|
@ -36,16 +37,16 @@ func NewDirectRouteMapping(timeout time.Duration) *DirectRouteMapping {
|
||||||
action.Close()
|
action.Close()
|
||||||
})
|
})
|
||||||
mapping.SetLifetime(timeout)
|
mapping.SetLifetime(timeout)
|
||||||
return &DirectRouteMapping{mapping}
|
return &DirectRouteMapping{mapping, timeout}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *DirectRouteMapping) Lookup(session DirectRouteSession, constructor func() (DirectRouteDestination, error)) (DirectRouteDestination, error) {
|
func (m *DirectRouteMapping) Lookup(session DirectRouteSession, constructor func(timeout time.Duration) (DirectRouteDestination, error)) (DirectRouteDestination, error) {
|
||||||
var (
|
var (
|
||||||
created DirectRouteDestination
|
created DirectRouteDestination
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
action, _, ok := m.mapping.GetAndRefreshOrAdd(session, func() (DirectRouteDestination, bool) {
|
action, _, ok := m.mapping.GetAndRefreshOrAdd(session, func() (DirectRouteDestination, bool) {
|
||||||
created, err = constructor()
|
created, err = constructor(m.timeout)
|
||||||
return created, err == nil
|
return created, err == nil
|
||||||
})
|
})
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
|
||||||
sourceAddr := M.AddrFromIP(ipHdr.SourceAddressSlice())
|
sourceAddr := M.AddrFromIP(ipHdr.SourceAddressSlice())
|
||||||
destinationAddr := M.AddrFromIP(ipHdr.DestinationAddressSlice())
|
destinationAddr := M.AddrFromIP(ipHdr.DestinationAddressSlice())
|
||||||
if destinationAddr != f.inet4Address {
|
if destinationAddr != f.inet4Address {
|
||||||
action, err := f.mapping.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func() (DirectRouteDestination, error) {
|
action, err := f.mapping.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func(timeout time.Duration) (DirectRouteDestination, error) {
|
||||||
return f.handler.PrepareConnection(
|
return f.handler.PrepareConnection(
|
||||||
N.NetworkICMPv4,
|
N.NetworkICMPv4,
|
||||||
M.SocksaddrFrom(sourceAddr, 0),
|
M.SocksaddrFrom(sourceAddr, 0),
|
||||||
|
|
@ -70,6 +70,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
|
||||||
source: ipHdr.SourceAddress(),
|
source: ipHdr.SourceAddress(),
|
||||||
sourceNetwork: header.IPv4ProtocolNumber,
|
sourceNetwork: header.IPv4ProtocolNumber,
|
||||||
},
|
},
|
||||||
|
timeout,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -117,7 +118,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
|
||||||
sourceAddr := M.AddrFromIP(ipHdr.SourceAddressSlice())
|
sourceAddr := M.AddrFromIP(ipHdr.SourceAddressSlice())
|
||||||
destinationAddr := M.AddrFromIP(ipHdr.DestinationAddressSlice())
|
destinationAddr := M.AddrFromIP(ipHdr.DestinationAddressSlice())
|
||||||
if destinationAddr != f.inet6Address {
|
if destinationAddr != f.inet6Address {
|
||||||
action, err := f.mapping.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func() (DirectRouteDestination, error) {
|
action, err := f.mapping.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func(timeout time.Duration) (DirectRouteDestination, error) {
|
||||||
return f.handler.PrepareConnection(
|
return f.handler.PrepareConnection(
|
||||||
N.NetworkICMPv6,
|
N.NetworkICMPv6,
|
||||||
M.SocksaddrFrom(sourceAddr, 0),
|
M.SocksaddrFrom(sourceAddr, 0),
|
||||||
|
|
@ -128,6 +129,7 @@ func (f *ICMPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pa
|
||||||
source: ipHdr.SourceAddress(),
|
source: ipHdr.SourceAddress(),
|
||||||
sourceNetwork: header.IPv6ProtocolNumber,
|
sourceNetwork: header.IPv6ProtocolNumber,
|
||||||
},
|
},
|
||||||
|
timeout,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ func (f *TCPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pac
|
||||||
func (f *TCPForwarder) Forward(r *tcp.ForwarderRequest) {
|
func (f *TCPForwarder) Forward(r *tcp.ForwarderRequest) {
|
||||||
source := M.SocksaddrFrom(AddrFromAddress(r.ID().RemoteAddress), r.ID().RemotePort)
|
source := M.SocksaddrFrom(AddrFromAddress(r.ID().RemoteAddress), r.ID().RemotePort)
|
||||||
destination := M.SocksaddrFrom(AddrFromAddress(r.ID().LocalAddress), r.ID().LocalPort)
|
destination := M.SocksaddrFrom(AddrFromAddress(r.ID().LocalAddress), r.ID().LocalPort)
|
||||||
_, pErr := f.handler.PrepareConnection(N.NetworkTCP, source, destination, nil)
|
_, pErr := f.handler.PrepareConnection(N.NetworkTCP, source, destination, nil, 0)
|
||||||
if pErr != nil {
|
if pErr != nil {
|
||||||
r.Complete(!errors.Is(pErr, ErrDrop))
|
r.Complete(!errors.Is(pErr, ErrDrop))
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ func (f *UDPForwarder) HandlePacket(id stack.TransportEndpointID, pkt *stack.Pac
|
||||||
func rangeIterate(r stack.Range, fn func(*buffer.View))
|
func rangeIterate(r stack.Range, fn func(*buffer.View))
|
||||||
|
|
||||||
func (f *UDPForwarder) PreparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
func (f *UDPForwarder) PreparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
||||||
_, pErr := f.handler.PrepareConnection(N.NetworkUDP, source, destination, nil)
|
_, pErr := f.handler.PrepareConnection(N.NetworkUDP, source, destination, nil, 0)
|
||||||
if pErr != nil {
|
if pErr != nil {
|
||||||
if !errors.Is(pErr, ErrDrop) {
|
if !errors.Is(pErr, ErrDrop) {
|
||||||
gWriteUnreachable(f.stack, userData.(*stack.PacketBuffer))
|
gWriteUnreachable(f.stack, userData.(*stack.PacketBuffer))
|
||||||
|
|
|
||||||
|
|
@ -609,7 +609,7 @@ func (s *System) processIPv6UDP(ipHdr header.IPv6, udpHdr header.UDP) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *System) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
func (s *System) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
||||||
_, pErr := s.handler.PrepareConnection(N.NetworkUDP, source, destination, nil)
|
_, pErr := s.handler.PrepareConnection(N.NetworkUDP, source, destination, nil, 0)
|
||||||
if pErr != nil {
|
if pErr != nil {
|
||||||
if !errors.Is(pErr, ErrDrop) {
|
if !errors.Is(pErr, ErrDrop) {
|
||||||
if source.IsIPv4() {
|
if source.IsIPv4() {
|
||||||
|
|
@ -658,12 +658,13 @@ func (s *System) processIPv4ICMP(ipHdr header.IPv4, icmpHdr header.ICMPv4) (bool
|
||||||
sourceAddr := ipHdr.SourceAddr()
|
sourceAddr := ipHdr.SourceAddr()
|
||||||
destinationAddr := ipHdr.DestinationAddr()
|
destinationAddr := ipHdr.DestinationAddr()
|
||||||
if destinationAddr != s.inet4Address {
|
if destinationAddr != s.inet4Address {
|
||||||
action, err := s.directNat.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func() (DirectRouteDestination, error) {
|
action, err := s.directNat.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func(timeout time.Duration) (DirectRouteDestination, error) {
|
||||||
return s.handler.PrepareConnection(
|
return s.handler.PrepareConnection(
|
||||||
N.NetworkICMPv4,
|
N.NetworkICMPv4,
|
||||||
M.SocksaddrFrom(sourceAddr, 0),
|
M.SocksaddrFrom(sourceAddr, 0),
|
||||||
M.SocksaddrFrom(destinationAddr, 0),
|
M.SocksaddrFrom(destinationAddr, 0),
|
||||||
&systemICMPDirectPacketWriter4{s.tun, s.frontHeadroom + PacketOffset, sourceAddr},
|
&systemICMPDirectPacketWriter4{s.tun, s.frontHeadroom + PacketOffset, sourceAddr},
|
||||||
|
timeout,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -729,12 +730,13 @@ func (s *System) processIPv6ICMP(ipHdr header.IPv6, icmpHdr header.ICMPv6) (bool
|
||||||
sourceAddr := ipHdr.SourceAddr()
|
sourceAddr := ipHdr.SourceAddr()
|
||||||
destinationAddr := ipHdr.DestinationAddr()
|
destinationAddr := ipHdr.DestinationAddr()
|
||||||
if destinationAddr != s.inet6Address {
|
if destinationAddr != s.inet6Address {
|
||||||
action, err := s.directNat.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func() (DirectRouteDestination, error) {
|
action, err := s.directNat.Lookup(DirectRouteSession{Source: sourceAddr, Destination: destinationAddr}, func(timeout time.Duration) (DirectRouteDestination, error) {
|
||||||
return s.handler.PrepareConnection(
|
return s.handler.PrepareConnection(
|
||||||
N.NetworkICMPv6,
|
N.NetworkICMPv6,
|
||||||
M.SocksaddrFrom(sourceAddr, 0),
|
M.SocksaddrFrom(sourceAddr, 0),
|
||||||
M.SocksaddrFrom(destinationAddr, 0),
|
M.SocksaddrFrom(destinationAddr, 0),
|
||||||
&systemICMPDirectPacketWriter6{s.tun, s.frontHeadroom + PacketOffset, sourceAddr},
|
&systemICMPDirectPacketWriter6{s.tun, s.frontHeadroom + PacketOffset, sourceAddr},
|
||||||
|
timeout,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ func (n *TCPNat) Lookup(source netip.AddrPort, destination netip.AddrPort, handl
|
||||||
if loaded {
|
if loaded {
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
_, pErr := handler.PrepareConnection(N.NetworkTCP, M.SocksaddrFromNetIP(source), M.SocksaddrFromNetIP(destination), nil)
|
_, pErr := handler.PrepareConnection(N.NetworkTCP, M.SocksaddrFromNetIP(source), M.SocksaddrFromNetIP(destination), nil, 0)
|
||||||
if pErr != nil {
|
if pErr != nil {
|
||||||
return 0, pErr
|
return 0, pErr
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
tun.go
9
tun.go
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
"github.com/sagernet/sing/common/control"
|
"github.com/sagernet/sing/common/control"
|
||||||
|
|
@ -18,7 +19,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
PrepareConnection(network string, source M.Socksaddr, destination M.Socksaddr, routeContext DirectRouteContext) (DirectRouteDestination, error)
|
PrepareConnection(
|
||||||
|
network string,
|
||||||
|
source M.Socksaddr,
|
||||||
|
destination M.Socksaddr,
|
||||||
|
routeContext DirectRouteContext,
|
||||||
|
timeout time.Duration,
|
||||||
|
) (DirectRouteDestination, error)
|
||||||
N.TCPConnectionHandlerEx
|
N.TCPConnectionHandlerEx
|
||||||
N.UDPConnectionHandlerEx
|
N.UDPConnectionHandlerEx
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue