tun.go 4.1 KB

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