124 lines
4.9 KiB
JavaScript
124 lines
4.9 KiB
JavaScript
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* frps NewProxy 插件验证接口
|
||
* frps 每次创建代理时调用此接口验证 subdomain 归属
|
||
*/
|
||
class FrpController extends Controller {
|
||
async newProxy() {
|
||
const { ctx, app } = this;
|
||
const { op, content } = ctx.request.body;
|
||
|
||
console.log('[Frp Plugin] 收到验证请求:', JSON.stringify(ctx.request.body));
|
||
|
||
if (op !== 'NewProxy') {
|
||
ctx.status = 200;
|
||
ctx.body = { reject: false, unchange: true };
|
||
return;
|
||
}
|
||
|
||
if (!content || !content.subdomain) {
|
||
console.log('[Frp Plugin] 拒绝: 缺少 subdomain');
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: '缺少 subdomain' };
|
||
return;
|
||
}
|
||
|
||
const subdomain = content.subdomain;
|
||
console.log(`[Frp Plugin] 验证 subdomain: ${subdomain}`);
|
||
|
||
// 解析 subdomain: w{warehouse}-c{computerId}
|
||
const match = subdomain.match(/^w(\d+)-c(\d+)$/);
|
||
if (!match) {
|
||
console.log(`[Frp Plugin] 拒绝: subdomain 格式不正确: ${subdomain}`);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: 'subdomain 格式不正确' };
|
||
return;
|
||
}
|
||
|
||
const warehouse = match[1];
|
||
const computerId = match[2];
|
||
console.log(`[Frp Plugin] 解析结果: warehouse=${warehouse}, computerId=${computerId}`);
|
||
|
||
try {
|
||
// 1. 检查 Redis:该电脑是否在线且为录制电脑
|
||
const computerInfoKey = `warehouse:${warehouse}:printer:${computerId}`;
|
||
const cachedInfo = await app.redis.get(computerInfoKey);
|
||
|
||
if (!cachedInfo) {
|
||
console.log(`[Frp Plugin] 拒绝: 电脑不在线 warehouse=${warehouse}, computerId=${computerId}`);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: '电脑不在线' };
|
||
return;
|
||
}
|
||
|
||
const onlineInfo = JSON.parse(cachedInfo);
|
||
if (onlineInfo.is_recording !== 1) {
|
||
console.log(`[Frp Plugin] 拒绝: 电脑不是录制电脑 warehouse=${warehouse}, computerId=${computerId}, is_recording=${onlineInfo.is_recording}`);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: '电脑不是录制电脑' };
|
||
return;
|
||
}
|
||
|
||
// 2. 检查 DB:该电脑是否存在且未被删除
|
||
const computerInfo = await app.mysql.get('wms').get('wms_print_computer', {
|
||
warehouse_id: warehouse,
|
||
id: computerId,
|
||
});
|
||
|
||
if (!computerInfo) {
|
||
console.log(`[Frp Plugin] 拒绝: 数据库中不存在 warehouse=${warehouse}, computerId=${computerId}`);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: '数据库中不存在该电脑' };
|
||
return;
|
||
}
|
||
|
||
if (computerInfo.deleted_at) {
|
||
console.log(`[Frp Plugin] 拒绝: 电脑已删除 warehouse=${warehouse}, computerId=${computerId}`);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: '电脑已被删除' };
|
||
return;
|
||
}
|
||
|
||
// 3. 检查同仓库是否已有其他录制电脑在线
|
||
const warehousePattern = `warehouse:${warehouse}:printer:*`;
|
||
let cursor = '0';
|
||
let hasOtherRecording = false;
|
||
do {
|
||
const [nextCursor, keys] = await app.redis.scan(cursor, 'MATCH', warehousePattern, 'COUNT', 100);
|
||
cursor = nextCursor;
|
||
for (const key of keys) {
|
||
if (key === computerInfoKey) continue;
|
||
const otherData = await app.redis.get(key);
|
||
if (!otherData) continue;
|
||
const other = JSON.parse(otherData);
|
||
if (other.is_recording === 1) {
|
||
console.log(`[Frp Plugin] 拒绝: 同仓库已有其他录制电脑在线: ${key}`);
|
||
hasOtherRecording = true;
|
||
break;
|
||
}
|
||
}
|
||
if (hasOtherRecording) break;
|
||
} while (cursor !== '0');
|
||
|
||
if (hasOtherRecording) {
|
||
ctx.status = 200;
|
||
ctx.body = { reject: true, rejectReason: '同仓库已有其他录制电脑在线' };
|
||
return;
|
||
}
|
||
|
||
// 全部验证通过
|
||
console.log(`[Frp Plugin] 通过: warehouse=${warehouse}, computerId=${computerId}, name=${computerInfo.name}`);
|
||
ctx.status = 200;
|
||
ctx.body = { reject: false, unchange: true };
|
||
|
||
} catch (error) {
|
||
console.error('[Frp Plugin] 验证异常:', error);
|
||
ctx.status = 403;
|
||
ctx.body = { reject: true, rejectReason: `验证异常: ${error.message}` };
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = FrpController;
|