mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-04-04 04:17:36 +03:00
refactor: Modular inbounds/outbounds
This commit is contained in:
parent
9f7683818f
commit
e233fd4fe5
152 changed files with 3116 additions and 2926 deletions
|
@ -1,100 +1,49 @@
|
|||
package option
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/json"
|
||||
"github.com/sagernet/sing/common/json/badjson"
|
||||
"github.com/sagernet/sing/service"
|
||||
)
|
||||
|
||||
type InboundOptionsRegistry interface {
|
||||
CreateOptions(outboundType string) (any, bool)
|
||||
}
|
||||
|
||||
type _Inbound struct {
|
||||
Type string `json:"type"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
TunOptions TunInboundOptions `json:"-"`
|
||||
RedirectOptions RedirectInboundOptions `json:"-"`
|
||||
TProxyOptions TProxyInboundOptions `json:"-"`
|
||||
DirectOptions DirectInboundOptions `json:"-"`
|
||||
SocksOptions SocksInboundOptions `json:"-"`
|
||||
HTTPOptions HTTPMixedInboundOptions `json:"-"`
|
||||
MixedOptions HTTPMixedInboundOptions `json:"-"`
|
||||
ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
|
||||
VMessOptions VMessInboundOptions `json:"-"`
|
||||
TrojanOptions TrojanInboundOptions `json:"-"`
|
||||
NaiveOptions NaiveInboundOptions `json:"-"`
|
||||
HysteriaOptions HysteriaInboundOptions `json:"-"`
|
||||
ShadowTLSOptions ShadowTLSInboundOptions `json:"-"`
|
||||
VLESSOptions VLESSInboundOptions `json:"-"`
|
||||
TUICOptions TUICInboundOptions `json:"-"`
|
||||
Hysteria2Options Hysteria2InboundOptions `json:"-"`
|
||||
Type string `json:"type"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Options any `json:"-"`
|
||||
}
|
||||
|
||||
type Inbound _Inbound
|
||||
|
||||
func (h *Inbound) RawOptions() (any, error) {
|
||||
var rawOptionsPtr any
|
||||
switch h.Type {
|
||||
case C.TypeTun:
|
||||
rawOptionsPtr = &h.TunOptions
|
||||
case C.TypeRedirect:
|
||||
rawOptionsPtr = &h.RedirectOptions
|
||||
case C.TypeTProxy:
|
||||
rawOptionsPtr = &h.TProxyOptions
|
||||
case C.TypeDirect:
|
||||
rawOptionsPtr = &h.DirectOptions
|
||||
case C.TypeSOCKS:
|
||||
rawOptionsPtr = &h.SocksOptions
|
||||
case C.TypeHTTP:
|
||||
rawOptionsPtr = &h.HTTPOptions
|
||||
case C.TypeMixed:
|
||||
rawOptionsPtr = &h.MixedOptions
|
||||
case C.TypeShadowsocks:
|
||||
rawOptionsPtr = &h.ShadowsocksOptions
|
||||
case C.TypeVMess:
|
||||
rawOptionsPtr = &h.VMessOptions
|
||||
case C.TypeTrojan:
|
||||
rawOptionsPtr = &h.TrojanOptions
|
||||
case C.TypeNaive:
|
||||
rawOptionsPtr = &h.NaiveOptions
|
||||
case C.TypeHysteria:
|
||||
rawOptionsPtr = &h.HysteriaOptions
|
||||
case C.TypeShadowTLS:
|
||||
rawOptionsPtr = &h.ShadowTLSOptions
|
||||
case C.TypeVLESS:
|
||||
rawOptionsPtr = &h.VLESSOptions
|
||||
case C.TypeTUIC:
|
||||
rawOptionsPtr = &h.TUICOptions
|
||||
case C.TypeHysteria2:
|
||||
rawOptionsPtr = &h.Hysteria2Options
|
||||
case "":
|
||||
return nil, E.New("missing inbound type")
|
||||
default:
|
||||
return nil, E.New("unknown inbound type: ", h.Type)
|
||||
}
|
||||
return rawOptionsPtr, nil
|
||||
func (h *Inbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
|
||||
return badjson.MarshallObjectsContext(ctx, (*_Inbound)(h), h.Options)
|
||||
}
|
||||
|
||||
func (h Inbound) MarshalJSON() ([]byte, error) {
|
||||
rawOptions, err := h.RawOptions()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return MarshallObjects((_Inbound)(h), rawOptions)
|
||||
}
|
||||
|
||||
func (h *Inbound) UnmarshalJSON(bytes []byte) error {
|
||||
err := json.Unmarshal(bytes, (*_Inbound)(h))
|
||||
func (h *Inbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
|
||||
err := json.Unmarshal(content, (*_Inbound)(h))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rawOptions, err := h.RawOptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions)
|
||||
registry := service.FromContext[InboundOptionsRegistry](ctx)
|
||||
if registry == nil {
|
||||
return E.New("missing inbound options registry in context")
|
||||
}
|
||||
options, loaded := registry.CreateOptions(h.Type)
|
||||
if !loaded {
|
||||
return E.New("unknown inbound type: ", h.Type)
|
||||
}
|
||||
err = badjson.UnmarshallExcludedContext(ctx, content, (*_Inbound)(h), options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
h.Options = options
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -105,19 +54,24 @@ type InboundOptions struct {
|
|||
SniffTimeout Duration `json:"sniff_timeout,omitempty"`
|
||||
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
|
||||
UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
|
||||
Detour string `json:"detour,omitempty"`
|
||||
}
|
||||
|
||||
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"`
|
||||
Listen *ListenAddress `json:"listen,omitempty"`
|
||||
ListenPort uint16 `json:"listen_port,omitempty"`
|
||||
TCPKeepAlive Duration `json:"tcp_keep_alive,omitempty"`
|
||||
TCPKeepAliveInterval Duration `json:"tcp_keep_alive_interval,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"`
|
||||
|
||||
// Deprecated: removed
|
||||
ProxyProtocol bool `json:"proxy_protocol,omitempty"`
|
||||
// Deprecated: removed
|
||||
ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
|
||||
InboundOptions
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue