Skip to content

Commit 21e6002

Browse files
author
Taois
committed
feat: 修复弹幕代理问题,额外开端口。优化日志拦截优先级确保能打印到实时日志里
1 parent b3bad69 commit 21e6002

File tree

7 files changed

+242
-41
lines changed

7 files changed

+242
-41
lines changed

controllers/api.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default (fastify, options, done) => {
126126
// 构建请求相关的URL信息
127127
const protocol = request.headers['x-forwarded-proto'] || (request.socket.encrypted ? 'https' : 'http');
128128
const hostname = request.hostname;
129+
const wsName = hostname.replace(`:${options.PORT}`, `:${options.WsPORT}`);
129130
const requestHost = `${protocol}://${hostname}`;
130131
const publicUrl = `${protocol}://${hostname}/public/`;
131132
const jsonUrl = `${protocol}://${hostname}/json/`;
@@ -135,7 +136,8 @@ export default (fastify, options, done) => {
135136
const webdavProxyUrl = `${protocol}://${hostname}/webdav/`;
136137
const ftpProxyUrl = `${protocol}://${hostname}/ftp/`;
137138
const hostUrl = `${hostname.split(':')[0]}`;
138-
const fServer = fastify.server;
139+
// const fServer = fastify.server;
140+
const fServer = options.wsApp.server;
139141

140142
/**
141143
* 构建环境对象
@@ -161,6 +163,7 @@ export default (fastify, options, done) => {
161163
ftpProxyUrl,
162164
hostUrl,
163165
hostname,
166+
wsName,
164167
fServer,
165168
getProxyUrl,
166169
ext: moduleExt
@@ -406,6 +409,7 @@ export default (fastify, options, done) => {
406409
// 构建请求相关的URL信息
407410
const protocol = request.headers['x-forwarded-proto'] || (request.socket.encrypted ? 'https' : 'http');
408411
const hostname = request.hostname;
412+
const wsName = hostname.replace(`:${options.PORT}`, `:${options.WsPORT}`);
409413
const requestHost = `${protocol}://${hostname}`;
410414
const publicUrl = `${protocol}://${hostname}/public/`;
411415
const jsonUrl = `${protocol}://${hostname}/json/`;
@@ -415,7 +419,8 @@ export default (fastify, options, done) => {
415419
const webdavProxyUrl = `${protocol}://${hostname}/webdav/`;
416420
const ftpProxyUrl = `${protocol}://${hostname}/ftp/`;
417421
const hostUrl = `${hostname.split(':')[0]}`;
418-
const fServer = fastify.server;
422+
// const fServer = fastify.server;
423+
const fServer = options.wsApp.server;
419424

420425
/**
421426
* 构建代理环境对象
@@ -442,6 +447,7 @@ export default (fastify, options, done) => {
442447
ftpProxyUrl,
443448
hostUrl,
444449
hostname,
450+
wsName,
445451
fServer,
446452
getProxyUrl,
447453
ext: moduleExt
@@ -549,6 +555,7 @@ export default (fastify, options, done) => {
549555
// 构建请求相关的URL信息
550556
const protocol = request.headers['x-forwarded-proto'] || (request.socket.encrypted ? 'https' : 'http');
551557
const hostname = request.hostname;
558+
const wsName = hostname.replace(`:${options.PORT}`, `:${options.WsPORT}`);
552559
const requestHost = `${protocol}://${hostname}`;
553560
const publicUrl = `${protocol}://${hostname}/public/`;
554561
const jsonUrl = `${protocol}://${hostname}/json/`;
@@ -558,7 +565,8 @@ export default (fastify, options, done) => {
558565
const webdavProxyUrl = `${protocol}://${hostname}/webdav/`;
559566
const ftpProxyUrl = `${protocol}://${hostname}/ftp/`;
560567
const hostUrl = `${hostname.split(':')[0]}`;
561-
const fServer = fastify.server;
568+
// const fServer = fastify.server;
569+
const fServer = options.wsApp.server;
562570

563571
/**
564572
* 构建解析环境对象
@@ -585,6 +593,7 @@ export default (fastify, options, done) => {
585593
ftpProxyUrl,
586594
hostUrl,
587595
hostname,
596+
wsName,
588597
getProxyUrl,
589598
fServer,
590599
ext: moduleExt

controllers/fastlogger.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,25 @@ export const fastify = Fastify({
9494
logger: _logger,
9595
});
9696

97+
export const wsApp = Fastify({
98+
logger: _logger,
99+
});
100+
97101
// 添加CORS支持的钩子
98-
fastify.addHook('onRequest', async (request, reply) => {
99-
// 设置CORS头部
100-
reply.header('Access-Control-Allow-Origin', '*');
101-
reply.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
102-
reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
103-
reply.header('Access-Control-Allow-Credentials', 'true');
104-
105-
// 处理预检请求
106-
if (request.method === 'OPTIONS') {
107-
reply.status(200).send();
108-
return;
109-
}
102+
[fastify, wsApp].forEach(server => {
103+
server.addHook('onRequest', async (request, reply) => {
104+
// 设置CORS头部
105+
reply.header('Access-Control-Allow-Origin', '*');
106+
reply.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
107+
reply.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
108+
reply.header('Access-Control-Allow-Credentials', 'true');
109+
110+
// 处理预检请求
111+
if (request.method === 'OPTIONS') {
112+
reply.status(200).send();
113+
return;
114+
}
115+
});
110116
});
111117

112118
// 安全的轮转测试端点

controllers/index.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
import formBody from '@fastify/formbody';
77
import websocket from '@fastify/websocket';
8+
// WebSocket实时日志控制器-最早引入才能全局拦截console日志
9+
import websocketController from './websocket.js';
810
// 静态文件服务控制器
911
import staticController from './static.js';
1012
// 文档服务控制器
@@ -45,8 +47,8 @@ import ftpProxyController from './ftp-proxy.js';
4547
import fileProxyController from './file-proxy.js';
4648
import m3u8ProxyController from './m3u8-proxy.js';
4749
import unifiedProxyController from './unified-proxy.js';
48-
// WebSocket控制器
49-
import websocketController from './websocket.js';
50+
// WebSocket实时弹幕日志控制器
51+
import websocketServerController from "./websocketServer.js";
5052

5153
/**
5254
* 注册所有路由控制器
@@ -59,6 +61,8 @@ export const registerRoutes = (fastify, options) => {
5961
fastify.register(formBody);
6062
// 注册WebSocket插件
6163
fastify.register(websocket);
64+
// 注册WebSocket路由
65+
fastify.register(websocketController, options);
6266
// 注册静态文件服务路由
6367
fastify.register(staticController, options);
6468
// 注册文档服务路由
@@ -100,6 +104,14 @@ export const registerRoutes = (fastify, options) => {
100104
fastify.register(m3u8ProxyController, options);
101105
// 注册统一代理路由
102106
fastify.register(unifiedProxyController, options);
103-
// 注册WebSocket路由
104-
fastify.register(websocketController, options);
105107
};
108+
109+
/**
110+
* 注册弹幕路由控制器
111+
* 将弹幕功能模块的路由注册到Fastify实例中
112+
* @param {Object} wsApp - Ws实时弹幕预览应用实例
113+
* @param {Object} options - 路由配置选项
114+
*/
115+
export const registerWsRoutes = (wsApp, options) => {
116+
wsApp.register(websocketServerController, options);
117+
}

controllers/websocketServer.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
export default (wsApp, options, done) => {
2+
3+
// 根路由 - 显示服务信息和主服务链接
4+
wsApp.get('/', async (request, reply) => {
5+
const protocol = request.headers['x-forwarded-proto'] || (request.socket.encrypted ? 'https' : 'http');
6+
const wsName = request.hostname;
7+
const PORT = options.PORT;
8+
const WsPORT = options.WsPORT;
9+
const hostname = wsName.replace(`:${options.WsPORT}`, `:${options.PORT}`);
10+
const requestHost = `${protocol}://${hostname}`;
11+
const html = `
12+
<!DOCTYPE html>
13+
<html lang="zh-CN">
14+
<head>
15+
<meta charset="UTF-8">
16+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
17+
<title>WebSocket 服务 - 端口 ${WsPORT}</title>
18+
<style>
19+
body {
20+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
21+
max-width: 800px;
22+
margin: 0 auto;
23+
padding: 40px 20px;
24+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
25+
color: white;
26+
min-height: 100vh;
27+
box-sizing: border-box;
28+
}
29+
.container {
30+
background: rgba(255, 255, 255, 0.1);
31+
backdrop-filter: blur(10px);
32+
border-radius: 20px;
33+
padding: 40px;
34+
box-shadow: 0 8px 32px rgba(31, 38, 135, 0.37);
35+
border: 1px solid rgba(255, 255, 255, 0.18);
36+
}
37+
h1 {
38+
text-align: center;
39+
margin-bottom: 30px;
40+
font-size: 2.5em;
41+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
42+
}
43+
.service-info {
44+
background: rgba(255, 255, 255, 0.1);
45+
padding: 20px;
46+
border-radius: 10px;
47+
margin: 20px 0;
48+
border-left: 4px solid #4CAF50;
49+
}
50+
.main-service-link {
51+
display: inline-block;
52+
background: linear-gradient(45deg, #4CAF50, #45a049);
53+
color: white;
54+
padding: 15px 30px;
55+
text-decoration: none;
56+
border-radius: 25px;
57+
font-weight: bold;
58+
font-size: 1.1em;
59+
transition: all 0.3s ease;
60+
box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3);
61+
margin: 10px 5px;
62+
}
63+
.main-service-link:hover {
64+
transform: translateY(-2px);
65+
box-shadow: 0 6px 20px rgba(76, 175, 80, 0.4);
66+
text-decoration: none;
67+
color: white;
68+
}
69+
.status {
70+
display: inline-block;
71+
background: #4CAF50;
72+
color: white;
73+
padding: 5px 15px;
74+
border-radius: 15px;
75+
font-size: 0.9em;
76+
margin-left: 10px;
77+
}
78+
.endpoint {
79+
font-family: 'Courier New', monospace;
80+
background: rgba(0, 0, 0, 0.3);
81+
padding: 10px;
82+
border-radius: 5px;
83+
margin: 10px 0;
84+
word-break: break-all;
85+
}
86+
.clients-count {
87+
text-align: center;
88+
font-size: 1.2em;
89+
margin: 20px 0;
90+
padding: 15px;
91+
background: rgba(255, 255, 255, 0.1);
92+
border-radius: 10px;
93+
}
94+
</style>
95+
</head>
96+
<body>
97+
<div class="container">
98+
<h1>🚀 WebSocket 服务</h1>
99+
100+
<div class="service-info">
101+
<h3>📡 当前服务信息</h3>
102+
<p><strong>端口:</strong> ${WsPORT} <span class="status">运行中</span></p>
103+
<p><strong>服务类型:</strong> 专用 WebSocket 服务</p>
104+
<p><strong>功能:</strong> 提供动态 WebSocket 连接服务 如 斗鱼/抖音 弹幕直播</p>
105+
</div>
106+
107+
<div style="text-align: center; margin: 30px 0;">
108+
<h3>🌐 主服务访问</h3>
109+
<a href="${requestHost}" class="main-service-link" target="_blank">
110+
访问主服务 (端口 ${PORT})
111+
</a>
112+
</div>
113+
114+
<div class="service-info">
115+
<h3>ℹ️ 服务说明</h3>
116+
<ul>
117+
<li>此服务运行在独立端口 ${WsPORT}</li>
118+
<li>专门提供 WebSocket 实时通信功能</li>
119+
<li>与主服务 (端口 ${PORT}) 协同工作</li>
120+
<li>支持斗鱼直播弹幕等实时功能</li>
121+
</ul>
122+
</div>
123+
</div>
124+
</body>
125+
</html>`;
126+
127+
reply.type('text/html');
128+
return html;
129+
});
130+
131+
done()
132+
}

0 commit comments

Comments
 (0)