mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-10-07 06:43:40 +00:00
Improve network reset
This commit is contained in:
@@ -77,6 +77,10 @@ func (i *Inbound) Start(stage adapter.StartStage) error {
|
||||
return i.listener.Start()
|
||||
}
|
||||
|
||||
func (i *Inbound) InterfaceUpdated() {
|
||||
i.udpNat.Purge()
|
||||
}
|
||||
|
||||
func (i *Inbound) Close() error {
|
||||
return i.listener.Close()
|
||||
}
|
||||
|
||||
@@ -29,10 +29,11 @@ func RegisterOutbound(registry *outbound.Registry) {
|
||||
}
|
||||
|
||||
var (
|
||||
_ N.ParallelDialer = (*Outbound)(nil)
|
||||
_ dialer.ParallelNetworkDialer = (*Outbound)(nil)
|
||||
_ dialer.DirectDialer = (*Outbound)(nil)
|
||||
_ adapter.FlowOutbound = (*Outbound)(nil)
|
||||
_ N.ParallelDialer = (*Outbound)(nil)
|
||||
_ dialer.ParallelNetworkDialer = (*Outbound)(nil)
|
||||
_ dialer.DirectDialer = (*Outbound)(nil)
|
||||
_ adapter.FlowOutbound = (*Outbound)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
||||
)
|
||||
|
||||
type Outbound struct {
|
||||
@@ -88,30 +89,43 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
func (h *Outbound) Start(stage adapter.StartStage) error {
|
||||
switch stage {
|
||||
case adapter.StartStatePostStart, adapter.StartStateStarted:
|
||||
h.fetchMyAddresses()
|
||||
if len(h.myAddresses.Load()) == 0 {
|
||||
h.fetchMyAddresses()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Outbound) fetchMyAddresses() {
|
||||
if len(h.myAddresses.Load()) > 0 {
|
||||
return
|
||||
}
|
||||
myInterfaceNames := h.network.InterfaceMonitor().MyInterfaces()
|
||||
if len(myInterfaceNames) == 0 {
|
||||
return
|
||||
}
|
||||
var myAddresses []netip.Prefix
|
||||
var (
|
||||
myAddresses []netip.Prefix
|
||||
found bool
|
||||
)
|
||||
for _, myInterfaceName := range myInterfaceNames {
|
||||
myInterface, err := h.network.InterfaceFinder().ByName(myInterfaceName)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
myAddresses = append(myAddresses, myInterface.Addresses...)
|
||||
}
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
h.myAddresses.Store(myAddresses)
|
||||
}
|
||||
|
||||
func (h *Outbound) InterfaceUpdated() {
|
||||
h.fetchMyAddresses()
|
||||
if h.icmpPort != nil {
|
||||
h.icmpPort.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Outbound) isMyLoopbackAddress(addresses ...netip.Addr) bool {
|
||||
for _, prefix := range h.myAddresses.Load() {
|
||||
for _, address := range addresses {
|
||||
|
||||
@@ -28,7 +28,10 @@ func RegisterURLTest(registry *outbound.Registry) {
|
||||
outbound.Register[option.URLTestOutboundOptions](registry, C.TypeURLTest, NewURLTest)
|
||||
}
|
||||
|
||||
var _ adapter.OutboundGroup = (*URLTest)(nil)
|
||||
var (
|
||||
_ adapter.OutboundGroup = (*URLTest)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*URLTest)(nil)
|
||||
)
|
||||
|
||||
type URLTest struct {
|
||||
outbound.Adapter
|
||||
@@ -118,6 +121,17 @@ func (s *URLTest) PerformUpdateCheck() {
|
||||
s.group.performUpdateCheck()
|
||||
}
|
||||
|
||||
func (s *URLTest) InterfaceUpdated() {
|
||||
group := s.group
|
||||
if group == nil {
|
||||
return
|
||||
}
|
||||
if group.pause.IsDevicePaused() || group.pause.IsNetworkPaused() {
|
||||
return
|
||||
}
|
||||
go group.CheckOutbounds(true)
|
||||
}
|
||||
|
||||
func (s *URLTest) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
s.group.Touch()
|
||||
var outbound adapter.Outbound
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
var (
|
||||
_ adapter.OutboundWithPreferredRoutes = (*Endpoint)(nil)
|
||||
_ adapter.FlowOutbound = (*Endpoint)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Endpoint)(nil)
|
||||
_ dialer.PacketDialerWithDestination = (*Endpoint)(nil)
|
||||
_ tun.Port = (*Endpoint)(nil)
|
||||
)
|
||||
@@ -396,6 +397,10 @@ func (e *Endpoint) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (e *Endpoint) InterfaceUpdated() {
|
||||
e.client.RestartSession()
|
||||
}
|
||||
|
||||
func (e *Endpoint) PreMatchFlow(network string, destination netip.Addr) adapter.PreMatchAction {
|
||||
return adapter.PreMatchFlow
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
var (
|
||||
_ adapter.OutboundWithPreferredRoutes = (*ClientEndpoint)(nil)
|
||||
_ adapter.FlowOutbound = (*ClientEndpoint)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*ClientEndpoint)(nil)
|
||||
_ dialer.PacketDialerWithDestination = (*ClientEndpoint)(nil)
|
||||
_ tun.Port = (*ClientEndpoint)(nil)
|
||||
)
|
||||
@@ -453,6 +454,10 @@ func (c *ClientEndpoint) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *ClientEndpoint) InterfaceUpdated() {
|
||||
c.client.RestartSession()
|
||||
}
|
||||
|
||||
func (c *ClientEndpoint) PreMatchFlow(network string, destination netip.Addr) adapter.PreMatchAction {
|
||||
return adapter.PreMatchFlow
|
||||
}
|
||||
|
||||
@@ -85,6 +85,10 @@ func (t *TProxy) Start(stage adapter.StartStage) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *TProxy) InterfaceUpdated() {
|
||||
t.udpNat.Purge()
|
||||
}
|
||||
|
||||
func (t *TProxy) Close() error {
|
||||
_ = t.udpNat.Close()
|
||||
return t.listener.Close()
|
||||
|
||||
@@ -32,9 +32,12 @@ type Outbound struct {
|
||||
serverAddr M.Socksaddr
|
||||
}
|
||||
|
||||
var _ adapter.InterfaceUpdateListener = (*Outbound)(nil)
|
||||
|
||||
type snellClient interface {
|
||||
snellprotocol.Method
|
||||
DialContext(ctx context.Context, destination M.Socksaddr) (net.Conn, error)
|
||||
Reset()
|
||||
Close() error
|
||||
}
|
||||
|
||||
@@ -136,6 +139,10 @@ func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (n
|
||||
return packetConn, nil
|
||||
}
|
||||
|
||||
func (h *Outbound) InterfaceUpdated() {
|
||||
h.client.Reset()
|
||||
}
|
||||
|
||||
func (h *Outbound) Close() error {
|
||||
return h.client.Close()
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ import (
|
||||
|
||||
var (
|
||||
_ adapter.OutboundWithPreferredRoutes = (*Endpoint)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Endpoint)(nil)
|
||||
_ dialer.PacketDialerWithDestination = (*Endpoint)(nil)
|
||||
_ tun.Port = (*Endpoint)(nil)
|
||||
)
|
||||
@@ -687,6 +688,16 @@ func (t *Endpoint) Close() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (t *Endpoint) InterfaceUpdated() {
|
||||
if !t.started.Load() {
|
||||
return
|
||||
}
|
||||
netMon, loaded := t.server.Sys().NetMon.GetOK()
|
||||
if loaded && netMon != nil {
|
||||
netMon.InjectEvent()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Endpoint) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
switch network {
|
||||
case N.NetworkTCP:
|
||||
|
||||
@@ -505,6 +505,13 @@ func (t *Inbound) updateRouteAddressSet(it adapter.RuleSet) {
|
||||
t.routeExcludeAddressSet = nil
|
||||
}
|
||||
|
||||
func (t *Inbound) InterfaceUpdated() {
|
||||
tunStack := t.tunStack
|
||||
if tunStack != nil {
|
||||
tunStack.ResetNetwork()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Inbound) Close() error {
|
||||
return common.Close(
|
||||
t.tunStack,
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
|
||||
var (
|
||||
_ adapter.OutboundWithPreferredRoutes = (*Endpoint)(nil)
|
||||
_ adapter.InterfaceUpdateListener = (*Endpoint)(nil)
|
||||
_ dialer.PacketDialerWithDestination = (*Endpoint)(nil)
|
||||
)
|
||||
|
||||
@@ -159,6 +160,16 @@ func (w *Endpoint) Close() error {
|
||||
return w.endpoint.Close()
|
||||
}
|
||||
|
||||
func (w *Endpoint) InterfaceUpdated() {
|
||||
if !w.started.Load() {
|
||||
return
|
||||
}
|
||||
err := w.endpoint.BindUpdate()
|
||||
if err != nil {
|
||||
w.logger.Error(E.Cause(err, "update bind"))
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Endpoint) PreMatchFlow(network string, destination netip.Addr) adapter.PreMatchAction {
|
||||
return adapter.PreMatchFlow
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user