132 lines
4.1 KiB
JavaScript
132 lines
4.1 KiB
JavaScript
const express = require('express');
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
|
||
// 配置信息
|
||
const config = {
|
||
port: 3000,
|
||
// 用户信息,可以通过配置文件覆盖
|
||
users: {
|
||
'testuser': 'testpassword',
|
||
'admin': 'adminpass',
|
||
'user1': 'password1'
|
||
},
|
||
// API路径
|
||
apiPath: '/api/checkperm'
|
||
};
|
||
|
||
// 尝试加载配置文件
|
||
try {
|
||
const configPath = path.join(__dirname, 'config.json');
|
||
if (fs.existsSync(configPath)) {
|
||
const fileConfig = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||
// 合并配置
|
||
Object.assign(config, fileConfig);
|
||
console.log('已加载配置文件');
|
||
}
|
||
} catch (error) {
|
||
console.log('加载配置文件失败,使用默认配置:', error.message);
|
||
}
|
||
|
||
const app = express();
|
||
|
||
// 添加中间件解析JSON和表单数据
|
||
app.use(express.json());
|
||
app.use(express.urlencoded({ extended: true }));
|
||
|
||
// 处理认证逻辑的函数
|
||
function handleAuth(username, password, mode) {
|
||
// 验证参数是否存在
|
||
if (!username) {
|
||
console.log('缺少必要参数: username');
|
||
return { status: 400, response: { status: 'error', message: '缺少必要参数: username' } };
|
||
}
|
||
|
||
// 检查用户是否存在
|
||
if (!config.users[username]) {
|
||
console.log(`用户不存在: ${username}`);
|
||
return { status: 401, response: { status: 'error', message: '用户不存在' } };
|
||
}
|
||
|
||
// 根据模式处理请求
|
||
if (mode === 'getpassword') {
|
||
// 密码获取模式 - 用于NTLM认证
|
||
console.log(`返回用户 ${username} 的密码`);
|
||
return {
|
||
status: 200,
|
||
response: {
|
||
status: 'success',
|
||
password: config.users[username]
|
||
}
|
||
};
|
||
} else {
|
||
// 默认为verify模式 - 验证用户名和密码
|
||
if (!password) {
|
||
console.log('缺少必要参数: password');
|
||
return { status: 400, response: { status: 'error', message: '缺少必要参数: password' } };
|
||
}
|
||
|
||
if (config.users[username] === password) {
|
||
console.log('认证成功');
|
||
return {
|
||
status: 200,
|
||
response: {
|
||
status: 'success',
|
||
user: username
|
||
}
|
||
};
|
||
} else {
|
||
console.log('认证失败: 密码不正确');
|
||
return { status: 401, response: { status: 'error', message: '认证失败' } };
|
||
}
|
||
}
|
||
}
|
||
|
||
// 认证API端点 - GET
|
||
app.get(config.apiPath, (req, res) => {
|
||
const { username, password, mode } = req.query;
|
||
|
||
console.log('收到GET认证请求:');
|
||
console.log(`username: ${username}`);
|
||
console.log(`mode: ${mode || 'verify'}`);
|
||
if (password) {
|
||
console.log(`password: ${'*'.repeat(password ? password.length : 0)}`); // 为安全起见不打印实际密码
|
||
}
|
||
|
||
const result = handleAuth(username, password, mode);
|
||
return res.status(result.status).json(result.response);
|
||
});
|
||
|
||
// 认证API端点 - POST
|
||
app.post(config.apiPath, (req, res) => {
|
||
const { username, password, mode } = req.body;
|
||
|
||
console.log('收到POST认证请求:');
|
||
console.log(`username: ${username}`);
|
||
console.log(`mode: ${mode || 'verify'}`);
|
||
if (password) {
|
||
console.log(`password: ${'*'.repeat(password ? password.length : 0)}`); // 为安全起见不打印实际密码
|
||
}
|
||
|
||
const result = handleAuth(username, password, mode);
|
||
return res.status(result.status).json(result.response);
|
||
});
|
||
|
||
// 根路径返回服务信息
|
||
app.get('/', (req, res) => {
|
||
res.send('RDPGW远程认证测试服务已启动');
|
||
});
|
||
|
||
// 启动服务器
|
||
app.listen(config.port, () => {
|
||
console.log(`认证服务器已启动,监听端口: ${config.port}`);
|
||
console.log('当前配置:');
|
||
console.log(`- 端口: ${config.port}`);
|
||
console.log(`- API路径: ${config.apiPath}`);
|
||
console.log(`- 已配置用户数: ${Object.keys(config.users).length}`);
|
||
console.log('\n支持的模式:');
|
||
console.log(`1. 验证模式 (GET): http://localhost:${config.port}${config.apiPath}?username=testuser&password=testpassword&mode=verify`);
|
||
console.log(`2. 密码获取模式 (GET): http://localhost:${config.port}${config.apiPath}?username=testuser&mode=getpassword`);
|
||
console.log('---');
|
||
console.log('POST请求也支持,可以通过请求体发送参数');
|
||
}); |