sing-box/option/outbound.go

129 lines
3.5 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-24 17:58:52 +08:00
"github.com/sagernet/sing-box/common/json"
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
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-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-08-08 08:56:04 +08:00
TrojanOptions TrojanOutboundOptions `json:"-"`
2022-08-16 23:37:51 +08:00
WireGuardOptions WireGuardOutboundOptions `json:"-"`
2022-08-21 00:59:49 +08:00
HysteriaOptions HysteriaOutboundOptions `json:"-"`
TorOptions TorOutboundOptions `json:"-"`
2022-07-21 21:03:41 +08:00
SelectorOptions SelectorOutboundOptions `json:"-"`
2022-06-30 21:27:56 +08:00
}
type Outbound _Outbound
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-23 09:15:47 +08:00
case C.TypeBlock, C.TypeDNS:
2022-07-18 12:32:31 +08:00
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-08-08 08:56:04 +08:00
case C.TypeTrojan:
v = h.TrojanOptions
2022-08-16 23:37:51 +08:00
case C.TypeWireGuard:
v = h.WireGuardOptions
case C.TypeHysteria:
2022-08-21 00:59:49 +08:00
v = h.HysteriaOptions
case C.TypeTor:
v = h.TorOptions
2022-07-21 21:03:41 +08:00
case C.TypeSelector:
v = h.SelectorOptions
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-23 09:15:47 +08:00
case C.TypeBlock, C.TypeDNS:
2022-07-18 12:32:31 +08:00
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-08-08 08:56:04 +08:00
case C.TypeTrojan:
v = &h.TrojanOptions
2022-08-16 23:37:51 +08:00
case C.TypeWireGuard:
v = &h.WireGuardOptions
case C.TypeHysteria:
2022-08-21 00:59:49 +08:00
v = &h.HysteriaOptions
case C.TypeTor:
v = &h.TorOptions
2022-07-21 21:03:41 +08:00
case C.TypeSelector:
v = &h.SelectorOptions
2022-06-30 21:27:56 +08:00
default:
2022-07-25 16:01:10 +08:00
return E.New("unknown outbound type: ", h.Type)
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)
}
2022-07-30 00:29:22 +08:00
type MultiplexOptions struct {
2022-08-03 21:51:34 +08:00
Enabled bool `json:"enabled,omitempty"`
Protocol string `json:"protocol,omitempty"`
MaxConnections int `json:"max_connections,omitempty"`
MinStreams int `json:"min_streams,omitempty"`
MaxStreams int `json:"max_streams,omitempty"`
2022-07-30 00:29:22 +08:00
}