github.com/sagernet/sing-box@v1.9.0-rc.20/option/inbound.go (about) 1 package option 2 3 import ( 4 "time" 5 6 C "github.com/sagernet/sing-box/constant" 7 E "github.com/sagernet/sing/common/exceptions" 8 "github.com/sagernet/sing/common/json" 9 ) 10 11 type _Inbound struct { 12 Type string `json:"type"` 13 Tag string `json:"tag,omitempty"` 14 TunOptions TunInboundOptions `json:"-"` 15 RedirectOptions RedirectInboundOptions `json:"-"` 16 TProxyOptions TProxyInboundOptions `json:"-"` 17 DirectOptions DirectInboundOptions `json:"-"` 18 SocksOptions SocksInboundOptions `json:"-"` 19 HTTPOptions HTTPMixedInboundOptions `json:"-"` 20 MixedOptions HTTPMixedInboundOptions `json:"-"` 21 ShadowsocksOptions ShadowsocksInboundOptions `json:"-"` 22 VMessOptions VMessInboundOptions `json:"-"` 23 TrojanOptions TrojanInboundOptions `json:"-"` 24 NaiveOptions NaiveInboundOptions `json:"-"` 25 HysteriaOptions HysteriaInboundOptions `json:"-"` 26 ShadowTLSOptions ShadowTLSInboundOptions `json:"-"` 27 VLESSOptions VLESSInboundOptions `json:"-"` 28 TUICOptions TUICInboundOptions `json:"-"` 29 Hysteria2Options Hysteria2InboundOptions `json:"-"` 30 } 31 32 type Inbound _Inbound 33 34 func (h *Inbound) RawOptions() (any, error) { 35 var rawOptionsPtr any 36 switch h.Type { 37 case C.TypeTun: 38 rawOptionsPtr = &h.TunOptions 39 case C.TypeRedirect: 40 rawOptionsPtr = &h.RedirectOptions 41 case C.TypeTProxy: 42 rawOptionsPtr = &h.TProxyOptions 43 case C.TypeDirect: 44 rawOptionsPtr = &h.DirectOptions 45 case C.TypeSOCKS: 46 rawOptionsPtr = &h.SocksOptions 47 case C.TypeHTTP: 48 rawOptionsPtr = &h.HTTPOptions 49 case C.TypeMixed: 50 rawOptionsPtr = &h.MixedOptions 51 case C.TypeShadowsocks: 52 rawOptionsPtr = &h.ShadowsocksOptions 53 case C.TypeVMess: 54 rawOptionsPtr = &h.VMessOptions 55 case C.TypeTrojan: 56 rawOptionsPtr = &h.TrojanOptions 57 case C.TypeNaive: 58 rawOptionsPtr = &h.NaiveOptions 59 case C.TypeHysteria: 60 rawOptionsPtr = &h.HysteriaOptions 61 case C.TypeShadowTLS: 62 rawOptionsPtr = &h.ShadowTLSOptions 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 "": 70 return nil, E.New("missing inbound type") 71 default: 72 return nil, E.New("unknown inbound type: ", h.Type) 73 } 74 return rawOptionsPtr, nil 75 } 76 77 func (h Inbound) MarshalJSON() ([]byte, error) { 78 rawOptions, err := h.RawOptions() 79 if err != nil { 80 return nil, err 81 } 82 return MarshallObjects((_Inbound)(h), rawOptions) 83 } 84 85 func (h *Inbound) UnmarshalJSON(bytes []byte) error { 86 err := json.Unmarshal(bytes, (*_Inbound)(h)) 87 if err != nil { 88 return err 89 } 90 rawOptions, err := h.RawOptions() 91 if err != nil { 92 return err 93 } 94 err = UnmarshallExcluded(bytes, (*_Inbound)(h), rawOptions) 95 if err != nil { 96 return err 97 } 98 return nil 99 } 100 101 type InboundOptions struct { 102 SniffEnabled bool `json:"sniff,omitempty"` 103 SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"` 104 SniffTimeout Duration `json:"sniff_timeout,omitempty"` 105 DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"` 106 UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"` 107 } 108 109 type ListenOptions struct { 110 Listen *ListenAddress `json:"listen,omitempty"` 111 ListenPort uint16 `json:"listen_port,omitempty"` 112 TCPFastOpen bool `json:"tcp_fast_open,omitempty"` 113 TCPMultiPath bool `json:"tcp_multi_path,omitempty"` 114 UDPFragment *bool `json:"udp_fragment,omitempty"` 115 UDPFragmentDefault bool `json:"-"` 116 UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"` 117 ProxyProtocol bool `json:"proxy_protocol,omitempty"` 118 ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"` 119 Detour string `json:"detour,omitempty"` 120 InboundOptions 121 } 122 123 type UDPTimeoutCompat Duration 124 125 func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) { 126 return json.Marshal((time.Duration)(c).String()) 127 } 128 129 func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error { 130 var valueNumber int64 131 err := json.Unmarshal(data, &valueNumber) 132 if err == nil { 133 *c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber)) 134 return nil 135 } 136 return json.Unmarshal(data, (*Duration)(c)) 137 } 138 139 type ListenOptionsWrapper interface { 140 TakeListenOptions() ListenOptions 141 ReplaceListenOptions(options ListenOptions) 142 } 143 144 func (o *ListenOptions) TakeListenOptions() ListenOptions { 145 return *o 146 } 147 148 func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) { 149 *o = options 150 }