tun.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package conf
  2. import (
  3. "encoding/json"
  4. "net/netip"
  5. "github.com/sagernet/sing/common"
  6. "github.com/xtls/xray-core/app/tun"
  7. )
  8. type TunConfig struct {
  9. InterfaceName string `json:"interface_name,omitempty"`
  10. MTU uint32 `json:"mtu,omitempty"`
  11. Inet4Address Listable[ListenPrefix] `json:"inet4_address,omitempty"`
  12. Inet6Address Listable[ListenPrefix] `json:"inet6_address,omitempty"`
  13. AutoRoute bool `json:"auto_route,omitempty"`
  14. StrictRoute bool `json:"strict_route,omitempty"`
  15. Inet4RouteAddress Listable[ListenPrefix] `json:"inet4_route_address,omitempty"`
  16. Inet6RouteAddress Listable[ListenPrefix] `json:"inet6_route_address,omitempty"`
  17. IncludeUID Listable[uint32] `json:"include_uid,omitempty"`
  18. IncludeUIDRange Listable[string] `json:"include_uid_range,omitempty"`
  19. ExcludeUID Listable[uint32] `json:"exclude_uid,omitempty"`
  20. ExcludeUIDRange Listable[string] `json:"exclude_uid_range,omitempty"`
  21. IncludeAndroidUser Listable[int] `json:"include_android_user,omitempty"`
  22. IncludePackage Listable[string] `json:"include_package,omitempty"`
  23. ExcludePackage Listable[string] `json:"exclude_package,omitempty"`
  24. EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
  25. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  26. Stack string `json:"stack,omitempty"`
  27. AutoDetectInterface bool `json:"auto_detect_interface,omitempty"`
  28. OverrideAndroidVPN bool `json:"override_android_vpn,omitempty"`
  29. }
  30. func (f *TunConfig) Build() (*tun.Config, error) {
  31. var config tun.Config
  32. config.InterfaceName = f.InterfaceName
  33. config.Mtu = f.MTU
  34. config.Inet4Address = common.Map(common.Map(f.Inet4Address, ListenPrefix.Build), netip.Prefix.String)
  35. config.Inet6Address = common.Map(common.Map(f.Inet6Address, ListenPrefix.Build), netip.Prefix.String)
  36. config.AutoRoute = f.AutoRoute
  37. config.StrictRoute = f.StrictRoute
  38. config.Inet4RouteAddress = common.Map(common.Map(f.Inet4RouteAddress, ListenPrefix.Build), netip.Prefix.String)
  39. config.Inet6RouteAddress = common.Map(common.Map(f.Inet6RouteAddress, ListenPrefix.Build), netip.Prefix.String)
  40. config.IncludeUid = f.IncludeUID
  41. config.IncludeUidRange = f.IncludeUIDRange
  42. config.ExcludeUid = f.ExcludeUID
  43. config.ExcludeUidRange = f.ExcludeUIDRange
  44. config.IncludeAndroidUser = common.Map(f.IncludeAndroidUser, func(it int) int32 {
  45. return int32(it)
  46. })
  47. config.IncludePackage = f.IncludePackage
  48. config.ExcludePackage = f.ExcludePackage
  49. config.EndpointIndependentNat = f.EndpointIndependentNat
  50. config.UdpTimeout = f.UDPTimeout
  51. config.Stack = f.Stack
  52. // for xray
  53. config.AutoDetectInterface = f.AutoDetectInterface
  54. config.OverrideAndroidVpn = f.OverrideAndroidVPN
  55. return &config, nil
  56. }
  57. type Listable[T comparable] []T
  58. func (l Listable[T]) MarshalJSON() ([]byte, error) {
  59. arrayList := []T(l)
  60. if len(arrayList) == 1 {
  61. return json.Marshal(arrayList[0])
  62. }
  63. return json.Marshal(arrayList)
  64. }
  65. func (l *Listable[T]) UnmarshalJSON(content []byte) error {
  66. err := json.Unmarshal(content, (*[]T)(l))
  67. if err == nil {
  68. return nil
  69. }
  70. var singleItem T
  71. err = json.Unmarshal(content, &singleItem)
  72. if err != nil {
  73. return err
  74. }
  75. *l = []T{singleItem}
  76. return nil
  77. }
  78. type ListenPrefix netip.Prefix
  79. func (p ListenPrefix) MarshalJSON() ([]byte, error) {
  80. prefix := netip.Prefix(p)
  81. if !prefix.IsValid() {
  82. return json.Marshal(nil)
  83. }
  84. return json.Marshal(prefix.String())
  85. }
  86. func (p *ListenPrefix) UnmarshalJSON(bytes []byte) error {
  87. var value string
  88. err := json.Unmarshal(bytes, &value)
  89. if err != nil {
  90. return err
  91. }
  92. prefix, err := netip.ParsePrefix(value)
  93. if err != nil {
  94. return err
  95. }
  96. *p = ListenPrefix(prefix)
  97. return nil
  98. }
  99. func (p ListenPrefix) Build() netip.Prefix {
  100. return netip.Prefix(p)
  101. }