sing-box/option/outbound.go

134 lines
3.5 KiB
Go
Raw Normal View History

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"
2022-07-02 06:07:50 +00:00
M "github.com/sagernet/sing/common/metadata"
2022-07-06 07:01:09 +00:00
"github.com/goccy/go-json"
2022-06-30 13:27:56 +00:00
)
type _Outbound struct {
2022-07-04 07:34:43 +00:00
Type string `json:"type"`
2022-07-02 17:57:04 +00:00
Tag string `json:"tag,omitempty"`
DirectOptions DirectOutboundOptions `json:"-"`
2022-07-03 05:14:49 +00:00
SocksOptions SocksOutboundOptions `json:"-"`
HTTPOptions HTTPOutboundOptions `json:"-"`
2022-07-02 17:57:04 +00:00
ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
2022-06-30 13:27:56 +00:00
}
type Outbound _Outbound
2022-07-02 19:42:57 +00:00
func (h Outbound) MarshalJSON() ([]byte, error) {
2022-07-02 17:57:04 +00:00
var v any
2022-07-02 19:42:57 +00:00
switch h.Type {
case C.TypeDirect:
2022-07-02 19:42:57 +00:00
v = h.DirectOptions
case C.TypeSocks:
2022-07-03 05:14:49 +00:00
v = h.SocksOptions
case C.TypeHTTP:
v = h.HTTPOptions
case C.TypeShadowsocks:
2022-07-02 19:42:57 +00:00
v = h.ShadowsocksOptions
case C.TypeBlock:
v = nil
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
}
2022-07-02 19:42:57 +00:00
return MarshallObjects((_Outbound)(h), v)
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
}
2022-07-02 17:57:04 +00:00
var v any
2022-07-02 19:42:57 +00:00
switch h.Type {
case C.TypeDirect:
2022-07-02 19:42:57 +00:00
v = &h.DirectOptions
case C.TypeSocks:
2022-07-03 05:14:49 +00:00
v = &h.SocksOptions
case C.TypeHTTP:
v = &h.HTTPOptions
case C.TypeShadowsocks:
2022-07-02 19:42:57 +00:00
v = &h.ShadowsocksOptions
case C.TypeBlock:
v = nil
2022-06-30 13:27:56 +00:00
default:
2022-07-02 17:57:04 +00:00
return nil
2022-06-30 13:27:56 +00:00
}
err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
if err != nil {
return E.Cause(err, "outbound options")
}
return nil
2022-06-30 13:27:56 +00:00
}
type DialerOptions struct {
2022-07-08 04:58:43 +00: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 15:36:32 +00:00
}
type OutboundDialerOptions struct {
DialerOptions
OverrideOptions *OverrideStreamOptions `json:"override,omitempty"`
2022-07-07 13:47:21 +00:00
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
2022-07-08 04:58:43 +00:00
FallbackDelay Duration `json:"fallback_delay,omitempty"`
}
type OverrideStreamOptions struct {
TLS bool `json:"tls,omitempty"`
TLSServerName string `json:"tls_servername,omitempty"`
TLSInsecure bool `json:"tls_insecure,omitempty"`
UDPOverTCP bool `json:"udp_over_tcp,omitempty"`
2022-06-30 13:27:56 +00:00
}
func (o *OverrideStreamOptions) IsValid() bool {
return o != nil && (o.TLS || o.UDPOverTCP)
2022-06-30 13:27:56 +00:00
}
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)
}
type DirectOutboundOptions struct {
2022-07-07 15:36:32 +00:00
OutboundDialerOptions
OverrideAddress string `json:"override_address,omitempty"`
OverridePort uint16 `json:"override_port,omitempty"`
}
2022-07-03 05:14:49 +00:00
type SocksOutboundOptions struct {
2022-07-07 15:36:32 +00:00
OutboundDialerOptions
2022-07-03 05:14:49 +00: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 15:36:32 +00:00
OutboundDialerOptions
ServerOptions
2022-07-03 05:14:49 +00:00
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
2022-07-02 06:07:50 +00:00
type ShadowsocksOutboundOptions struct {
2022-07-07 15:36:32 +00:00
OutboundDialerOptions
2022-07-02 06:07:50 +00:00
ServerOptions
Method string `json:"method"`
Password string `json:"password"`
Network NetworkList `json:"network,omitempty"`
2022-06-30 13:27:56 +00:00
}