sing-box/option/inbound.go

151 lines
4.7 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 (
"time"
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"
"github.com/sagernet/sing/common/json"
2022-06-30 13:27:56 +00:00
)
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-08-08 00:56:04 +00:00
TrojanOptions TrojanInboundOptions `json:"-"`
2022-08-10 12:19:16 +00:00
NaiveOptions NaiveInboundOptions `json:"-"`
HysteriaOptions HysteriaInboundOptions `json:"-"`
ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
VLESSOptions VLESSInboundOptions `json:"-"`
2023-07-23 06:42:19 +00:00
TUICOptions TUICInboundOptions `json:"-"`
2023-08-31 12:07:32 +00:00
Hysteria2Options Hysteria2InboundOptions `json:"-"`
2022-06-30 13:27:56 +00:00
}
type Inbound _Inbound
func (h *Inbound) RawOptions() (any, error) {
var rawOptionsPtr any
2022-07-02 19:42:57 +00:00
switch h.Type {
2022-07-18 04:32:31 +00:00
case C.TypeTun:
rawOptionsPtr = &h.TunOptions
2022-07-18 04:32:31 +00:00
case C.TypeRedirect:
rawOptionsPtr = &h.RedirectOptions
2022-07-18 04:32:31 +00:00
case C.TypeTProxy:
rawOptionsPtr = &h.TProxyOptions
case C.TypeDirect:
rawOptionsPtr = &h.DirectOptions
2023-08-04 09:13:46 +00:00
case C.TypeSOCKS:
rawOptionsPtr = &h.SocksOptions
case C.TypeHTTP:
rawOptionsPtr = &h.HTTPOptions
case C.TypeMixed:
rawOptionsPtr = &h.MixedOptions
case C.TypeShadowsocks:
rawOptionsPtr = &h.ShadowsocksOptions
2022-07-18 04:32:31 +00:00
case C.TypeVMess:
rawOptionsPtr = &h.VMessOptions
2022-08-08 00:56:04 +00:00
case C.TypeTrojan:
rawOptionsPtr = &h.TrojanOptions
2022-08-10 12:19:16 +00:00
case C.TypeNaive:
rawOptionsPtr = &h.NaiveOptions
case C.TypeHysteria:
rawOptionsPtr = &h.HysteriaOptions
case C.TypeShadowTLS:
rawOptionsPtr = &h.ShadowTLSOptions
case C.TypeVLESS:
rawOptionsPtr = &h.VLESSOptions
2023-07-23 06:42:19 +00:00
case C.TypeTUIC:
rawOptionsPtr = &h.TUICOptions
2023-08-31 12:07:32 +00:00
case C.TypeHysteria2:
rawOptionsPtr = &h.Hysteria2Options
case "":
return nil, E.New("missing inbound type")
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
}
return rawOptionsPtr, nil
}
func (h Inbound) MarshalJSON() ([]byte, error) {
rawOptions, err := h.RawOptions()
if err != nil {
return nil, err
}
return MarshallObjects((_Inbound)(h), rawOptions)
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
}
rawOptions, err := h.RawOptions()
if err != nil {
return err
2022-06-30 13:27:56 +00:00
}
err = UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions)
if err != nil {
return err
}
return nil
2022-06-30 13:27:56 +00:00
}
type InboundOptions struct {
SniffEnabled bool `json:"sniff,omitempty"`
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
SniffTimeout Duration `json:"sniff_timeout,omitempty"`
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
2022-06-30 13:27:56 +00:00
}
type ListenOptions struct {
Listen *ListenAddress `json:"listen,omitempty"`
ListenPort uint16 `json:"listen_port,omitempty"`
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
UDPFragment *bool `json:"udp_fragment,omitempty"`
UDPFragmentDefault bool `json:"-"`
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
ProxyProtocol bool `json:"proxy_protocol,omitempty"`
ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
Detour string `json:"detour,omitempty"`
InboundOptions
}
type UDPTimeoutCompat Duration
2023-12-26 02:51:39 +00:00
func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
return json.Marshal((time.Duration)(c).String())
}
func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
var valueNumber int64
err := json.Unmarshal(data, &valueNumber)
if err == nil {
2023-12-26 02:51:39 +00:00
*c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
return nil
}
2023-12-26 02:51:39 +00:00
return json.Unmarshal(data, (*Duration)(c))
}
type ListenOptionsWrapper interface {
TakeListenOptions() ListenOptions
ReplaceListenOptions(options ListenOptions)
}
func (o *ListenOptions) TakeListenOptions() ListenOptions {
return *o
}
func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
*o = options
}