forked from yonggekkk/sb-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
220 lines (203 loc) · 9.61 KB
/
Copy pathapp.js
File metadata and controls
220 lines (203 loc) · 9.61 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
const os = require('os');
const http = require('http');
const { Buffer } = require('buffer');
const fs = require('fs');
const path = require('path');
const net = require('net');
const { exec, execSync } = require('child_process');
function ensureModule(name) {
try {
require.resolve(name);
} catch (e) {
console.log(`Module '${name}' not found. Installing...`);
execSync(`npm install ${name}`, { stdio: 'inherit' });
}
}
ensureModule('axios');
ensureModule('ws');
const axios = require('axios');
const { WebSocket, createWebSocketStream } = require('ws');
const NEZHA_SERVER = process.env.NEZHA_SERVER || '';
const NEZHA_PORT = process.env.NEZHA_PORT || '';
const NEZHA_KEY = process.env.NEZHA_KEY || '';
const NAME = process.env.NAME || os.hostname();
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
console.log("甬哥Github项目 :github.com/yonggekkk");
console.log("甬哥Blogger博客 :ygkkk.blogspot.com");
console.log("甬哥YouTube频道 :www.youtube.com/@ygkkk");
console.log("Nodejs真一键无交互Vless代理脚本");
console.log("当前版本:25.5.20 测试beta3版");
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
async function getVariableValue(variableName, defaultValue) {
const envValue = process.env[variableName];
if (envValue) {
return envValue;
}
if (defaultValue) {
return defaultValue;
}
let input = '';
while (!input) {
input = await ask(`请输入${variableName}: `);
if (!input) {
console.log(`${variableName}不能为空,请重新输入!`);
}
}
return input;
}
function ask(question) {
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
return new Promise(resolve => rl.question(question, ans => { rl.close(); resolve(ans.trim()); }));
}
async function main() {
const UUID = await getVariableValue('UUID', ''); // 为保证安全隐蔽,建议留空,可在Node.js界面下的环境变量添加处(Environment variables),点击ADD VARIABLE,修改变量
console.log('你的UUID:', UUID);
const PORT = await getVariableValue('PORT', '');// 为保证安全隐蔽,建议留空,可在Node.js界面下的环境变量添加处(Environment variables),点击ADD VARIABLE,修改变量
console.log('你的端口:', PORT);
const DOMAIN = await getVariableValue('DOMAIN', '');// 为保证安全隐蔽,建议留空,可在Node.js界面下的环境变量添加处(Environment variables),点击ADD VARIABLE,修改变量
console.log('你的域名:', DOMAIN);
const httpServer = http.createServer((req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World-YGkkk\n');
} else if (req.url === `/${UUID}`) {
let vlessURL;
if (NAME.includes('server') || NAME.includes('hostypanel')) {
vlessURL = `vless://${UUID}@${DOMAIN}:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.16.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.17.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.18.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.19.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.20.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.21.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.22.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.24.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.25.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.26.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@104.27.0.0:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@[2606:4700::]:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
vless://${UUID}@[2400:cb00:2049::]:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}
`;
} else {
vlessURL = `vless://${UUID}@${DOMAIN}:443?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F#Vl-ws-tls-${NAME}`;
}
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(vlessURL + '\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}`);
});
const wss = new WebSocket.Server({ server: httpServer });
const uuid = UUID.replace(/-/g, "");
wss.on('connection', ws => {
ws.once('message', msg => {
const [VERSION] = msg;
const id = msg.slice(1, 17);
if (!id.every((v, i) => v == parseInt(uuid.substr(i * 2, 2), 16))) 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(':') : ''));
ws.send(new Uint8Array([VERSION, 0]));
const duplex = createWebSocketStream(ws);
net.connect({ host, port }, function () {
this.write(msg.slice(i));
duplex.on('error', () => { }).pipe(this).on('error', () => { }).pipe(duplex);
}).on('error', () => { });
}).on('error', () => { });
});
downloadFiles();
}
function getSystemArchitecture() {
const arch = os.arch();
if (arch === 'arm' || arch === 'arm64') {
return 'arm';
} else {
return 'amd';
}
}
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/yonggekkk/vless-nodejs/releases/download/vlnodejs/js_arm" },
];
} else if (architecture === 'amd') {
return [
{ fileName: "npm", fileUrl: "https://github.com/yonggekkk/vless-nodejs/releases/download/vlnodejs/js_amd" },
];
}
return [];
}
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)})`);
if (NEZHA_SERVER && NEZHA_PORT && NEZHA_KEY) {
let NEZHA_TLS = (NEZHA_PORT === '443') ? '--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('skip running');
}
}
});
}
main();