162 lines
5.3 KiB
JavaScript
162 lines
5.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* ═══════════════════════════════════════════════════════════
|
|
* 🏛️ 铸渊主权服务器 · Zhuyuan Sovereign Server
|
|
* ═══════════════════════════════════════════════════════════
|
|
*
|
|
* 编号: ZY-SVR-002
|
|
* 端口: 3800
|
|
* 守护: 铸渊 · ICE-GL-ZY001
|
|
* 版权: 国作登字-2026-A-00037559
|
|
*
|
|
* 新增GitHub/Notion OAuth API
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
const cors = require('cors');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
const https = require('https');
|
|
const { execSync } = require('child_process');
|
|
const emailAuth = require('./modules/email-auth');
|
|
const oauth = require('./modules/oauth-providers');
|
|
const agentHandshake = require('./modules/agent-handshake');
|
|
|
|
// ─── 常量 ───
|
|
const PORT = process.env.PORT || 3800;
|
|
const IS_DEV = process.env.NODE_ENV !== 'production';
|
|
const ZY_BASE_URL = process.env.ZY_BASE_URL || `http://localhost:${PORT}`;
|
|
|
|
// ─── Express 应用 ───
|
|
const app = express();
|
|
app.use(cors({
|
|
origin: IS_DEV ? '*' : ZY_BASE_URL,
|
|
credentials: true
|
|
}));
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
// ─── 健康检查 ───
|
|
app.get('/api/health', (req, res) => {
|
|
res.json({
|
|
status: 'healthy',
|
|
version: '1.0.0',
|
|
server: os.hostname(),
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
});
|
|
|
|
// ─── GitHub OAuth API ───
|
|
app.get('/api/oauth/github', async (req, res) => {
|
|
try {
|
|
const email = req.query.email;
|
|
if (!email) return res.status(400).json({ error: true, message: '缺少邮箱参数' });
|
|
|
|
const url = await oauth.getGitHubOAuthUrl(email);
|
|
res.json({ success: true, url });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/api/oauth/github/callback', async (req, res) => {
|
|
try {
|
|
const { code, state } = req.query;
|
|
if (!code || !state) return res.redirect(`${ZY_BASE_URL}/?error=invalid_params`);
|
|
|
|
const token = await oauth.handleGitHubCallback(code, state);
|
|
res.redirect(`${ZY_BASE_URL}/?token=${token.token}`);
|
|
} catch (err) {
|
|
res.redirect(`${ZY_BASE_URL}/?error=${encodeURIComponent(err.message)}`);
|
|
}
|
|
});
|
|
|
|
// ─── Notion OAuth API ───
|
|
app.get('/api/oauth/notion', async (req, res) => {
|
|
try {
|
|
const email = req.query.email;
|
|
if (!email) return res.status(400).json({ error: true, message: '缺少邮箱参数' });
|
|
|
|
const url = await oauth.getNotionOAuthUrl(email);
|
|
res.json({ success: true, url });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.get('/api/oauth/notion/callback', async (req, res) => {
|
|
try {
|
|
const { code, state } = req.query;
|
|
if (!code || !state) return res.redirect(`${ZY_BASE_URL}/?error=invalid_params`);
|
|
|
|
const token = await oauth.handleNotionCallback(code, state);
|
|
res.redirect(`${ZY_BASE_URL}/?token=${token.token}`);
|
|
} catch (err) {
|
|
res.redirect(`${ZY_BASE_URL}/?error=${encodeURIComponent(err.message)}`);
|
|
}
|
|
});
|
|
|
|
// ─── Agent握手协议API ───
|
|
app.post('/api/agent/handshake/initiate', async (req, res) => {
|
|
try {
|
|
const { initiatorId, targetId, token } = req.body;
|
|
if (!initiatorId || !targetId || !token) {
|
|
return res.status(400).json({ error: true, message: '缺少必要参数' });
|
|
}
|
|
|
|
const result = agentHandshake.initiateHandshake(initiatorId, targetId, token);
|
|
res.json({ success: true, ...result });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/agent/handshake/complete', async (req, res) => {
|
|
try {
|
|
const { handshakeId, targetId, targetKey, token } = req.body;
|
|
if (!handshakeId || !targetId || !targetKey || !token) {
|
|
return res.status(400).json({ error: true, message: '缺少必要参数' });
|
|
}
|
|
|
|
const result = agentHandshake.completeHandshake(handshakeId, targetId, targetKey, token);
|
|
res.json({ success: true, ...result });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/agent/handshake/heartbeat', async (req, res) => {
|
|
try {
|
|
const { connectionId, token } = req.body;
|
|
if (!connectionId || !token) {
|
|
return res.status(400).json({ error: true, message: '缺少必要参数' });
|
|
}
|
|
|
|
const result = agentHandshake.heartbeat(connectionId, token);
|
|
res.json({ success: true, ...result });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
app.post('/api/agent/handshake/disconnect', async (req, res) => {
|
|
try {
|
|
const { connectionId, token } = req.body;
|
|
if (!connectionId || !token) {
|
|
return res.status(400).json({ error: true, message: '缺少必要参数' });
|
|
}
|
|
|
|
const result = agentHandshake.disconnect(connectionId, token);
|
|
res.json({ success: true, ...result });
|
|
} catch (err) {
|
|
res.status(403).json({ error: true, message: err.message });
|
|
}
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`铸渊主权服务器运行中 · 端口 ${PORT} · ${new Date().toISOString()}`);
|
|
}); |