outbound_legacy.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "github.com/sagernet/sing/common/json/badjson"
  7. )
  8. type _LegacyOutbound 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 LegacyOutbound _LegacyOutbound
  30. func (h *LegacyOutbound) 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 = new(StubOptions)
  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 *LegacyOutbound) MarshalJSON() ([]byte, error) {
  77. rawOptions, err := h.RawOptions()
  78. if err != nil {
  79. return nil, err
  80. }
  81. return badjson.MarshallObjects((*_LegacyOutbound)(h), rawOptions)
  82. }
  83. func (h *LegacyOutbound) UnmarshalJSON(bytes []byte) error {
  84. err := json.Unmarshal(bytes, (*_LegacyOutbound)(h))
  85. if err != nil {
  86. return err
  87. }
  88. rawOptions, err := h.RawOptions()
  89. if err != nil {
  90. return err
  91. }
  92. err = badjson.UnmarshallExcluded(bytes, (*_LegacyOutbound)(h), rawOptions)
  93. if err != nil {
  94. return err
  95. }
  96. return nil
  97. }