inbound.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package option
  2. import (
  3. "time"
  4. C "github.com/sagernet/sing-box/constant"
  5. E "github.com/sagernet/sing/common/exceptions"
  6. "github.com/sagernet/sing/common/json"
  7. )
  8. type _Inbound 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 Inbound _Inbound
  29. func (h *Inbound) 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 Inbound) MarshalJSON() ([]byte, error) {
  72. rawOptions, err := h.RawOptions()
  73. if err != nil {
  74. return nil, err
  75. }
  76. return MarshallObjects((_Inbound)(h), rawOptions)
  77. }
  78. func (h *Inbound) UnmarshalJSON(bytes []byte) error {
  79. err := json.Unmarshal(bytes, (*_Inbound)(h))
  80. if err != nil {
  81. return err
  82. }
  83. rawOptions, err := h.RawOptions()
  84. if err != nil {
  85. return err
  86. }
  87. err = UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions)
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. type InboundOptions struct {
  94. SniffEnabled bool `json:"sniff,omitempty"`
  95. SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
  96. SniffTimeout Duration `json:"sniff_timeout,omitempty"`
  97. DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
  98. UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
  99. }
  100. type ListenOptions struct {
  101. Listen *ListenAddress `json:"listen,omitempty"`
  102. ListenPort uint16 `json:"listen_port,omitempty"`
  103. TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
  104. TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
  105. UDPFragment *bool `json:"udp_fragment,omitempty"`
  106. UDPFragmentDefault bool `json:"-"`
  107. UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
  108. ProxyProtocol bool `json:"proxy_protocol,omitempty"`
  109. ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
  110. Detour string `json:"detour,omitempty"`
  111. InboundOptions
  112. }
  113. type UDPTimeoutCompat Duration
  114. func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
  115. return json.Marshal((time.Duration)(c).String())
  116. }
  117. func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
  118. var valueNumber int64
  119. err := json.Unmarshal(data, &valueNumber)
  120. if err == nil {
  121. *c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
  122. return nil
  123. }
  124. return json.Unmarshal(data, (*Duration)(c))
  125. }
  126. type ListenOptionsWrapper interface {
  127. TakeListenOptions() ListenOptions
  128. ReplaceListenOptions(options ListenOptions)
  129. }
  130. func (o *ListenOptions) TakeListenOptions() ListenOptions {
  131. return *o
  132. }
  133. func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
  134. *o = options
  135. }