tun.go 2.8 KB

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