FakeUKey/sidecar/src/main.js
2025-02-01 10:19:10 +08:00

101 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const bodyParser = require('body-parser');
const ukey = require("./FakeUKey")
const cors = require('cors');
const Proxy = require("./Proxy")
const Config = require("./Config")
const Version = require("./Version")
const app = express();
// const port = 3000;
const port = 18090;
// 使用body-parser中间件来解析urlencoded格式的请求体
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.all("/", (req, res) => {
res.json({
server : "Fake UKey",
version : Version.version,
"rev": 0
})
})
/**
* 新建 FakeUKey
*/
app.post("/fakeUkey/add", (req, res) => {
Config.newFakeUKey();
res.json({
"rev": 0
})
})
/**
* 删除 FakeUKey
*/
app.post("/fakeUkey/del", (req, res) => {
Config.delFakeUKey(req.body.name);
res.json({
"rev": 0
})
})
app.post('/do', (req, res) => {
// 先尝试代理
let pd = null
try {
pd = Proxy.call(req.body)
}catch (e) {
console.error("proxy error", pd)
}
// 获取请求体中的数据
let requestData = {
"rev": -1
};
let func = ukey[ req.body.order ]
if ( func ){
try {
let ret = func(req.body, pd)
requestData = Object.assign(ret, {
"rev": 0
})
} catch ( e ) {
console.error( e )
requestData.msg = e;
}
}
// 设置响应类型为application/x-json
res.setHeader('Content-Type', 'application/x-json'); // 注意application/x-json不是标准MIME类型通常使用application/json
// 将请求的数据返回为json格式的响应体
res.json(requestData);
});
(async () => {
// long running process running on a given port(s), e.g. a http-server
// takes a number, number[], string or string[]
// single port
// await killPortProcess(8090);
app.listen(port, "127.0.0.1", () => {
console.log(`Server is running on port ${port}`);
}).on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
console.log('端口监听失败。');
}
});
})();