2022-07-02 06:07:50 +00:00
|
|
|
package option
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
2023-12-01 12:15:11 +00:00
|
|
|
"github.com/sagernet/sing/common/json"
|
2022-07-02 06:07:50 +00:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-06-30 13:27:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type _Outbound struct {
|
2022-09-12 09:30:54 +00:00
|
|
|
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:"-"`
|
2022-09-12 13:59:27 +00:00
|
|
|
VLESSOptions VLESSOutboundOptions `json:"-"`
|
2023-07-23 06:42:19 +00:00
|
|
|
TUICOptions TUICOutboundOptions `json:"-"`
|
2023-08-31 12:07:32 +00:00
|
|
|
Hysteria2Options Hysteria2OutboundOptions `json:"-"`
|
2022-09-12 09:30:54 +00:00
|
|
|
SelectorOptions SelectorOutboundOptions `json:"-"`
|
2022-09-15 07:22:08 +00:00
|
|
|
URLTestOptions URLTestOutboundOptions `json:"-"`
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Outbound _Outbound
|
|
|
|
|
2023-12-11 10:36:06 +00:00
|
|
|
func (h *Outbound) RawOptions() (any, error) {
|
|
|
|
var rawOptionsPtr any
|
2022-07-02 19:42:57 +00:00
|
|
|
switch h.Type {
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeDirect:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.DirectOptions
|
2022-07-23 01:15:47 +00:00
|
|
|
case C.TypeBlock, C.TypeDNS:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = nil
|
2023-08-04 09:13:46 +00:00
|
|
|
case C.TypeSOCKS:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.SocksOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeHTTP:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.HTTPOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeShadowsocks:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.ShadowsocksOptions
|
2022-07-18 04:32:31 +00:00
|
|
|
case C.TypeVMess:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.VMessOptions
|
2022-08-08 00:56:04 +00:00
|
|
|
case C.TypeTrojan:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.TrojanOptions
|
2022-08-16 15:37:51 +00:00
|
|
|
case C.TypeWireGuard:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.WireGuardOptions
|
2022-08-19 07:42:57 +00:00
|
|
|
case C.TypeHysteria:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.HysteriaOptions
|
2022-08-20 16:59:49 +00:00
|
|
|
case C.TypeTor:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.TorOptions
|
2022-08-21 11:36:08 +00:00
|
|
|
case C.TypeSSH:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.SSHOptions
|
2022-08-31 06:21:53 +00:00
|
|
|
case C.TypeShadowTLS:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.ShadowTLSOptions
|
2022-09-12 09:30:54 +00:00
|
|
|
case C.TypeShadowsocksR:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.ShadowsocksROptions
|
2022-09-12 13:59:27 +00:00
|
|
|
case C.TypeVLESS:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.VLESSOptions
|
2023-07-23 06:42:19 +00:00
|
|
|
case C.TypeTUIC:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.TUICOptions
|
2023-08-31 12:07:32 +00:00
|
|
|
case C.TypeHysteria2:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.Hysteria2Options
|
2022-07-21 13:03:41 +00:00
|
|
|
case C.TypeSelector:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.SelectorOptions
|
2022-09-15 07:22:08 +00:00
|
|
|
case C.TypeURLTest:
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptionsPtr = &h.URLTestOptions
|
|
|
|
case "":
|
|
|
|
return nil, E.New("missing outbound type")
|
2022-06-30 13:27:56 +00:00
|
|
|
default:
|
2022-07-02 19:42:57 +00:00
|
|
|
return nil, E.New("unknown outbound type: ", h.Type)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2023-12-11 10:36:06 +00:00
|
|
|
return rawOptionsPtr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Outbound) MarshalJSON() ([]byte, error) {
|
|
|
|
rawOptions, err := h.RawOptions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return MarshallObjects((*_Outbound)(h), rawOptions)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:57 +00:00
|
|
|
func (h *Outbound) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_Outbound)(h))
|
2022-07-02 17:57:04 +00:00
|
|
|
if err != nil {
|
2022-06-30 13:27:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-12-11 10:36:06 +00:00
|
|
|
rawOptions, err := h.RawOptions()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2023-12-11 10:36:06 +00:00
|
|
|
err = UnmarshallExcluded(bytes, (*_Outbound)(h), rawOptions)
|
2022-07-03 03:28:15 +00:00
|
|
|
if err != nil {
|
2023-12-01 12:15:11 +00:00
|
|
|
return err
|
2022-07-03 03:28:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 10:36:06 +00:00
|
|
|
type DialerOptionsWrapper interface {
|
|
|
|
TakeDialerOptions() DialerOptions
|
|
|
|
ReplaceDialerOptions(options DialerOptions)
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:27:56 +00:00
|
|
|
type DialerOptions struct {
|
2023-12-20 12:00:00 +00:00
|
|
|
Detour string `json:"detour,omitempty"`
|
|
|
|
BindInterface string `json:"bind_interface,omitempty"`
|
|
|
|
Inet4BindAddress *ListenAddress `json:"inet4_bind_address,omitempty"`
|
|
|
|
Inet6BindAddress *ListenAddress `json:"inet6_bind_address,omitempty"`
|
|
|
|
ProtectPath string `json:"protect_path,omitempty"`
|
2024-06-24 01:49:15 +00:00
|
|
|
RoutingMark uint32 `json:"routing_mark,omitempty"`
|
2023-12-20 12:00:00 +00:00
|
|
|
ReuseAddr bool `json:"reuse_addr,omitempty"`
|
|
|
|
ConnectTimeout Duration `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 DomainStrategy `json:"domain_strategy,omitempty"`
|
|
|
|
FallbackDelay Duration `json:"fallback_delay,omitempty"`
|
|
|
|
IsWireGuardListener bool `json:"-"`
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 10:36:06 +00:00
|
|
|
func (o *DialerOptions) TakeDialerOptions() DialerOptions {
|
|
|
|
return *o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
|
|
|
|
*o = options
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerOptionsWrapper interface {
|
|
|
|
TakeServerOptions() ServerOptions
|
|
|
|
ReplaceServerOptions(options ServerOptions)
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:07:50 +00:00
|
|
|
type ServerOptions struct {
|
2022-06-30 13:27:56 +00:00
|
|
|
Server string `json:"server"`
|
|
|
|
ServerPort uint16 `json:"server_port"`
|
2022-07-02 06:07:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o ServerOptions) Build() M.Socksaddr {
|
|
|
|
return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
|
|
|
|
}
|
2023-12-11 10:36:06 +00:00
|
|
|
|
|
|
|
func (o *ServerOptions) TakeServerOptions() ServerOptions {
|
|
|
|
return *o
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
|
|
|
|
*o = options
|
|
|
|
}
|