outbound.go 3.7 KB

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