2024-06-13 07:38:57 +02:00
|
|
|
import { response } from "@/response";
|
2024-06-27 01:11:39 +02:00
|
|
|
import { getLogger } from "@logtape/logtape";
|
2024-05-06 10:19:42 +02:00
|
|
|
import type { SocketAddress } from "bun";
|
|
|
|
|
import { createMiddleware } from "hono/factory";
|
|
|
|
|
import { matches } from "ip-matching";
|
2024-05-29 02:59:49 +02:00
|
|
|
import { config } from "~/packages/config-manager";
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
const baitFile = async () => {
|
|
|
|
|
const file = Bun.file(config.http.bait.send_file || "./beemovie.txt");
|
|
|
|
|
|
|
|
|
|
if (await file.exists()) {
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 01:11:39 +02:00
|
|
|
const logger = getLogger("server");
|
|
|
|
|
|
|
|
|
|
logger.error`Bait file not found: ${config.http.bait.send_file}`;
|
2024-06-13 07:38:57 +02:00
|
|
|
};
|
|
|
|
|
|
2024-05-06 10:19:42 +02:00
|
|
|
export const bait = createMiddleware(async (context, next) => {
|
2024-06-13 04:26:43 +02:00
|
|
|
const requestIp = context.env?.ip as SocketAddress | undefined | null;
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
if (!config.http.bait.enabled) {
|
|
|
|
|
return await next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const file = await baitFile();
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
if (!file) {
|
|
|
|
|
return await next();
|
|
|
|
|
}
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
// Check for bait IPs
|
|
|
|
|
if (requestIp?.address) {
|
|
|
|
|
for (const ip of config.http.bait.bait_ips) {
|
|
|
|
|
if (matches(ip, requestIp.address)) {
|
|
|
|
|
return response(file);
|
2024-05-06 10:19:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-13 07:38:57 +02:00
|
|
|
}
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
// Check for bait user agents (regex)
|
|
|
|
|
const ua = context.req.header("user-agent") ?? "";
|
2024-05-06 10:19:42 +02:00
|
|
|
|
2024-06-13 07:38:57 +02:00
|
|
|
for (const agent of config.http.bait.bait_user_agents) {
|
|
|
|
|
if (new RegExp(agent).test(ua)) {
|
|
|
|
|
return response(file);
|
2024-05-06 10:19:42 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await next();
|
|
|
|
|
});
|