outbound.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package option
  2. import (
  3. C "github.com/sagernet/sing-box/constant"
  4. E "github.com/sagernet/sing/common/exceptions"
  5. M "github.com/sagernet/sing/common/metadata"
  6. "github.com/goccy/go-json"
  7. )
  8. type _Outbound struct {
  9. Type string `json:"type"`
  10. Tag string `json:"tag,omitempty"`
  11. DirectOptions DirectOutboundOptions `json:"-"`
  12. SocksOptions SocksOutboundOptions `json:"-"`
  13. HTTPOptions HTTPOutboundOptions `json:"-"`
  14. ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
  15. }
  16. type Outbound _Outbound
  17. func (h Outbound) MarshalJSON() ([]byte, error) {
  18. var v any
  19. switch h.Type {
  20. case C.TypeDirect:
  21. v = h.DirectOptions
  22. case C.TypeSocks:
  23. v = h.SocksOptions
  24. case C.TypeHTTP:
  25. v = h.HTTPOptions
  26. case C.TypeShadowsocks:
  27. v = h.ShadowsocksOptions
  28. case C.TypeBlock:
  29. v = nil
  30. default:
  31. return nil, E.New("unknown outbound type: ", h.Type)
  32. }
  33. return MarshallObjects((_Outbound)(h), v)
  34. }
  35. func (h *Outbound) UnmarshalJSON(bytes []byte) error {
  36. err := json.Unmarshal(bytes, (*_Outbound)(h))
  37. if err != nil {
  38. return err
  39. }
  40. var v any
  41. switch h.Type {
  42. case C.TypeDirect:
  43. v = &h.DirectOptions
  44. case C.TypeSocks:
  45. v = &h.SocksOptions
  46. case C.TypeHTTP:
  47. v = &h.HTTPOptions
  48. case C.TypeShadowsocks:
  49. v = &h.ShadowsocksOptions
  50. case C.TypeBlock:
  51. v = nil
  52. default:
  53. return nil
  54. }
  55. err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
  56. if err != nil {
  57. return E.Cause(err, "outbound options")
  58. }
  59. return nil
  60. }
  61. type DialerOptions struct {
  62. Detour string `json:"detour,omitempty"`
  63. BindInterface string `json:"bind_interface,omitempty"`
  64. ProtectPath string `json:"protect_path,omitempty"`
  65. RoutingMark int `json:"routing_mark,omitempty"`
  66. ReuseAddr bool `json:"reuse_addr,omitempty"`
  67. ConnectTimeout Duration `json:"connect_timeout,omitempty"`
  68. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  69. }
  70. type OutboundDialerOptions struct {
  71. DialerOptions
  72. OverrideOptions *OverrideStreamOptions `json:"override,omitempty"`
  73. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  74. FallbackDelay Duration `json:"fallback_delay,omitempty"`
  75. }
  76. type OverrideStreamOptions struct {
  77. TLS bool `json:"tls,omitempty"`
  78. TLSServerName string `json:"tls_servername,omitempty"`
  79. TLSInsecure bool `json:"tls_insecure,omitempty"`
  80. UDPOverTCP bool `json:"udp_over_tcp,omitempty"`
  81. }
  82. func (o *OverrideStreamOptions) IsValid() bool {
  83. return o != nil && (o.TLS || o.UDPOverTCP)
  84. }
  85. type ServerOptions struct {
  86. Server string `json:"server"`
  87. ServerPort uint16 `json:"server_port"`
  88. }
  89. func (o ServerOptions) Build() M.Socksaddr {
  90. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  91. }
  92. type DirectOutboundOptions struct {
  93. OutboundDialerOptions
  94. OverrideAddress string `json:"override_address,omitempty"`
  95. OverridePort uint16 `json:"override_port,omitempty"`
  96. }
  97. type SocksOutboundOptions struct {
  98. OutboundDialerOptions
  99. ServerOptions
  100. Version string `json:"version,omitempty"`
  101. Username string `json:"username,omitempty"`
  102. Password string `json:"password,omitempty"`
  103. Network NetworkList `json:"network,omitempty"`
  104. }
  105. type HTTPOutboundOptions struct {
  106. OutboundDialerOptions
  107. ServerOptions
  108. Username string `json:"username,omitempty"`
  109. Password string `json:"password,omitempty"`
  110. }
  111. type ShadowsocksOutboundOptions struct {
  112. OutboundDialerOptions
  113. ServerOptions
  114. Method string `json:"method"`
  115. Password string `json:"password"`
  116. Network NetworkList `json:"network,omitempty"`
  117. }