From 3b161ab30c933d3f2448a9c24a5be6151654b76a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 21 Oct 2023 12:00:00 +0800 Subject: [PATCH] Fix netip.Prefix usage --- inbound/tun.go | 8 ++++---- option/dns.go | 6 ++++-- option/tun.go | 10 ++++++---- option/types.go | 28 ---------------------------- option/wireguard.go | 4 +++- outbound/wireguard.go | 10 ++++------ route/router.go | 4 ++-- 7 files changed, 23 insertions(+), 47 deletions(-) diff --git a/inbound/tun.go b/inbound/tun.go index 9156d203..0b57482d 100644 --- a/inbound/tun.go +++ b/inbound/tun.go @@ -73,14 +73,14 @@ func NewTun(ctx context.Context, router adapter.Router, logger log.ContextLogger tunOptions: tun.Options{ Name: options.InterfaceName, MTU: tunMTU, - Inet4Address: common.Map(options.Inet4Address, option.ListenPrefix.Build), - Inet6Address: common.Map(options.Inet6Address, option.ListenPrefix.Build), + Inet4Address: options.Inet4Address, + Inet6Address: options.Inet6Address, AutoRoute: options.AutoRoute, StrictRoute: options.StrictRoute, IncludeInterface: options.IncludeInterface, ExcludeInterface: options.ExcludeInterface, - Inet4RouteAddress: common.Map(options.Inet4RouteAddress, option.ListenPrefix.Build), - Inet6RouteAddress: common.Map(options.Inet6RouteAddress, option.ListenPrefix.Build), + Inet4RouteAddress: options.Inet4RouteAddress, + Inet6RouteAddress: options.Inet6RouteAddress, IncludeUID: includeUID, ExcludeUID: excludeUID, IncludeAndroidUser: options.IncludeAndroidUser, diff --git a/option/dns.go b/option/dns.go index 1e73fb5f..e0d237b7 100644 --- a/option/dns.go +++ b/option/dns.go @@ -1,5 +1,7 @@ package option +import "net/netip" + type DNSOptions struct { Servers []DNSServerOptions `json:"servers,omitempty"` Rules []DNSRule `json:"rules,omitempty"` @@ -28,6 +30,6 @@ type DNSClientOptions struct { type DNSFakeIPOptions struct { Enabled bool `json:"enabled,omitempty"` - Inet4Range *ListenPrefix `json:"inet4_range,omitempty"` - Inet6Range *ListenPrefix `json:"inet6_range,omitempty"` + Inet4Range *netip.Prefix `json:"inet4_range,omitempty"` + Inet6Range *netip.Prefix `json:"inet6_range,omitempty"` } diff --git a/option/tun.go b/option/tun.go index f566f098..4cf77804 100644 --- a/option/tun.go +++ b/option/tun.go @@ -1,14 +1,16 @@ package option +import "net/netip" + type TunInboundOptions struct { InterfaceName string `json:"interface_name,omitempty"` MTU uint32 `json:"mtu,omitempty"` - Inet4Address Listable[ListenPrefix] `json:"inet4_address,omitempty"` - Inet6Address Listable[ListenPrefix] `json:"inet6_address,omitempty"` + Inet4Address Listable[netip.Prefix] `json:"inet4_address,omitempty"` + Inet6Address Listable[netip.Prefix] `json:"inet6_address,omitempty"` AutoRoute bool `json:"auto_route,omitempty"` StrictRoute bool `json:"strict_route,omitempty"` - Inet4RouteAddress Listable[ListenPrefix] `json:"inet4_route_address,omitempty"` - Inet6RouteAddress Listable[ListenPrefix] `json:"inet6_route_address,omitempty"` + Inet4RouteAddress Listable[netip.Prefix] `json:"inet4_route_address,omitempty"` + Inet6RouteAddress Listable[netip.Prefix] `json:"inet6_route_address,omitempty"` IncludeInterface Listable[string] `json:"include_interface,omitempty"` ExcludeInterface Listable[string] `json:"exclude_interface,omitempty"` IncludeUID Listable[uint32] `json:"include_uid,omitempty"` diff --git a/option/types.go b/option/types.go index a357387f..f2fed663 100644 --- a/option/types.go +++ b/option/types.go @@ -172,34 +172,6 @@ func (d *Duration) UnmarshalJSON(bytes []byte) error { return nil } -type ListenPrefix netip.Prefix - -func (p ListenPrefix) MarshalJSON() ([]byte, error) { - prefix := netip.Prefix(p) - if !prefix.IsValid() { - return json.Marshal(nil) - } - return json.Marshal(prefix.String()) -} - -func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error { - var value string - err := json.Unmarshal(bytes, &value) - if err != nil { - return err - } - prefix, err := netip.ParsePrefix(value) - if err != nil { - return err - } - *p = ListenPrefix(prefix) - return nil -} - -func (p ListenPrefix) Build() netip.Prefix { - return netip.Prefix(p) -} - type DNSQueryType uint16 func (t DNSQueryType) MarshalJSON() ([]byte, error) { diff --git a/option/wireguard.go b/option/wireguard.go index 21b6715b..5ede7a61 100644 --- a/option/wireguard.go +++ b/option/wireguard.go @@ -1,10 +1,12 @@ package option +import "net/netip" + type WireGuardOutboundOptions struct { DialerOptions SystemInterface bool `json:"system_interface,omitempty"` InterfaceName string `json:"interface_name,omitempty"` - LocalAddress Listable[ListenPrefix] `json:"local_address"` + LocalAddress Listable[netip.Prefix] `json:"local_address"` PrivateKey string `json:"private_key"` Peers []WireGuardPeer `json:"peers,omitempty"` ServerOptions diff --git a/outbound/wireguard.go b/outbound/wireguard.go index ad9145ba..e645f056 100644 --- a/outbound/wireguard.go +++ b/outbound/wireguard.go @@ -18,7 +18,6 @@ import ( "github.com/sagernet/sing-box/transport/wireguard" "github.com/sagernet/sing-dns" "github.com/sagernet/sing-tun" - "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/debug" E "github.com/sagernet/sing/common/exceptions" M "github.com/sagernet/sing/common/metadata" @@ -71,8 +70,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context return nil, err } outbound.bind = wireguard.NewClientBind(ctx, outbound, outboundDialer, isConnect, connectAddr, reserved) - localPrefixes := common.Map(options.LocalAddress, option.ListenPrefix.Build) - if len(localPrefixes) == 0 { + if len(options.LocalAddress) == 0 { return nil, E.New("missing local address") } var privateKey string @@ -143,7 +141,7 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context ipcConf += "\npreshared_key=" + preSharedKey } var has4, has6 bool - for _, address := range localPrefixes { + for _, address := range options.LocalAddress { if address.Addr().Is4() { has4 = true } else { @@ -163,9 +161,9 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context } var wireTunDevice wireguard.Device if !options.SystemInterface && tun.WithGVisor { - wireTunDevice, err = wireguard.NewStackDevice(localPrefixes, mtu) + wireTunDevice, err = wireguard.NewStackDevice(options.LocalAddress, mtu) } else { - wireTunDevice, err = wireguard.NewSystemDevice(router, options.InterfaceName, localPrefixes, mtu) + wireTunDevice, err = wireguard.NewSystemDevice(router, options.InterfaceName, options.LocalAddress, mtu) } if err != nil { return nil, E.Cause(err, "create WireGuard device") diff --git a/route/router.go b/route/router.go index 6506f05f..676ebd7b 100644 --- a/route/router.go +++ b/route/router.go @@ -253,10 +253,10 @@ func NewRouter( var inet4Range netip.Prefix var inet6Range netip.Prefix if fakeIPOptions.Inet4Range != nil { - inet4Range = fakeIPOptions.Inet4Range.Build() + inet4Range = *fakeIPOptions.Inet4Range } if fakeIPOptions.Inet6Range != nil { - inet6Range = fakeIPOptions.Inet6Range.Build() + inet6Range = *fakeIPOptions.Inet6Range } router.fakeIPStore = fakeip.NewStore(router, router.logger, inet4Range, inet6Range) }