tun.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. )
  9. type TunInboundOptions struct {
  10. InterfaceName string `json:"interface_name,omitempty"`
  11. MTU uint32 `json:"mtu,omitempty"`
  12. GSO bool `json:"gso,omitempty"`
  13. Address 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 Listable[netip.Prefix] `json:"route_address,omitempty"`
  22. RouteAddressSet Listable[string] `json:"route_address_set,omitempty"`
  23. RouteExcludeAddress Listable[netip.Prefix] `json:"route_exclude_address,omitempty"`
  24. RouteExcludeAddressSet Listable[string] `json:"route_exclude_address_set,omitempty"`
  25. IncludeInterface Listable[string] `json:"include_interface,omitempty"`
  26. ExcludeInterface Listable[string] `json:"exclude_interface,omitempty"`
  27. IncludeUID Listable[uint32] `json:"include_uid,omitempty"`
  28. IncludeUIDRange Listable[string] `json:"include_uid_range,omitempty"`
  29. ExcludeUID Listable[uint32] `json:"exclude_uid,omitempty"`
  30. ExcludeUIDRange Listable[string] `json:"exclude_uid_range,omitempty"`
  31. IncludeAndroidUser Listable[int] `json:"include_android_user,omitempty"`
  32. IncludePackage Listable[string] `json:"include_package,omitempty"`
  33. ExcludePackage Listable[string] `json:"exclude_package,omitempty"`
  34. EndpointIndependentNat bool `json:"endpoint_independent_nat,omitempty"`
  35. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  36. Stack string `json:"stack,omitempty"`
  37. Platform *TunPlatformOptions `json:"platform,omitempty"`
  38. InboundOptions
  39. // Deprecated: merged to Address
  40. Inet4Address Listable[netip.Prefix] `json:"inet4_address,omitempty"`
  41. // Deprecated: merged to Address
  42. Inet6Address Listable[netip.Prefix] `json:"inet6_address,omitempty"`
  43. // Deprecated: merged to RouteAddress
  44. Inet4RouteAddress Listable[netip.Prefix] `json:"inet4_route_address,omitempty"`
  45. // Deprecated: merged to RouteAddress
  46. Inet6RouteAddress Listable[netip.Prefix] `json:"inet6_route_address,omitempty"`
  47. // Deprecated: merged to RouteExcludeAddress
  48. Inet4RouteExcludeAddress Listable[netip.Prefix] `json:"inet4_route_exclude_address,omitempty"`
  49. // Deprecated: merged to RouteExcludeAddress
  50. Inet6RouteExcludeAddress Listable[netip.Prefix] `json:"inet6_route_exclude_address,omitempty"`
  51. }
  52. type FwMark uint32
  53. func (f FwMark) MarshalJSON() ([]byte, error) {
  54. return json.Marshal(F.ToString("0x", strconv.FormatUint(uint64(f), 16)))
  55. }
  56. func (f *FwMark) UnmarshalJSON(bytes []byte) error {
  57. var stringValue string
  58. err := json.Unmarshal(bytes, &stringValue)
  59. if err != nil {
  60. if rawErr := json.Unmarshal(bytes, (*uint32)(f)); rawErr == nil {
  61. return nil
  62. }
  63. return E.Cause(err, "invalid number or string mark")
  64. }
  65. intValue, err := strconv.ParseUint(stringValue, 0, 32)
  66. if err != nil {
  67. return err
  68. }
  69. *f = FwMark(intValue)
  70. return nil
  71. }