forked from DrStrangeVN/Surge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetX.js
362 lines (326 loc) · 10.7 KB
/
NetX.js
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
const NAME = 'network-info';
const $ = new Env(NAME);
let arg;
if (typeof $argument != 'undefined') {
arg = Object.fromEntries($argument.split('&').map(item => item.split('=')));
} else {
arg = {};
}
$.log(`Input $argument: ${$.toStr(arg)}`);
// Merge arguments with stored settings
arg = { ...arg, ...$.getjson(NAME, {}) };
$.log(`Parameters after reading from persistent storage: ${$.toStr(arg)}`);
if (typeof $environment !== 'undefined' && $.lodash_get($environment, 'executor') === 'event-network') {
$.log(`QX event script cannot carry parameters, fixing runtime environment`);
$.lodash_set(arg, 'TYPE', 'EVENT');
}
if (!isInteraction() && !isRequest() && !isTile() && !isPanel()) {
$.log(`No parameters, fixing runtime environment for non-interactive, non-request, non-tile, and non-panel cases`);
$.lodash_set(arg, 'TYPE', 'EVENT');
}
if (isRequest()) {
arg = { ...arg, ...parseQueryString($request.url) };
$.log(`Parameters after reading from request: ${$.toStr(arg)}`);
}
const keya = 'spe';
const keyb = 'ge';
const keyc = 'pin';
const keyd = 'gan';
const keye = 'pi';
const keyf = 'ob';
const keyg = 'qi';
const keyh = 'xin';
const bay = 'edtest';
let result = {};
let proxy_policy = '';
let title = '';
let content = '';
(async () => {
if ($.lodash_get(arg, 'TYPE') === 'EVENT') {
const eventDelay = parseFloat($.lodash_get(arg, 'EVENT_DELAY') || 3);
$.log(`Network change, waiting ${eventDelay} seconds before querying`);
if (eventDelay) {
await $.wait(1000 * eventDelay);
}
}
if (isTile()) {
await notify('Network Info', 'Tile', 'Starting query');
}
let SSID = '';
let LAN = '';
let LAN_IPv4 = '';
let LAN_IPv6 = '';
if (typeof $network !== 'undefined') {
$.log($.toStr($network));
const v4 = $.lodash_get($network, 'v4.primaryAddress');
const v6 = $.lodash_get($network, 'v6.primaryAddress');
if ($.lodash_get(arg, 'SSID') == 1) {
SSID = $.lodash_get($network, 'wifi.ssid');
}
if (v4 && $.lodash_get(arg, 'LAN') == 1) {
LAN_IPv4 = v4;
}
if (v6 && $.lodash_get(arg, 'LAN') == 1 && $.lodash_get(arg, 'IPv6') == 1) {
LAN_IPv6 = v6;
}
} else if (typeof $config !== 'undefined') {
try {
let conf = $config.getConfig();
$.log(conf);
conf = JSON.parse(conf);
if ($.lodash_get(arg, 'SSID') == 1) {
SSID = $.lodash_get(conf, 'ssid');
}
} catch (e) {}
} else if (typeof $environment !== 'undefined') {
try {
$.log($.toStr($environment));
const version = $.lodash_get($environment, 'version');
const os = version?.split(' ')?.[0];
if (os !== 'macOS' && $.lodash_get(arg, 'SSID') == 1) {
SSID = $.lodash_get($environment, 'ssid');
} else if (os === 'macOS' && $.lodash_get(arg, 'LAN') == 1) {
LAN_IPv4 = $.lodash_get($environment, 'ssid');
}
} catch (e) {}
}
if (LAN_IPv4 || LAN_IPv6) {
LAN = ['LAN:', LAN_IPv4, maskIP(LAN_IPv6)].filter(i => i).join(' ');
}
if (LAN) {
LAN = `${LAN}\n\n`;
}
if (SSID) {
SSID = `SSID: ${SSID}\n\n`;
} else {
SSID = '';
}
let { PROXIES = [] } = await getProxies();
let [
{ CN_IP = '', CN_INFO = '', CN_POLICY = '' } = {},
{ PROXY_IP = '', PROXY_INFO = '', PROXY_PRIVACY = '', PROXY_POLICY = '', ENTRANCE_IP = '' } = {},
{ CN_IPv6 = '' } = {},
{ PROXY_IPv6 = '' } = {},
] = await Promise.all(
$.lodash_get(arg, 'IPv6') == 1
? [getDirectRequestInfo({ PROXIES }), getProxyRequestInfo({ PROXIES }), getDirectInfoIPv6(), getProxyInfoIPv6()]
: [getDirectRequestInfo({ PROXIES }), getProxyRequestInfo({ PROXIES })]
);
let continueFlag = true;
if ($.lodash_get(arg, 'TYPE') === 'EVENT') {
const lastNetworkInfoEvent = $.getjson('lastNetworkInfoEvent');
if (
CN_IP !== $.lodash_get(lastNetworkInfoEvent, 'CN_IP') ||
CN_IPv6 !== $.lodash_get(lastNetworkInfoEvent, 'CN_IPv6') ||
PROXY_IP !== $.lodash_get(lastNetworkInfoEvent, 'PROXY_IP') ||
PROXY_IPv6 !== $.lodash_get(lastNetworkInfoEvent, 'PROXY_IPv6')
) {
$.setjson({ CN_IP, PROXY_IP, CN_IPv6, PROXY_IPv6 }, 'lastNetworkInfoEvent');
} else {
$.log('No change in network information, stopping');
continueFlag = false;
}
}
if (continueFlag) {
if ($.lodash_get(arg, 'PRIVACY') == '1' && PROXY_PRIVACY) {
PROXY_PRIVACY = `\n${PROXY_PRIVACY}`;
}
let ENTRANCE = '';
if (ENTRANCE_IP) {
const { IP: resolvedIP } = await resolveDomain(ENTRANCE_IP);
if (resolvedIP) {
$.log(`Entrance domain resolved: ${ENTRANCE_IP} ➟ ${resolvedIP}`);
ENTRANCE_IP = resolvedIP;
}
}
if (ENTRANCE_IP && ENTRANCE_IP !== PROXY_IP) {
const entranceDelay = parseFloat($.lodash_get(arg, 'ENTRANCE_DELAY') || 0);
$.log(`Entrance: ${ENTRANCE_IP} differs from landing IP: ${PROXY_IP}, waiting ${entranceDelay} seconds before querying entrance`);
if (entranceDelay) {
await $.wait(1000 * entranceDelay);
}
let [{ CN_INFO: ENTRANCE_INFO1 = '', isCN = false } = {}, { PROXY_INFO: ENTRANCE_INFO2 = '' } = {}] = await Promise.all([
getDirectInfo(ENTRANCE_IP, $.lodash_get(arg, 'DOMESTIC_IPv4')),
getProxyInfo(ENTRANCE_IP, $.lodash_get(arg, 'LANDING_IPv4')),
]);
if (ENTRANCE_INFO1 && isCN) {
ENTRANCE = `Entrance: ${maskIP(ENTRANCE_IP) || '-'}\n${maskAddr(ENTRANCE_INFO1)}`;
}
if (ENTRANCE_INFO2) {
if (ENTRANCE) {
ENTRANCE = `${ENTRANCE.replace(/^(.*?):/gim, '$1¹:')}\n${maskAddr(
ENTRANCE_INFO2.replace(/^(.*?):/gim, '$1²:')
)}`;
} else {
ENTRANCE = `Entrance: ${maskIP(ENTRANCE_IP) || '-'}\n${maskAddr(ENTRANCE_INFO2)}`;
}
}
}
if (ENTRANCE) {
ENTRANCE = `${ENTRANCE}\n\n`;
}
if (CN_IPv6 && isIPv6(CN_IPv6) && $.lodash_get(arg, 'IPv6') == 1) {
CN_IPv6 = `\n${maskIP(CN_IPv6)}`;
} else {
CN_IPv6 = '';
}
if (PROXY_IPv6 && isIPv6(PROXY_IPv6) && $.lodash_get(arg, 'IPv6') == 1) {
PROXY_IPv6 = `\n${maskIP(PROXY_IPv6)}`;
} else {
PROXY_IPv6 = '';
}
if ($.isSurge() || $.isStash()) {
if (CN_POLICY === 'DIRECT') {
CN_POLICY = ``;
} else {
CN_POLICY = `Policy: ${maskAddr(CN_POLICY) || '-'}\n`;
}
}
if (CN_INFO) {
CN_INFO = `\n${CN_INFO}`;
}
const policy_prefix = $.isQuanX() || $.isLoon() ? 'Node: ' : 'Proxy Policy: ';
if (PROXY_POLICY) {
proxy_policy = `${policy_prefix}${maskAddr(PROXY_POLICY) || '-'}\n`;
}
title = 'Network Information';
content = `${SSID}${LAN}${ENTRANCE}Direct Connection: ${maskIP(CN_IP) || '-'}${CN_IPv6}${CN_INFO}\n${CN_POLICY}${proxy_policy}Proxy: ${maskIP(PROXY_IP) || '-'}${PROXY_IPv6}${PROXY_INFO}${PROXY_PRIVACY}`;
result = { title, content };
await notify(title, '', content);
}
$.log($.toStr(result));
$.done(result);
})()
.catch((e) => {
$.logErr(e);
$.done();
});
function isTile() {
if (typeof $environment != 'undefined') {
return $.lodash_get($environment, 'event') === 'widget';
}
return false;
}
function isPanel() {
if (typeof $environment != 'undefined') {
return $.lodash_get($environment, 'event') === 'panel';
}
return false;
}
function isRequest() {
if (typeof $request != 'undefined') {
return true;
}
return false;
}
function isInteraction() {
if (typeof $environment != 'undefined') {
return $.lodash_get($environment, 'executor') === 'interact';
}
return false;
}
function notify(subtitle = '', message = '', content = '') {
if ($.isQuanX()) {
$notify('Network Information', subtitle, `${message}\n${content}`);
} else if ($.isSurge() || $.isStash()) {
$notification.post('Network Information', subtitle, `${message}\n${content}`);
} else if ($.isLoon()) {
$notification.post('Network Information', subtitle, `${message}\n${content}`);
}
}
async function getProxies() {
const proxies = [];
if ($.isSurge() || $.isStash()) {
proxies.push({ server: $network.vpn.server, password: $network.vpn.password });
}
if ($.isLoon()) {
const conf = await $httpClient.get('http://127.0.0.1:6000/proxy/policy', { timeout: 5 });
const proxyList = JSON.parse(conf.body);
proxyList.forEach((proxy) => {
proxies.push(proxy);
});
}
return { PROXIES: proxies };
}
function parseQueryString(url) {
const queryString = url.split('?')[1];
if (!queryString) {
return {};
}
return Object.fromEntries(
queryString.split('&').map((item) => item.split('='))
);
}
function maskIP(ip) {
if (!ip) return '';
return ip.replace(/(\d+\.\d+\.\d+)\.\d+/, '$1.***');
}
function maskAddr(addr) {
if (!addr) return '';
return addr.replace(/(.{3})(.*)(.{3})/, '$1***$3');
}
function isIPv6(ip) {
return ip.includes(':');
}
async function getDirectRequestInfo({ PROXIES }) {
const direct = { CN_IP: '', CN_INFO: '', CN_POLICY: '' };
const response = await $httpClient.get('http://ipinfo.io/json');
const { ip, city, region, country, org } = JSON.parse(response.body);
direct.CN_IP = ip;
direct.CN_INFO = `${city}, ${region}, ${country}\n${org}`;
return direct;
}
async function getProxyRequestInfo({ PROXIES }) {
const proxy = { PROXY_IP: '', PROXY_INFO: '', PROXY_PRIVACY: '', PROXY_POLICY: '', ENTRANCE_IP: '' };
const response = await $httpClient.get('http://ipinfo.io/json');
const { ip, city, region, country, org } = JSON.parse(response.body);
proxy.PROXY_IP = ip;
proxy.PROXY_INFO = `${city}, ${region}, ${country}\n${org}`;
proxy.PROXY_POLICY = PROXIES[0].server;
proxy.PROXY_PRIVACY = 'This is proxy information';
return proxy;
}
async function getDirectInfoIPv6() {
const directIPv6 = { CN_IPv6: '' };
const response = await $httpClient.get('http://ipv6.ip6.me/');
const match = response.body.match(/client ip: ([\da-f:]+)/i);
if (match) {
directIPv6.CN_IPv6 = match[1];
}
return directIPv6;
}
async function getProxyInfoIPv6() {
const proxyIPv6 = { PROXY_IPv6: '' };
const response = await $httpClient.get('http://ipv6.ip6.me/');
const match = response.body.match(/client ip: ([\da-f:]+)/i);
if (match) {
proxyIPv6.PROXY_IPv6 = match[1];
}
return proxyIPv6;
}
async function resolveDomain(domain) {
let resolvedIP = '';
try {
const response = await $httpClient.get(`http://dns.google/resolve?name=${domain}`);
const { Answer } = JSON.parse(response.body);
resolvedIP = Answer?.[0]?.data || '';
} catch (e) {
$.logErr(e);
}
return { IP: resolvedIP };
}
async function getDirectInfo(ip, domestic) {
const direct = { CN_INFO: '', isCN: false };
const response = await $httpClient.get(`http://ipinfo.io/${ip}/json`);
const { city, region, country, org } = JSON.parse(response.body);
direct.CN_INFO = `${city}, ${region}, ${country}\n${org}`;
direct.isCN = country === 'CN';
return direct;
}
async function getProxyInfo(ip, landing) {
const proxy = { PROXY_INFO: '' };
const response = await $httpClient.get(`http://ipinfo.io/${ip}/json`);
const { city, region, country, org } = JSON.parse(response.body);
proxy.PROXY_INFO = `${city}, ${region}, ${country}\n${org}`;
return proxy;
}