block-proxy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/.eslintignore +3 -0
  2. package/AD_BLOCK.md +38 -0
  3. package/Dockerfile +51 -0
  4. package/LICENSE +21 -0
  5. package/README.md +182 -0
  6. package/bin/start.js +45 -0
  7. package/cert/rootCA.crt +20 -0
  8. package/cert/rootCA.key +27 -0
  9. package/config.json +234 -0
  10. package/craco.config.js +52 -0
  11. package/hack-of-anyproxy/lib/requestHandler.js +1028 -0
  12. package/package.json +54 -0
  13. package/proxy/attacker.js +135 -0
  14. package/proxy/domain.js +26 -0
  15. package/proxy/fs.js +46 -0
  16. package/proxy/http.js +224 -0
  17. package/proxy/mitm/persistentStore.js +34 -0
  18. package/proxy/mitm/persistentStore.json +3 -0
  19. package/proxy/mitm/rule.js +116 -0
  20. package/proxy/mitm/uaFilter.js +47 -0
  21. package/proxy/mitm/ydcd/ydcd.js +34 -0
  22. package/proxy/mitm/youtube/youtube.response.js +39 -0
  23. package/proxy/monitor.js +283 -0
  24. package/proxy/proxy.js +1488 -0
  25. package/proxy/scan.js +120 -0
  26. package/proxy/start.js +7 -0
  27. package/proxy/wanip.js +76 -0
  28. package/public/favicon.ico +0 -0
  29. package/public/index.html +43 -0
  30. package/public/iphone-proxy-setting.jpg +0 -0
  31. package/public/logo192.png +0 -0
  32. package/public/logo512.png +0 -0
  33. package/public/manifest.json +25 -0
  34. package/public/proxy.jpg +0 -0
  35. package/public/robots.txt +3 -0
  36. package/server/express.js +232 -0
  37. package/server/start.js +24 -0
  38. package/server/util.js +166 -0
  39. package/socks5/server.js +321 -0
  40. package/socks5/start.js +7 -0
  41. package/socks5/test_tls_reuse.js +40 -0
  42. package/src/App.css +505 -0
  43. package/src/App.js +759 -0
  44. package/src/App.test.js +8 -0
  45. package/src/index.css +13 -0
  46. package/src/index.js +17 -0
  47. package/src/logo.svg +1 -0
  48. package/src/reportWebVitals.js +13 -0
  49. package/src/setupTests.js +5 -0
@@ -0,0 +1,283 @@
1
+ // /proxy/monitor.js
2
+ const os = require('os');
3
+ const { exec } = require('child_process');
4
+ const fs = require('fs').promises;
5
+
6
+ const isLinux = os.platform() === 'linux';
7
+ const isMacOS = os.platform() === 'darwin';
8
+
9
+ const guideLine = [
10
+ "\n\n\n",
11
+ "操作:",
12
+ "1. <a href='/enable_express'>启用管理后台</a>",
13
+ "2. <a href='/disable_express'>关闭管理后台</a>",
14
+ "3. <a href='/enable_socks5'>启用Socks5代理</a>",
15
+ "4. <a href='/disable_socks5'>关闭Socks5代理</a>",
16
+ "5. <a href='/enable_webinterface'>启用webinterface</a>",
17
+ "6. <a href='/disable_webinterface'>关闭webinterface</a>",
18
+ "7. <a href='/restart_docker'>重启 Docker</a>"
19
+ ].join("\n");
20
+
21
+ function promisifyExec(cmd) {
22
+ return new Promise((resolve, reject) => {
23
+ exec(cmd, (error, stdout, stderr) => {
24
+ if (error) {
25
+ reject(error);
26
+ } else {
27
+ resolve(stdout.trim());
28
+ }
29
+ });
30
+ });
31
+ }
32
+
33
+ async function getSystemMonitorInfo() {
34
+ if (isLinux) {
35
+ let output = '';
36
+
37
+ // === 1. 主机名 & 系统信息 ===
38
+ const hostname = os.hostname();
39
+ const platform = os.type();
40
+ const machine = os.machine();
41
+ const release = os.release();
42
+ const osVersion = os.version();
43
+ output += `主机名:${hostname}\n`;
44
+ output += `系统架构:${machine}\n`;
45
+ output += `系统名称和类型:${platform} ${release}\n`;
46
+ output += `系统版本:${osVersion}\n\n`;
47
+
48
+ // === 2. CPU ===
49
+ const cpuCores = os.cpus().length;
50
+ let cpuUsage = 0;
51
+
52
+ const readCPUStat = async () => {
53
+ const stat = await promisifyExec('cat /proc/stat | grep "^cpu "');
54
+ const parts = stat.split(/\s+/).slice(1);
55
+ const total = parts.reduce((a, b) => a + parseInt(b || '0', 10), 0);
56
+ const idle = parseInt(parts[3] || '0', 10);
57
+ return { total, idle };
58
+ };
59
+
60
+ const start = await readCPUStat();
61
+ await new Promise(r => setTimeout(r, 500));
62
+ const end = await readCPUStat();
63
+ cpuUsage = Math.min(100, Math.max(0,
64
+ (1 - (end.idle - start.idle) / (end.total - start.total)) * 100
65
+ ));
66
+ output += `CPU 核数:${cpuCores},使用占比:${Math.round(cpuUsage)}%\n`;
67
+
68
+ // === CPU 负载 ===
69
+ const [load1, load5, load15] = os.loadavg();
70
+ output += `CPU 负载:${load1.toFixed(2)}, ${load5.toFixed(2)}, ${load15.toFixed(2)}\n`;
71
+
72
+ // === CPU 温度 ===
73
+ let cpuTemp = 'N/A';
74
+ const tempPaths = [
75
+ '/sys/class/thermal/thermal_zone0/temp',
76
+ '/sys/class/hwmon/hwmon0/temp1_input',
77
+ '/sys/class/hwmon/hwmon1/temp1_input'
78
+ ];
79
+ for (const p of tempPaths) {
80
+ try {
81
+ const tempStr = await fs.readFile(p, 'utf8');
82
+ const tempRaw = parseInt(tempStr.trim(), 10);
83
+ if (!isNaN(tempRaw)) {
84
+ const celsius = Math.round(tempRaw / 1000);
85
+ if (celsius > 0 && celsius < 120) {
86
+ cpuTemp = `${celsius}°C`;
87
+ break;
88
+ }
89
+ }
90
+ } catch {}
91
+ }
92
+ output += `CPU 温度:${cpuTemp}\n`;
93
+
94
+ // === 3. Memory ===
95
+ const totalMem = os.totalmem();
96
+ const freeMem = os.freemem();
97
+ const usedMem = totalMem - freeMem;
98
+ const memTotalMB = Math.round(totalMem / 1024 / 1024);
99
+ const memUsedMB = Math.round(usedMem / 1024 / 1024);
100
+ const memPercent = Math.round((usedMem / totalMem) * 100);
101
+ output += `内存总数:${memTotalMB} MB,使用数量:${memUsedMB} MB,使用占比:${memPercent}%\n`;
102
+
103
+ // === 4. Disk ===
104
+ let diskUsedGB = 'N/A', diskTotalGB = 'N/A', diskPerc = 'N/A';
105
+ try {
106
+ const dfLine = await promisifyExec(`df / | awk 'NR==2 {print $2,$3,$5}'`);
107
+ const [totalKB, usedKB, percStr] = dfLine.split(/\s+/);
108
+ if (totalKB && usedKB && percStr) {
109
+ diskUsedGB = Math.round(parseInt(usedKB) / 1024 / 1024);
110
+ diskTotalGB = Math.round(parseInt(totalKB) / 1024 / 1024);
111
+ diskPerc = percStr.replace('%', '');
112
+ }
113
+ } catch (e) {}
114
+ output += `硬盘总数:${diskTotalGB} GB,使用数量:${diskUsedGB} GB,使用占比:${diskPerc}%\n`;
115
+
116
+ // === 5. TCP / UDP Connections ===
117
+ let tcp = 0, udp = 0;
118
+ try {
119
+ tcp = (await promisifyExec("cat /proc/net/tcp 2>/dev/null")).split('\n').length - 1;
120
+ udp = (await promisifyExec("cat /proc/net/udp 2>/dev/null")).split('\n').length - 1;
121
+ } catch (e) {}
122
+ output += `TCP 连接数:${tcp},UDP 连接数:${udp}\n\n`;
123
+
124
+ // === 6. 所有 node 进程(含真实 CPU%)===
125
+ output += '所有 node 进程的 CPU 占比和内存占用:\n';
126
+
127
+ try {
128
+ let allPidRss = new Map();
129
+ try {
130
+ const psOutput = await promisifyExec('ps -o pid,rss --no-headers 2>/dev/null');
131
+ psOutput.split('\n').forEach(line => {
132
+ const parts = line.trim().split(/\s+/);
133
+ if (parts.length >= 2) {
134
+ const pidNum = parseInt(parts[0], 10);
135
+ const rssKB = parseInt(parts[1], 10) || 0;
136
+ if (!isNaN(pidNum) && !isNaN(rssKB)) {
137
+ allPidRss.set(pidNum, rssKB);
138
+ }
139
+ }
140
+ });
141
+ } catch (e) {}
142
+
143
+ const pids = (await fs.readdir('/proc')).filter(pid => /^\d+$/.test(pid));
144
+ let hasNode = false;
145
+ const ticksPerSec = 100; // standard on most Linux systems
146
+ const cpuCores = os.cpus().length;
147
+
148
+ for (const pid of pids) {
149
+ let cmdline = '';
150
+ try {
151
+ const buf = await fs.readFile(`/proc/${pid}/cmdline`);
152
+ cmdline = buf.toString().replace(/\0/g, ' ').trim();
153
+ } catch (e) {
154
+ continue;
155
+ }
156
+
157
+ if (!cmdline || !cmdline.startsWith('node')) continue;
158
+ hasNode = true;
159
+
160
+ // --- Memory ---
161
+ let rssKB = allPidRss.get(parseInt(pid, 10)) || 0;
162
+ if (rssKB === 0) {
163
+ try {
164
+ const statusContent = await fs.readFile(`/proc/${pid}/status`, 'utf8');
165
+ const match = statusContent.match(/VmRSS:\s*(\d+)/);
166
+ if (match) rssKB = parseInt(match[1], 10) || 0;
167
+ } catch (e) {
168
+ rssKB = 0;
169
+ }
170
+ }
171
+ const memoryMB = rssKB ? Math.round(rssKB / 1024) : 0;
172
+
173
+ // --- CPU % via /proc/pid/stat ---
174
+ let cpuPercent = 0;
175
+ try {
176
+ const stat1 = await fs.readFile(`/proc/${pid}/stat`, 'utf8');
177
+ const fields1 = stat1.trim().split(/\s+/);
178
+ const utime1 = parseInt(fields1[13], 10) || 0;
179
+ const stime1 = parseInt(fields1[14], 10) || 0;
180
+
181
+ await new Promise(r => setTimeout(r, 500));
182
+
183
+ const stat2 = await fs.readFile(`/proc/${pid}/stat`, 'utf8');
184
+ const fields2 = stat2.trim().split(/\s+/);
185
+ const utime2 = parseInt(fields2[13], 10) || 0;
186
+ const stime2 = parseInt(fields2[14], 10) || 0;
187
+
188
+ const deltaTicks = (utime2 + stime2) - (utime1 + stime1);
189
+ const cpuUsageSingleCore = (deltaTicks / ticksPerSec) / 0.5 * 100; // over 0.5 seconds
190
+ cpuPercent = Math.min(100 * cpuCores, Math.max(0, cpuUsageSingleCore));
191
+ } catch (e) {
192
+ cpuPercent = 0;
193
+ }
194
+
195
+ output += `PID ${pid}: ${cmdline}\n`;
196
+ output += ` CPU: ${cpuPercent.toFixed(1)}%, 内存: ${memoryMB} MB\n`;
197
+ }
198
+
199
+ if (!hasNode) {
200
+ output += '(无 node 进程)\n';
201
+ }
202
+ } catch (e) {
203
+ output += '(无法读取进程信息)\n';
204
+ }
205
+
206
+ return output + guideLine;
207
+ }
208
+
209
+ // --- macOS 实现 ---
210
+ if (isMacOS) {
211
+ let output = '';
212
+
213
+ const hostname = os.hostname();
214
+ const machine = os.machine();
215
+ const osInfo = await promisifyExec('sw_vers -productName').catch(() => 'macOS');
216
+ const osVersion = await promisifyExec('sw_vers -productVersion').catch(() => os.release());
217
+ output += `主机名:${hostname}\n`;
218
+ output += `系统型号:${machine}\n`;
219
+ output += `系统名称和类型:${osInfo} ${osVersion}\n\n`;
220
+
221
+ const cpuCores = os.cpus().length;
222
+ let cpuUsage = 0;
223
+ try {
224
+ const topOutput = await promisifyExec('top -l 2 -n 0 | grep -E "^CPU" | tail -1');
225
+ const match = topOutput.match(/(\d+\.\d+)%\s+idle/);
226
+ if (match) cpuUsage = 100 - parseFloat(match[1]);
227
+ } catch (e) {
228
+ cpuUsage = 0;
229
+ }
230
+ output += `CPU 核数:${cpuCores},使用占比:${Math.round(cpuUsage)}%\n`;
231
+
232
+ const [load1, load5, load15] = os.loadavg();
233
+ output += `CPU 负载:${load1.toFixed(2)}, ${load5.toFixed(2)}, ${load15.toFixed(2)}\n`;
234
+
235
+ let cpuTemp = 'N/A';
236
+ try {
237
+ const tempOutput = await promisifyExec('osx-cpu-temp 2>/dev/null || echo "N/A"');
238
+ if (tempOutput && !tempOutput.includes('N/A') && !tempOutput.includes('command not found')) {
239
+ const match = tempOutput.match(/(\d+\.?\d*)/);
240
+ if (match) {
241
+ const t = parseFloat(match[1]);
242
+ if (!isNaN(t) && t > 0 && t < 120) cpuTemp = `${t}°C`;
243
+ }
244
+ }
245
+ } catch (e) {}
246
+ output += `CPU 温度:${cpuTemp}\n`;
247
+
248
+ const totalMem = os.totalmem();
249
+ const freeMem = os.freemem();
250
+ const usedMem = totalMem - freeMem;
251
+ const memTotalMB = Math.round(totalMem / 1024 / 1024);
252
+ const memUsedMB = Math.round(usedMem / 1024 / 1024);
253
+ const memPercent = Math.round((usedMem / totalMem) * 100);
254
+ output += `内存总数:${memTotalMB} MB,使用数量:${memUsedMB} MB,使用占比:${memPercent}%\n`;
255
+
256
+ let diskUsedGB = 'N/A', diskTotalGB = 'N/A', diskPerc = 'N/A';
257
+ try {
258
+ const dfLine = await promisifyExec(`df -g / | awk 'NR==2 {print $2,$3,$5}'`);
259
+ const [totalGB, usedGB, percStr] = dfLine.split(/\s+/);
260
+ if (totalGB && usedGB && percStr) {
261
+ diskUsedGB = Math.round(parseFloat(usedGB));
262
+ diskTotalGB = Math.round(parseFloat(totalGB));
263
+ diskPerc = percStr.replace('%', '');
264
+ }
265
+ } catch (e) {}
266
+ output += `硬盘总数:${diskTotalGB} GB,使用数量:${diskUsedGB} GB,使用占比:${diskPerc}%\n`;
267
+
268
+ let tcp = 0, udp = 0;
269
+ try {
270
+ tcp = parseInt((await promisifyExec("netstat -an -p tcp | grep -E '^tcp' | wc -l")).trim(), 10) || 0;
271
+ udp = parseInt((await promisifyExec("netstat -an -p udp | grep -E '^udp' | wc -l")).trim(), 10) || 0;
272
+ } catch (e) {}
273
+ output += `TCP 连接数:${tcp},UDP 连接数:${udp}\n\n`;
274
+
275
+ output += '(macOS 不支持单独列出 node 进程的 CPU/内存)\n';
276
+
277
+ return output + guideLine;
278
+ }
279
+
280
+ return 'Platform not supported for system monitoring.';
281
+ }
282
+
283
+ module.exports = { getSystemMonitorInfo };