Skip to content

Commit

Permalink
feat: support proxy for btsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
divyam234 committed Oct 18, 2024
1 parent 0d232a2 commit 0bff2ee
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 11 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM node:alpine AS build
FROM node:alpine AS builder
RUN apk add --no-cache ca-certificates && update-ca-certificates
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
Expand All @@ -10,7 +11,8 @@ RUN pnpm run build:server

FROM ghcr.io/tgdrive/node
WORKDIR /app
COPY --from=build /app/build ./build
COPY --from=builder /app/build ./build
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENV NODE_ENV=production
EXPOSE 8080
ENTRYPOINT [ "node", "build/server/index.mjs" ]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"bencode": "^4.0.0",
"clsx": "^2.1.1",
"fast-xml-parser": "^4.5.0",
"feaxios": "^0.0.22",
"feaxios": "^0.0.23",
"filesize": "^10.1.6",
"framer-motion": "^11.11.9",
"hono": "^4.6.5",
Expand All @@ -46,6 +46,7 @@
"react-hot-toast": "^2.4.1",
"tailwindcss-animate": "^1.0.7",
"uint8-util": "^2.2.5",
"undici": "^6.20.1",
"usehooks-ts": "^3.1.0",
"valibot": "^0.42.1",
"zod": "^3.23.8",
Expand Down
23 changes: 16 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion server/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { env } from "hono/adapter";
import { logger } from "hono/logger";
import { serve } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static";
import app from "@/server/app";
import { config } from "dotenv";
import path from "node:path";
import { fileURLToPath } from "node:url";
import IndexRouter from "./routes";
import { getProxyAgent } from "./utils/proxy-agent";
import type { HonoBinding } from "@/types";
import type { ProxyAgent } from "undici";

config({ path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../.env") });

declare module "hono" {
interface ContextVariableMap {
proxyAgent: ProxyAgent | null;
}
}

const app = new Hono<HonoBinding>({ strict: false }).basePath("/");

app.use(logger());

app.use(
"/api/*",
cors({
origin: "*",
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["*"],
maxAge: 86400,
}),
);

app.use("/api/*", (c, next) => {
c.env = env(c);
c.set("proxyAgent", getProxyAgent(c.env.PROXY_URL));
return next();
});

app.route("/api", IndexRouter);

app
.use("*", async (c, next) => {
await next();
Expand Down
7 changes: 7 additions & 0 deletions server/routes/btsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ router.get("/", verifyAuth(), async (c) => {
const { q, page, orderBy, category } = result.output;

try {
const fetchOptions = {
dispatcher: c.var.proxyAgent,
};
const reqPromises = [
axios.get("https://bt4gprx.com/search", {
params: {
Expand All @@ -97,13 +100,17 @@ router.get("/", verifyAuth(), async (c) => {
category,
page: "rss",
},
//@ts-ignore
fetchOptions,
}),
axios.get("https://bt4gprx.com/search", {
params: {
q,
category,
orderby: orderBy,
},
//@ts-ignore
fetchOptions,
}),
];
const responses = await Promise.all(reqPromises);
Expand Down
24 changes: 24 additions & 0 deletions server/utils/proxy-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ProxyAgent } from "undici";
import { getRuntimeKey } from "hono/adapter";

let proxyAgent: ProxyAgent | null = null;

function createProxyAgent(proxyUrl: string) {
const url = new URL(proxyUrl);

const token = `Basic ${Buffer.from(`${url.username}:${url.password}`).toString("base64")}`;

const proxyAgent = new ProxyAgent({
uri: url.origin,
token,
});

return proxyAgent;
}

export function getProxyAgent(proxyUrl?: string) {
const runtime = getRuntimeKey();
if (runtime === "node" && !proxyAgent && proxyUrl) proxyAgent = createProxyAgent(proxyUrl);

return proxyAgent;
}
1 change: 1 addition & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type HonoBinding = {
Bindings: {
DEBRID_TOKEN: string;
FORWARD_IP: string;
PROXY_URL: string;
};
};

Expand Down

0 comments on commit 0bff2ee

Please sign in to comment.