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

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +29 -17
index.js CHANGED
@@ -4,10 +4,10 @@ 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 Space 域名
9
- const WSPATH = 'ws'; // 路徑,可改
10
- const SUB_PATH = 'sub'; // 訂閱路徑,建議改成較複雜的
11
  const PORT = 7860;
12
  // ========================================
13
 
@@ -15,7 +15,7 @@ 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>Hello from HF Spaces</h1>' : content);
19
  });
20
  }
21
  else if (req.url === `/${SUB_PATH}`) {
@@ -24,7 +24,7 @@ const requestHandler = (req, res) => {
24
 
25
  res.writeHead(200, { 'Content-Type': 'text/plain' });
26
  res.end(base64);
27
- console.log('Subscription requested');
28
  }
29
  else {
30
  res.writeHead(404);
@@ -35,21 +35,33 @@ const requestHandler = (req, res) => {
35
  const server = http.createServer(requestHandler);
36
  const wss = new WebSocketServer({ server });
37
 
38
- wss.on('connection', (ws) => {
39
- console.log('New connection');
 
40
  ws.once('message', (data) => {
41
- // 簡單 VLESS + Trojan 處理(基本版)
42
- const firstByte = data[0];
43
- if (firstByte === 0 || firstByte === 1) {
44
- // VLESS
45
- ws.write(Buffer.from([0, 0])); // 回應
46
- } else {
47
- // Trojan 簡易處理
48
- ws.write(Buffer.from([0x01, 0x00]));
 
 
 
 
49
  }
50
  });
51
 
52
- ws.on('error', () => ws.terminate());
 
 
 
 
 
 
 
53
  });
54
 
55
  server.listen(PORT, () => {
 
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
 
 
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}`) {
 
24
 
25
  res.writeHead(200, { 'Content-Type': 'text/plain' });
26
  res.end(base64);
27
+ console.log(' 訂閱被請求');
28
  }
29
  else {
30
  res.writeHead(404);
 
35
  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, () => {