1
0

tun.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package option
  2. import (
  3. "net/netip"
  4. "strconv"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. F "github.com/sagernet/sing/common/format"
  7. "github.com/sagernet/sing/common/json"
  8. "github.com/sagernet/sing/common/json/badoption"
  9. )
  10. type TunInboundOptions struct {
  11. InterfaceName string `json:"interface_name,omitempty"`
  12. MTU uint32 `json:"mtu,omitempty"`
  13. Address badoption.Listable[netip.Prefix] `json:"address,omitempty"`
  14. AutoRoute bool `json:"auto_route,omitempty"`
  15. IPRoute2TableIndex int `json:"iproute2_table_index,omitempty"`
  16. IPRoute2RuleIndex int `json:"iproute2_rule_index,omitempty"`
  17. AutoRedirect bool `json:"auto_redirect,omitempty"`
  18. AutoRedirectInputMark FwMark `json:"auto_redirect_input_mark,omitempty"`
  19. AutoRedirectOutputMark FwMark `json:"auto_redirect_output_mark,omitempty"`
  20. StrictRoute bool `json:"strict_route,omitempty"`
  21. RouteAddress badoption.Listable[netip.Prefix] `json:"route_address,omitempty"`
  22. RouteAddressSet badoption.Listable[string] `json:"route_address_set,omitempty"`
  23. RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
  24. RouteExcludeAddressSet badoption.Listable[string] `json:"route_exclude_address_set,omitempty"`
  25. IncludeInterface badoption.Listable[string] `json:"include_interface,omitempty"`
  26. ExcludeInterface badoption.Listable[string] `json:"exclude_interface,omitempty"`
  27. IncludeUID badoption.Listable[uint32] `json:"include_uid,omitempty"`
  28. IncludeUIDRange badoption.Listable[string] `json:"include_uid_range,omitempty"`
  29. ExcludeUID badoption.Listable[uint32] `json:"exclude_uid,omitempty"`
  30. ExcludeUIDRange badoption.Listable[string] `json:"exclude_uid_range,omitempty"`
  31. IncludeAndroidUser badoption.Listable[int] `json:"include_android_user,omitempty"`
  32. IncludePackage badoption.Listable[string] `json:"include_package,omitempty"`
  33. ExcludePackage badoption.Listable[string] `json:"exclude_package,omitempty"`
  34. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  35. Stack string `json:"stack,omitempty"`
  36. Platform *TunPlatformOptions `json:"platform,omitempty"`
  37. InboundOptions
  38. // Deprecated: removed
  39. GSO bool `json:"gso,omitempty"`
  40. // Deprecated: merged to Address
  41. Inet4Address badoption.Listable[netip.Prefix] `json:"inet4_address,omitempty"`
  42. // Deprecated: merged to Address
  43. Inet6Address badoption.Listable[netip.Prefix] `json:"inet6_address,omitempty"`
  44. // Deprecated: merged to RouteAddress
  45. Inet4RouteAddress badoption.Listable[netip.Prefix] `json:"inet4_route_address,omitempty"`
  46. // Deprecated: merged to RouteAddress
  47. Inet6RouteAddress badoption.Listable[netip.Prefix] `json:"inet6_route_address,omitempty"`
  48. // Deprecated: merged to RouteExcludeAddress
  49. Inet4RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"inet4_route_exclude_address,omitempty"`
  50. // Deprecated: merged to RouteExcludeAddress
  51. Inet6RouteExcludeAddress badoption.Listable[netip.Prefix] `json:"inet6_route_exclude_address,omitempty"`
  52. // Deprecated: removed
  53. EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
  54. }
  55. type FwMark uint32
  56. func (f FwMark) MarshalJSON() ([]byte, error) {
  57. return json.Marshal(F.ToString("0x", strconv.FormatUint(uint64(f), 16)))
  58. }
  59. func (f *FwMark) UnmarshalJSON(bytes []byte) error {
  60. var stringValue string
  61. err := json.Unmarshal(bytes, &stringValue)
  62. if err != nil {
  63. if rawErr := json.Unmarshal(bytes, (*uint32)(f)); rawErr == nil {
  64. return nil
  65. }
  66. return E.Cause(err, "invalid number or string mark")
  67. }
  68. intValue, err := strconv.ParseUint(stringValue, 0, 32)
  69. if err != nil {
  70. return err
  71. }
  72. *f = FwMark(intValue)
  73. return nil
  74. }