sing-box/option/inbound.go

100 lines
2.6 KiB
Go
Raw Normal View History

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