github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/option/inbound.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  )
     8  
     9  type _Inbound struct {
    10  	Type               string                    `json:"type"`
    11  	Tag                string                    `json:"tag,omitempty"`
    12  	TunOptions         TunInboundOptions         `json:"-"`
    13  	RedirectOptions    RedirectInboundOptions    `json:"-"`
    14  	TProxyOptions      TProxyInboundOptions      `json:"-"`
    15  	DirectOptions      DirectInboundOptions      `json:"-"`
    16  	SocksOptions       SocksInboundOptions       `json:"-"`
    17  	HTTPOptions        HTTPMixedInboundOptions   `json:"-"`
    18  	MixedOptions       HTTPMixedInboundOptions   `json:"-"`
    19  	ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
    20  	VMessOptions       VMessInboundOptions       `json:"-"`
    21  	TrojanOptions      TrojanInboundOptions      `json:"-"`
    22  	NaiveOptions       NaiveInboundOptions       `json:"-"`
    23  	HysteriaOptions    HysteriaInboundOptions    `json:"-"`
    24  	ShadowTLSOptions   ShadowTLSInboundOptions   `json:"-"`
    25  	VLESSOptions       VLESSInboundOptions       `json:"-"`
    26  	TUICOptions        TUICInboundOptions        `json:"-"`
    27  	Hysteria2Options   Hysteria2InboundOptions   `json:"-"`
    28  }
    29  
    30  type Inbound _Inbound
    31  
    32  func (h Inbound) MarshalJSON() ([]byte, error) {
    33  	var v any
    34  	switch h.Type {
    35  	case C.TypeTun:
    36  		v = h.TunOptions
    37  	case C.TypeRedirect:
    38  		v = h.RedirectOptions
    39  	case C.TypeTProxy:
    40  		v = h.TProxyOptions
    41  	case C.TypeDirect:
    42  		v = h.DirectOptions
    43  	case C.TypeSOCKS:
    44  		v = h.SocksOptions
    45  	case C.TypeHTTP:
    46  		v = h.HTTPOptions
    47  	case C.TypeMixed:
    48  		v = h.MixedOptions
    49  	case C.TypeShadowsocks:
    50  		v = h.ShadowsocksOptions
    51  	case C.TypeVMess:
    52  		v = h.VMessOptions
    53  	case C.TypeTrojan:
    54  		v = h.TrojanOptions
    55  	case C.TypeNaive:
    56  		v = h.NaiveOptions
    57  	case C.TypeHysteria:
    58  		v = h.HysteriaOptions
    59  	case C.TypeShadowTLS:
    60  		v = h.ShadowTLSOptions
    61  	case C.TypeVLESS:
    62  		v = h.VLESSOptions
    63  	case C.TypeTUIC:
    64  		v = h.TUICOptions
    65  	case C.TypeHysteria2:
    66  		v = h.Hysteria2Options
    67  	default:
    68  		return nil, E.New("unknown inbound type: ", h.Type)
    69  	}
    70  	return MarshallObjects((_Inbound)(h), v)
    71  }
    72  
    73  func (h *Inbound) UnmarshalJSON(bytes []byte) error {
    74  	err := json.Unmarshal(bytes, (*_Inbound)(h))
    75  	if err != nil {
    76  		return err
    77  	}
    78  	var v any
    79  	switch h.Type {
    80  	case C.TypeTun:
    81  		v = &h.TunOptions
    82  	case C.TypeRedirect:
    83  		v = &h.RedirectOptions
    84  	case C.TypeTProxy:
    85  		v = &h.TProxyOptions
    86  	case C.TypeDirect:
    87  		v = &h.DirectOptions
    88  	case C.TypeSOCKS:
    89  		v = &h.SocksOptions
    90  	case C.TypeHTTP:
    91  		v = &h.HTTPOptions
    92  	case C.TypeMixed:
    93  		v = &h.MixedOptions
    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.TypeNaive:
   101  		v = &h.NaiveOptions
   102  	case C.TypeHysteria:
   103  		v = &h.HysteriaOptions
   104  	case C.TypeShadowTLS:
   105  		v = &h.ShadowTLSOptions
   106  	case C.TypeVLESS:
   107  		v = &h.VLESSOptions
   108  	case C.TypeTUIC:
   109  		v = &h.TUICOptions
   110  	case C.TypeHysteria2:
   111  		v = &h.Hysteria2Options
   112  	default:
   113  		return E.New("unknown inbound type: ", h.Type)
   114  	}
   115  	err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
   116  	if err != nil {
   117  		return E.Cause(err, "inbound options")
   118  	}
   119  	return nil
   120  }
   121  
   122  type InboundOptions struct {
   123  	SniffEnabled             bool           `json:"sniff,omitempty"`
   124  	SniffOverrideDestination bool           `json:"sniff_override_destination,omitempty"`
   125  	SniffTimeout             Duration       `json:"sniff_timeout,omitempty"`
   126  	DomainStrategy           DomainStrategy `json:"domain_strategy,omitempty"`
   127  	DelayClose               int            `json:"delay_close"`
   128  }
   129  
   130  type ListenOptions struct {
   131  	Listen                      *ListenAddress `json:"listen,omitempty"`
   132  	ListenPort                  uint16         `json:"listen_port,omitempty"`
   133  	TCPFastOpen                 bool           `json:"tcp_fast_open,omitempty"`
   134  	TCPMultiPath                bool           `json:"tcp_multi_path,omitempty"`
   135  	UDPFragment                 *bool          `json:"udp_fragment,omitempty"`
   136  	UDPFragmentDefault          bool           `json:"-"`
   137  	UDPTimeout                  int64          `json:"udp_timeout,omitempty"`
   138  	ProxyProtocol               bool           `json:"proxy_protocol,omitempty"`
   139  	ProxyProtocolAcceptNoHeader bool           `json:"proxy_protocol_accept_no_header,omitempty"`
   140  	Detour                      string         `json:"detour,omitempty"`
   141  	InboundOptions
   142  }