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

     1  package option
     2  
     3  import (
     4  	"github.com/sagernet/sing-box/common/json"
     5  	C "github.com/sagernet/sing-box/constant"
     6  	E "github.com/sagernet/sing/common/exceptions"
     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  	SelectorOptions     SelectorOutboundOptions     `json:"-"`
    27  	URLTestOptions      URLTestOutboundOptions      `json:"-"`
    28  }
    29  
    30  type Outbound _Outbound
    31  
    32  func (h Outbound) MarshalJSON() ([]byte, error) {
    33  	var v any
    34  	switch h.Type {
    35  	case C.TypeDirect:
    36  		v = h.DirectOptions
    37  	case C.TypeBlock, C.TypeDNS:
    38  		v = nil
    39  	case C.TypeSocks:
    40  		v = h.SocksOptions
    41  	case C.TypeHTTP:
    42  		v = h.HTTPOptions
    43  	case C.TypeShadowsocks:
    44  		v = h.ShadowsocksOptions
    45  	case C.TypeVMess:
    46  		v = h.VMessOptions
    47  	case C.TypeTrojan:
    48  		v = h.TrojanOptions
    49  	case C.TypeWireGuard:
    50  		v = h.WireGuardOptions
    51  	case C.TypeHysteria:
    52  		v = h.HysteriaOptions
    53  	case C.TypeTor:
    54  		v = h.TorOptions
    55  	case C.TypeSSH:
    56  		v = h.SSHOptions
    57  	case C.TypeShadowTLS:
    58  		v = h.ShadowTLSOptions
    59  	case C.TypeShadowsocksR:
    60  		v = h.ShadowsocksROptions
    61  	case C.TypeVLESS:
    62  		v = h.VLESSOptions
    63  	case C.TypeSelector:
    64  		v = h.SelectorOptions
    65  	case C.TypeURLTest:
    66  		v = h.URLTestOptions
    67  	default:
    68  		return nil, E.New("unknown outbound type: ", h.Type)
    69  	}
    70  	return MarshallObjects((_Outbound)(h), v)
    71  }
    72  
    73  func (h *Outbound) UnmarshalJSON(bytes []byte) error {
    74  	err := json.Unmarshal(bytes, (*_Outbound)(h))
    75  	if err != nil {
    76  		return err
    77  	}
    78  	var v any
    79  	switch h.Type {
    80  	case C.TypeDirect:
    81  		v = &h.DirectOptions
    82  	case C.TypeBlock, C.TypeDNS:
    83  		v = nil
    84  	case C.TypeSocks:
    85  		v = &h.SocksOptions
    86  	case C.TypeHTTP:
    87  		v = &h.HTTPOptions
    88  	case C.TypeShadowsocks:
    89  		v = &h.ShadowsocksOptions
    90  	case C.TypeVMess:
    91  		v = &h.VMessOptions
    92  	case C.TypeTrojan:
    93  		v = &h.TrojanOptions
    94  	case C.TypeWireGuard:
    95  		v = &h.WireGuardOptions
    96  	case C.TypeHysteria:
    97  		v = &h.HysteriaOptions
    98  	case C.TypeTor:
    99  		v = &h.TorOptions
   100  	case C.TypeSSH:
   101  		v = &h.SSHOptions
   102  	case C.TypeShadowTLS:
   103  		v = &h.ShadowTLSOptions
   104  	case C.TypeShadowsocksR:
   105  		v = &h.ShadowsocksROptions
   106  	case C.TypeVLESS:
   107  		v = &h.VLESSOptions
   108  	case C.TypeSelector:
   109  		v = &h.SelectorOptions
   110  	case C.TypeURLTest:
   111  		v = &h.URLTestOptions
   112  	default:
   113  		return E.New("unknown outbound type: ", h.Type)
   114  	}
   115  	err = UnmarshallExcluded(bytes, (*_Outbound)(h), v)
   116  	if err != nil {
   117  		return E.Cause(err, "outbound options")
   118  	}
   119  	return nil
   120  }
   121  
   122  type DialerOptions struct {
   123  	Detour             string         `json:"detour,omitempty"`
   124  	BindInterface      string         `json:"bind_interface,omitempty"`
   125  	Inet4BindAddress   *ListenAddress `json:"inet4_bind_address,omitempty"`
   126  	Inet6BindAddress   *ListenAddress `json:"inet6_bind_address,omitempty"`
   127  	ProtectPath        string         `json:"protect_path,omitempty"`
   128  	RoutingMark        int            `json:"routing_mark,omitempty"`
   129  	ReuseAddr          bool           `json:"reuse_addr,omitempty"`
   130  	ConnectTimeout     Duration       `json:"connect_timeout,omitempty"`
   131  	TCPFastOpen        bool           `json:"tcp_fast_open,omitempty"`
   132  	UDPFragment        *bool          `json:"udp_fragment,omitempty"`
   133  	UDPFragmentDefault bool           `json:"-"`
   134  	DomainStrategy     DomainStrategy `json:"domain_strategy,omitempty"`
   135  	FallbackDelay      Duration       `json:"fallback_delay,omitempty"`
   136  }
   137  
   138  type ServerOptions struct {
   139  	Server     string `json:"server"`
   140  	ServerPort uint16 `json:"server_port"`
   141  }
   142  
   143  func (o ServerOptions) Build() M.Socksaddr {
   144  	return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
   145  }
   146  
   147  type MultiplexOptions struct {
   148  	Enabled        bool   `json:"enabled,omitempty"`
   149  	Protocol       string `json:"protocol,omitempty"`
   150  	MaxConnections int    `json:"max_connections,omitempty"`
   151  	MinStreams     int    `json:"min_streams,omitempty"`
   152  	MaxStreams     int    `json:"max_streams,omitempty"`
   153  }