Add preferred_by route rule item

This commit is contained in:
世界
2025-08-15 12:45:06 +08:00
parent 5be1887f92
commit 239e6ec701
9 changed files with 194 additions and 19 deletions

View File

@@ -8,7 +8,9 @@ import (
"net"
"net/netip"
"os"
"reflect"
"strings"
"unsafe"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
@@ -30,6 +32,7 @@ type Endpoint struct {
allowedAddress []netip.Prefix
tunDevice Device
device *device.Device
allowedIPs *device.AllowedIPs
pause pause.Manager
pauseCallback *list.Element[pause.Callback]
}
@@ -191,6 +194,7 @@ func (e *Endpoint) Start(resolve bool) error {
if e.pause != nil {
e.pauseCallback = e.pause.RegisterCallback(e.onPauseUpdated)
}
e.allowedIPs = (*device.AllowedIPs)(unsafe.Pointer(reflect.Indirect(reflect.ValueOf(wgDevice)).FieldByName("allowedips").UnsafeAddr()))
return nil
}
@@ -218,6 +222,13 @@ func (e *Endpoint) Close() error {
return nil
}
func (e *Endpoint) Lookup(address netip.Addr) *device.Peer {
if e.allowedIPs == nil {
return nil
}
return e.allowedIPs.Lookup(address.AsSlice())
}
func (e *Endpoint) onPauseUpdated(event int) {
switch event {
case pause.EventDevicePaused, pause.EventNetworkPause: