inbound.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package option
  2. import (
  3. "github.com/sagernet/sing-box/common/json"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. )
  7. type _Inbound struct {
  8. Type string `json:"type"`
  9. Tag string `json:"tag,omitempty"`
  10. TunOptions TunInboundOptions `json:"-"`
  11. RedirectOptions RedirectInboundOptions `json:"-"`
  12. TProxyOptions TProxyInboundOptions `json:"-"`
  13. DirectOptions DirectInboundOptions `json:"-"`
  14. SocksOptions SocksInboundOptions `json:"-"`
  15. HTTPOptions HTTPMixedInboundOptions `json:"-"`
  16. MixedOptions HTTPMixedInboundOptions `json:"-"`
  17. ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
  18. VMessOptions VMessInboundOptions `json:"-"`
  19. TrojanOptions TrojanInboundOptions `json:"-"`
  20. NaiveOptions NaiveInboundOptions `json:"-"`
  21. HysteriaOptions HysteriaInboundOptions `json:"-"`
  22. }
  23. type Inbound _Inbound
  24. func (h Inbound) MarshalJSON() ([]byte, error) {
  25. var v any
  26. switch h.Type {
  27. case C.TypeTun:
  28. v = h.TunOptions
  29. case C.TypeRedirect:
  30. v = h.RedirectOptions
  31. case C.TypeTProxy:
  32. v = h.TProxyOptions
  33. case C.TypeDirect:
  34. v = h.DirectOptions
  35. case C.TypeSocks:
  36. v = h.SocksOptions
  37. case C.TypeHTTP:
  38. v = h.HTTPOptions
  39. case C.TypeMixed:
  40. v = h.MixedOptions
  41. case C.TypeShadowsocks:
  42. v = h.ShadowsocksOptions
  43. case C.TypeVMess:
  44. v = h.VMessOptions
  45. case C.TypeTrojan:
  46. v = h.TrojanOptions
  47. case C.TypeNaive:
  48. v = h.NaiveOptions
  49. case C.TypeHysteria:
  50. v = h.HysteriaOptions
  51. default:
  52. return nil, E.New("unknown inbound type: ", h.Type)
  53. }
  54. return MarshallObjects((_Inbound)(h), v)
  55. }
  56. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  57. err := json.Unmarshal(bytes, (*_Inbound)(h))
  58. if err != nil {
  59. return err
  60. }
  61. var v any
  62. switch h.Type {
  63. case C.TypeTun:
  64. v = &h.TunOptions
  65. case C.TypeRedirect:
  66. v = &h.RedirectOptions
  67. case C.TypeTProxy:
  68. v = &h.TProxyOptions
  69. case C.TypeDirect:
  70. v = &h.DirectOptions
  71. case C.TypeSocks:
  72. v = &h.SocksOptions
  73. case C.TypeHTTP:
  74. v = &h.HTTPOptions
  75. case C.TypeMixed:
  76. v = &h.MixedOptions
  77. case C.TypeShadowsocks:
  78. v = &h.ShadowsocksOptions
  79. case C.TypeVMess:
  80. v = &h.VMessOptions
  81. case C.TypeTrojan:
  82. v = &h.TrojanOptions
  83. case C.TypeNaive:
  84. v = &h.NaiveOptions
  85. case C.TypeHysteria:
  86. v = &h.HysteriaOptions
  87. default:
  88. return E.New("unknown inbound type: ", h.Type)
  89. }
  90. err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
  91. if err != nil {
  92. return E.Cause(err, "inbound options")
  93. }
  94. return nil
  95. }
  96. type InboundOptions struct {
  97. SniffEnabled bool `json:"sniff,omitempty"`
  98. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  99. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  100. }
  101. type ListenOptions struct {
  102. Listen ListenAddress `json:"listen"`
  103. ListenPort uint16 `json:"listen_port,omitempty"`
  104. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  105. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  106. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  107. InboundOptions
  108. }