tailscale.go 5.2 KB

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