2023-01-13 05:40:33 +01:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import * as stream from "node:stream";
|
|
|
|
import * as util from "node:util";
|
|
|
|
import got, * as Got from "got";
|
|
|
|
import { httpAgent, httpsAgent, StatusError } from "./fetch.js";
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import Logger from "@/services/logger.js";
|
|
|
|
import IPCIDR from "ip-cidr";
|
|
|
|
import PrivateIp from "private-ip";
|
2019-03-20 20:50:44 +01:00
|
|
|
|
2020-04-11 11:28:40 +02:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2021-12-10 10:24:26 +01:00
|
|
|
export async function downloadUrl(url: string, path: string): Promise<void> {
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = new Logger("download");
|
2019-03-20 20:50:44 +01:00
|
|
|
|
2020-04-09 16:42:23 +02:00
|
|
|
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
|
|
|
|
2021-09-03 14:00:44 +02:00
|
|
|
const timeout = 30 * 1000;
|
|
|
|
const operationTimeout = 60 * 1000;
|
2021-09-04 13:33:14 +02:00
|
|
|
const maxSize = config.maxFileSize || 262144000;
|
2021-09-03 14:00:44 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const req = got
|
|
|
|
.stream(url, {
|
|
|
|
headers: {
|
|
|
|
"User-Agent": config.userAgent,
|
|
|
|
},
|
|
|
|
timeout: {
|
|
|
|
lookup: timeout,
|
|
|
|
connect: timeout,
|
|
|
|
secureConnect: timeout,
|
|
|
|
socket: timeout, // read timeout
|
|
|
|
response: timeout,
|
|
|
|
send: timeout,
|
|
|
|
request: operationTimeout, // whole operation timeout
|
|
|
|
},
|
|
|
|
agent: {
|
|
|
|
http: httpAgent,
|
|
|
|
https: httpsAgent,
|
|
|
|
},
|
|
|
|
http2: false, // default
|
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.on("response", (res: Got.Response) => {
|
|
|
|
if (
|
|
|
|
(process.env.NODE_ENV === "production" ||
|
|
|
|
process.env.NODE_ENV === "test") &&
|
|
|
|
!config.proxy &&
|
|
|
|
res.ip
|
|
|
|
) {
|
|
|
|
if (isPrivateIp(res.ip)) {
|
|
|
|
logger.warn(`Blocked address: ${res.ip}`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
2021-09-03 14:00:44 +02:00
|
|
|
}
|
2021-09-04 13:33:14 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const contentLength = res.headers["content-length"];
|
|
|
|
if (contentLength != null) {
|
|
|
|
const size = Number(contentLength);
|
|
|
|
if (size > maxSize) {
|
|
|
|
logger.warn(`maxSize exceeded (${size} > ${maxSize}) on response`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("downloadProgress", (progress: Got.Progress) => {
|
|
|
|
if (progress.transferred > maxSize) {
|
|
|
|
logger.warn(
|
|
|
|
`maxSize exceeded (${progress.transferred} > ${maxSize}) on downloadProgress`,
|
|
|
|
);
|
2021-09-04 13:33:14 +02:00
|
|
|
req.destroy();
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
});
|
2019-03-20 20:50:44 +01:00
|
|
|
|
2021-10-16 10:16:24 +02:00
|
|
|
try {
|
|
|
|
await pipeline(req, fs.createWriteStream(path));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Got.HTTPError) {
|
2023-01-13 05:40:33 +01:00
|
|
|
throw new StatusError(
|
|
|
|
`${e.response.statusCode} ${e.response.statusMessage}`,
|
|
|
|
e.response.statusCode,
|
|
|
|
e.response.statusMessage,
|
|
|
|
);
|
2021-10-16 10:16:24 +02:00
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 20:50:44 +01:00
|
|
|
|
2020-04-11 11:28:40 +02:00
|
|
|
logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
2019-03-20 20:50:44 +01:00
|
|
|
}
|
2021-09-03 14:00:44 +02:00
|
|
|
|
2022-02-03 13:09:15 +01:00
|
|
|
function isPrivateIp(ip: string): boolean {
|
2021-09-03 14:00:44 +02:00
|
|
|
for (const net of config.allowedPrivateNetworks || []) {
|
|
|
|
const cidr = new IPCIDR(net);
|
|
|
|
if (cidr.contains(ip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrivateIp(ip);
|
|
|
|
}
|