1
0

tailscale.go 3.7 KB

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