sing-box/option/outbound.go

194 lines
5.6 KiB
Go
Raw Normal View History

2022-07-02 14:07:50 +08:00
package option
2022-06-30 21:27:56 +08:00
import (
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-21 21:03:41 +08:00
"github.com/sagernet/sing/common"
2022-06-30 21:27:56 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-02 14:07:50 +08:00
M "github.com/sagernet/sing/common/metadata"
2022-07-06 15:01:09 +08:00
"github.com/goccy/go-json"
2022-06-30 21:27:56 +08:00
)
type _Outbound struct {
2022-07-04 15:34:43 +08:00
Type string `json:"type"`
2022-07-03 01:57:04 +08:00
Tag string `json:"tag,omitempty"`
DirectOptions DirectOutboundOptions `json:"-"`
2022-07-03 13:14:49 +08:00
SocksOptions SocksOutboundOptions `json:"-"`
HTTPOptions HTTPOutboundOptions `json:"-"`
2022-07-03 01:57:04 +08:00
ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
2022-07-18 12:32:31 +08:00
VMessOptions VMessOutboundOptions `json:"-"`
2022-07-21 21:03:41 +08:00
SelectorOptions SelectorOutboundOptions `json:"-"`
2022-07-22 13:51:08 +08:00
URLTestOptions URLTestOutboundOptions `json:"-"`
2022-06-30 21:27:56 +08:00
}
type Outbound _Outbound
2022-07-21 21:03:41 +08:00
func (h Outbound) Equals(other Outbound) bool {
return h.Type == other.Type &&
h.Tag == other.Tag &&
h.DirectOptions == other.DirectOptions &&
h.SocksOptions == other.SocksOptions &&
h.HTTPOptions == other.HTTPOptions &&
h.ShadowsocksOptions == other.ShadowsocksOptions &&
h.VMessOptions == other.VMessOptions &&
2022-07-22 13:51:08 +08:00
common.Equals(h.SelectorOptions, other.SelectorOptions) &&
common.Equals(h.URLTestOptions, other.URLTestOptions)
2022-07-21 21:03:41 +08:00
}
2022-07-03 03:42:57 +08:00
func (h Outbound) MarshalJSON() ([]byte, error) {
2022-07-03 01:57:04 +08:00
var v any
2022-07-03 03:42:57 +08:00
switch h.Type {
case C.TypeDirect:
2022-07-03 03:42:57 +08:00
v = h.DirectOptions
2022-07-18 12:32:31 +08:00
case C.TypeBlock:
v = nil
case C.TypeSocks:
2022-07-03 13:14:49 +08:00
v = h.SocksOptions
case C.TypeHTTP:
v = h.HTTPOptions
case C.TypeShadowsocks:
2022-07-03 03:42:57 +08:00
v = h.ShadowsocksOptions
2022-07-18 12:32:31 +08:00
case C.TypeVMess:
v = h.VMessOptions
2022-07-21 21:03:41 +08:00
case C.TypeSelector:
v = h.SelectorOptions
2022-07-22 13:51:08 +08:00
case C.TypeURLTest:
v = h.URLTestOptions
2022-06-30 21:27:56 +08:00
default:
2022-07-03 03:42:57 +08:00
return nil, E.New("unknown outbound type: ", h.Type)
2022-06-30 21:27:56 +08:00
}
2022-07-03 03:42:57 +08:00
return MarshallObjects((_Outbound)(h), v)
2022-06-30 21:27:56 +08:00
}
2022-07-03 03:42:57 +08:00
func (h *Outbound) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Outbound)(h))
2022-07-03 01:57:04 +08:00
if err != nil {
2022-06-30 21:27:56 +08:00
return err
}
2022-07-03 01:57:04 +08:00
var v any
2022-07-03 03:42:57 +08:00
switch h.Type {
case C.TypeDirect:
2022-07-03 03:42:57 +08:00
v = &h.DirectOptions
2022-07-18 12:32:31 +08:00
case C.TypeBlock:
v = nil
case C.TypeSocks:
2022-07-03 13:14:49 +08:00
v = &h.SocksOptions
case C.TypeHTTP:
v = &h.HTTPOptions
case C.TypeShadowsocks:
2022-07-03 03:42:57 +08:00
v = &h.ShadowsocksOptions
2022-07-18 12:32:31 +08:00
case C.TypeVMess:
v = &h.VMessOptions
2022-07-21 21:03:41 +08:00
case C.TypeSelector:
v = &h.SelectorOptions
2022-07-22 13:51:08 +08:00
case C.TypeURLTest:
v = &h.URLTestOptions
2022-06-30 21:27:56 +08:00
default:
2022-07-03 01:57:04 +08:00
return nil
2022-06-30 21:27:56 +08:00
}
err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
if err != nil {
return E.Cause(err, "outbound options")
}
return nil
2022-06-30 21:27:56 +08:00
}
type DialerOptions struct {
2022-07-08 12:58:43 +08:00
Detour string `json:"detour,omitempty"`
BindInterface string `json:"bind_interface,omitempty"`
ProtectPath string `json:"protect_path,omitempty"`
RoutingMark int `json:"routing_mark,omitempty"`
ReuseAddr bool `json:"reuse_addr,omitempty"`
ConnectTimeout Duration `json:"connect_timeout,omitempty"`
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
2022-07-07 23:36:32 +08:00
}
type OutboundDialerOptions struct {
DialerOptions
2022-07-18 20:40:14 +08:00
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
FallbackDelay Duration `json:"fallback_delay,omitempty"`
2022-06-30 21:27:56 +08:00
}
2022-07-02 14:07:50 +08:00
type ServerOptions struct {
2022-06-30 21:27:56 +08:00
Server string `json:"server"`
ServerPort uint16 `json:"server_port"`
2022-07-02 14:07:50 +08:00
}
func (o ServerOptions) Build() M.Socksaddr {
return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
}
type DirectOutboundOptions struct {
2022-07-07 23:36:32 +08:00
OutboundDialerOptions
OverrideAddress string `json:"override_address,omitempty"`
OverridePort uint16 `json:"override_port,omitempty"`
}
2022-07-03 13:14:49 +08:00
type SocksOutboundOptions struct {
2022-07-07 23:36:32 +08:00
OutboundDialerOptions
2022-07-03 13:14:49 +08:00
ServerOptions
Version string `json:"version,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Network NetworkList `json:"network,omitempty"`
}
type HTTPOutboundOptions struct {
2022-07-07 23:36:32 +08:00
OutboundDialerOptions
ServerOptions
2022-07-18 20:40:14 +08:00
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
TLSOptions *OutboundTLSOptions `json:"tls,omitempty"`
}
type OutboundTLSOptions struct {
Enabled bool `json:"enabled,omitempty"`
DisableSNI bool `json:"disable_sni,omitempty"`
ServerName string `json:"server_name,omitempty"`
Insecure bool `json:"insecure,omitempty"`
2022-07-03 13:14:49 +08:00
}
2022-07-02 14:07:50 +08:00
type ShadowsocksOutboundOptions struct {
2022-07-07 23:36:32 +08:00
OutboundDialerOptions
2022-07-02 14:07:50 +08:00
ServerOptions
Method string `json:"method"`
Password string `json:"password"`
Network NetworkList `json:"network,omitempty"`
2022-06-30 21:27:56 +08:00
}
2022-07-18 12:32:31 +08:00
type VMessOutboundOptions struct {
OutboundDialerOptions
ServerOptions
2022-07-18 20:40:14 +08:00
UUID string `json:"uuid"`
Security string `json:"security"`
AlterId int `json:"alter_id,omitempty"`
GlobalPadding bool `json:"global_padding,omitempty"`
AuthenticatedLength bool `json:"authenticated_length,omitempty"`
Network NetworkList `json:"network,omitempty"`
TLSOptions *OutboundTLSOptions `json:"tls,omitempty"`
2022-07-18 12:32:31 +08:00
}
2022-07-21 21:03:41 +08:00
type SelectorOutboundOptions struct {
Outbounds []string `json:"outbounds"`
Default string `json:"default,omitempty"`
}
func (o SelectorOutboundOptions) Equals(other SelectorOutboundOptions) bool {
return common.ComparableSliceEquals(o.Outbounds, other.Outbounds) &&
o.Default == other.Default
}
2022-07-22 13:51:08 +08:00
type URLTestOutboundOptions struct {
Outbounds []string `json:"outbounds"`
URL string `json:"url,omitempty"`
Interval Duration `json:"interval,omitempty"`
Tolerance uint16 `json:"tolerance,omitempty"`
}
func (o URLTestOutboundOptions) Equals(other URLTestOutboundOptions) bool {
return common.ComparableSliceEquals(o.Outbounds, other.Outbounds) &&
o.URL == other.URL &&
o.Interval == other.Interval &&
o.Tolerance == other.Tolerance
}