outbound.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
  12. }
  13. type Outbound _Outbound
  14. func (h Outbound) MarshalJSON() ([]byte, error) {
  15. var v any
  16. switch h.Type {
  17. case "direct":
  18. v = h.DirectOptions
  19. case "shadowsocks":
  20. v = h.ShadowsocksOptions
  21. default:
  22. return nil, E.New("unknown outbound type: ", h.Type)
  23. }
  24. return MarshallObjects((_Outbound)(h), v)
  25. }
  26. func (h *Outbound) UnmarshalJSON(bytes []byte) error {
  27. err := json.Unmarshal(bytes, (*_Outbound)(h))
  28. if err != nil {
  29. return err
  30. }
  31. var v any
  32. switch h.Type {
  33. case "direct":
  34. v = &h.DirectOptions
  35. case "shadowsocks":
  36. v = &h.ShadowsocksOptions
  37. default:
  38. return nil
  39. }
  40. err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
  41. if err != nil {
  42. return E.Cause(err, "outbound options")
  43. }
  44. return nil
  45. }
  46. type DialerOptions struct {
  47. Detour string `json:"detour,omitempty"`
  48. BindInterface string `json:"bind_interface,omitempty"`
  49. RoutingMark int `json:"routing_mark,omitempty"`
  50. ReuseAddr bool `json:"reuse_addr,omitempty"`
  51. ConnectTimeout int `json:"connect_timeout,omitempty"`
  52. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  53. }
  54. type DirectOutboundOptions struct {
  55. DialerOptions
  56. OverrideAddress string `json:"override_address,omitempty"`
  57. OverridePort uint16 `json:"override_port,omitempty"`
  58. }
  59. type ServerOptions struct {
  60. Server string `json:"server"`
  61. ServerPort uint16 `json:"server_port"`
  62. }
  63. func (o ServerOptions) Build() M.Socksaddr {
  64. return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
  65. }
  66. type ShadowsocksOutboundOptions struct {
  67. DialerOptions
  68. ServerOptions
  69. Method string `json:"method"`
  70. Password string `json:"password"`
  71. }