Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
165 lines (137 loc) · 5.01 KB
/
Copy pathindex.js
File metadata and controls
165 lines (137 loc) · 5.01 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
process.on("uncaughtException", () => {});
process.on("unhandledRejection", () => {});
// ====== 只修改两个核心变量 UUID/DOMAIN ======
const UUID = (process.env.UUID || "abcd1eb2-1c20-345a-96fa-cdf394612345").trim(); // 替换"双引号中的UUID"
const DOMAIN = (process.env.DOMAIN || "abc.domain.dpdns.org").trim(); // 替换"双引号中的完整域名"
// Panel 配置
const NAME = "DirectAdmin-easyshare";
const LISTEN_PORT = Number(process.env.PORT) || 0; // 自适应端口
const BEST_DOMAINS = [
"www.visa.cn",
"www.shopify.com",
"store.ubi.com",
"www.wto.org",
"time.is",
"www.udemy.com",
];
// ============================================================
// =============== 模块加载区 ================================
// ============================================================
const http = require("http");
const net = require("net");
const { WebSocketServer, createWebSocketStream } = require("ws");
// ============================================================
// =============== WebSocket Path ============================
// ============================================================
const WS_PATH = `/${UUID}`;
// ============================================================
// =============== 生成 VLESS 节点链接函数 ====================
// ============================================================
function generateLink(address) {
return (
`vless://${UUID}@${address}:443` +
`?encryption=none&security=tls&sni=${DOMAIN}` +
`&fp=chrome&type=ws&host=${DOMAIN}` +
`&path=${encodeURIComponent(WS_PATH)}` +
`#${NAME}`
);
}
// ============================================================
// =============== HTTP 服务 ==================================
// ============================================================
const server = http.createServer((req, res) => {
if (req.headers.upgrade) {
res.writeHead(426);
return res.end();
}
if (req.url === "/") {
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
return res.end(`VLESS WS TLS Running\n访问 ${WS_PATH} 查看节点\n`);
}
if (req.url === WS_PATH) {
let txt = "═════ EasyShare VLESS WS TLS ═════\n\n";
for (const d of BEST_DOMAINS) {
txt += generateLink(d) + "\n\n";
}
txt += "节点已全部生成,可直接复制使用。\n";
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
return res.end(txt);
}
res.writeHead(404);
res.end("404 Not Found");
});
// ============================================================
// =============== WebSocket 后端 ============================
// ============================================================
const wss = new WebSocketServer({
noServer: true,
maxPayload: 256 * 1024,
});
const uuidClean = UUID.replace(/-/g, "");
server.on("upgrade", (req, socket, head) => {
if (req.url !== WS_PATH) {
socket.destroy();
return;
}
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
});
wss.on("connection", (ws) => {
let tcp = null;
ws.once("message", (msg) => {
if (!Buffer.isBuffer(msg) || msg.length < 18) {
ws.close();
return;
}
const version = msg[0];
const id = msg.slice(1, 17);
for (let i = 0; i < 16; i++) {
if (id[i] !== parseInt(uuidClean.substr(i * 2, 2), 16)) {
ws.close();
return;
}
}
let p = msg[17] + 19;
const port = msg.readUInt16BE(p); p += 2;
const atyp = msg[p++];
let host = "";
if (atyp === 1) {
host = Array.from(msg.slice(p, p + 4)).join(".");
p += 4;
} else if (atyp === 2) {
const len = msg[p];
host = msg.slice(p + 1, p + 1 + len).toString();
p += 1 + len;
} else if (atyp === 3) {
const raw = msg.slice(p, p + 16);
const parts = [];
for (let i = 0; i < 16; i += 2) {
parts.push(raw.readUInt16BE(i).toString(16));
}
host = parts.join(":");
p += 16;
} else {
ws.close();
return;
}
ws.send(Buffer.from([version, 0]));
tcp = net.connect({ host, port }, () => {
tcp.setNoDelay(true);
tcp.write(msg.slice(p));
const duplex = createWebSocketStream(ws);
duplex.pipe(tcp).pipe(duplex);
});
tcp.on("error", () => {
try { ws.close(); } catch {}
});
});
ws.on("close", () => {
try { tcp && tcp.destroy(); } catch {}
});
ws.on("error", () => {});
});
// ============================================================
// =============== 启动 ============================
// ============================================================
server.listen(LISTEN_PORT, "0.0.0.0");