From 03f7cc1402413145affa03c88fc9ab8b33a70f21 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sat, 6 Sep 2025 06:32:17 +0000 Subject: fix: Fix feed worker to fetch feeds with proxy --- apps/workers/utils.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'apps/workers/utils.ts') diff --git a/apps/workers/utils.ts b/apps/workers/utils.ts index 2f56d3f0..55204570 100644 --- a/apps/workers/utils.ts +++ b/apps/workers/utils.ts @@ -1,3 +1,9 @@ +import { HttpProxyAgent } from "http-proxy-agent"; +import { HttpsProxyAgent } from "https-proxy-agent"; +import fetch from "node-fetch"; + +import serverConfig from "@karakeep/shared/config"; + export function withTimeout( func: (param: T) => Promise, timeoutSec: number, @@ -14,3 +20,52 @@ export function withTimeout( ]); }; } + +function getProxyAgent(url: string) { + const { proxy } = serverConfig; + + if (!proxy.httpProxy && !proxy.httpsProxy) { + return undefined; + } + + const urlObj = new URL(url); + const protocol = urlObj.protocol; + + // Check if URL should bypass proxy + if (proxy.noProxy) { + const noProxyList = proxy.noProxy.split(",").map((host) => host.trim()); + const hostname = urlObj.hostname; + + for (const noProxyHost of noProxyList) { + if ( + noProxyHost === hostname || + (noProxyHost.startsWith(".") && hostname.endsWith(noProxyHost)) || + hostname.endsWith("." + noProxyHost) + ) { + return undefined; + } + } + } + + if (protocol === "https:" && proxy.httpsProxy) { + return new HttpsProxyAgent(proxy.httpsProxy); + } else if (protocol === "http:" && proxy.httpProxy) { + return new HttpProxyAgent(proxy.httpProxy); + } else if (proxy.httpProxy) { + // Fallback to HTTP proxy for HTTPS if HTTPS proxy not configured + return new HttpProxyAgent(proxy.httpProxy); + } + + return undefined; +} + +export const fetchWithProxy = ( + url: string, + options: Record = {}, +) => { + const agent = getProxyAgent(url); + if (agent) { + options.agent = agent; + } + return fetch(url, options); +}; -- cgit v1.2.3-70-g09d2