github.com/sagernet/sing-box@v1.2.7/option/inbound.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  )
     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  }
    27  
    28  type Inbound _Inbound
    29  
    30  func (h Inbound) MarshalJSON() ([]byte, error) {
    31  	var v any
    32  	switch h.Type {
    33  	case C.TypeTun:
    34  		v = h.TunOptions
    35  	case C.TypeRedirect:
    36  		v = h.RedirectOptions
    37  	case C.TypeTProxy:
    38  		v = h.TProxyOptions
    39  	case C.TypeDirect:
    40  		v = h.DirectOptions
    41  	case C.TypeSocks:
    42  		v = h.SocksOptions
    43  	case C.TypeHTTP:
    44  		v = h.HTTPOptions
    45  	case C.TypeMixed:
    46  		v = h.MixedOptions
    47  	case C.TypeShadowsocks:
    48  		v = h.ShadowsocksOptions
    49  	case C.TypeVMess:
    50  		v = h.VMessOptions
    51  	case C.TypeTrojan:
    52  		v = h.TrojanOptions
    53  	case C.TypeNaive:
    54  		v = h.NaiveOptions
    55  	case C.TypeHysteria:
    56  		v = h.HysteriaOptions
    57  	case C.TypeShadowTLS:
    58  		v = h.ShadowTLSOptions
    59  	case C.TypeVLESS:
    60  		v = h.VLESSOptions
    61  	default:
    62  		return nil, E.New("unknown inbound type: ", h.Type)
    63  	}
    64  	return MarshallObjects((_Inbound)(h), v)
    65  }
    66  
    67  func (h *Inbound) UnmarshalJSON(bytes []byte) error {
    68  	err := json.Unmarshal(bytes, (*_Inbound)(h))
    69  	if err != nil {
    70  		return err
    71  	}
    72  	var v any
    73  	switch h.Type {
    74  	case C.TypeTun:
    75  		v = &h.TunOptions
    76  	case C.TypeRedirect:
    77  		v = &h.RedirectOptions
    78  	case C.TypeTProxy:
    79  		v = &h.TProxyOptions
    80  	case C.TypeDirect:
    81  		v = &h.DirectOptions
    82  	case C.TypeSocks:
    83  		v = &h.SocksOptions
    84  	case C.TypeHTTP:
    85  		v = &h.HTTPOptions
    86  	case C.TypeMixed:
    87  		v = &h.MixedOptions
    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.TypeNaive:
    95  		v = &h.NaiveOptions
    96  	case C.TypeHysteria:
    97  		v = &h.HysteriaOptions
    98  	case C.TypeShadowTLS:
    99  		v = &h.ShadowTLSOptions
   100  	case C.TypeVLESS:
   101  		v = &h.VLESSOptions
   102  	default:
   103  		return E.New("unknown inbound type: ", h.Type)
   104  	}
   105  	err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
   106  	if err != nil {
   107  		return E.Cause(err, "inbound options")
   108  	}
   109  	return nil
   110  }
   111  
   112  type InboundOptions struct {
   113  	SniffEnabled             bool           `json:"sniff,omitempty"`
   114  	SniffOverrideDestination bool           `json:"sniff_override_destination,omitempty"`
   115  	SniffTimeout             Duration       `json:"sniff_timeout,omitempty"`
   116  	DomainStrategy           DomainStrategy `json:"domain_strategy,omitempty"`
   117  }
   118  
   119  type ListenOptions struct {
   120  	Listen                      *ListenAddress `json:"listen,omitempty"`
   121  	ListenPort                  uint16         `json:"listen_port,omitempty"`
   122  	TCPFastOpen                 bool           `json:"tcp_fast_open,omitempty"`
   123  	UDPFragment                 *bool          `json:"udp_fragment,omitempty"`
   124  	UDPFragmentDefault          bool           `json:"-"`
   125  	UDPTimeout                  int64          `json:"udp_timeout,omitempty"`
   126  	ProxyProtocol               bool           `json:"proxy_protocol,omitempty"`
   127  	ProxyProtocolAcceptNoHeader bool           `json:"proxy_protocol_accept_no_header,omitempty"`
   128  	Detour                      string         `json:"detour,omitempty"`
   129  	InboundOptions
   130  }