platform: Fix VPN route

This commit is contained in:
世界 2023-12-09 22:25:29 +08:00
parent a64b57451a
commit a1044af579
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -8,7 +8,6 @@ import (
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
) )
type TunOptions interface { type TunOptions interface {
@ -20,6 +19,10 @@ type TunOptions interface {
GetStrictRoute() bool GetStrictRoute() bool
GetInet4RouteAddress() RoutePrefixIterator GetInet4RouteAddress() RoutePrefixIterator
GetInet6RouteAddress() RoutePrefixIterator GetInet6RouteAddress() RoutePrefixIterator
GetInet4RouteExcludeAddress() RoutePrefixIterator
GetInet6RouteExcludeAddress() RoutePrefixIterator
GetInet4RouteRange() RoutePrefixIterator
GetInet6RouteRange() RoutePrefixIterator
GetIncludePackage() StringIterator GetIncludePackage() StringIterator
GetExcludePackage() StringIterator GetExcludePackage() StringIterator
IsHTTPProxyEnabled() bool IsHTTPProxyEnabled() bool
@ -28,18 +31,30 @@ type TunOptions interface {
} }
type RoutePrefix struct { type RoutePrefix struct {
Address string address netip.Addr
Prefix int32 prefix int
}
func (p *RoutePrefix) Address() string {
return p.address.String()
}
func (p *RoutePrefix) Prefix() int32 {
return int32(p.prefix)
} }
func (p *RoutePrefix) Mask() string { func (p *RoutePrefix) Mask() string {
var bits int var bits int
if M.ParseSocksaddr(p.Address).Addr.Is6() { if p.address.Is6() {
bits = 128 bits = 128
} else { } else {
bits = 32 bits = 32
} }
return net.IP(net.CIDRMask(int(p.Prefix), bits)).String() return net.IP(net.CIDRMask(p.prefix, bits)).String()
}
func (p *RoutePrefix) String() string {
return netip.PrefixFrom(p.address, p.prefix).String()
} }
type RoutePrefixIterator interface { type RoutePrefixIterator interface {
@ -50,8 +65,8 @@ type RoutePrefixIterator interface {
func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator { func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix { return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
return &RoutePrefix{ return &RoutePrefix{
Address: prefix.Addr().String(), address: prefix.Addr(),
Prefix: int32(prefix.Bits()), prefix: prefix.Bits(),
} }
})) }))
} }
@ -92,12 +107,28 @@ func (o *tunOptions) GetStrictRoute() bool {
} }
func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator { func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4RouteAddress)
}
func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6RouteAddress)
}
func (o *tunOptions) GetInet4RouteExcludeAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4RouteExcludeAddress)
}
func (o *tunOptions) GetInet6RouteExcludeAddress() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6RouteExcludeAddress)
}
func (o *tunOptions) GetInet4RouteRange() RoutePrefixIterator {
return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool { return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
return it.Addr().Is4() return it.Addr().Is4()
})) }))
} }
func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator { func (o *tunOptions) GetInet6RouteRange() RoutePrefixIterator {
return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool { return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
return it.Addr().Is6() return it.Addr().Is6()
})) }))