2022-07-02 14:07:50 +08:00
|
|
|
package option
|
2022-06-30 21:27:56 +08:00
|
|
|
|
|
|
|
import (
|
2022-07-24 17:58:52 +08:00
|
|
|
"github.com/sagernet/sing-box/common/json"
|
2022-07-08 23:03:57 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-06-30 21:27:56 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type _Inbound struct {
|
2022-07-03 01:57:04 +08:00
|
|
|
Type string `json:"type"`
|
2022-07-03 23:23:18 +08:00
|
|
|
Tag string `json:"tag,omitempty"`
|
2022-07-18 12:32:31 +08:00
|
|
|
TunOptions TunInboundOptions `json:"-"`
|
|
|
|
RedirectOptions RedirectInboundOptions `json:"-"`
|
|
|
|
TProxyOptions TProxyInboundOptions `json:"-"`
|
2022-07-03 01:57:04 +08:00
|
|
|
DirectOptions DirectInboundOptions `json:"-"`
|
2022-07-14 14:04:57 +08:00
|
|
|
SocksOptions SocksInboundOptions `json:"-"`
|
|
|
|
HTTPOptions HTTPMixedInboundOptions `json:"-"`
|
|
|
|
MixedOptions HTTPMixedInboundOptions `json:"-"`
|
2022-07-03 01:57:04 +08:00
|
|
|
ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
|
2022-07-18 12:32:31 +08:00
|
|
|
VMessOptions VMessInboundOptions `json:"-"`
|
2022-08-08 08:56:04 +08:00
|
|
|
TrojanOptions TrojanInboundOptions `json:"-"`
|
2022-08-10 20:19:16 +08:00
|
|
|
NaiveOptions NaiveInboundOptions `json:"-"`
|
2022-08-19 15:42:57 +08:00
|
|
|
HysteriaOptions HysteriaInboundOptions `json:"-"`
|
2022-08-31 14:21:53 +08:00
|
|
|
ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Inbound _Inbound
|
|
|
|
|
2022-07-03 03:42:57 +08:00
|
|
|
func (h Inbound) MarshalJSON() ([]byte, error) {
|
2022-07-03 01:57:04 +08:00
|
|
|
var v any
|
2022-07-03 03:42:57 +08:00
|
|
|
switch h.Type {
|
2022-07-18 12:32:31 +08:00
|
|
|
case C.TypeTun:
|
|
|
|
v = h.TunOptions
|
|
|
|
case C.TypeRedirect:
|
|
|
|
v = h.RedirectOptions
|
|
|
|
case C.TypeTProxy:
|
|
|
|
v = h.TProxyOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeDirect:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = h.DirectOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeSocks:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = h.SocksOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeHTTP:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = h.HTTPOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeMixed:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = h.MixedOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeShadowsocks:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = h.ShadowsocksOptions
|
2022-07-18 12:32:31 +08:00
|
|
|
case C.TypeVMess:
|
|
|
|
v = h.VMessOptions
|
2022-08-08 08:56:04 +08:00
|
|
|
case C.TypeTrojan:
|
|
|
|
v = h.TrojanOptions
|
2022-08-10 20:19:16 +08:00
|
|
|
case C.TypeNaive:
|
|
|
|
v = h.NaiveOptions
|
2022-08-19 15:42:57 +08:00
|
|
|
case C.TypeHysteria:
|
|
|
|
v = h.HysteriaOptions
|
2022-08-31 14:21:53 +08:00
|
|
|
case C.TypeShadowTLS:
|
|
|
|
v = h.ShadowTLSOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
default:
|
2022-07-03 03:42:57 +08:00
|
|
|
return nil, E.New("unknown inbound type: ", h.Type)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 03:42:57 +08:00
|
|
|
return MarshallObjects((_Inbound)(h), v)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-03 03:42:57 +08:00
|
|
|
func (h *Inbound) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_Inbound)(h))
|
2022-06-30 21:27:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
var v any
|
2022-07-03 03:42:57 +08:00
|
|
|
switch h.Type {
|
2022-07-18 12:32:31 +08:00
|
|
|
case C.TypeTun:
|
|
|
|
v = &h.TunOptions
|
|
|
|
case C.TypeRedirect:
|
|
|
|
v = &h.RedirectOptions
|
|
|
|
case C.TypeTProxy:
|
|
|
|
v = &h.TProxyOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeDirect:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = &h.DirectOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeSocks:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = &h.SocksOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeHTTP:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = &h.HTTPOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeMixed:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = &h.MixedOptions
|
2022-07-03 23:23:18 +08:00
|
|
|
case C.TypeShadowsocks:
|
2022-07-03 03:42:57 +08:00
|
|
|
v = &h.ShadowsocksOptions
|
2022-07-18 12:32:31 +08:00
|
|
|
case C.TypeVMess:
|
|
|
|
v = &h.VMessOptions
|
2022-08-08 08:56:04 +08:00
|
|
|
case C.TypeTrojan:
|
|
|
|
v = &h.TrojanOptions
|
2022-08-10 20:19:16 +08:00
|
|
|
case C.TypeNaive:
|
|
|
|
v = &h.NaiveOptions
|
2022-08-19 15:42:57 +08:00
|
|
|
case C.TypeHysteria:
|
|
|
|
v = &h.HysteriaOptions
|
2022-08-31 14:21:53 +08:00
|
|
|
case C.TypeShadowTLS:
|
|
|
|
v = &h.ShadowTLSOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
default:
|
2022-07-18 12:32:31 +08:00
|
|
|
return E.New("unknown inbound type: ", h.Type)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 11:28:15 +08:00
|
|
|
err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "inbound options")
|
|
|
|
}
|
|
|
|
return nil
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-10 08:18:52 +08:00
|
|
|
type InboundOptions struct {
|
2022-07-07 23:36:32 +08:00
|
|
|
SniffEnabled bool `json:"sniff,omitempty"`
|
|
|
|
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
|
|
|
|
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
2022-07-10 08:18:52 +08:00
|
|
|
type ListenOptions struct {
|
2022-09-07 23:01:57 +08:00
|
|
|
Listen ListenAddress `json:"listen"`
|
|
|
|
ListenPort uint16 `json:"listen_port,omitempty"`
|
|
|
|
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
|
|
|
UDPFragment bool `json:"udp_fragment,omitempty"`
|
|
|
|
UDPTimeout int64 `json:"udp_timeout,omitempty"`
|
|
|
|
ProxyProtocol bool `json:"proxy_protocol,omitempty"`
|
|
|
|
ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
|
|
|
|
Detour string `json:"detour,omitempty"`
|
2022-07-10 08:18:52 +08:00
|
|
|
InboundOptions
|
|
|
|
}
|