|
|
@@ -80,7 +80,7 @@ function parseCookies(setCookieHeaders) {
|
|
|
}
|
|
|
|
|
|
// 生成跳转 HTML
|
|
|
-function generateRedirectHTML(cookieData, targetHost, targetDomain) {
|
|
|
+function generateRedirectHTML(cookieData, targetHost, targetDomain, requestId = '') {
|
|
|
return `
|
|
|
<!DOCTYPE html>
|
|
|
<html lang="zh-CN">
|
|
|
@@ -128,13 +128,24 @@ function generateRedirectHTML(cookieData, targetHost, targetDomain) {
|
|
|
<iframe id="cookieFrame" style="display:none;"></iframe>
|
|
|
<script>
|
|
|
(function() {
|
|
|
+ const requestId = '${requestId}';
|
|
|
const cookies = ${JSON.stringify(cookieData)};
|
|
|
const targetUrl = 'http://${targetHost}/';
|
|
|
const targetDomain = '${targetDomain}';
|
|
|
|
|
|
- console.log('准备设置 Cookie:', cookies);
|
|
|
+ console.log('========================================');
|
|
|
+ console.log('[浏览器端] 自动登录脚本开始执行');
|
|
|
+ console.log('[浏览器端] 请求ID:', requestId);
|
|
|
+ console.log('[浏览器端] 目标URL:', targetUrl);
|
|
|
+ console.log('[浏览器端] 目标域名:', targetDomain);
|
|
|
+ console.log('[浏览器端] Cookie 数量:', cookies.length);
|
|
|
+ console.log('[浏览器端] Cookie 详情:', cookies);
|
|
|
|
|
|
// 方法1: 尝试直接设置 Cookie(可能因为跨域限制而失败)
|
|
|
+ console.log('[浏览器端] 开始尝试设置 Cookie...');
|
|
|
+ let successCount = 0;
|
|
|
+ let failCount = 0;
|
|
|
+
|
|
|
cookies.forEach(function(cookie) {
|
|
|
try {
|
|
|
// 构建 Cookie 字符串
|
|
|
@@ -157,22 +168,46 @@ function generateRedirectHTML(cookieData, targetHost, targetDomain) {
|
|
|
cookieStr += '; domain=' + targetDomain;
|
|
|
|
|
|
document.cookie = cookieStr;
|
|
|
- console.log('尝试设置 Cookie:', cookie.name);
|
|
|
+ console.log('[浏览器端] ✓ 尝试设置 Cookie:', cookie.name);
|
|
|
+ successCount++;
|
|
|
+
|
|
|
+ // 验证 Cookie 是否设置成功
|
|
|
+ const allCookies = document.cookie;
|
|
|
+ if (allCookies.indexOf(cookie.name + '=') !== -1) {
|
|
|
+ console.log('[浏览器端] ✓ Cookie 设置成功:', cookie.name);
|
|
|
+ } else {
|
|
|
+ console.warn('[浏览器端] ⚠ Cookie 可能未设置成功:', cookie.name, '(可能是跨域限制)');
|
|
|
+ }
|
|
|
} catch(e) {
|
|
|
- console.error('设置 Cookie 失败:', cookie.name, e);
|
|
|
+ console.error('[浏览器端] ✗ 设置 Cookie 失败:', cookie.name, e);
|
|
|
+ failCount++;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ console.log('[浏览器端] Cookie 设置结果: 成功 ' + successCount + ', 失败 ' + failCount);
|
|
|
+
|
|
|
// 方法2: 使用隐藏的 iframe 加载目标站点,让服务器设置 Cookie
|
|
|
// 然后跳转到目标站点
|
|
|
+ console.log('[浏览器端] 创建隐藏 iframe 加载目标站点...');
|
|
|
const iframe = document.getElementById('cookieFrame');
|
|
|
+
|
|
|
+ iframe.onload = function() {
|
|
|
+ console.log('[浏览器端] iframe 加载完成');
|
|
|
+ };
|
|
|
+
|
|
|
+ iframe.onerror = function(error) {
|
|
|
+ console.error('[浏览器端] iframe 加载失败:', error);
|
|
|
+ };
|
|
|
+
|
|
|
iframe.src = targetUrl;
|
|
|
|
|
|
// 延迟跳转,确保 iframe 加载完成
|
|
|
setTimeout(function() {
|
|
|
- console.log('跳转到目标站点:', targetUrl);
|
|
|
+ console.log('[浏览器端] 准备跳转到目标站点:', targetUrl);
|
|
|
+ console.log('[浏览器端] 当前页面 Cookie:', document.cookie);
|
|
|
+ console.log('========================================');
|
|
|
window.location.href = targetUrl;
|
|
|
- }, 1000);
|
|
|
+ }, 1500);
|
|
|
})();
|
|
|
</script>
|
|
|
</body>
|
|
|
@@ -429,11 +464,81 @@ app.get('/api/auto-login/:siteId', async (req, res) => {
|
|
|
const duration = Date.now() - startTime;
|
|
|
console.log(`[${requestId}] 总耗时: ${duration}ms`);
|
|
|
console.log('='.repeat(80) + '\n');
|
|
|
- return res.status(500).json({
|
|
|
- success: false,
|
|
|
- message: loginResult.message,
|
|
|
- response: loginResult.response
|
|
|
- });
|
|
|
+
|
|
|
+ // 返回错误页面而不是 JSON
|
|
|
+ const errorHtml = `
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="zh-CN">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>自动登录失败</title>
|
|
|
+ <style>
|
|
|
+ body {
|
|
|
+ display: flex;
|
|
|
+ justify-content: center;
|
|
|
+ align-items: center;
|
|
|
+ height: 100vh;
|
|
|
+ margin: 0;
|
|
|
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
|
+ background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
|
|
+ }
|
|
|
+ .error-container {
|
|
|
+ background: white;
|
|
|
+ padding: 40px;
|
|
|
+ border-radius: 12px;
|
|
|
+ box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
|
|
|
+ max-width: 600px;
|
|
|
+ text-align: center;
|
|
|
+ }
|
|
|
+ .error-icon {
|
|
|
+ font-size: 64px;
|
|
|
+ margin-bottom: 20px;
|
|
|
+ }
|
|
|
+ .error-title {
|
|
|
+ font-size: 24px;
|
|
|
+ color: #e74c3c;
|
|
|
+ margin-bottom: 15px;
|
|
|
+ }
|
|
|
+ .error-message {
|
|
|
+ font-size: 16px;
|
|
|
+ color: #666;
|
|
|
+ margin-bottom: 20px;
|
|
|
+ line-height: 1.6;
|
|
|
+ }
|
|
|
+ .error-details {
|
|
|
+ background: #f8f9fa;
|
|
|
+ padding: 15px;
|
|
|
+ border-radius: 8px;
|
|
|
+ margin-top: 20px;
|
|
|
+ text-align: left;
|
|
|
+ font-size: 14px;
|
|
|
+ color: #555;
|
|
|
+ }
|
|
|
+ .error-details pre {
|
|
|
+ margin: 0;
|
|
|
+ white-space: pre-wrap;
|
|
|
+ word-wrap: break-word;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <div class="error-container">
|
|
|
+ <div class="error-icon">❌</div>
|
|
|
+ <div class="error-title">自动登录失败</div>
|
|
|
+ <div class="error-message">${loginResult.message}</div>
|
|
|
+ <div class="error-details">
|
|
|
+ <strong>请求ID:</strong> ${requestId}<br>
|
|
|
+ <strong>网站:</strong> ${config.name}<br>
|
|
|
+ <strong>详细信息:</strong>
|
|
|
+ <pre>${JSON.stringify(loginResult.response, null, 2)}</pre>
|
|
|
+ </div>
|
|
|
+ <button onclick="window.history.back()" style="margin-top: 20px; padding: 10px 20px; background: #3498db; color: white; border: none; border-radius: 6px; cursor: pointer;">返回</button>
|
|
|
+ </div>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+ `;
|
|
|
+ return res.status(500).send(errorHtml);
|
|
|
}
|
|
|
|
|
|
console.log(`[${requestId}] 登录成功!`);
|
|
|
@@ -445,12 +550,13 @@ app.get('/api/auto-login/:siteId', async (req, res) => {
|
|
|
console.log(`[${requestId}] Cookie ${index + 1}: ${cookie.name} = ${cookie.value.substring(0, 20)}...`);
|
|
|
});
|
|
|
|
|
|
- // 生成跳转 HTML
|
|
|
+ // 生成跳转 HTML(添加更多调试信息)
|
|
|
console.log(`[${requestId}] 生成跳转页面,目标: http://${config.targetHost}/`);
|
|
|
const html = generateRedirectHTML(
|
|
|
cookieData,
|
|
|
config.targetHost,
|
|
|
- config.targetDomain
|
|
|
+ config.targetDomain,
|
|
|
+ requestId
|
|
|
);
|
|
|
|
|
|
// 在响应头中设置 Cookie
|
|
|
@@ -506,11 +612,61 @@ app.get('/api/auto-login', (req, res) => {
|
|
|
|
|
|
// 健康检查端点
|
|
|
app.get('/api/health', (req, res) => {
|
|
|
- res.json({ status: 'ok' });
|
|
|
+ res.json({
|
|
|
+ status: 'ok',
|
|
|
+ timestamp: new Date().toISOString(),
|
|
|
+ port: PORT,
|
|
|
+ configuredSites: Object.keys(autoLoginConfig)
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+// 测试端点 - 用于验证配置
|
|
|
+app.get('/api/test/:siteId', (req, res) => {
|
|
|
+ const { siteId } = req.params;
|
|
|
+ const config = autoLoginConfig[siteId];
|
|
|
+
|
|
|
+ if (!config) {
|
|
|
+ return res.json({
|
|
|
+ success: false,
|
|
|
+ message: `未找到网站ID "${siteId}" 的配置`,
|
|
|
+ availableSites: Object.keys(autoLoginConfig)
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ const envUsername = process.env[config.credentials.envUsername];
|
|
|
+ const envPassword = process.env[config.credentials.envPassword];
|
|
|
+ const credentials = {
|
|
|
+ username: envUsername || config.credentials.username,
|
|
|
+ password: envPassword || config.credentials.password
|
|
|
+ };
|
|
|
+
|
|
|
+ res.json({
|
|
|
+ success: true,
|
|
|
+ siteId,
|
|
|
+ config: {
|
|
|
+ name: config.name,
|
|
|
+ targetBaseUrl: config.targetBaseUrl,
|
|
|
+ loginMethod: config.loginMethod,
|
|
|
+ loginUrl: config.loginUrl,
|
|
|
+ hasCredentials: !!(credentials.username && credentials.password),
|
|
|
+ credentialsSource: envUsername ? '环境变量' : '配置文件',
|
|
|
+ username: credentials.username,
|
|
|
+ passwordLength: credentials.password ? credentials.password.length : 0
|
|
|
+ }
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
app.listen(PORT, '0.0.0.0', () => {
|
|
|
- console.log(`后端服务器运行在 http://0.0.0.0:${PORT}`);
|
|
|
- console.log(`已配置的自动登录网站: ${Object.keys(autoLoginConfig).join(', ') || '无'}`);
|
|
|
- console.log(`自动登录端点格式: http://0.0.0.0:${PORT}/api/auto-login/:siteId`);
|
|
|
+ console.log('\n' + '='.repeat(80));
|
|
|
+ console.log('🚀 后端服务器启动成功!');
|
|
|
+ console.log('='.repeat(80));
|
|
|
+ console.log(`📍 服务器地址: http://0.0.0.0:${PORT}`);
|
|
|
+ console.log(`📍 外部访问: http://222.243.138.146:${PORT}`);
|
|
|
+ console.log(`\n📋 已配置的自动登录网站: ${Object.keys(autoLoginConfig).join(', ') || '无'}`);
|
|
|
+ console.log(`\n🔗 可用端点:`);
|
|
|
+ console.log(` - 健康检查: http://222.243.138.146:${PORT}/api/health`);
|
|
|
+ console.log(` - 测试配置: http://222.243.138.146:${PORT}/api/test/:siteId`);
|
|
|
+ console.log(` - 自动登录: http://222.243.138.146:${PORT}/api/auto-login/:siteId`);
|
|
|
+ console.log(`\n💡 提示: 查看 TROUBLESHOOTING.md 了解问题排查方法`);
|
|
|
+ console.log('='.repeat(80) + '\n');
|
|
|
});
|