forked from qwer-search/Webhostmost-ws-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
183 lines (167 loc) · 6.09 KB
/
app.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
const os = require('os');
const http = require('http');
const { Buffer } = require('buffer');
const fs = require('fs');
const axios = require('axios');
const path = require('path');
const net = require('net');
const { exec, execSync } = require('child_process');
const { WebSocket, createWebSocketStream } = require('ws');
const logcb = (...args) => console.log.bind(this, ...args);
const errcb = (...args) => console.error.bind(this, ...args);
const UUID = process.env.UUID || 'b28f60af-d0b9-4ddf-baaa-7e49c93c380b';
const uuid = UUID.replace(/-/g, "");
const NEZHA_SERVER = process.env.NEZHA_SERVER || 'nezha.gvkoyeb.eu.org';
const NEZHA_PORT = process.env.NEZHA_PORT || '443'; // 端口为443时自动开启tls
const NEZHA_KEY = process.env.NEZHA_KEY || ''; // 哪吒三个变量不全不运行
const DOMAIN = process.env.DOMAIN || ''; //项目域名或已反代的域名,不带前缀,建议填已反代的域名
const NAME = process.env.NAME || 'JP-webhostmost-GCP';
const port = process.env.PORT || 3000;
// 创建HTTP路由
const httpServer = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World\n');
} else if (req.url === '/sub') {
const vlessURL = `vless://${UUID}@skk.moe:443?encryption=none&security=tls&sni=${DOMAIN}&type=ws&host=${DOMAIN}&path=%2F#${NAME}`;
const base64Content = Buffer.from(vlessURL).toString('base64');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(base64Content + '\n');
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found\n');
}
});
httpServer.listen(port, () => {
console.log(`HTTP Server is running on port ${port}`);
});
// 判断系统架构
function getSystemArchitecture() {
const arch = os.arch();
if (arch === 'arm' || arch === 'arm64') {
return 'arm';
} else {
return 'amd';
}
}
// 下载对应系统架构的ne-zha
function downloadFile(fileName, fileUrl, callback) {
const filePath = path.join("./", fileName);
const writer = fs.createWriteStream(filePath);
axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
})
.then(response => {
response.data.pipe(writer);
writer.on('finish', function() {
writer.close();
callback(null, fileName);
});
})
.catch(error => {
callback(`Download ${fileName} failed: ${error.message}`);
});
}
function downloadFiles() {
const architecture = getSystemArchitecture();
const filesToDownload = getFilesForArchitecture(architecture);
if (filesToDownload.length === 0) {
console.log(`Can't find a file for the current architecture`);
return;
}
let downloadedCount = 0;
filesToDownload.forEach(fileInfo => {
downloadFile(fileInfo.fileName, fileInfo.fileUrl, (err, fileName) => {
if (err) {
console.log(`Download ${fileName} failed`);
} else {
console.log(`Download ${fileName} successfully`);
downloadedCount++;
if (downloadedCount === filesToDownload.length) {
setTimeout(() => {
authorizeFiles();
}, 3000);
}
}
});
});
}
function getFilesForArchitecture(architecture) {
if (architecture === 'arm') {
return [
{ fileName: "npm", fileUrl: "https://github.com/eooce/test/releases/download/ARM/swith" },
];
} else if (architecture === 'amd') {
return [
{ fileName: "npm", fileUrl: "https://github.com/eooce/test/releases/download/bulid/swith" },
];
}
return [];
}
// 授权并运行ne-zha
function authorizeFiles() {
const filePath = './npm';
const newPermissions = 0o775;
fs.chmod(filePath, newPermissions, (err) => {
if (err) {
console.error(`Empowerment failed:${err}`);
} else {
console.log(`Empowerment success:${newPermissions.toString(8)} (${newPermissions.toString(10)})`);
// 运行ne-zha
let NEZHA_TLS = '';
if (NEZHA_SERVER && NEZHA_PORT && NEZHA_KEY) {
if (NEZHA_PORT === '443') {
NEZHA_TLS = '--tls';
} else {
NEZHA_TLS = '';
}
const command = `./npm -s ${NEZHA_SERVER}:${NEZHA_PORT} -p ${NEZHA_KEY} ${NEZHA_TLS} --skip-conn --disable-auto-update --skip-procs --report-delay 4 >/dev/null 2>&1 &`;
try {
exec(command);
console.log('npm is running');
} catch (error) {
console.error(`npm running error: ${error}`);
}
} else {
console.log('NEZHA variable is empty,skip running');
}
}
});
}
downloadFiles();
// WebSocket 服务器
const wss = new WebSocket.Server({ server: httpServer });
wss.on('connection', ws => {
console.log("WebSocket 连接成功");
ws.on('message', msg => {
if (msg.length < 18) {
console.error("数据长度无效");
return;
}
try {
const [VERSION] = msg;
const id = msg.slice(1, 17);
if (!id.every((v, i) => v == parseInt(uuid.substr(i * 2, 2), 16))) {
console.error("UUID 验证失败");
return;
}
let i = msg.slice(17, 18).readUInt8() + 19;
const port = msg.slice(i, i += 2).readUInt16BE(0);
const ATYP = msg.slice(i, i += 1).readUInt8();
const host = ATYP === 1 ? msg.slice(i, i += 4).join('.') :
(ATYP === 2 ? new TextDecoder().decode(msg.slice(i + 1, i += 1 + msg.slice(i, i + 1).readUInt8())) :
(ATYP === 3 ? msg.slice(i, i += 16).reduce((s, b, i, a) => (i % 2 ? s.concat(a.slice(i - 1, i + 1)) : s), []).map(b => b.readUInt16BE(0).toString(16)).join(':') : ''));
console.log('连接到:', host, port);
ws.send(new Uint8Array([VERSION, 0]));
const duplex = createWebSocketStream(ws);
net.connect({ host, port }, function () {
this.write(msg.slice(i));
duplex.on('error', err => console.error("E1:", err.message)).pipe(this).on('error', err => console.error("E2:", err.message)).pipe(duplex);
}).on('error', err => console.error("连接错误:", err.message));
} catch (err) {
console.error("处理消息时出错:", err.message);
}
}).on('error', err => console.error("WebSocket 错误:", err.message));
});