Merge branch 'fix/download-url-agent' into 'develop'

fix: download-url should use proxy bypass hosts

Co-authored-by: 老周部落 <laozhoubuluo@gmail.com>

See merge request firefish/firefish!10739
This commit is contained in:
naskya 2024-04-21 21:27:55 +00:00
commit 280dddf464
2 changed files with 21 additions and 5 deletions

View file

@ -2,7 +2,7 @@ import * as fs from "node:fs";
import * as stream from "node:stream"; import * as stream from "node:stream";
import * as util from "node:util"; import * as util from "node:util";
import got, * as Got from "got"; import got, * as Got from "got";
import { httpAgent, httpsAgent, StatusError } from "./fetch.js"; import { getAgentByHostname, StatusError } from "./fetch.js";
import config from "@/config/index.js"; import config from "@/config/index.js";
import chalk from "chalk"; import chalk from "chalk";
import Logger from "@/services/logger.js"; import Logger from "@/services/logger.js";
@ -40,10 +40,7 @@ export async function downloadUrl(url: string, path: string): Promise<void> {
send: timeout, send: timeout,
request: operationTimeout, // whole operation timeout request: operationTimeout, // whole operation timeout
}, },
agent: { agent: getAgentByHostname(new URL(url).hostname),
http: httpAgent,
https: httpsAgent,
},
http2: false, // default http2: false, // default
retry: { retry: {
limit: 0, limit: 0,

View file

@ -171,6 +171,25 @@ export function getAgentByUrl(url: URL, bypassProxy = false) {
} }
} }
/**
* Get agent by Hostname
* @param hostname Hostname
* @param bypassProxy Allways bypass proxy
*/
export function getAgentByHostname(hostname: string, bypassProxy = false) {
if (bypassProxy || (config.proxyBypassHosts || []).includes(hostname)) {
return {
http: _http,
https: _https,
};
} else {
return {
http: httpAgent,
https: httpsAgent,
};
}
}
export class StatusError extends Error { export class StatusError extends Error {
public statusCode: number; public statusCode: number;
public statusMessage?: string; public statusMessage?: string;