sing-box/experimental/libbox/tun.go

159 lines
3.7 KiB
Go
Raw Normal View History

2022-10-25 04:55:00 +00:00
package libbox
import (
2023-02-26 11:16:28 +00:00
"net"
2022-10-25 04:55:00 +00:00
"net/netip"
2023-02-28 11:02:27 +00:00
"github.com/sagernet/sing-box/option"
2022-10-25 04:55:00 +00:00
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
)
type TunOptions interface {
GetInet4Address() RoutePrefixIterator
GetInet6Address() RoutePrefixIterator
GetDNSServerAddress() (string, error)
GetMTU() int32
GetAutoRoute() bool
GetStrictRoute() bool
GetInet4RouteAddress() RoutePrefixIterator
GetInet6RouteAddress() RoutePrefixIterator
2023-12-09 14:25:29 +00:00
GetInet4RouteExcludeAddress() RoutePrefixIterator
GetInet6RouteExcludeAddress() RoutePrefixIterator
GetInet4RouteRange() RoutePrefixIterator
GetInet6RouteRange() RoutePrefixIterator
2022-10-25 04:55:00 +00:00
GetIncludePackage() StringIterator
GetExcludePackage() StringIterator
2023-02-28 11:02:27 +00:00
IsHTTPProxyEnabled() bool
GetHTTPProxyServer() string
GetHTTPProxyServerPort() int32
2022-10-25 04:55:00 +00:00
}
type RoutePrefix struct {
2023-12-09 14:25:29 +00:00
address netip.Addr
prefix int
}
func (p *RoutePrefix) Address() string {
return p.address.String()
}
func (p *RoutePrefix) Prefix() int32 {
return int32(p.prefix)
2022-10-25 04:55:00 +00:00
}
2023-02-26 11:16:28 +00:00
func (p *RoutePrefix) Mask() string {
var bits int
2023-12-09 14:25:29 +00:00
if p.address.Is6() {
2023-02-26 11:16:28 +00:00
bits = 128
} else {
bits = 32
}
2023-12-09 14:25:29 +00:00
return net.IP(net.CIDRMask(p.prefix, bits)).String()
}
func (p *RoutePrefix) String() string {
return netip.PrefixFrom(p.address, p.prefix).String()
2023-02-26 11:16:28 +00:00
}
2022-10-25 04:55:00 +00:00
type RoutePrefixIterator interface {
Next() *RoutePrefix
HasNext() bool
}
func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
return &RoutePrefix{
2023-12-09 14:25:29 +00:00
address: prefix.Addr(),
prefix: prefix.Bits(),
2022-10-25 04:55:00 +00:00
}
}))
}
var _ TunOptions = (*tunOptions)(nil)
2023-02-28 11:02:27 +00:00
type tunOptions struct {
*tun.Options
routeRanges []netip.Prefix
2023-02-28 11:02:27 +00:00
option.TunPlatformOptions
}
2022-10-25 04:55:00 +00:00
func (o *tunOptions) GetInet4Address() RoutePrefixIterator {
return mapRoutePrefix(o.Inet4Address)
}
func (o *tunOptions) GetInet6Address() RoutePrefixIterator {
return mapRoutePrefix(o.Inet6Address)
}
func (o *tunOptions) GetDNSServerAddress() (string, error) {
if len(o.Inet4Address) == 0 || o.Inet4Address[0].Bits() == 32 {
return "", E.New("need one more IPv4 address for DNS hijacking")
}
return o.Inet4Address[0].Addr().Next().String(), nil
}
func (o *tunOptions) GetMTU() int32 {
return int32(o.MTU)
}
func (o *tunOptions) GetAutoRoute() bool {
return o.AutoRoute
}
func (o *tunOptions) GetStrictRoute() bool {
return o.StrictRoute
}
func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
2023-12-09 14:25:29 +00:00
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 it.Addr().Is4()
}))
2022-10-25 04:55:00 +00:00
}
2023-12-09 14:25:29 +00:00
func (o *tunOptions) GetInet6RouteRange() RoutePrefixIterator {
return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
return it.Addr().Is6()
}))
2022-10-25 04:55:00 +00:00
}
func (o *tunOptions) GetIncludePackage() StringIterator {
return newIterator(o.IncludePackage)
}
func (o *tunOptions) GetExcludePackage() StringIterator {
return newIterator(o.ExcludePackage)
}
2023-02-28 11:02:27 +00:00
func (o *tunOptions) IsHTTPProxyEnabled() bool {
if o.TunPlatformOptions.HTTPProxy == nil {
return false
}
return o.TunPlatformOptions.HTTPProxy.Enabled
}
func (o *tunOptions) GetHTTPProxyServer() string {
return o.TunPlatformOptions.HTTPProxy.Server
}
func (o *tunOptions) GetHTTPProxyServerPort() int32 {
return int32(o.TunPlatformOptions.HTTPProxy.ServerPort)
}