wky commited on
Commit
b3d2f02
·
verified ·
1 Parent(s): 48684b6

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +39 -29
index.js CHANGED
@@ -2,31 +2,27 @@ const http = require('http');
2
  const fs = require('fs');
3
  const path = require('path');
4
  const { WebSocketServer } = require('ws');
 
5
 
6
- // =============== 修改這裡 ===============
7
- const UUID = 'b831381d-6324-4d53-ad4f-8cda48b30811'; // 你的 UUID
8
- const DOMAIN = 'wky-tty.hf.space'; // 你的 HF 域名
9
- const WSPATH = 'ws'; // WS 路徑
10
- const SUB_PATH = 'sub'; // 訂閱路徑
11
  const PORT = 7860;
12
- // ========================================
13
 
14
  const requestHandler = (req, res) => {
15
  if (req.url === '/') {
16
- fs.readFile(path.join(__dirname, 'index.html'), (err, content) => {
17
- res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
18
- res.end(err ? '<h1>Hugging Face Proxy</h1>' : content);
19
- });
20
  }
21
  else if (req.url === `/${SUB_PATH}`) {
22
  const config = `vless://${UUID}@${DOMAIN}?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F${WSPATH}#HF-${DOMAIN}`;
23
  const base64 = Buffer.from(config).toString('base64');
24
-
25
  res.writeHead(200, { 'Content-Type': 'text/plain' });
26
  res.end(base64);
27
- console.log('✅ 訂閱被請求');
28
- }
29
- else {
30
  res.writeHead(404);
31
  res.end('Not Found');
32
  }
@@ -36,35 +32,49 @@ const server = http.createServer(requestHandler);
36
  const wss = new WebSocketServer({ server });
37
 
38
  wss.on('connection', (ws, req) => {
39
- console.log('New WS Connection');
 
 
40
 
41
  ws.once('message', (data) => {
42
  const buf = Buffer.from(data);
43
 
44
- // VLESS 協議簡單回應(版本 0)
45
  if (buf[0] === 0) {
46
- const response = Buffer.from([0x00, 0x00]); // VLESS 成功回應
47
- ws.send(response);
48
- console.log('VLESS Handshake OK');
49
- }
50
- // Trojan 簡易處理
51
- else if (buf.length > 56) {
52
- ws.send(Buffer.from([0x01, 0x00]));
53
- console.log('Trojan Handshake');
 
 
 
 
 
 
 
 
54
  }
55
  });
56
 
57
- ws.on('error', (err) => {
58
- console.log('WS Error:', err.message);
59
- ws.terminate();
 
60
  });
61
 
62
  ws.on('close', () => {
 
63
  console.log('Connection closed');
64
  });
 
 
65
  });
66
 
67
  server.listen(PORT, () => {
68
- console.log(`✅ Xray Proxy 運行在端口 ${PORT}`);
69
- console.log(`訂閱地址: https://${DOMAIN}/${SUB_PATH}`);
70
  });
 
2
  const fs = require('fs');
3
  const path = require('path');
4
  const { WebSocketServer } = require('ws');
5
+ const net = require('net');
6
 
7
+ // =============== 配置區 ===============
8
+ const UUID = 'b831381d-6324-4d53-ad4f-8cda48b30811';
9
+ const DOMAIN = 'wky-tty.hf.space';
10
+ const WSPATH = 'ws';
11
+ const SUB_PATH = 'sub';
12
  const PORT = 7860;
13
+ // =====================================
14
 
15
  const requestHandler = (req, res) => {
16
  if (req.url === '/') {
17
+ res.writeHead(200, { 'Content-Type': 'text/html' });
18
+ res.end('<h1>Hugging Face VLESS Proxy</h1><p>Running...</p>');
 
 
19
  }
20
  else if (req.url === `/${SUB_PATH}`) {
21
  const config = `vless://${UUID}@${DOMAIN}?encryption=none&security=tls&sni=${DOMAIN}&fp=chrome&type=ws&host=${DOMAIN}&path=%2F${WSPATH}#HF-${DOMAIN}`;
22
  const base64 = Buffer.from(config).toString('base64');
 
23
  res.writeHead(200, { 'Content-Type': 'text/plain' });
24
  res.end(base64);
25
+ } else {
 
 
26
  res.writeHead(404);
27
  res.end('Not Found');
28
  }
 
32
  const wss = new WebSocketServer({ server });
33
 
34
  wss.on('connection', (ws, req) => {
35
+ console.log('New Connection from', req.socket.remoteAddress);
36
+
37
+ let targetSocket = null;
38
 
39
  ws.once('message', (data) => {
40
  const buf = Buffer.from(data);
41
 
42
+ // 簡單 VLESS 握手
43
  if (buf[0] === 0) {
44
+ ws.send(Buffer.from([0x00, 0x00])); // 成功回應
45
+ console.log('✅ VLESS Handshake OK');
46
+
47
+ // 建立到真實目標的 TCP 連線(這裡是簡化版)
48
+ targetSocket = net.createConnection(443, '1.1.1.1'); // 測試用,可改成其他
49
+
50
+ targetSocket.on('connect', () => {
51
+ console.log('Target connected');
52
+ });
53
+
54
+ targetSocket.on('data', (chunk) => {
55
+ if (ws.readyState === ws.OPEN) ws.send(chunk);
56
+ });
57
+
58
+ targetSocket.on('error', () => ws.close());
59
+ targetSocket.on('close', () => ws.close());
60
  }
61
  });
62
 
63
+ ws.on('message', (data) => {
64
+ if (targetSocket && !targetSocket.destroyed) {
65
+ targetSocket.write(data);
66
+ }
67
  });
68
 
69
  ws.on('close', () => {
70
+ if (targetSocket) targetSocket.destroy();
71
  console.log('Connection closed');
72
  });
73
+
74
+ ws.on('error', () => ws.terminate());
75
  });
76
 
77
  server.listen(PORT, () => {
78
+ console.log(`✅ Server running on ${PORT}`);
79
+ console.log(`訂閱: https://${DOMAIN}/${SUB_PATH}`);
80
  });