Skip to content

Commit 2fb9746

Browse files
committed
Fix: Add query parameter authentication as fallback
- Add detailed logging for debugging auth issues - Support authentication via query parameter - Log URL, headers, and expected values - Help diagnose connection problems
1 parent 842ce45 commit 2fb9746

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/index.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,36 @@ const io = new Server(httpServer, {
8484
allowRequest: (req, callback) => {
8585
// 详细日志
8686
console.log('[WatchRoom] Connection attempt from:', req.headers.origin);
87-
console.log('[WatchRoom] Authorization header:', req.headers.authorization);
88-
console.log('[WatchRoom] All headers:', JSON.stringify(req.headers, null, 2));
87+
console.log('[WatchRoom] URL:', req.url);
88+
89+
// 尝试从多个地方获取认证信息
90+
const authHeader = req.headers.authorization;
91+
const urlParams = new URL(req.url || '', 'http://localhost').searchParams;
92+
const authFromQuery = urlParams.get('auth');
93+
94+
console.log('[WatchRoom] Authorization header:', authHeader);
95+
console.log('[WatchRoom] Auth from query:', authFromQuery);
8996
console.log('[WatchRoom] Expected AUTH_KEY:', AUTH_KEY);
9097

91-
const auth = req.headers.authorization;
92-
if (auth === `Bearer ${AUTH_KEY}`) {
93-
console.log('[WatchRoom] ✅ Authentication successful');
98+
// 检查 Authorization header
99+
if (authHeader === `Bearer ${AUTH_KEY}`) {
100+
console.log('[WatchRoom] ✅ Authentication successful (via header)');
101+
callback(null, true);
102+
return;
103+
}
104+
105+
// 检查 query 参数(作为备用方案)
106+
if (authFromQuery === AUTH_KEY) {
107+
console.log('[WatchRoom] ✅ Authentication successful (via query)');
94108
callback(null, true);
95-
} else {
96-
console.log('[WatchRoom] ❌ Authentication failed');
97-
console.log('[WatchRoom] Received:', auth);
98-
console.log('[WatchRoom] Expected:', `Bearer ${AUTH_KEY}`);
99-
callback('Unauthorized', false);
109+
return;
100110
}
111+
112+
console.log('[WatchRoom] ❌ Authentication failed');
113+
console.log('[WatchRoom] Received header:', authHeader);
114+
console.log('[WatchRoom] Received query:', authFromQuery);
115+
console.log('[WatchRoom] Expected:', `Bearer ${AUTH_KEY}`);
116+
callback('Unauthorized', false);
101117
},
102118
});
103119

0 commit comments

Comments
 (0)