outbound.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package option
  2. import (
  3. "github.com/sagernet/sing-box/common/json"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. M "github.com/sagernet/sing/common/metadata"
  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. VMessOptions VMessOutboundOptions `json:"-"`
  16. TrojanOptions TrojanOutboundOptions `json:"-"`
  17. WireGuardOptions WireGuardOutboundOptions `json:"-"`
  18. HysteriaOptions HysteriaOutboundOptions `json:"-"`
  19. TorOptions TorOutboundOptions `json:"-"`
  20. SSHOptions SSHOutboundOptions `json:"-"`
  21. ShadowTLSOptions ShadowTLSOutboundOptions `json:"-"`
  22. ShadowsocksROptions ShadowsocksROutboundOptions `json:"-"`
  23. VLESSOptions VLESSOutboundOptions `json:"-"`
  24. TUICOptions TUICOutboundOptions `json:"-"`
  25. Hysteria2Options Hysteria2OutboundOptions `json:"-"`
  26. SelectorOptions SelectorOutboundOptions `json:"-"`
  27. URLTestOptions URLTestOutboundOptions `json:"-"`
  28. }
  29. type Outbound _Outbound
  30. func (h Outbound) MarshalJSON() ([]byte, error) {
  31. var v any
  32. switch h.Type {
  33. case C.TypeDirect:
  34. v = h.DirectOptions
  35. case C.TypeBlock, C.TypeDNS:
  36. v = nil
  37. case C.TypeSOCKS:
  38. v = h.SocksOptions
  39. case C.TypeHTTP:
  40. v = h.HTTPOptions
  41. case C.TypeShadowsocks:
  42. v = h.ShadowsocksOptions
  43. case C.TypeVMess:
  44. v = h.VMessOptions
  45. case C.TypeTrojan:
  46. v = h.TrojanOptions
  47. case C.TypeWireGuard:
  48. v = h.WireGuardOptions
  49. case C.TypeHysteria:
  50. v = h.HysteriaOptions
  51. case C.TypeTor:
  52. v = h.TorOptions
  53. case C.TypeSSH:
  54. v = h.SSHOptions
  55. case C.TypeShadowTLS:
  56. v = h.ShadowTLSOptions
  57. case C.TypeShadowsocksR:
  58. v = h.ShadowsocksROptions
  59. case C.TypeVLESS:
  60. v = h.VLESSOptions
  61. case C.TypeTUIC:
  62. v = h.TUICOptions
  63. case C.TypeHysteria2:
  64. v = h.Hysteria2Options
  65. case C.TypeSelector:
  66. v = h.SelectorOptions
  67. case C.TypeURLTest:
  68. v = h.URLTestOptions
  69. default:
  70. return nil, E.New("unknown outbound type: ", h.Type)
  71. }
  72. return MarshallObjects((_Outbound)(h), v)
  73. }
  74. func (h *Outbound) UnmarshalJSON(bytes []byte) error {
  75. err := json.Unmarshal(bytes, (*_Outbound)(h))
  76. if err != nil {
  77. return err
  78. }
  79. var v any
  80. switch h.Type {
  81. case C.TypeDirect:
  82. v = &h.DirectOptions
  83. case C.TypeBlock, C.TypeDNS:
  84. v = nil
  85. case C.TypeSOCKS:
  86. v = &h.SocksOptions
  87. case C.TypeHTTP:
  88. v = &h.HTTPOptions
  89. case C.TypeShadowsocks:
  90. v = &h.ShadowsocksOptions
  91. case C.TypeVMess:
  92. v = &h.VMessOptions
  93. case C.TypeTrojan:
  94. v = &h.TrojanOptions
  95. case C.TypeWireGuard:
  96. v = &h.WireGuardOptions
  97. case C.TypeHysteria:
  98. v = &h.HysteriaOptions
  99. case C.TypeTor:
  100. v = &h.TorOptions
  101. case C.TypeSSH:
  102. v = &h.SSHOptions
  103. case C.TypeShadowTLS:
  104. v = &h.ShadowTLSOptions
  105. case C.TypeShadowsocksR:
  106. v = &h.ShadowsocksROptions
  107. case C.TypeVLESS:
  108. v = &h.VLESSOptions
  109. case C.TypeTUIC:
  110. v = &h.TUICOptions
  111. case C.TypeHysteria2:
  112. v = &h.Hysteria2Options
  113. case C.TypeSelector:
  114. v = &h.SelectorOptions
  115. case C.TypeURLTest:
  116. v = &h.URLTestOptions
  117. default:
  118. return E.New("unknown outbound type: ", h.Type)
  119. }
  120. err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
  121. if err != nil {
  122. return E.Cause(err, "outbound options")
  123. }
  124. return nil
  125. }
  126. type DialerOptions struct {
  127. Detour string `json:"detour,omitempty"`
  128. BindInterface string `json:"bind_interface,omitempty"`
  129. Inet4BindAddress *ListenAddress `json:"inet4_bind_address,omitempty"`
  130. Inet6BindAddress *ListenAddress `json:"inet6_bind_address,omitempty"`
  131. ProtectPath string `json:"protect_path,omitempty"`
  132. RoutingMark int `json:"routing_mark,omitempty"`
  133. ReuseAddr bool `json:"reuse_addr,omitempty"`
  134. ConnectTimeout Duration `json:"connect_timeout,omitempty"`
  135. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  136. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  137. UDPFragment *bool `json:"udp_fragment,omitempty"`
  138. UDPFragmentDefault bool `json:"-"`
  139. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  140. FallbackDelay Duration `json:"fallback_delay,omitempty"`
  141. }
  142. type ServerOptions struct {
  143. Server string `json:"server"`
  144. ServerPort uint16 `json:"server_port"`
  145. }
  146. func (o ServerOptions) Build() M.Socksaddr {
  147. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  148. }
  149. type MultiplexOptions struct {
  150. Enabled bool `json:"enabled,omitempty"`
  151. Protocol string `json:"protocol,omitempty"`
  152. MaxConnections int `json:"max_connections,omitempty"`
  153. MinStreams int `json:"min_streams,omitempty"`
  154. MaxStreams int `json:"max_streams,omitempty"`
  155. Padding bool `json:"padding,omitempty"`
  156. }