forked from wyx2685/V2bX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
303 lines (278 loc) · 7.35 KB
/
node.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
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
package panel
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/InazumaV/V2bX/common/crypt"
"github.com/goccy/go-json"
)
// Security type
const (
None = 0
Tls = 1
Reality = 2
)
type NodeInfo struct {
Id int
Type string
Security int
PushInterval time.Duration
PullInterval time.Duration
RawDNS RawDNS
Rules Rules
// origin
VAllss *VAllssNode
Shadowsocks *ShadowsocksNode
Trojan *TrojanNode
Hysteria *HysteriaNode
Hysteria2 *Hysteria2Node
Common *CommonNode
}
type CommonNode struct {
Host string `json:"host"`
ServerPort int `json:"server_port"`
ServerName string `json:"server_name"`
Routes []Route `json:"routes"`
BaseConfig *BaseConfig `json:"base_config"`
}
type Route struct {
Id int `json:"id"`
Match interface{} `json:"match"`
Action string `json:"action"`
ActionValue string `json:"action_value"`
}
type BaseConfig struct {
PushInterval any `json:"push_interval"`
PullInterval any `json:"pull_interval"`
}
// VAllssNode is vmess and vless node info
type VAllssNode struct {
CommonNode
Tls int `json:"tls"`
TlsSettings TlsSettings `json:"tls_settings"`
TlsSettingsBack *TlsSettings `json:"tlsSettings"`
Network string `json:"network"`
NetworkSettings json.RawMessage `json:"network_settings"`
NetworkSettingsBack json.RawMessage `json:"networkSettings"`
ServerName string `json:"server_name"`
// vless only
Flow string `json:"flow"`
RealityConfig RealityConfig `json:"-"`
}
type TlsSettings struct {
ServerName string `json:"server_name"`
Dest string `json:"dest"`
ServerPort string `json:"server_port"`
ShortId string `json:"short_id"`
PrivateKey string `json:"private_key"`
Xver uint64 `json:"xver,string"`
}
type RealityConfig struct {
Xver uint64 `json:"Xver"`
MinClientVer string `json:"MinClientVer"`
MaxClientVer string `json:"MaxClientVer"`
MaxTimeDiff string `json:"MaxTimeDiff"`
}
type ShadowsocksNode struct {
CommonNode
Cipher string `json:"cipher"`
ServerKey string `json:"server_key"`
}
type TrojanNode struct {
CommonNode
Network string `json:"network"`
NetworkSettings json.RawMessage `json:"networkSettings"`
}
type HysteriaNode struct {
CommonNode
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
Obfs string `json:"obfs"`
}
type Hysteria2Node struct {
CommonNode
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
ObfsType string `json:"obfs"`
ObfsPassword string `json:"obfs-password"`
}
type RawDNS struct {
DNSMap map[string]map[string]interface{}
DNSJson []byte
}
type Rules struct {
Regexp []string
Protocol []string
}
func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
const path = "/api/v1/server/UniProxy/config"
r, err := c.client.
R().
SetHeader("If-None-Match", c.nodeEtag).
ForceContentType("application/json").
Get(path)
if r.StatusCode() == 304 {
return nil, nil
}
hash := sha256.Sum256(r.Body())
newBodyHash := hex.EncodeToString(hash[:])
if c.responseBodyHash == newBodyHash {
return nil, nil
}
c.responseBodyHash = newBodyHash
c.nodeEtag = r.Header().Get("ETag")
if err = c.checkResponse(r, path, err); err != nil {
return nil, err
}
if r != nil {
defer func() {
if r.RawBody() != nil {
r.RawBody().Close()
}
}()
} else {
return nil, fmt.Errorf("received nil response")
}
node = &NodeInfo{
Id: c.NodeId,
Type: c.NodeType,
RawDNS: RawDNS{
DNSMap: make(map[string]map[string]interface{}),
DNSJson: []byte(""),
},
}
// parse protocol params
var cm *CommonNode
switch c.NodeType {
case "vmess", "vless":
rsp := &VAllssNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
}
if len(rsp.NetworkSettingsBack) > 0 {
rsp.NetworkSettings = rsp.NetworkSettingsBack
rsp.NetworkSettingsBack = nil
}
if rsp.TlsSettingsBack != nil {
rsp.TlsSettings = *rsp.TlsSettingsBack
rsp.TlsSettingsBack = nil
}
cm = &rsp.CommonNode
node.VAllss = rsp
node.Security = node.VAllss.Tls
if len(rsp.NetworkSettings) > 0 {
err = json.Unmarshal(rsp.NetworkSettings, &rsp.RealityConfig)
if err != nil {
return nil, fmt.Errorf("decode reality config error: %s", err)
}
}
if node.Security == Reality {
if rsp.TlsSettings.PrivateKey == "" {
key := crypt.GenX25519Private([]byte("vless" + c.Token))
rsp.TlsSettings.PrivateKey = base64.RawURLEncoding.EncodeToString(key)
}
}
case "shadowsocks":
rsp := &ShadowsocksNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode shadowsocks params error: %s", err)
}
cm = &rsp.CommonNode
node.Shadowsocks = rsp
node.Security = None
case "trojan":
rsp := &TrojanNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode trojan params error: %s", err)
}
cm = &rsp.CommonNode
node.Trojan = rsp
node.Security = Tls
case "hysteria":
rsp := &HysteriaNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode hysteria params error: %s", err)
}
cm = &rsp.CommonNode
node.Hysteria = rsp
node.Security = Tls
case "hysteria2":
rsp := &Hysteria2Node{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode hysteria2 params error: %s", err)
}
cm = &rsp.CommonNode
node.Hysteria2 = rsp
node.Security = Tls
}
// parse rules and dns
for i := range cm.Routes {
var matchs []string
if _, ok := cm.Routes[i].Match.(string); ok {
matchs = strings.Split(cm.Routes[i].Match.(string), ",")
} else if _, ok = cm.Routes[i].Match.([]string); ok {
matchs = cm.Routes[i].Match.([]string)
} else {
temp := cm.Routes[i].Match.([]interface{})
matchs = make([]string, len(temp))
for i := range temp {
matchs[i] = temp[i].(string)
}
}
switch cm.Routes[i].Action {
case "block":
for _, v := range matchs {
if strings.HasPrefix(v, "protocol:") {
// protocol
node.Rules.Protocol = append(node.Rules.Protocol, strings.TrimPrefix(v, "protocol:"))
} else {
// domain
node.Rules.Regexp = append(node.Rules.Regexp, strings.TrimPrefix(v, "regexp:"))
}
}
case "dns":
var domains []string
domains = append(domains, matchs...)
if matchs[0] != "main" {
node.RawDNS.DNSMap[strconv.Itoa(i)] = map[string]interface{}{
"address": cm.Routes[i].ActionValue,
"domains": domains,
}
} else {
dns := []byte(strings.Join(matchs[1:], ""))
node.RawDNS.DNSJson = dns
}
}
}
// set interval
node.PushInterval = intervalToTime(cm.BaseConfig.PushInterval)
node.PullInterval = intervalToTime(cm.BaseConfig.PullInterval)
node.Common = cm
// clear
cm.Routes = nil
cm.BaseConfig = nil
return node, nil
}
func intervalToTime(i interface{}) time.Duration {
switch reflect.TypeOf(i).Kind() {
case reflect.Int:
return time.Duration(i.(int)) * time.Second
case reflect.String:
i, _ := strconv.Atoi(i.(string))
return time.Duration(i) * time.Second
case reflect.Float64:
return time.Duration(i.(float64)) * time.Second
default:
return time.Duration(reflect.ValueOf(i).Int()) * time.Second
}
}