tun.go 3.7 KB

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