Skip to content

Commit

Permalink
添加请求类型判断
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-bigniu committed Nov 13, 2023
1 parent 330a3be commit 1c5ff97
Showing 1 changed file with 47 additions and 32 deletions.
79 changes: 47 additions & 32 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,81 @@ const PluginAwsc = require('./plugin/awsc.js')
const app = express();
const decodeRouter = express.Router(); // 创建一个新的Router实例

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());

// 为Router实例添加路由
decodeRouter.post('/v7', (req, res) => {
processDecodeRequest(req, res, PluginSojsonV7);
processDecodeRequest(req, res, PluginSojsonV7);
});

decodeRouter.post('/sojson', (req, res) => {
processDecodeRequest(req, res, PluginSojson);
processDecodeRequest(req, res, PluginSojson);
});

decodeRouter.post('/common', (req, res) => {
processDecodeRequest(req, res, PluginCommon);
processDecodeRequest(req, res, PluginCommon);
});
decodeRouter.post('/jj', (req, res) => {
processDecodeRequest(req, res, PluginJjencode);
processDecodeRequest(req, res, PluginJjencode);
});
decodeRouter.post('/Obfuscator', (req, res) => {
processDecodeRequest(req, res, PluginObfuscator);
processDecodeRequest(req, res, PluginObfuscator);
});
decodeRouter.post('/awsc', (req, res) => {
processDecodeRequest(req, res, PluginAwsc);
processDecodeRequest(req, res, PluginAwsc);
});
// 这个函数可以处理解码请求,减少代码重复
function processDecodeRequest(req, res, Plugin) {
try {
console.log('request come', req.body.code);
const sourceCode = req.body.code;
const decodedCode = Plugin(sourceCode);
if (!decodedCode) {
throw new Error("解码失败");
}
res.status(200).json({
code: 1,
msg: "success",
data: decodedCode
});
} catch (e) {
console.error(e);
res.status(500).json({
code: 0,
msg: e.message
});
}
try {

let sourceCode;
const contentType = req.headers['content-type'];
let code;
if (contentType === 'application/json') {
// 对于 JSON 请求,使用 req.body
sourceCode = req.body.code;
} else if (contentType === 'application/x-www-form-urlencoded') {
// 对于 URL 编码的请求,也使用 req.body
sourceCode = req.body.code;
} else {
// 如果不是这两种类型,发送错误响应
throw new Error("参数错误");
}
console.log('request come', sourceCode);
const decodedCode = Plugin(sourceCode);
if (!decodedCode) {
throw new Error("解码失败");
}
res.status(200).json({
code: 1,
msg: "success",
data: decodedCode
});
} catch (e) {
console.error(e);
res.status(500).json({
code: 0,
msg: e.message
});
}
}

// 使用/decode前缀来使用Router
app.use('/decode', decodeRouter);

// 处理404响应
app.use((req, res) => {
res.status(404).json({
code: 0,
msg: "Not Found"
});
res.status(404).json({
code: 0,
msg: "Not Found"
});
});

const PORT = 8085;
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
console.log('服务启动了~');
console.log(`Server is listening on port ${PORT}`);
console.log('服务启动了~');
});

0 comments on commit 1c5ff97

Please sign in to comment.