inbound.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
  23. VLESSOptions VLESSInboundOptions `json:"-"`
  24. }
  25. type Inbound _Inbound
  26. func (h Inbound) MarshalJSON() ([]byte, error) {
  27. var v any
  28. switch h.Type {
  29. case C.TypeTun:
  30. v = h.TunOptions
  31. case C.TypeRedirect:
  32. v = h.RedirectOptions
  33. case C.TypeTProxy:
  34. v = h.TProxyOptions
  35. case C.TypeDirect:
  36. v = h.DirectOptions
  37. case C.TypeSocks:
  38. v = h.SocksOptions
  39. case C.TypeHTTP:
  40. v = h.HTTPOptions
  41. case C.TypeMixed:
  42. v = h.MixedOptions
  43. case C.TypeShadowsocks:
  44. v = h.ShadowsocksOptions
  45. case C.TypeVMess:
  46. v = h.VMessOptions
  47. case C.TypeTrojan:
  48. v = h.TrojanOptions
  49. case C.TypeNaive:
  50. v = h.NaiveOptions
  51. case C.TypeHysteria:
  52. v = h.HysteriaOptions
  53. case C.TypeShadowTLS:
  54. v = h.ShadowTLSOptions
  55. case C.TypeVLESS:
  56. v = h.VLESSOptions
  57. default:
  58. return nil, E.New("unknown inbound type: ", h.Type)
  59. }
  60. return MarshallObjects((_Inbound)(h), v)
  61. }
  62. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  63. err := json.Unmarshal(bytes, (*_Inbound)(h))
  64. if err != nil {
  65. return err
  66. }
  67. var v any
  68. switch h.Type {
  69. case C.TypeTun:
  70. v = &h.TunOptions
  71. case C.TypeRedirect:
  72. v = &h.RedirectOptions
  73. case C.TypeTProxy:
  74. v = &h.TProxyOptions
  75. case C.TypeDirect:
  76. v = &h.DirectOptions
  77. case C.TypeSocks:
  78. v = &h.SocksOptions
  79. case C.TypeHTTP:
  80. v = &h.HTTPOptions
  81. case C.TypeMixed:
  82. v = &h.MixedOptions
  83. case C.TypeShadowsocks:
  84. v = &h.ShadowsocksOptions
  85. case C.TypeVMess:
  86. v = &h.VMessOptions
  87. case C.TypeTrojan:
  88. v = &h.TrojanOptions
  89. case C.TypeNaive:
  90. v = &h.NaiveOptions
  91. case C.TypeHysteria:
  92. v = &h.HysteriaOptions
  93. case C.TypeShadowTLS:
  94. v = &h.ShadowTLSOptions
  95. case C.TypeVLESS:
  96. v = &h.VLESSOptions
  97. default:
  98. return E.New("unknown inbound type: ", h.Type)
  99. }
  100. err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
  101. if err != nil {
  102. return E.Cause(err, "inbound options")
  103. }
  104. return nil
  105. }
  106. type InboundOptions struct {
  107. SniffEnabled bool `json:"sniff,omitempty"`
  108. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  109. SniffTimeout Duration `json:"sniff_timeout,omitempty"`
  110. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  111. }
  112. type ListenOptions struct {
  113. Listen *ListenAddress `json:"listen,omitempty"`
  114. ListenPort uint16 `json:"listen_port,omitempty"`
  115. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  116. UDPFragment *bool `json:"udp_fragment,omitempty"`
  117. UDPFragmentDefault bool `json:"-"`
  118. UDPTimeout int64 `json:"udp_timeout,omitempty"`
  119. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  120. ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
  121. Detour string `json:"detour,omitempty"`
  122. InboundOptions
  123. }