outbound.go 4.7 KB

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