tailscale.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package option
  2. import (
  3. "net/netip"
  4. "net/url"
  5. "reflect"
  6. "github.com/sagernet/sing/common/json"
  7. "github.com/sagernet/sing/common/json/badoption"
  8. M "github.com/sagernet/sing/common/metadata"
  9. )
  10. type TailscaleEndpointOptions struct {
  11. DialerOptions
  12. StateDirectory string `json:"state_directory,omitempty"`
  13. AuthKey string `json:"auth_key,omitempty"`
  14. ControlURL string `json:"control_url,omitempty"`
  15. Ephemeral bool `json:"ephemeral,omitempty"`
  16. Hostname string `json:"hostname,omitempty"`
  17. AcceptRoutes bool `json:"accept_routes,omitempty"`
  18. ExitNode string `json:"exit_node,omitempty"`
  19. ExitNodeAllowLANAccess bool `json:"exit_node_allow_lan_access,omitempty"`
  20. AdvertiseRoutes []netip.Prefix `json:"advertise_routes,omitempty"`
  21. AdvertiseExitNode bool `json:"advertise_exit_node,omitempty"`
  22. AdvertiseTags badoption.Listable[string] `json:"advertise_tags,omitempty"`
  23. RelayServerPort *uint16 `json:"relay_server_port,omitempty"`
  24. RelayServerStaticEndpoints []netip.AddrPort `json:"relay_server_static_endpoints,omitempty"`
  25. SystemInterface bool `json:"system_interface,omitempty"`
  26. SystemInterfaceName string `json:"system_interface_name,omitempty"`
  27. SystemInterfaceMTU uint32 `json:"system_interface_mtu,omitempty"`
  28. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  29. }
  30. type TailscaleDNSServerOptions struct {
  31. Endpoint string `json:"endpoint,omitempty"`
  32. AcceptDefaultResolvers bool `json:"accept_default_resolvers,omitempty"`
  33. }
  34. type DERPServiceOptions struct {
  35. ListenOptions
  36. InboundTLSOptionsContainer
  37. ConfigPath string `json:"config_path,omitempty"`
  38. VerifyClientEndpoint badoption.Listable[string] `json:"verify_client_endpoint,omitempty"`
  39. VerifyClientURL badoption.Listable[*DERPVerifyClientURLOptions] `json:"verify_client_url,omitempty"`
  40. Home string `json:"home,omitempty"`
  41. MeshWith badoption.Listable[*DERPMeshOptions] `json:"mesh_with,omitempty"`
  42. MeshPSK string `json:"mesh_psk,omitempty"`
  43. MeshPSKFile string `json:"mesh_psk_file,omitempty"`
  44. STUN *DERPSTUNListenOptions `json:"stun,omitempty"`
  45. }
  46. type _DERPVerifyClientURLOptions struct {
  47. URL string `json:"url,omitempty"`
  48. DialerOptions
  49. }
  50. type DERPVerifyClientURLOptions _DERPVerifyClientURLOptions
  51. func (d DERPVerifyClientURLOptions) ServerIsDomain() bool {
  52. verifyURL, err := url.Parse(d.URL)
  53. if err != nil {
  54. return false
  55. }
  56. return M.IsDomainName(verifyURL.Host)
  57. }
  58. func (d DERPVerifyClientURLOptions) MarshalJSON() ([]byte, error) {
  59. if reflect.DeepEqual(d, _DERPVerifyClientURLOptions{}) {
  60. return json.Marshal(d.URL)
  61. } else {
  62. return json.Marshal(_DERPVerifyClientURLOptions(d))
  63. }
  64. }
  65. func (d *DERPVerifyClientURLOptions) UnmarshalJSON(bytes []byte) error {
  66. var stringValue string
  67. err := json.Unmarshal(bytes, &stringValue)
  68. if err == nil {
  69. d.URL = stringValue
  70. return nil
  71. }
  72. return json.Unmarshal(bytes, (*_DERPVerifyClientURLOptions)(d))
  73. }
  74. type DERPMeshOptions struct {
  75. ServerOptions
  76. Host string `json:"host,omitempty"`
  77. OutboundTLSOptionsContainer
  78. DialerOptions
  79. }
  80. type _DERPSTUNListenOptions struct {
  81. Enabled bool
  82. ListenOptions
  83. }
  84. type DERPSTUNListenOptions _DERPSTUNListenOptions
  85. func (d DERPSTUNListenOptions) MarshalJSON() ([]byte, error) {
  86. portOptions := _DERPSTUNListenOptions{
  87. Enabled: d.Enabled,
  88. ListenOptions: ListenOptions{
  89. ListenPort: d.ListenPort,
  90. },
  91. }
  92. if _DERPSTUNListenOptions(d) == portOptions {
  93. return json.Marshal(d.Enabled)
  94. } else {
  95. return json.Marshal(_DERPSTUNListenOptions(d))
  96. }
  97. }
  98. func (d *DERPSTUNListenOptions) UnmarshalJSON(bytes []byte) error {
  99. var portValue uint16
  100. err := json.Unmarshal(bytes, &portValue)
  101. if err == nil {
  102. d.Enabled = true
  103. d.ListenPort = portValue
  104. return nil
  105. }
  106. return json.Unmarshal(bytes, (*_DERPSTUNListenOptions)(d))
  107. }