github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/experimental/libbox/tun.go (about)

     1  package libbox
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	"github.com/inazumav/sing-box/option"
     8  	"github.com/sagernet/sing-tun"
     9  	"github.com/sagernet/sing/common"
    10  	E "github.com/sagernet/sing/common/exceptions"
    11  	M "github.com/sagernet/sing/common/metadata"
    12  )
    13  
    14  type TunOptions interface {
    15  	GetInet4Address() RoutePrefixIterator
    16  	GetInet6Address() RoutePrefixIterator
    17  	GetDNSServerAddress() (string, error)
    18  	GetMTU() int32
    19  	GetAutoRoute() bool
    20  	GetStrictRoute() bool
    21  	GetInet4RouteAddress() RoutePrefixIterator
    22  	GetInet6RouteAddress() RoutePrefixIterator
    23  	GetIncludePackage() StringIterator
    24  	GetExcludePackage() StringIterator
    25  	IsHTTPProxyEnabled() bool
    26  	GetHTTPProxyServer() string
    27  	GetHTTPProxyServerPort() int32
    28  }
    29  
    30  type RoutePrefix struct {
    31  	Address string
    32  	Prefix  int32
    33  }
    34  
    35  func (p *RoutePrefix) Mask() string {
    36  	var bits int
    37  	if M.ParseSocksaddr(p.Address).Addr.Is6() {
    38  		bits = 128
    39  	} else {
    40  		bits = 32
    41  	}
    42  	return net.IP(net.CIDRMask(int(p.Prefix), bits)).String()
    43  }
    44  
    45  type RoutePrefixIterator interface {
    46  	Next() *RoutePrefix
    47  	HasNext() bool
    48  }
    49  
    50  func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
    51  	return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
    52  		return &RoutePrefix{
    53  			Address: prefix.Addr().String(),
    54  			Prefix:  int32(prefix.Bits()),
    55  		}
    56  	}))
    57  }
    58  
    59  var _ TunOptions = (*tunOptions)(nil)
    60  
    61  type tunOptions struct {
    62  	*tun.Options
    63  	option.TunPlatformOptions
    64  }
    65  
    66  func (o *tunOptions) GetInet4Address() RoutePrefixIterator {
    67  	return mapRoutePrefix(o.Inet4Address)
    68  }
    69  
    70  func (o *tunOptions) GetInet6Address() RoutePrefixIterator {
    71  	return mapRoutePrefix(o.Inet6Address)
    72  }
    73  
    74  func (o *tunOptions) GetDNSServerAddress() (string, error) {
    75  	if len(o.Inet4Address) == 0 || o.Inet4Address[0].Bits() == 32 {
    76  		return "", E.New("need one more IPv4 address for DNS hijacking")
    77  	}
    78  	return o.Inet4Address[0].Addr().Next().String(), nil
    79  }
    80  
    81  func (o *tunOptions) GetMTU() int32 {
    82  	return int32(o.MTU)
    83  }
    84  
    85  func (o *tunOptions) GetAutoRoute() bool {
    86  	return o.AutoRoute
    87  }
    88  
    89  func (o *tunOptions) GetStrictRoute() bool {
    90  	return o.StrictRoute
    91  }
    92  
    93  func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
    94  	return mapRoutePrefix(o.Inet4RouteAddress)
    95  }
    96  
    97  func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
    98  	return mapRoutePrefix(o.Inet6RouteAddress)
    99  }
   100  
   101  func (o *tunOptions) GetIncludePackage() StringIterator {
   102  	return newIterator(o.IncludePackage)
   103  }
   104  
   105  func (o *tunOptions) GetExcludePackage() StringIterator {
   106  	return newIterator(o.ExcludePackage)
   107  }
   108  
   109  func (o *tunOptions) IsHTTPProxyEnabled() bool {
   110  	if o.TunPlatformOptions.HTTPProxy == nil {
   111  		return false
   112  	}
   113  	return o.TunPlatformOptions.HTTPProxy.Enabled
   114  }
   115  
   116  func (o *tunOptions) GetHTTPProxyServer() string {
   117  	return o.TunPlatformOptions.HTTPProxy.Server
   118  }
   119  
   120  func (o *tunOptions) GetHTTPProxyServerPort() int32 {
   121  	return int32(o.TunPlatformOptions.HTTPProxy.ServerPort)
   122  }