outbound.go 4.5 KB

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