182 lines
6.0 KiB
TypeScript
182 lines
6.0 KiB
TypeScript
import express from 'express';
|
|
import type { Request, Response, NextFunction } from 'express';
|
|
|
|
const app = express();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// 中间件:解析 JSON 请求体
|
|
app.use(express.json({ limit: '10mb' }));
|
|
|
|
// 中间件:解析 URL 编码的请求体
|
|
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
|
|
|
// 中间件:解析原始请求体
|
|
app.use(express.raw({ type: '*/*', limit: '10mb' }));
|
|
|
|
// 自定义中间件:打印所有请求的详细信息
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
|
const timestamp = new Date().toISOString();
|
|
|
|
console.log('\n' + '='.repeat(80));
|
|
console.log(`[${timestamp}] 收到请求:`);
|
|
console.log(`方法: ${req.method}`);
|
|
console.log(`路径: ${req.path}`);
|
|
console.log(`完整URL: ${req.originalUrl}`);
|
|
console.log(`查询参数:`, req.query);
|
|
console.log(`请求头:`, req.headers);
|
|
|
|
// 打印请求体(如果存在)
|
|
if (req.body) {
|
|
if (Buffer.isBuffer(req.body)) {
|
|
console.log(`请求体 (原始):`, req.body.toString());
|
|
} else if (typeof req.body === 'object' && Object.keys(req.body).length > 0) {
|
|
console.log(`请求体 (JSON):`, req.body);
|
|
} else if (req.body) {
|
|
console.log(`请求体:`, req.body);
|
|
}
|
|
}
|
|
|
|
console.log(`客户端IP: ${req.ip}`);
|
|
console.log(`User-Agent: ${req.get('User-Agent')}`);
|
|
console.log('='.repeat(80));
|
|
|
|
next();
|
|
});
|
|
|
|
// QQ机器人回显功能
|
|
app.post('/', async (req: Request, res: Response) => {
|
|
try {
|
|
// 检查是否是消息类型的请求
|
|
if (req.body && req.body.post_type === 'message') {
|
|
const { target_id, raw_message, message_type, user_id } = req.body;
|
|
|
|
console.log(`\n[QQ机器人] 收到${message_type}消息,准备回显...`);
|
|
console.log(`发送者ID: ${user_id || target_id}`);
|
|
console.log(`消息内容: ${raw_message}`);
|
|
|
|
// 构建回显消息体
|
|
const replyMessage = {
|
|
user_id: String(target_id || user_id),
|
|
message: [
|
|
{
|
|
type: "text",
|
|
data: {
|
|
text: raw_message || "收到空消息"
|
|
}
|
|
}
|
|
]
|
|
};
|
|
|
|
// 发送回显消息 - 使用请求来源的IP地址
|
|
const clientIP = req.ip || req.connection.remoteAddress || req.socket.remoteAddress;
|
|
// 清理IPv6映射的IPv4地址格式
|
|
const cleanIP = clientIP?.replace(/^::ffff:/, '') || 'localhost';
|
|
const replyUrl = `http://${cleanIP}:30000/send_private_msg`;
|
|
|
|
console.log(`[QQ机器人] 发送回显消息到: ${replyUrl}`);
|
|
console.log(`[QQ机器人] 回显内容:`, JSON.stringify(replyMessage, null, 2));
|
|
|
|
// 使用 fetch 发送回显消息
|
|
const response = await fetch(replyUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(replyMessage)
|
|
});
|
|
|
|
if (response.ok) {
|
|
const responseData = await response.text();
|
|
console.log(`[QQ机器人] 回显成功! 响应:`, responseData);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
message: '消息已回显',
|
|
echo_sent: true,
|
|
original_message: raw_message,
|
|
target_user: target_id || user_id
|
|
});
|
|
} else {
|
|
console.error(`[QQ机器人] 回显失败! HTTP状态:`, response.status);
|
|
const errorText = await response.text();
|
|
console.error(`[QQ机器人] 错误响应:`, errorText);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
message: '消息已接收,但回显失败',
|
|
echo_sent: false,
|
|
error: `HTTP ${response.status}: ${errorText}`,
|
|
original_message: raw_message
|
|
});
|
|
}
|
|
} else {
|
|
// 非消息类型的请求,只记录不回显
|
|
console.log(`\n[QQ机器人] 收到非消息类型请求: ${req.body?.post_type || '未知类型'}`);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
message: '请求已接收(非消息类型,不回显)',
|
|
post_type: req.body?.post_type || 'unknown'
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error(`\n[QQ机器人] 处理请求时发生错误:`, error);
|
|
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '处理请求时发生错误',
|
|
error: error instanceof Error ? error.message : '未知错误'
|
|
});
|
|
}
|
|
});
|
|
|
|
/* // 捕获所有其他路径的处理器
|
|
app.use((req: Request, res: Response) => {
|
|
const responseData = {
|
|
timestamp: new Date().toISOString(),
|
|
method: req.method,
|
|
path: req.path,
|
|
originalUrl: req.originalUrl,
|
|
query: req.query,
|
|
headers: req.headers,
|
|
body: req.body,
|
|
ip: req.ip,
|
|
userAgent: req.get('User-Agent'),
|
|
message: '请求已接收并记录'
|
|
};
|
|
|
|
console.log(`\n[通用处理器] 向客户端返回确认信息`);
|
|
|
|
// 返回 JSON 响应
|
|
res.status(200).json({
|
|
success: true,
|
|
message: '请求已成功接收并记录到控制台',
|
|
data: responseData
|
|
});
|
|
}); */
|
|
|
|
// 错误处理中间件
|
|
app.use((error: any, req: Request, res: Response, next: NextFunction) => {
|
|
const timestamp = new Date().toISOString();
|
|
console.error(`\n[${timestamp}] 错误:`, error);
|
|
|
|
res.status(500).json({
|
|
success: false,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
});
|
|
|
|
// 启动服务器
|
|
app.listen(PORT, () => {
|
|
console.log(`\n🚀 HTTP 调试服务器已启动!`);
|
|
console.log(`📡 监听端口: ${PORT}`);
|
|
console.log(`🌐 访问地址: http://localhost:${PORT}`);
|
|
console.log(`📝 所有请求都会在控制台显示详细信息`);
|
|
console.log(`\n支持的测试方法:`);
|
|
console.log(` GET: curl http://localhost:${PORT}/test`);
|
|
console.log(` POST: curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' http://localhost:${PORT}/api/test`);
|
|
console.log(` PUT: curl -X PUT -H "Content-Type: application/json" -d '{"data":"test"}' http://localhost:${PORT}/update`);
|
|
console.log(` DELETE: curl -X DELETE http://localhost:${PORT}/delete/123`);
|
|
console.log(`\n按 Ctrl+C 停止服务器\n`);
|
|
}); |