2023-03-14 08:04:10 +00:00
|
|
|
require("dotenv").config();
|
|
|
|
const Koa = require("koa");
|
|
|
|
const bodyParser = require("koa-bodyparser");
|
|
|
|
const cors = require("koa2-cors");
|
|
|
|
const serve = require("koa-static");
|
|
|
|
const views = require("koa-views");
|
|
|
|
|
|
|
|
const app = new Koa();
|
|
|
|
const net = require("net");
|
|
|
|
const router = require("./routes");
|
|
|
|
|
|
|
|
// 配置信息
|
|
|
|
let domain = process.env.ALLOWED_DOMAIN || "*";
|
|
|
|
let port = process.env.PORT || 6688;
|
|
|
|
|
|
|
|
// 解析请求体
|
|
|
|
app.use(bodyParser());
|
|
|
|
|
|
|
|
// 静态文件目录
|
|
|
|
app.use(serve(__dirname + "/public"));
|
|
|
|
app.use(views(__dirname + "/public"));
|
|
|
|
|
|
|
|
// 跨域
|
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: domain,
|
2023-09-06 06:48:27 +00:00
|
|
|
}),
|
2023-03-14 08:04:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
if (domain === "*") {
|
|
|
|
await next();
|
|
|
|
} else {
|
2023-03-20 07:50:16 +00:00
|
|
|
if (ctx.headers.origin === domain || ctx.headers.referer === domain) {
|
|
|
|
await next();
|
|
|
|
} else {
|
|
|
|
ctx.status = 403;
|
2023-03-14 08:04:10 +00:00
|
|
|
ctx.body = {
|
2023-03-20 07:50:16 +00:00
|
|
|
code: 403,
|
2023-03-14 08:04:10 +00:00
|
|
|
message: "请通过正确的域名访问",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 使用路由中间件
|
|
|
|
app.use(router.routes());
|
|
|
|
app.use(router.allowedMethods());
|
|
|
|
|
|
|
|
// 启动应用程序并监听端口
|
|
|
|
const startApp = (port) => {
|
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`成功在 ${port} 端口上运行`);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// 检测端口是否被占用
|
|
|
|
const checkPort = (port) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const server = net
|
|
|
|
.createServer()
|
|
|
|
.once("error", (err) => {
|
|
|
|
if (err.code === "EADDRINUSE") {
|
|
|
|
console.log(`端口 ${port} 已被占用, 正在尝试其他端口...`);
|
|
|
|
server.close();
|
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.once("listening", () => {
|
|
|
|
server.close();
|
|
|
|
resolve(true);
|
|
|
|
})
|
|
|
|
.listen(port);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// 尝试启动应用程序
|
|
|
|
const tryStartApp = async (port) => {
|
|
|
|
let isPortAvailable = await checkPort(port);
|
|
|
|
while (!isPortAvailable) {
|
|
|
|
port++;
|
|
|
|
isPortAvailable = await checkPort(port);
|
|
|
|
}
|
|
|
|
startApp(port);
|
|
|
|
};
|
|
|
|
|
|
|
|
tryStartApp(port);
|