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