sing-box/option/outbound.go

118 lines
4.1 KiB
Go
Raw Permalink Normal View History

2022-07-02 06:07:50 +00:00
package option
2022-06-30 13:27:56 +00:00
import (
2024-11-01 16:39:02 +00:00
"context"
2024-11-07 04:02:36 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/experimental/deprecated"
2022-06-30 13:27:56 +00:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/json/badjson"
2024-11-18 10:55:34 +00:00
"github.com/sagernet/sing/common/json/badoption"
2022-07-02 06:07:50 +00:00
M "github.com/sagernet/sing/common/metadata"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/service"
2022-06-30 13:27:56 +00:00
)
2024-11-01 16:39:02 +00:00
type OutboundOptionsRegistry interface {
CreateOptions(outboundType string) (any, bool)
}
2022-06-30 13:27:56 +00:00
type _Outbound struct {
2024-11-01 16:39:02 +00:00
Type string `json:"type"`
Tag string `json:"tag,omitempty"`
Options any `json:"-"`
2022-06-30 13:27:56 +00:00
}
type Outbound _Outbound
2024-11-01 16:39:02 +00:00
func (h *Outbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
return badjson.MarshallObjectsContext(ctx, (*_Outbound)(h), h.Options)
}
2024-11-01 16:39:02 +00:00
func (h *Outbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
2024-11-21 10:10:41 +00:00
err := json.UnmarshalContext(ctx, content, (*_Outbound)(h))
2022-07-02 17:57:04 +00:00
if err != nil {
2022-06-30 13:27:56 +00:00
return err
}
2024-11-01 16:39:02 +00:00
registry := service.FromContext[OutboundOptionsRegistry](ctx)
if registry == nil {
return E.New("missing outbound options registry in context")
}
2024-11-07 04:02:36 +00:00
switch h.Type {
case C.TypeBlock, C.TypeDNS:
deprecated.Report(ctx, deprecated.OptionSpecialOutbounds)
}
2024-11-01 16:39:02 +00:00
options, loaded := registry.CreateOptions(h.Type)
if !loaded {
return E.New("unknown outbound type: ", h.Type)
2022-06-30 13:27:56 +00:00
}
2024-11-01 16:39:02 +00:00
err = badjson.UnmarshallExcludedContext(ctx, content, (*_Outbound)(h), options)
if err != nil {
return err
}
2024-11-07 04:02:36 +00:00
if listenWrapper, isListen := options.(ListenOptionsWrapper); isListen {
if listenWrapper.TakeListenOptions().InboundOptions != (InboundOptions{}) {
deprecated.Report(ctx, deprecated.OptionInboundOptions)
}
}
2024-11-01 16:39:02 +00:00
h.Options = options
return nil
2022-06-30 13:27:56 +00:00
}
type DialerOptionsWrapper interface {
TakeDialerOptions() DialerOptions
ReplaceDialerOptions(options DialerOptions)
}
2022-06-30 13:27:56 +00:00
type DialerOptions struct {
2024-11-13 11:05:28 +00:00
Detour string `json:"detour,omitempty"`
BindInterface string `json:"bind_interface,omitempty"`
Inet4BindAddress *badoption.Addr `json:"inet4_bind_address,omitempty"`
Inet6BindAddress *badoption.Addr `json:"inet6_bind_address,omitempty"`
ProtectPath string `json:"protect_path,omitempty"`
RoutingMark FwMark `json:"routing_mark,omitempty"`
ReuseAddr bool `json:"reuse_addr,omitempty"`
ConnectTimeout badoption.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"`
NetworkStrategy NetworkStrategy `json:"network_strategy,omitempty"`
NetworkType badoption.Listable[InterfaceType] `json:"network_type,omitempty"`
FallbackNetworkType badoption.Listable[InterfaceType] `json:"fallback_network_type,omitempty"`
FallbackDelay badoption.Duration `json:"fallback_delay,omitempty"`
NetworkFallbackDelay badoption.Duration `json:"network_fallback_delay,omitempty"`
IsWireGuardListener bool `json:"-"`
2022-06-30 13:27:56 +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)
}
func (o *ServerOptions) TakeServerOptions() ServerOptions {
return *o
}
func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
*o = options
}