github.com/sagernet/sing-box@v1.9.0-rc.20/option/outbound.go (about)

     1  package option
     2  
     3  import (
     4  	C "github.com/sagernet/sing-box/constant"
     5  	E "github.com/sagernet/sing/common/exceptions"
     6  	"github.com/sagernet/sing/common/json"
     7  	M "github.com/sagernet/sing/common/metadata"
     8  )
     9  
    10  type _Outbound struct {
    11  	Type                string                      `json:"type"`
    12  	Tag                 string                      `json:"tag,omitempty"`
    13  	DirectOptions       DirectOutboundOptions       `json:"-"`
    14  	SocksOptions        SocksOutboundOptions        `json:"-"`
    15  	HTTPOptions         HTTPOutboundOptions         `json:"-"`
    16  	ShadowsocksOptions  ShadowsocksOutboundOptions  `json:"-"`
    17  	VMessOptions        VMessOutboundOptions        `json:"-"`
    18  	TrojanOptions       TrojanOutboundOptions       `json:"-"`
    19  	WireGuardOptions    WireGuardOutboundOptions    `json:"-"`
    20  	HysteriaOptions     HysteriaOutboundOptions     `json:"-"`
    21  	TorOptions          TorOutboundOptions          `json:"-"`
    22  	SSHOptions          SSHOutboundOptions          `json:"-"`
    23  	ShadowTLSOptions    ShadowTLSOutboundOptions    `json:"-"`
    24  	ShadowsocksROptions ShadowsocksROutboundOptions `json:"-"`
    25  	VLESSOptions        VLESSOutboundOptions        `json:"-"`
    26  	TUICOptions         TUICOutboundOptions         `json:"-"`
    27  	Hysteria2Options    Hysteria2OutboundOptions    `json:"-"`
    28  	SelectorOptions     SelectorOutboundOptions     `json:"-"`
    29  	URLTestOptions      URLTestOutboundOptions      `json:"-"`
    30  }
    31  
    32  type Outbound _Outbound
    33  
    34  func (h *Outbound) RawOptions() (any, error) {
    35  	var rawOptionsPtr any
    36  	switch h.Type {
    37  	case C.TypeDirect:
    38  		rawOptionsPtr = &h.DirectOptions
    39  	case C.TypeBlock, C.TypeDNS:
    40  		rawOptionsPtr = nil
    41  	case C.TypeSOCKS:
    42  		rawOptionsPtr = &h.SocksOptions
    43  	case C.TypeHTTP:
    44  		rawOptionsPtr = &h.HTTPOptions
    45  	case C.TypeShadowsocks:
    46  		rawOptionsPtr = &h.ShadowsocksOptions
    47  	case C.TypeVMess:
    48  		rawOptionsPtr = &h.VMessOptions
    49  	case C.TypeTrojan:
    50  		rawOptionsPtr = &h.TrojanOptions
    51  	case C.TypeWireGuard:
    52  		rawOptionsPtr = &h.WireGuardOptions
    53  	case C.TypeHysteria:
    54  		rawOptionsPtr = &h.HysteriaOptions
    55  	case C.TypeTor:
    56  		rawOptionsPtr = &h.TorOptions
    57  	case C.TypeSSH:
    58  		rawOptionsPtr = &h.SSHOptions
    59  	case C.TypeShadowTLS:
    60  		rawOptionsPtr = &h.ShadowTLSOptions
    61  	case C.TypeShadowsocksR:
    62  		rawOptionsPtr = &h.ShadowsocksROptions
    63  	case C.TypeVLESS:
    64  		rawOptionsPtr = &h.VLESSOptions
    65  	case C.TypeTUIC:
    66  		rawOptionsPtr = &h.TUICOptions
    67  	case C.TypeHysteria2:
    68  		rawOptionsPtr = &h.Hysteria2Options
    69  	case C.TypeSelector:
    70  		rawOptionsPtr = &h.SelectorOptions
    71  	case C.TypeURLTest:
    72  		rawOptionsPtr = &h.URLTestOptions
    73  	case "":
    74  		return nil, E.New("missing outbound type")
    75  	default:
    76  		return nil, E.New("unknown outbound type: ", h.Type)
    77  	}
    78  	return rawOptionsPtr, nil
    79  }
    80  
    81  func (h *Outbound) MarshalJSON() ([]byte, error) {
    82  	rawOptions, err := h.RawOptions()
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	return MarshallObjects((*_Outbound)(h), rawOptions)
    87  }
    88  
    89  func (h *Outbound) UnmarshalJSON(bytes []byte) error {
    90  	err := json.Unmarshal(bytes, (*_Outbound)(h))
    91  	if err != nil {
    92  		return err
    93  	}
    94  	rawOptions, err := h.RawOptions()
    95  	if err != nil {
    96  		return err
    97  	}
    98  	err = UnmarshallExcluded(bytes, (*_Outbound)(h), rawOptions)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	return nil
   103  }
   104  
   105  type DialerOptionsWrapper interface {
   106  	TakeDialerOptions() DialerOptions
   107  	ReplaceDialerOptions(options DialerOptions)
   108  }
   109  
   110  type DialerOptions struct {
   111  	Detour              string         `json:"detour,omitempty"`
   112  	BindInterface       string         `json:"bind_interface,omitempty"`
   113  	Inet4BindAddress    *ListenAddress `json:"inet4_bind_address,omitempty"`
   114  	Inet6BindAddress    *ListenAddress `json:"inet6_bind_address,omitempty"`
   115  	ProtectPath         string         `json:"protect_path,omitempty"`
   116  	RoutingMark         int            `json:"routing_mark,omitempty"`
   117  	ReuseAddr           bool           `json:"reuse_addr,omitempty"`
   118  	ConnectTimeout      Duration       `json:"connect_timeout,omitempty"`
   119  	TCPFastOpen         bool           `json:"tcp_fast_open,omitempty"`
   120  	TCPMultiPath        bool           `json:"tcp_multi_path,omitempty"`
   121  	UDPFragment         *bool          `json:"udp_fragment,omitempty"`
   122  	UDPFragmentDefault  bool           `json:"-"`
   123  	DomainStrategy      DomainStrategy `json:"domain_strategy,omitempty"`
   124  	FallbackDelay       Duration       `json:"fallback_delay,omitempty"`
   125  	IsWireGuardListener bool           `json:"-"`
   126  }
   127  
   128  func (o *DialerOptions) TakeDialerOptions() DialerOptions {
   129  	return *o
   130  }
   131  
   132  func (o *DialerOptions) ReplaceDialerOptions(options DialerOptions) {
   133  	*o = options
   134  }
   135  
   136  type ServerOptionsWrapper interface {
   137  	TakeServerOptions() ServerOptions
   138  	ReplaceServerOptions(options ServerOptions)
   139  }
   140  
   141  type ServerOptions struct {
   142  	Server     string `json:"server"`
   143  	ServerPort uint16 `json:"server_port"`
   144  }
   145  
   146  func (o ServerOptions) Build() M.Socksaddr {
   147  	return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
   148  }
   149  
   150  func (o *ServerOptions) TakeServerOptions() ServerOptions {
   151  	return *o
   152  }
   153  
   154  func (o *ServerOptions) ReplaceServerOptions(options ServerOptions) {
   155  	*o = options
   156  }