-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
311 lines (299 loc) · 7.97 KB
/
node.go
File metadata and controls
311 lines (299 loc) · 7.97 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package xray
import (
"context"
"errors"
"fmt"
"github.com/InazumaV/Ratte-Core-Xray/common"
"github.com/InazumaV/Ratte-Core-Xray/limiter"
e2 "github.com/InazumaV/Ratte-Interface/common/errors"
"github.com/InazumaV/Ratte-Interface/core"
"github.com/goccy/go-json"
mapS "github.com/mitchellh/mapstructure"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/serial"
xc "github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/inbound"
"github.com/xtls/xray-core/features/outbound"
coreConf "github.com/xtls/xray-core/infra/conf"
"strings"
)
func (c *Xray) getInboundConfig(
name string,
n *core.NodeInfo,
exp *ExpendNodeOptions,
tls *core.TlsOptions,
) (ind *xc.InboundHandlerConfig, err error) {
in := &coreConf.InboundDetourConfig{}
if len(exp.RawInbound) > 0 {
err = json.Unmarshal(exp.RawInbound, in)
if err != nil {
return nil, err
}
}
var netProtocol string // network protocol
var port uint32
enableTls := false
switch n.Type {
case "vmess":
netProtocol = n.VMess.Network
err = parseV2rayInboundConfig(n, in)
case "vless":
netProtocol = n.VLess.Network
err = parseV2rayInboundConfig(n, in)
case "trojan":
netProtocol = "tcp"
enableTls = true
err = parseTrojanInboundConfig(in)
case "shadowsocks":
netProtocol = "tcp"
err = parseShadowsocksInboundConfig(n, in)
default:
return nil, fmt.Errorf("unsupported node type: %s", n.Type)
}
if err != nil {
return nil, err
}
port = uint32(n.Port)
if port == 0 {
return nil, fmt.Errorf("invalid port: %d", port)
}
// Set network protocol
// Set server port
in.PortList = &coreConf.PortList{
Range: []coreConf.PortRange{
{
From: port,
To: port,
}},
}
// Set Listen IP address
ipAddress := net.ParseAddress(exp.SendIp)
in.ListenOn = &coreConf.Address{Address: ipAddress}
// Set SniffingConfig
if in.SniffingConfig == nil {
sniffingConfig := &coreConf.SniffingConfig{
Enabled: true,
DestOverride: &coreConf.StringList{"http", "tls"},
}
in.SniffingConfig = sniffingConfig
}
switch netProtocol {
case "tcp":
if in.StreamSetting.TCPSettings == nil {
tcpSetting := &coreConf.TCPConfig{
AcceptProxyProtocol: n.ProxyProtocol,
} //Enable proxy protocol
in.StreamSetting.TCPSettings = tcpSetting
} else {
in.StreamSetting.TCPSettings.AcceptProxyProtocol = n.ProxyProtocol
}
case "ws":
if in.StreamSetting.WSSettings == nil {
in.StreamSetting.WSSettings = &coreConf.WebSocketConfig{
AcceptProxyProtocol: n.ProxyProtocol,
} //Enable proxy protocol
} else {
in.StreamSetting.WSSettings.AcceptProxyProtocol = n.ProxyProtocol
}
default:
socketConfig := &coreConf.SocketConfig{
AcceptProxyProtocol: n.ProxyProtocol,
TFO: n.ProxyProtocol,
} //Enable proxy protocol
in.StreamSetting.SocketSettings = socketConfig
}
if enableTls {
if tls.CertPath == "" || tls.KeyPath == "" {
return nil, errors.New("cert or key path is not vail")
}
in.StreamSetting.Security = "tls"
if in.StreamSetting.TLSSettings == nil {
tlss := in.StreamSetting.TLSSettings
tlss.Certs = append(tlss.Certs, &coreConf.TLSCertConfig{
CertFile: tls.CertPath,
KeyFile: tls.KeyPath,
OcspStapling: 3600,
})
} else {
in.StreamSetting.TLSSettings = &coreConf.TLSConfig{
Certs: []*coreConf.TLSCertConfig{
{
CertFile: tls.CertPath,
KeyFile: tls.KeyPath,
OcspStapling: 3600,
},
},
}
}
}
in.Tag = name
return in.Build()
}
func (c *Xray) getOutboundConfig(name string, exp *ExpendNodeOptions) (outH *xc.OutboundHandlerConfig, err error) {
var rawC json.RawMessage
oc := &coreConf.OutboundDetourConfig{
Protocol: "freedom",
}
if len(exp.RawOutbound) > 0 {
err = json.Unmarshal(rawC, &oc)
}
oc.Tag = name
return oc.Build()
}
type ruleObj struct {
DomainMatcher string `json:"domainMatcher"`
Type string `json:"type"`
Domain []string `json:"domain"`
Ip []string `json:"ip"`
Port any `json:"port"`
SourcePort string `json:"sourcePort"`
Network string `json:"network"`
Source []string `json:"source"`
User []string `json:"user"`
InboundTag []string `json:"inboundTag"`
Protocol []string `json:"protocol"`
Attrs struct {
Method string `json:":method"`
} `json:"attrs"`
OutboundTag string `json:"outboundTag"`
BalancerTag string `json:"balancerTag"`
RuleTag string `json:"ruleTag"`
}
func (c *Xray) addRulesRouting(rs []string) error {
rules := make([]json.RawMessage, 0, len(rs))
for i, r := range rs {
var temp ruleObj
rO := strings.Split(r, "!")
tag := fmt.Sprintf("block-%d", i)
if len(rO) == 1 {
temp = ruleObj{
DomainMatcher: "hybrid",
Domain: []string{rO[0]},
OutboundTag: "block",
RuleTag: tag,
}
} else {
switch rO[0] {
case "domain":
temp = ruleObj{
DomainMatcher: "hybrid",
Domain: []string{rO[1]},
OutboundTag: "block",
RuleTag: tag,
}
case "port":
temp = ruleObj{
DomainMatcher: "hybrid",
Port: rO[1],
OutboundTag: "block",
RuleTag: tag,
}
}
}
b, err := json.Marshal(temp)
if err != nil {
return err
}
rules = append(rules, b)
}
rc := &coreConf.RouterConfig{
DomainMatcher: "hybrid",
DomainStrategy: common.NewValue("AsIs"),
RuleList: rules,
}
tc, err := rc.Build()
if err != nil {
return fmt.Errorf("failed to build router config: %v", err)
}
return c.ru.AddRule(serial.ToTypedMessage(tc), true)
}
func (c *Xray) delRulesRouting(r []string) error {
for i := range r {
tag := fmt.Sprintf("block-%d", i)
if err := c.ru.RemoveRule(tag); err != nil {
return fmt.Errorf("remove rule %s error: %v", tag, err)
}
}
return nil
}
type ExpendNodeOptions struct {
SendIp string `mapstructure:"SendIp"`
RawOutbound json.RawMessage `mapstructure:"RawOutbound"`
RawInbound json.RawMessage `mapstructure:"RawInbound"`
}
func (c *Xray) AddNode(p *core.AddNodeParams) (err error) {
defer func() {
if err != nil {
err = e2.NewStringFromErr(err)
}
}()
expO := &ExpendNodeOptions{}
err = mapS.Decode(p.NodeInfo.Options, expO)
if err != nil {
return fmt.Errorf("unmarshal expend node options failed: %s", err)
}
in, err := c.getInboundConfig(p.Name, p.NodeInfo, expO, &p.TlsOptions)
if err != nil {
return fmt.Errorf("get inbound config error: %s", err)
}
out, err := c.getOutboundConfig(common.FormatDefaultOutboundName(p.Name), expO)
if err != nil {
return fmt.Errorf("get outbound config error: %s", err)
}
limit := p.NodeInfo.Limit
_ = c.dispatcher.AddLimiter(
p.Name,
limiter.NewLimiter(
limit.IPLimit,
limit.SpeedLimit,
p.NodeInfo.Rules),
)
rawInH, err := xc.CreateObject(c.Server, in)
if err != nil {
return err
}
inH, ok := rawInH.(inbound.Handler)
if !ok {
return fmt.Errorf("not an InboundHandler: %s", err)
}
err = c.ihm.AddHandler(context.Background(), inH)
if err != nil {
return fmt.Errorf("add inbound handler error: %s", err)
}
rawOutH, err := xc.CreateObject(c.Server, out)
if err != nil {
return err
}
handler, ok := rawOutH.(outbound.Handler)
if !ok {
return fmt.Errorf("not an OutboundHandler: %s", err)
}
if err = c.ohm.AddHandler(context.Background(), handler); err != nil {
return fmt.Errorf("add outbound handler error: %s", err)
}
c.nodes.Set(p.Name, p.NodeInfo)
return nil
}
func (c *Xray) DelNode(name string) (err error) {
defer func() {
if err != nil {
err = e2.NewStringFromErr(err)
}
}()
err = c.ihm.RemoveHandler(context.Background(), name)
if err != nil {
return fmt.Errorf("remove inbound %s error: %v", name, err)
}
err = c.ohm.RemoveHandler(context.Background(), common.FormatDefaultOutboundName(name))
if err != nil {
return fmt.Errorf("remove outbound %s error: %v", name, err)
}
n, _ := c.nodes.Get(name)
c.nodes.Remove(name)
_ = c.dispatcher.RemoveLimiter(name)
err = c.delRulesRouting(n.Rules)
if err != nil {
return fmt.Errorf("remove rules routing error: %v", err)
}
return nil
}