tun.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package libbox
  2. import (
  3. "net"
  4. "net/netip"
  5. "github.com/sagernet/sing-box/option"
  6. "github.com/sagernet/sing-tun"
  7. "github.com/sagernet/sing/common"
  8. E "github.com/sagernet/sing/common/exceptions"
  9. M "github.com/sagernet/sing/common/metadata"
  10. )
  11. type TunOptions interface {
  12. GetInet4Address() RoutePrefixIterator
  13. GetInet6Address() RoutePrefixIterator
  14. GetDNSServerAddress() (string, error)
  15. GetMTU() int32
  16. GetAutoRoute() bool
  17. GetStrictRoute() bool
  18. GetInet4RouteAddress() RoutePrefixIterator
  19. GetInet6RouteAddress() RoutePrefixIterator
  20. GetIncludePackage() StringIterator
  21. GetExcludePackage() StringIterator
  22. IsHTTPProxyEnabled() bool
  23. GetHTTPProxyServer() string
  24. GetHTTPProxyServerPort() int32
  25. }
  26. type RoutePrefix struct {
  27. Address string
  28. Prefix int32
  29. }
  30. func (p *RoutePrefix) Mask() string {
  31. var bits int
  32. if M.ParseSocksaddr(p.Address).Addr.Is6() {
  33. bits = 128
  34. } else {
  35. bits = 32
  36. }
  37. return net.IP(net.CIDRMask(int(p.Prefix), bits)).String()
  38. }
  39. type RoutePrefixIterator interface {
  40. Next() *RoutePrefix
  41. HasNext() bool
  42. }
  43. func mapRoutePrefix(prefixes []netip.Prefix) RoutePrefixIterator {
  44. return newIterator(common.Map(prefixes, func(prefix netip.Prefix) *RoutePrefix {
  45. return &RoutePrefix{
  46. Address: prefix.Addr().String(),
  47. Prefix: int32(prefix.Bits()),
  48. }
  49. }))
  50. }
  51. var _ TunOptions = (*tunOptions)(nil)
  52. type tunOptions struct {
  53. *tun.Options
  54. routeRanges []netip.Prefix
  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(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
  80. return it.Addr().Is4()
  81. }))
  82. }
  83. func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
  84. return mapRoutePrefix(common.Filter(o.routeRanges, func(it netip.Prefix) bool {
  85. return it.Addr().Is6()
  86. }))
  87. }
  88. func (o *tunOptions) GetIncludePackage() StringIterator {
  89. return newIterator(o.IncludePackage)
  90. }
  91. func (o *tunOptions) GetExcludePackage() StringIterator {
  92. return newIterator(o.ExcludePackage)
  93. }
  94. func (o *tunOptions) IsHTTPProxyEnabled() bool {
  95. if o.TunPlatformOptions.HTTPProxy == nil {
  96. return false
  97. }
  98. return o.TunPlatformOptions.HTTPProxy.Enabled
  99. }
  100. func (o *tunOptions) GetHTTPProxyServer() string {
  101. return o.TunPlatformOptions.HTTPProxy.Server
  102. }
  103. func (o *tunOptions) GetHTTPProxyServerPort() int32 {
  104. return int32(o.TunPlatformOptions.HTTPProxy.ServerPort)
  105. }