2019-03-20 19:50:44 +00:00
|
|
|
import * as fs from 'fs';
|
2020-04-11 09:28:40 +00:00
|
|
|
import * as stream from 'stream';
|
|
|
|
import * as util from 'util';
|
2021-09-03 12:00:44 +00:00
|
|
|
import got, * as Got from 'got';
|
2021-10-16 08:16:24 +00:00
|
|
|
import { httpAgent, httpsAgent, StatusError } from './fetch';
|
2021-08-19 12:55:45 +00:00
|
|
|
import config from '@/config/index';
|
2019-11-24 08:09:32 +00:00
|
|
|
import * as chalk from 'chalk';
|
2021-08-19 12:55:45 +00:00
|
|
|
import Logger from '@/services/logger';
|
2021-09-03 12:00:44 +00:00
|
|
|
import * as IPCIDR from 'ip-cidr';
|
|
|
|
const PrivateIp = require('private-ip');
|
2019-03-20 19:50:44 +00:00
|
|
|
|
2020-04-11 09:28:40 +00:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2021-12-10 09:24:26 +00:00
|
|
|
export async function downloadUrl(url: string, path: string): Promise<void> {
|
2019-06-30 18:25:31 +00:00
|
|
|
const logger = new Logger('download');
|
2019-03-20 19:50:44 +00:00
|
|
|
|
2020-04-09 14:42:23 +00:00
|
|
|
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
|
|
|
|
2021-09-03 12:00:44 +00:00
|
|
|
const timeout = 30 * 1000;
|
|
|
|
const operationTimeout = 60 * 1000;
|
2021-09-04 11:33:14 +00:00
|
|
|
const maxSize = config.maxFileSize || 262144000;
|
2021-09-03 12:00:44 +00:00
|
|
|
|
|
|
|
const req = got.stream(url, {
|
2020-04-09 14:42:23 +00:00
|
|
|
headers: {
|
2021-12-09 14:58:30 +00:00
|
|
|
'User-Agent': config.userAgent,
|
2020-04-09 14:42:23 +00:00
|
|
|
},
|
2021-09-03 12:00:44 +00:00
|
|
|
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,
|
|
|
|
},
|
2021-10-16 08:16:24 +00:00
|
|
|
http2: false, // default
|
2022-02-03 12:09:15 +00:00
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
2021-09-03 12:00:44 +00:00
|
|
|
}).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-04 11:33:14 +00: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`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
2020-04-09 14:42:23 +00:00
|
|
|
});
|
2019-03-20 19:50:44 +00:00
|
|
|
|
2021-10-16 08:16:24 +00:00
|
|
|
try {
|
|
|
|
await pipeline(req, fs.createWriteStream(path));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Got.HTTPError) {
|
|
|
|
throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2019-03-20 19:50:44 +00:00
|
|
|
|
2020-04-11 09:28:40 +00:00
|
|
|
logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
2019-03-20 19:50:44 +00:00
|
|
|
}
|
2021-09-03 12:00:44 +00:00
|
|
|
|
2022-02-03 12:09:15 +00:00
|
|
|
function isPrivateIp(ip: string): boolean {
|
2021-09-03 12:00:44 +00:00
|
|
|
for (const net of config.allowedPrivateNetworks || []) {
|
|
|
|
const cidr = new IPCIDR(net);
|
|
|
|
if (cidr.contains(ip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrivateIp(ip);
|
|
|
|
}
|