mirror of
https://github.com/nitezs/sub2sing-box.git
synced 2024-12-24 12:34:43 -05:00
168 lines
4.9 KiB
Go
168 lines
4.9 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
C "sub2sing-box/constant"
|
|
"sub2sing-box/util"
|
|
)
|
|
|
|
type _Outbound struct {
|
|
Type string `json:"type"`
|
|
Tag string `json:"tag,omitempty"`
|
|
DirectOptions DirectOutboundOptions `json:"-"`
|
|
SocksOptions SocksOutboundOptions `json:"-"`
|
|
HTTPOptions HTTPOutboundOptions `json:"-"`
|
|
ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
|
|
VMessOptions VMessOutboundOptions `json:"-"`
|
|
TrojanOptions TrojanOutboundOptions `json:"-"`
|
|
WireGuardOptions WireGuardOutboundOptions `json:"-"`
|
|
HysteriaOptions HysteriaOutboundOptions `json:"-"`
|
|
TorOptions TorOutboundOptions `json:"-"`
|
|
SSHOptions SSHOutboundOptions `json:"-"`
|
|
ShadowTLSOptions ShadowTLSOutboundOptions `json:"-"`
|
|
ShadowsocksROptions ShadowsocksROutboundOptions `json:"-"`
|
|
VLESSOptions VLESSOutboundOptions `json:"-"`
|
|
TUICOptions TUICOutboundOptions `json:"-"`
|
|
Hysteria2Options Hysteria2OutboundOptions `json:"-"`
|
|
SelectorOptions SelectorOutboundOptions `json:"-"`
|
|
URLTestOptions URLTestOutboundOptions `json:"-"`
|
|
}
|
|
type Outbound _Outbound
|
|
|
|
func (h *Outbound) RawOptions() (any, error) {
|
|
var rawOptionsPtr any
|
|
switch h.Type {
|
|
case C.TypeDirect:
|
|
rawOptionsPtr = &h.DirectOptions
|
|
case C.TypeBlock, C.TypeDNS:
|
|
rawOptionsPtr = nil
|
|
case C.TypeSOCKS:
|
|
rawOptionsPtr = &h.SocksOptions
|
|
case C.TypeHTTP:
|
|
rawOptionsPtr = &h.HTTPOptions
|
|
case C.TypeShadowsocks:
|
|
rawOptionsPtr = &h.ShadowsocksOptions
|
|
case C.TypeVMess:
|
|
rawOptionsPtr = &h.VMessOptions
|
|
case C.TypeTrojan:
|
|
rawOptionsPtr = &h.TrojanOptions
|
|
case C.TypeWireGuard:
|
|
rawOptionsPtr = &h.WireGuardOptions
|
|
case C.TypeHysteria:
|
|
rawOptionsPtr = &h.HysteriaOptions
|
|
case C.TypeTor:
|
|
rawOptionsPtr = &h.TorOptions
|
|
case C.TypeSSH:
|
|
rawOptionsPtr = &h.SSHOptions
|
|
case C.TypeShadowTLS:
|
|
rawOptionsPtr = &h.ShadowTLSOptions
|
|
case C.TypeShadowsocksR:
|
|
rawOptionsPtr = &h.ShadowsocksROptions
|
|
case C.TypeVLESS:
|
|
rawOptionsPtr = &h.VLESSOptions
|
|
case C.TypeTUIC:
|
|
rawOptionsPtr = &h.TUICOptions
|
|
case C.TypeHysteria2:
|
|
rawOptionsPtr = &h.Hysteria2Options
|
|
case C.TypeSelector:
|
|
rawOptionsPtr = &h.SelectorOptions
|
|
case C.TypeURLTest:
|
|
rawOptionsPtr = &h.URLTestOptions
|
|
case "":
|
|
return nil, errors.New("missing outbound type")
|
|
default:
|
|
return nil, errors.New("unknown outbound type: " + h.Type)
|
|
}
|
|
return rawOptionsPtr, nil
|
|
}
|
|
|
|
func (h *Outbound) MarshalJSON() ([]byte, error) {
|
|
rawOptions, err := h.RawOptions()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := util.MergeAndMarshal(struct {
|
|
Type string `json:"type"`
|
|
Tag string `json:"tag,omitempty"`
|
|
}{
|
|
Type: h.Type,
|
|
Tag: h.Tag,
|
|
}, rawOptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(result), nil
|
|
}
|
|
|
|
func (h *Outbound) UnmarshalJSON(bytes []byte) error {
|
|
err := json.Unmarshal(bytes, (*_Outbound)(h))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rawOptions, err := h.RawOptions()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if rawOptions == nil {
|
|
return nil
|
|
}
|
|
err = json.Unmarshal(bytes, rawOptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rawOptionsType := reflect.TypeOf(rawOptions).Elem()
|
|
hValue := reflect.ValueOf(h).Elem()
|
|
for i := 0; i < hValue.NumField(); i++ {
|
|
fieldType := hValue.Field(i).Type()
|
|
if fieldType == rawOptionsType {
|
|
hValue.Field(i).Set(reflect.ValueOf(rawOptions).Elem())
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("unknown outbound type: " + h.Type)
|
|
}
|
|
|
|
func (h *Outbound) GetOutbounds() []string {
|
|
if h.Type == C.TypeSelector {
|
|
return h.SelectorOptions.Outbounds
|
|
}
|
|
if h.Type == C.TypeURLTest {
|
|
return h.URLTestOptions.Outbounds
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (h *Outbound) SetOutbounds(outbounds []string) {
|
|
if h.Type == C.TypeSelector {
|
|
h.SelectorOptions.Outbounds = outbounds
|
|
}
|
|
if h.Type == C.TypeURLTest {
|
|
h.URLTestOptions.Outbounds = outbounds
|
|
}
|
|
}
|
|
|
|
type DialerOptions struct {
|
|
Detour string `json:"detour,omitempty"`
|
|
BindInterface string `json:"bind_interface,omitempty"`
|
|
Inet4BindAddress string `json:"inet4_bind_address,omitempty"`
|
|
Inet6BindAddress string `json:"inet6_bind_address,omitempty"`
|
|
ProtectPath string `json:"protect_path,omitempty"`
|
|
RoutingMark int `json:"routing_mark,omitempty"`
|
|
ReuseAddr bool `json:"reuse_addr,omitempty"`
|
|
ConnectTimeout string `json:"connect_timeout,omitempty"`
|
|
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
|
TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
|
|
UDPFragment *bool `json:"udp_fragment,omitempty"`
|
|
UDPFragmentDefault bool `json:"-"`
|
|
DomainStrategy string `json:"domain_strategy,omitempty"`
|
|
FallbackDelay string `json:"fallback_delay,omitempty"`
|
|
IsWireGuardListener bool `json:"-"`
|
|
}
|
|
|
|
type ServerOptions struct {
|
|
Server string `json:"server"`
|
|
ServerPort uint16 `json:"server_port"`
|
|
}
|