tty / index.js
wky's picture
Update index.js
b3d2f02 verified
Raw
History Blame Contribute Delete
2.53 kB
const http = require('http');
const fs = require('fs');
const path = require('path');
const { WebSocketServer } = require('ws');
const net = require('net');
// =============== 配置區 ===============
const UUID = 'b831381d-6324-4d53-ad4f-8cda48b30811';
const DOMAIN = 'wky-tty.hf.space';
const WSPATH = 'ws';
const SUB_PATH = 'sub';
const PORT = 7860;
// =====================================
const requestHandler = (req, res) => {
if (req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<h1>Hugging Face VLESS Proxy</h1><p>Running...</p>');
}
else if (req.url === `/${SUB_PATH}`) {
const config = `vless://${UUID}@${DOMAIN}?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F${WSPATH}#HF-${DOMAIN}`;
const base64 = Buffer.from(config).toString('base64');
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(base64);
} else {
res.writeHead(404);
res.end('Not Found');
}
};
const server = http.createServer(requestHandler);
const wss = new WebSocketServer({ server });
wss.on('connection', (ws, req) => {
console.log('New Connection from', req.socket.remoteAddress);
let targetSocket = null;
ws.once('message', (data) => {
const buf = Buffer.from(data);
// 簡單 VLESS 握手
if (buf[0] === 0) {
ws.send(Buffer.from([0x00, 0x00])); // 成功回應
console.log('✅ VLESS Handshake OK');
// 建立到真實目標的 TCP 連線(這裡是簡化版)
targetSocket = net.createConnection(443, '1.1.1.1'); // 測試用,可改成其他
targetSocket.on('connect', () => {
console.log('Target connected');
});
targetSocket.on('data', (chunk) => {
if (ws.readyState === ws.OPEN) ws.send(chunk);
});
targetSocket.on('error', () => ws.close());
targetSocket.on('close', () => ws.close());
}
});
ws.on('message', (data) => {
if (targetSocket && !targetSocket.destroyed) {
targetSocket.write(data);
}
});
ws.on('close', () => {
if (targetSocket) targetSocket.destroy();
console.log('Connection closed');
});
ws.on('error', () => ws.terminate());
});
server.listen(PORT, () => {
console.log(`✅ Server running on ${PORT}`);
console.log(`訂閱: https://${DOMAIN}/${SUB_PATH}`);
});