mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-09 14:58:47 +00:00
Feature: allow disable ipv6 in proxy, refactor cacheFetch to use proxy (#5011)
This commit is contained in:
parent
934ad3a6f1
commit
b4dc53c7c0
10 changed files with 61 additions and 65 deletions
|
@ -3,6 +3,7 @@
|
|||
import { createUnzip, constants as zlibConstants } from "node:zlib";
|
||||
|
||||
import { http, https } from "follow-redirects";
|
||||
import cache from "memory-cache";
|
||||
|
||||
import { addCookieToJar, setCookieHeader } from "./cookie-jar";
|
||||
import { sanitizeErrorURL } from "./api-helpers";
|
||||
|
@ -81,20 +82,46 @@ export function httpRequest(url, params) {
|
|||
return handleRequest(http, url, params);
|
||||
}
|
||||
|
||||
export async function cachedRequest(url, duration = 5, ua = "homepage") {
|
||||
const cached = cache.get(url);
|
||||
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
|
||||
const options = {
|
||||
headers: {
|
||||
"User-Agent": ua,
|
||||
Accept: "application/json",
|
||||
},
|
||||
};
|
||||
let [, , data] = await httpProxy(url, options);
|
||||
if (Buffer.isBuffer(data)) {
|
||||
try {
|
||||
data = JSON.parse(Buffer.from(data).toString());
|
||||
} catch (e) {
|
||||
logger.debug("Error parsing cachedRequest data for %s: %s %s", url, Buffer.from(data).toString(), e);
|
||||
data = Buffer.from(data).toString();
|
||||
}
|
||||
}
|
||||
cache.put(url, data, duration * 1000 * 60);
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function httpProxy(url, params = {}) {
|
||||
const constructedUrl = new URL(url);
|
||||
const disableIpv6 = process.env.HOMEPAGE_PROXY_DISABLE_IPV6 === "true";
|
||||
const agentOptions = disableIpv6 ? { family: 4, autoSelectFamily: false } : {};
|
||||
|
||||
let request = null;
|
||||
if (constructedUrl.protocol === "https:") {
|
||||
request = httpsRequest(constructedUrl, {
|
||||
agent: new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
}),
|
||||
agent: new https.Agent({ ...agentOptions, rejectUnauthorized: false }),
|
||||
...params,
|
||||
});
|
||||
} else {
|
||||
request = httpRequest(constructedUrl, {
|
||||
agent: new http.Agent(),
|
||||
agent: new http.Agent(agentOptions),
|
||||
...params,
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue