2019-03-20 14:50:44 -05:00
|
|
|
import * as fs from 'fs';
|
2020-04-11 04:28:40 -05:00
|
|
|
import * as stream from 'stream';
|
|
|
|
import * as util from 'util';
|
2021-09-03 07:00:44 -05:00
|
|
|
import got, * as Got from 'got';
|
|
|
|
import { httpAgent, httpsAgent } from './fetch';
|
2021-08-19 07:55:45 -05:00
|
|
|
import config from '@/config/index';
|
2019-11-24 02:09:32 -06:00
|
|
|
import * as chalk from 'chalk';
|
2021-08-19 07:55:45 -05:00
|
|
|
import Logger from '@/services/logger';
|
2021-09-03 07:00:44 -05:00
|
|
|
import * as IPCIDR from 'ip-cidr';
|
|
|
|
const PrivateIp = require('private-ip');
|
2019-03-20 14:50:44 -05:00
|
|
|
|
2020-04-11 04:28:40 -05:00
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
|
|
|
|
2019-03-20 14:50:44 -05:00
|
|
|
export async function downloadUrl(url: string, path: string) {
|
2019-06-30 13:25:31 -05:00
|
|
|
const logger = new Logger('download');
|
2019-03-20 14:50:44 -05:00
|
|
|
|
2020-04-09 09:42:23 -05:00
|
|
|
logger.info(`Downloading ${chalk.cyan(url)} ...`);
|
|
|
|
|
2021-09-03 07:00:44 -05:00
|
|
|
const timeout = 30 * 1000;
|
|
|
|
const operationTimeout = 60 * 1000;
|
|
|
|
|
|
|
|
const req = got.stream(url, {
|
2020-04-09 09:42:23 -05:00
|
|
|
headers: {
|
|
|
|
'User-Agent': config.userAgent
|
|
|
|
},
|
2021-09-03 07:00:44 -05: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,
|
|
|
|
},
|
|
|
|
retry: 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).on('error', (e: any) => {
|
|
|
|
if (e.name === 'HTTPError') {
|
|
|
|
const statusCode = e.response?.statusCode;
|
|
|
|
const statusMessage = e.response?.statusMessage;
|
|
|
|
e.name = `StatusError`;
|
|
|
|
e.statusCode = statusCode;
|
|
|
|
e.message = `${statusCode} ${statusMessage}`;
|
|
|
|
}
|
2020-04-09 09:42:23 -05:00
|
|
|
});
|
2019-03-20 14:50:44 -05:00
|
|
|
|
2021-09-03 07:00:44 -05:00
|
|
|
await pipeline(req, fs.createWriteStream(path));
|
2019-03-20 14:50:44 -05:00
|
|
|
|
2020-04-11 04:28:40 -05:00
|
|
|
logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
2019-03-20 14:50:44 -05:00
|
|
|
}
|
2021-09-03 07:00:44 -05:00
|
|
|
|
|
|
|
function isPrivateIp(ip: string) {
|
|
|
|
for (const net of config.allowedPrivateNetworks || []) {
|
|
|
|
const cidr = new IPCIDR(net);
|
|
|
|
if (cidr.contains(ip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PrivateIp(ip);
|
|
|
|
}
|