outbound.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package option
  2. import (
  3. C "github.com/sagernet/sing-box/constant"
  4. E "github.com/sagernet/sing/common/exceptions"
  5. "github.com/sagernet/sing/common/json"
  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) RawOptions() (any, error) {
  31. var rawOptionsPtr any
  32. switch h.Type {
  33. case C.TypeDirect:
  34. rawOptionsPtr = &h.DirectOptions
  35. case C.TypeBlock, C.TypeDNS:
  36. rawOptionsPtr = nil
  37. case C.TypeSOCKS:
  38. rawOptionsPtr = &h.SocksOptions
  39. case C.TypeHTTP:
  40. rawOptionsPtr = &h.HTTPOptions
  41. case C.TypeShadowsocks:
  42. rawOptionsPtr = &h.ShadowsocksOptions
  43. case C.TypeVMess:
  44. rawOptionsPtr = &h.VMessOptions
  45. case C.TypeTrojan:
  46. rawOptionsPtr = &h.TrojanOptions
  47. case C.TypeWireGuard:
  48. rawOptionsPtr = &h.WireGuardOptions
  49. case C.TypeHysteria:
  50. rawOptionsPtr = &h.HysteriaOptions
  51. case C.TypeTor:
  52. rawOptionsPtr = &h.TorOptions
  53. case C.TypeSSH:
  54. rawOptionsPtr = &h.SSHOptions
  55. case C.TypeShadowTLS:
  56. rawOptionsPtr = &h.ShadowTLSOptions
  57. case C.TypeShadowsocksR:
  58. rawOptionsPtr = &h.ShadowsocksROptions
  59. case C.TypeVLESS:
  60. rawOptionsPtr = &h.VLESSOptions
  61. case C.TypeTUIC:
  62. rawOptionsPtr = &h.TUICOptions
  63. case C.TypeHysteria2:
  64. rawOptionsPtr = &h.Hysteria2Options
  65. case C.TypeSelector:
  66. rawOptionsPtr = &h.SelectorOptions
  67. case C.TypeURLTest:
  68. rawOptionsPtr = &h.URLTestOptions
  69. case "":
  70. return nil, E.New("missing outbound type")
  71. default:
  72. return nil, E.New("unknown outbound type: ", h.Type)
  73. }
  74. return rawOptionsPtr, nil
  75. }
  76. func (h *Outbound) MarshalJSON() ([]byte, error) {
  77. rawOptions, err := h.RawOptions()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return MarshallObjects((*_Outbound)(h), rawOptions)
  82. }
  83. func (h *Outbound) UnmarshalJSON(bytes []byte) error {
  84. err := json.Unmarshal(bytes, (*_Outbound)(h))
  85. if err != nil {
  86. return err
  87. }
  88. rawOptions, err := h.RawOptions()
  89. if err != nil {
  90. return err
  91. }
  92. err = UnmarshallExcluded(bytes, (*_Outbound)(h), rawOptions)
  93. if err != nil {
  94. return err
  95. }
  96. return nil
  97. }
  98. type DialerOptionsWrapper interface {
  99. TakeDialerOptions() DialerOptions
  100. ReplaceDialerOptions(options DialerOptions)
  101. }
  102. type DialerOptions struct {
  103. Detour string `json:"detour,omitempty"`
  104. BindInterface string `json:"bind_interface,omitempty"`
  105. Inet4BindAddress *ListenAddress `json:"inet4_bind_address,omitempty"`
  106. Inet6BindAddress *ListenAddress `json:"inet6_bind_address,omitempty"`
  107. ProtectPath string `json:"protect_path,omitempty"`
  108. RoutingMark int `json:"routing_mark,omitempty"`
  109. ReuseAddr bool `json:"reuse_addr,omitempty"`
  110. ConnectTimeout Duration `json:"connect_timeout,omitempty"`
  111. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  112. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  113. UDPFragment *bool `json:"udp_fragment,omitempty"`
  114. UDPFragmentDefault bool `json:"-"`
  115. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  116. FallbackDelay Duration `json:"fallback_delay,omitempty"`
  117. IsWireGuardListener bool `json:"-"`
  118. }
  119. func (o *DialerOptions) TakeDialerOptions() DialerOptions {
  120. return *o
  121. }
  122. func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
  123. *o = options
  124. }
  125. type ServerOptionsWrapper interface {
  126. TakeServerOptions() ServerOptions
  127. ReplaceServerOptions(options ServerOptions)
  128. }
  129. type ServerOptions struct {
  130. Server string `json:"server"`
  131. ServerPort uint16 `json:"server_port"`
  132. }
  133. func (o ServerOptions) Build() M.Socksaddr {
  134. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  135. }
  136. func (o *ServerOptions) TakeServerOptions() ServerOptions {
  137. return *o
  138. }
  139. func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
  140. *o = options
  141. }