Skip to content

Commit

Permalink
Refactor inbound/outbound options struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jan 3, 2024
1 parent 36b0f2e commit 6ddcd39
Show file tree
Hide file tree
Showing 31 changed files with 844 additions and 697 deletions.
50 changes: 13 additions & 37 deletions cmd/sing-box/cmd_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,50 +65,26 @@ func merge(outputPath string) error {

func mergePathResources(options *option.Options) error {
for index, inbound := range options.Inbounds {
switch inbound.Type {
case C.TypeHTTP:
inbound.HTTPOptions.TLS = mergeTLSInboundOptions(inbound.HTTPOptions.TLS)
case C.TypeMixed:
inbound.MixedOptions.TLS = mergeTLSInboundOptions(inbound.MixedOptions.TLS)
case C.TypeVMess:
inbound.VMessOptions.TLS = mergeTLSInboundOptions(inbound.VMessOptions.TLS)
case C.TypeTrojan:
inbound.TrojanOptions.TLS = mergeTLSInboundOptions(inbound.TrojanOptions.TLS)
case C.TypeNaive:
inbound.NaiveOptions.TLS = mergeTLSInboundOptions(inbound.NaiveOptions.TLS)
case C.TypeHysteria:
inbound.HysteriaOptions.TLS = mergeTLSInboundOptions(inbound.HysteriaOptions.TLS)
case C.TypeVLESS:
inbound.VLESSOptions.TLS = mergeTLSInboundOptions(inbound.VLESSOptions.TLS)
case C.TypeTUIC:
inbound.TUICOptions.TLS = mergeTLSInboundOptions(inbound.TUICOptions.TLS)
case C.TypeHysteria2:
inbound.Hysteria2Options.TLS = mergeTLSInboundOptions(inbound.Hysteria2Options.TLS)
default:
continue
rawOptions, err := inbound.RawOptions()
if err != nil {
return err
}
if tlsOptions, containsTLSOptions := rawOptions.(option.InboundTLSOptionsWrapper); containsTLSOptions {
tlsOptions.ReplaceInboundTLSOptions(mergeTLSInboundOptions(tlsOptions.TakeInboundTLSOptions()))
}
options.Inbounds[index] = inbound
}
for index, outbound := range options.Outbounds {
rawOptions, err := outbound.RawOptions()
if err != nil {
return err
}
switch outbound.Type {
case C.TypeHTTP:
outbound.HTTPOptions.TLS = mergeTLSOutboundOptions(outbound.HTTPOptions.TLS)
case C.TypeVMess:
outbound.VMessOptions.TLS = mergeTLSOutboundOptions(outbound.VMessOptions.TLS)
case C.TypeTrojan:
outbound.TrojanOptions.TLS = mergeTLSOutboundOptions(outbound.TrojanOptions.TLS)
case C.TypeHysteria:
outbound.HysteriaOptions.TLS = mergeTLSOutboundOptions(outbound.HysteriaOptions.TLS)
case C.TypeSSH:
outbound.SSHOptions = mergeSSHOutboundOptions(outbound.SSHOptions)
case C.TypeVLESS:
outbound.VLESSOptions.TLS = mergeTLSOutboundOptions(outbound.VLESSOptions.TLS)
case C.TypeTUIC:
outbound.TUICOptions.TLS = mergeTLSOutboundOptions(outbound.TUICOptions.TLS)
case C.TypeHysteria2:
outbound.Hysteria2Options.TLS = mergeTLSOutboundOptions(outbound.Hysteria2Options.TLS)
default:
continue
}
if tlsOptions, containsTLSOptions := rawOptions.(option.OutboundTLSOptionsWrapper); containsTLSOptions {
tlsOptions.ReplaceOutboundTLSOptions(mergeTLSOutboundOptions(tlsOptions.TakeOutboundTLSOptions()))
}
options.Outbounds[index] = outbound
}
Expand Down
46 changes: 23 additions & 23 deletions option/hysteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package option

type HysteriaInboundOptions struct {
ListenOptions
Up string `json:"up,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
Down string `json:"down,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs string `json:"obfs,omitempty"`
Users []HysteriaUser `json:"users,omitempty"`
ReceiveWindowConn uint64 `json:"recv_window_conn,omitempty"`
ReceiveWindowClient uint64 `json:"recv_window_client,omitempty"`
MaxConnClient int `json:"max_conn_client,omitempty"`
DisableMTUDiscovery bool `json:"disable_mtu_discovery,omitempty"`
TLS *InboundTLSOptions `json:"tls,omitempty"`
Up string `json:"up,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
Down string `json:"down,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs string `json:"obfs,omitempty"`
Users []HysteriaUser `json:"users,omitempty"`
ReceiveWindowConn uint64 `json:"recv_window_conn,omitempty"`
ReceiveWindowClient uint64 `json:"recv_window_client,omitempty"`
MaxConnClient int `json:"max_conn_client,omitempty"`
DisableMTUDiscovery bool `json:"disable_mtu_discovery,omitempty"`
InboundTLSOptionsContainer
}

type HysteriaUser struct {
Expand All @@ -24,16 +24,16 @@ type HysteriaUser struct {
type HysteriaOutboundOptions struct {
DialerOptions
ServerOptions
Up string `json:"up,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
Down string `json:"down,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs string `json:"obfs,omitempty"`
Auth []byte `json:"auth,omitempty"`
AuthString string `json:"auth_str,omitempty"`
ReceiveWindowConn uint64 `json:"recv_window_conn,omitempty"`
ReceiveWindow uint64 `json:"recv_window,omitempty"`
DisableMTUDiscovery bool `json:"disable_mtu_discovery,omitempty"`
Network NetworkList `json:"network,omitempty"`
TLS *OutboundTLSOptions `json:"tls,omitempty"`
Up string `json:"up,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
Down string `json:"down,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs string `json:"obfs,omitempty"`
Auth []byte `json:"auth,omitempty"`
AuthString string `json:"auth_str,omitempty"`
ReceiveWindowConn uint64 `json:"recv_window_conn,omitempty"`
ReceiveWindow uint64 `json:"recv_window,omitempty"`
DisableMTUDiscovery bool `json:"disable_mtu_discovery,omitempty"`
Network NetworkList `json:"network,omitempty"`
OutboundTLSOptionsContainer
}
30 changes: 15 additions & 15 deletions option/hysteria2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package option

type Hysteria2InboundOptions struct {
ListenOptions
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Users []Hysteria2User `json:"users,omitempty"`
IgnoreClientBandwidth bool `json:"ignore_client_bandwidth,omitempty"`
TLS *InboundTLSOptions `json:"tls,omitempty"`
Masquerade string `json:"masquerade,omitempty"`
BrutalDebug bool `json:"brutal_debug,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Users []Hysteria2User `json:"users,omitempty"`
IgnoreClientBandwidth bool `json:"ignore_client_bandwidth,omitempty"`
InboundTLSOptionsContainer
Masquerade string `json:"masquerade,omitempty"`
BrutalDebug bool `json:"brutal_debug,omitempty"`
}

type Hysteria2Obfs struct {
Expand All @@ -25,11 +25,11 @@ type Hysteria2User struct {
type Hysteria2OutboundOptions struct {
DialerOptions
ServerOptions
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"`
TLS *OutboundTLSOptions `json:"tls,omitempty"`
BrutalDebug bool `json:"brutal_debug,omitempty"`
UpMbps int `json:"up_mbps,omitempty"`
DownMbps int `json:"down_mbps,omitempty"`
Obfs *Hysteria2Obfs `json:"obfs,omitempty"`
Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"`
OutboundTLSOptionsContainer
BrutalDebug bool `json:"brutal_debug,omitempty"`
}
104 changes: 46 additions & 58 deletions option/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,92 +31,67 @@ type _Inbound struct {

type Inbound _Inbound

func (h Inbound) MarshalJSON() ([]byte, error) {
var v any
func (h *Inbound) RawOptions() (any, error) {
var rawOptionsPtr any
switch h.Type {
case C.TypeTun:
v = h.TunOptions
rawOptionsPtr = &h.TunOptions
case C.TypeRedirect:
v = h.RedirectOptions
rawOptionsPtr = &h.RedirectOptions
case C.TypeTProxy:
v = h.TProxyOptions
rawOptionsPtr = &h.TProxyOptions
case C.TypeDirect:
v = h.DirectOptions
rawOptionsPtr = &h.DirectOptions
case C.TypeSOCKS:
v = h.SocksOptions
rawOptionsPtr = &h.SocksOptions
case C.TypeHTTP:
v = h.HTTPOptions
rawOptionsPtr = &h.HTTPOptions
case C.TypeMixed:
v = h.MixedOptions
rawOptionsPtr = &h.MixedOptions
case C.TypeShadowsocks:
v = h.ShadowsocksOptions
rawOptionsPtr = &h.ShadowsocksOptions
case C.TypeVMess:
v = h.VMessOptions
rawOptionsPtr = &h.VMessOptions
case C.TypeTrojan:
v = h.TrojanOptions
rawOptionsPtr = &h.TrojanOptions
case C.TypeNaive:
v = h.NaiveOptions
rawOptionsPtr = &h.NaiveOptions
case C.TypeHysteria:
v = h.HysteriaOptions
rawOptionsPtr = &h.HysteriaOptions
case C.TypeShadowTLS:
v = h.ShadowTLSOptions
rawOptionsPtr = &h.ShadowTLSOptions
case C.TypeVLESS:
v = h.VLESSOptions
rawOptionsPtr = &h.VLESSOptions
case C.TypeTUIC:
v = h.TUICOptions
rawOptionsPtr = &h.TUICOptions
case C.TypeHysteria2:
v = h.Hysteria2Options
rawOptionsPtr = &h.Hysteria2Options
case "":
return nil, E.New("missing inbound type")
default:
return nil, E.New("unknown inbound type: ", h.Type)
}
return MarshallObjects((_Inbound)(h), v)
return rawOptionsPtr, nil
}

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))
if err != nil {
return err
}
var v any
switch h.Type {
case C.TypeTun:
v = &h.TunOptions
case C.TypeRedirect:
v = &h.RedirectOptions
case C.TypeTProxy:
v = &h.TProxyOptions
case C.TypeDirect:
v = &h.DirectOptions
case C.TypeSOCKS:
v = &h.SocksOptions
case C.TypeHTTP:
v = &h.HTTPOptions
case C.TypeMixed:
v = &h.MixedOptions
case C.TypeShadowsocks:
v = &h.ShadowsocksOptions
case C.TypeVMess:
v = &h.VMessOptions
case C.TypeTrojan:
v = &h.TrojanOptions
case C.TypeNaive:
v = &h.NaiveOptions
case C.TypeHysteria:
v = &h.HysteriaOptions
case C.TypeShadowTLS:
v = &h.ShadowTLSOptions
case C.TypeVLESS:
v = &h.VLESSOptions
case C.TypeTUIC:
v = &h.TUICOptions
case C.TypeHysteria2:
v = &h.Hysteria2Options
case "":
return E.New("missing inbound type")
default:
return E.New("unknown inbound type: ", h.Type)
rawOptions, err := h.RawOptions()
if err != nil {
return err
}
err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
err = UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions)
if err != nil {
return err
}
Expand Down Expand Up @@ -160,3 +135,16 @@ func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
}
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
}
6 changes: 3 additions & 3 deletions option/naive.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "github.com/sagernet/sing/common/auth"

type NaiveInboundOptions struct {
ListenOptions
Users []auth.User `json:"users,omitempty"`
Network NetworkList `json:"network,omitempty"`
TLS *InboundTLSOptions `json:"tls,omitempty"`
Users []auth.User `json:"users,omitempty"`
Network NetworkList `json:"network,omitempty"`
InboundTLSOptionsContainer
}
Loading

0 comments on commit 6ddcd39

Please sign in to comment.