2022-09-17 18:27:08 +00:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as stream from 'node:stream';
|
|
|
|
import * as util from 'node:util';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import IPCIDR from 'ip-cidr';
|
|
|
|
import PrivateIp from 'private-ip';
|
|
|
|
import chalk from 'chalk';
|
2023-01-25 03:00:04 +00:00
|
|
|
import got, * as Got from 'got';
|
2023-03-04 07:51:07 +00:00
|
|
|
import { parse } from 'content-disposition';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2023-01-25 03:00:04 +00:00
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { createTemp } from '@/misc/create-temp.js';
|
|
|
|
import { StatusError } from '@/misc/status-error.js';
|
2022-09-18 14:07:41 +00:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
const pipeline = util.promisify(stream.pipeline);
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class DownloadService {
|
2022-09-18 18:11:50 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private httpRequestService: HttpRequestService,
|
2022-09-18 14:07:41 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger = this.loggerService.getLogger('download');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-03-04 07:51:07 +00:00
|
|
|
public async downloadUrl(url: string, path: string): Promise<{
|
|
|
|
filename: string;
|
|
|
|
}> {
|
2022-12-30 03:00:50 +00:00
|
|
|
this.logger.info(`Downloading ${chalk.cyan(url)} to ${chalk.cyanBright(path)} ...`);
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const timeout = 30 * 1000;
|
|
|
|
const operationTimeout = 60 * 1000;
|
|
|
|
const maxSize = this.config.maxFileSize ?? 262144000;
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2023-03-04 07:51:07 +00:00
|
|
|
const urlObj = new URL(url);
|
|
|
|
let filename = urlObj.pathname.split('/').pop() ?? 'untitled';
|
|
|
|
|
2023-01-25 03:00:04 +00:00
|
|
|
const req = got.stream(url, {
|
|
|
|
headers: {
|
|
|
|
'User-Agent': this.config.userAgent,
|
|
|
|
},
|
|
|
|
timeout: {
|
|
|
|
lookup: timeout,
|
|
|
|
connect: timeout,
|
|
|
|
secureConnect: timeout,
|
|
|
|
socket: timeout, // read timeout
|
|
|
|
response: timeout,
|
|
|
|
send: timeout,
|
|
|
|
request: operationTimeout, // whole operation timeout
|
|
|
|
},
|
|
|
|
agent: {
|
|
|
|
http: this.httpRequestService.httpAgent,
|
|
|
|
https: this.httpRequestService.httpsAgent,
|
|
|
|
},
|
|
|
|
http2: false, // default
|
|
|
|
retry: {
|
|
|
|
limit: 0,
|
|
|
|
},
|
2023-02-05 04:51:59 +00:00
|
|
|
enableUnixSockets: false,
|
2023-01-25 03:00:04 +00:00
|
|
|
}).on('response', (res: Got.Response) => {
|
|
|
|
if ((process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') && !this.config.proxy && res.ip) {
|
|
|
|
if (this.isPrivateIp(res.ip)) {
|
|
|
|
this.logger.warn(`Blocked address: ${res.ip}`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2023-01-25 03:00:04 +00:00
|
|
|
const contentLength = res.headers['content-length'];
|
|
|
|
if (contentLength != null) {
|
|
|
|
const size = Number(contentLength);
|
|
|
|
if (size > maxSize) {
|
|
|
|
this.logger.warn(`maxSize exceeded (${size} > ${maxSize}) on response`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 07:51:07 +00:00
|
|
|
|
|
|
|
const contentDisposition = res.headers['content-disposition'];
|
|
|
|
if (contentDisposition != null) {
|
|
|
|
const parsed = parse(contentDisposition);
|
|
|
|
if (parsed.parameters.filename) {
|
|
|
|
filename = parsed.parameters.filename;
|
|
|
|
}
|
|
|
|
}
|
2023-01-25 03:00:04 +00:00
|
|
|
}).on('downloadProgress', (progress: Got.Progress) => {
|
|
|
|
if (progress.transferred > maxSize) {
|
|
|
|
this.logger.warn(`maxSize exceeded (${progress.transferred} > ${maxSize}) on downloadProgress`);
|
|
|
|
req.destroy();
|
|
|
|
}
|
|
|
|
});
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2023-01-25 03:00:04 +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;
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.succ(`Download finished: ${chalk.cyan(url)}`);
|
2023-03-04 07:51:07 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
filename,
|
|
|
|
};
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async downloadTextFile(url: string): Promise<string> {
|
|
|
|
// Create temp file
|
|
|
|
const [path, cleanup] = await createTemp();
|
|
|
|
|
2022-09-18 18:11:50 +00:00
|
|
|
this.logger.info(`text file: Temp file is ${path}`);
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// write content at URL to temp file
|
|
|
|
await this.downloadUrl(url, path);
|
|
|
|
|
|
|
|
const text = await util.promisify(fs.readFile)(path, 'utf8');
|
|
|
|
|
|
|
|
return text;
|
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:03:02 +00:00
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private isPrivateIp(ip: string): boolean {
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const net of this.config.allowedPrivateNetworks ?? []) {
|
|
|
|
const cidr = new IPCIDR(net);
|
|
|
|
if (cidr.contains(ip)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:03:02 +00:00
|
|
|
return PrivateIp(ip) ?? false;
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|