tailscale.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. RelayServerPort *uint16 `json:"relay_server_port,omitempty"`
  23. RelayServerStaticEndpoints []netip.AddrPort `json:"relay_server_static_endpoints,omitempty"`
  24. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  25. }
  26. type TailscaleDNSServerOptions struct {
  27. Endpoint string `json:"endpoint,omitempty"`
  28. AcceptDefaultResolvers bool `json:"accept_default_resolvers,omitempty"`
  29. }
  30. type DERPServiceOptions struct {
  31. ListenOptions
  32. InboundTLSOptionsContainer
  33. ConfigPath string `json:"config_path,omitempty"`
  34. VerifyClientEndpoint badoption.Listable[string] `json:"verify_client_endpoint,omitempty"`
  35. VerifyClientURL badoption.Listable[*DERPVerifyClientURLOptions] `json:"verify_client_url,omitempty"`
  36. Home string `json:"home,omitempty"`
  37. MeshWith badoption.Listable[*DERPMeshOptions] `json:"mesh_with,omitempty"`
  38. MeshPSK string `json:"mesh_psk,omitempty"`
  39. MeshPSKFile string `json:"mesh_psk_file,omitempty"`
  40. STUN *DERPSTUNListenOptions `json:"stun,omitempty"`
  41. }
  42. type _DERPVerifyClientURLOptions struct {
  43. URL string `json:"url,omitempty"`
  44. DialerOptions
  45. }
  46. type DERPVerifyClientURLOptions _DERPVerifyClientURLOptions
  47. func (d DERPVerifyClientURLOptions) ServerIsDomain() bool {
  48. verifyURL, err := url.Parse(d.URL)
  49. if err != nil {
  50. return false
  51. }
  52. return M.IsDomainName(verifyURL.Host)
  53. }
  54. func (d DERPVerifyClientURLOptions) MarshalJSON() ([]byte, error) {
  55. if reflect.DeepEqual(d, _DERPVerifyClientURLOptions{}) {
  56. return json.Marshal(d.URL)
  57. } else {
  58. return json.Marshal(_DERPVerifyClientURLOptions(d))
  59. }
  60. }
  61. func (d *DERPVerifyClientURLOptions) UnmarshalJSON(bytes []byte) error {
  62. var stringValue string
  63. err := json.Unmarshal(bytes, &stringValue)
  64. if err == nil {
  65. d.URL = stringValue
  66. return nil
  67. }
  68. return json.Unmarshal(bytes, (*_DERPVerifyClientURLOptions)(d))
  69. }
  70. type DERPMeshOptions struct {
  71. ServerOptions
  72. Host string `json:"host,omitempty"`
  73. OutboundTLSOptionsContainer
  74. DialerOptions
  75. }
  76. type _DERPSTUNListenOptions struct {
  77. Enabled bool
  78. ListenOptions
  79. }
  80. type DERPSTUNListenOptions _DERPSTUNListenOptions
  81. func (d DERPSTUNListenOptions) MarshalJSON() ([]byte, error) {
  82. portOptions := _DERPSTUNListenOptions{
  83. Enabled: d.Enabled,
  84. ListenOptions: ListenOptions{
  85. ListenPort: d.ListenPort,
  86. },
  87. }
  88. if _DERPSTUNListenOptions(d) == portOptions {
  89. return json.Marshal(d.Enabled)
  90. } else {
  91. return json.Marshal(_DERPSTUNListenOptions(d))
  92. }
  93. }
  94. func (d *DERPSTUNListenOptions) UnmarshalJSON(bytes []byte) error {
  95. var portValue uint16
  96. err := json.Unmarshal(bytes, &portValue)
  97. if err == nil {
  98. d.Enabled = true
  99. d.ListenPort = portValue
  100. return nil
  101. }
  102. return json.Unmarshal(bytes, (*_DERPSTUNListenOptions)(d))
  103. }