inbound_legacy.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 _LegacyInbound struct {
  9. Type string `json:"type"`
  10. Tag string `json:"tag,omitempty"`
  11. TunOptions TunInboundOptions `json:"-"`
  12. RedirectOptions RedirectInboundOptions `json:"-"`
  13. TProxyOptions TProxyInboundOptions `json:"-"`
  14. DirectOptions DirectInboundOptions `json:"-"`
  15. SocksOptions SocksInboundOptions `json:"-"`
  16. HTTPOptions HTTPMixedInboundOptions `json:"-"`
  17. MixedOptions HTTPMixedInboundOptions `json:"-"`
  18. ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
  19. VMessOptions VMessInboundOptions `json:"-"`
  20. TrojanOptions TrojanInboundOptions `json:"-"`
  21. NaiveOptions NaiveInboundOptions `json:"-"`
  22. HysteriaOptions HysteriaInboundOptions `json:"-"`
  23. ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
  24. VLESSOptions VLESSInboundOptions `json:"-"`
  25. TUICOptions TUICInboundOptions `json:"-"`
  26. Hysteria2Options Hysteria2InboundOptions `json:"-"`
  27. }
  28. type LegacyInbound _LegacyInbound
  29. func (h *LegacyInbound) RawOptions() (any, error) {
  30. var rawOptionsPtr any
  31. switch h.Type {
  32. case C.TypeTun:
  33. rawOptionsPtr = &h.TunOptions
  34. case C.TypeRedirect:
  35. rawOptionsPtr = &h.RedirectOptions
  36. case C.TypeTProxy:
  37. rawOptionsPtr = &h.TProxyOptions
  38. case C.TypeDirect:
  39. rawOptionsPtr = &h.DirectOptions
  40. case C.TypeSOCKS:
  41. rawOptionsPtr = &h.SocksOptions
  42. case C.TypeHTTP:
  43. rawOptionsPtr = &h.HTTPOptions
  44. case C.TypeMixed:
  45. rawOptionsPtr = &h.MixedOptions
  46. case C.TypeShadowsocks:
  47. rawOptionsPtr = &h.ShadowsocksOptions
  48. case C.TypeVMess:
  49. rawOptionsPtr = &h.VMessOptions
  50. case C.TypeTrojan:
  51. rawOptionsPtr = &h.TrojanOptions
  52. case C.TypeNaive:
  53. rawOptionsPtr = &h.NaiveOptions
  54. case C.TypeHysteria:
  55. rawOptionsPtr = &h.HysteriaOptions
  56. case C.TypeShadowTLS:
  57. rawOptionsPtr = &h.ShadowTLSOptions
  58. case C.TypeVLESS:
  59. rawOptionsPtr = &h.VLESSOptions
  60. case C.TypeTUIC:
  61. rawOptionsPtr = &h.TUICOptions
  62. case C.TypeHysteria2:
  63. rawOptionsPtr = &h.Hysteria2Options
  64. case "":
  65. return nil, E.New("missing inbound type")
  66. default:
  67. return nil, E.New("unknown inbound type: ", h.Type)
  68. }
  69. return rawOptionsPtr, nil
  70. }
  71. func (h LegacyInbound) MarshalJSON() ([]byte, error) {
  72. rawOptions, err := h.RawOptions()
  73. if err != nil {
  74. return nil, err
  75. }
  76. return badjson.MarshallObjects((_LegacyInbound)(h), rawOptions)
  77. }
  78. func (h *LegacyInbound) UnmarshalJSON(bytes []byte) error {
  79. err := json.Unmarshal(bytes, (*_LegacyInbound)(h))
  80. if err != nil {
  81. return err
  82. }
  83. rawOptions, err := h.RawOptions()
  84. if err != nil {
  85. return err
  86. }
  87. err = badjson.UnmarshallExcluded(bytes, (*_LegacyInbound)(h), rawOptions)
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }