outbound.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package option
  2. import (
  3. "github.com/goccy/go-json"
  4. E "github.com/sagernet/sing/common/exceptions"
  5. M "github.com/sagernet/sing/common/metadata"
  6. )
  7. type _Outbound struct {
  8. Tag string `json:"tag,omitempty"`
  9. Type string `json:"type,omitempty"`
  10. DirectOptions DirectOutboundOptions `json:"-"`
  11. SocksOptions SocksOutboundOptions `json:"-"`
  12. ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
  13. }
  14. type Outbound _Outbound
  15. func (h Outbound) MarshalJSON() ([]byte, error) {
  16. var v any
  17. switch h.Type {
  18. case "direct":
  19. v = h.DirectOptions
  20. case "socks":
  21. v = h.SocksOptions
  22. case "shadowsocks":
  23. v = h.ShadowsocksOptions
  24. default:
  25. return nil, E.New("unknown outbound type: ", h.Type)
  26. }
  27. return MarshallObjects((_Outbound)(h), v)
  28. }
  29. func (h *Outbound) UnmarshalJSON(bytes []byte) error {
  30. err := json.Unmarshal(bytes, (*_Outbound)(h))
  31. if err != nil {
  32. return err
  33. }
  34. var v any
  35. switch h.Type {
  36. case "direct":
  37. v = &h.DirectOptions
  38. case "socks":
  39. v = &h.SocksOptions
  40. case "shadowsocks":
  41. v = &h.ShadowsocksOptions
  42. default:
  43. return nil
  44. }
  45. err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
  46. if err != nil {
  47. return E.Cause(err, "outbound options")
  48. }
  49. return nil
  50. }
  51. type DialerOptions struct {
  52. Detour string `json:"detour,omitempty"`
  53. BindInterface string `json:"bind_interface,omitempty"`
  54. RoutingMark int `json:"routing_mark,omitempty"`
  55. ReuseAddr bool `json:"reuse_addr,omitempty"`
  56. ConnectTimeout int `json:"connect_timeout,omitempty"`
  57. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  58. OverrideOptions *OverrideStreamOptions `json:"override,omitempty"`
  59. }
  60. type OverrideStreamOptions struct {
  61. TLS bool `json:"tls,omitempty"`
  62. TLSServerName string `json:"tls_servername,omitempty"`
  63. TLSInsecure bool `json:"tls_insecure,omitempty"`
  64. UDPOverTCP bool `json:"udp_over_tcp,omitempty"`
  65. }
  66. type DirectOutboundOptions struct {
  67. DialerOptions
  68. OverrideAddress string `json:"override_address,omitempty"`
  69. OverridePort uint16 `json:"override_port,omitempty"`
  70. }
  71. type ServerOptions struct {
  72. Server string `json:"server"`
  73. ServerPort uint16 `json:"server_port"`
  74. }
  75. func (o ServerOptions) Build() M.Socksaddr {
  76. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  77. }
  78. type SocksOutboundOptions struct {
  79. DialerOptions
  80. ServerOptions
  81. Version string `json:"version,omitempty"`
  82. Username string `json:"username,omitempty"`
  83. Password string `json:"password,omitempty"`
  84. }
  85. type ShadowsocksOutboundOptions struct {
  86. DialerOptions
  87. ServerOptions
  88. Method string `json:"method"`
  89. Password string `json:"password"`
  90. }