github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/option/outbound.go (about)

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