tun.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. option.TunPlatformOptions
  55. }
  56. func (o *tunOptions) GetInet4Address() RoutePrefixIterator {
  57. return mapRoutePrefix(o.Inet4Address)
  58. }
  59. func (o *tunOptions) GetInet6Address() RoutePrefixIterator {
  60. return mapRoutePrefix(o.Inet6Address)
  61. }
  62. func (o *tunOptions) GetDNSServerAddress() (string, error) {
  63. if len(o.Inet4Address) == 0 || o.Inet4Address[0].Bits() == 32 {
  64. return "", E.New("need one more IPv4 address for DNS hijacking")
  65. }
  66. return o.Inet4Address[0].Addr().Next().String(), nil
  67. }
  68. func (o *tunOptions) GetMTU() int32 {
  69. return int32(o.MTU)
  70. }
  71. func (o *tunOptions) GetAutoRoute() bool {
  72. return o.AutoRoute
  73. }
  74. func (o *tunOptions) GetStrictRoute() bool {
  75. return o.StrictRoute
  76. }
  77. func (o *tunOptions) GetInet4RouteAddress() RoutePrefixIterator {
  78. return mapRoutePrefix(o.Inet4RouteAddress)
  79. }
  80. func (o *tunOptions) GetInet6RouteAddress() RoutePrefixIterator {
  81. return mapRoutePrefix(o.Inet6RouteAddress)
  82. }
  83. func (o *tunOptions) GetIncludePackage() StringIterator {
  84. return newIterator(o.IncludePackage)
  85. }
  86. func (o *tunOptions) GetExcludePackage() StringIterator {
  87. return newIterator(o.ExcludePackage)
  88. }
  89. func (o *tunOptions) IsHTTPProxyEnabled() bool {
  90. if o.TunPlatformOptions.HTTPProxy == nil {
  91. return false
  92. }
  93. return o.TunPlatformOptions.HTTPProxy.Enabled
  94. }
  95. func (o *tunOptions) GetHTTPProxyServer() string {
  96. return o.TunPlatformOptions.HTTPProxy.Server
  97. }
  98. func (o *tunOptions) GetHTTPProxyServerPort() int32 {
  99. return int32(o.TunPlatformOptions.HTTPProxy.ServerPort)
  100. }