2022-03-23 16:11:22 +01:00
|
|
|
import axios, { AxiosResponse } from 'axios';
|
2021-12-20 23:00:50 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import config from './config';
|
|
|
|
import logger from './logger';
|
2022-03-23 16:11:22 +01:00
|
|
|
import { SocksProxyAgent } from 'socks-proxy-agent';
|
2021-12-20 23:00:50 +01:00
|
|
|
|
|
|
|
const PATH = './';
|
|
|
|
|
|
|
|
class SyncAssets {
|
|
|
|
constructor() { }
|
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
public async syncAssets$() {
|
2021-12-20 23:00:50 +01:00
|
|
|
for (const url of config.MEMPOOL.EXTERNAL_ASSETS) {
|
2022-04-22 10:03:08 +02:00
|
|
|
try {
|
|
|
|
await this.downloadFile$(url);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Failed to download external asset. ` + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
2021-12-20 23:00:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
private async downloadFile$(url: string) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const fileName = url.split('/').slice(-1)[0];
|
2022-03-23 16:11:22 +01:00
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
try {
|
|
|
|
if (config.SOCKS5PROXY.ENABLED) {
|
|
|
|
const socksOptions: any = {
|
|
|
|
agentOptions: {
|
|
|
|
keepAlive: true,
|
|
|
|
},
|
2022-05-12 04:08:41 +02:00
|
|
|
hostname: config.SOCKS5PROXY.HOST,
|
2022-04-22 10:03:08 +02:00
|
|
|
port: config.SOCKS5PROXY.PORT
|
|
|
|
};
|
2022-03-23 16:11:22 +01:00
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
if (config.SOCKS5PROXY.USERNAME && config.SOCKS5PROXY.PASSWORD) {
|
|
|
|
socksOptions.username = config.SOCKS5PROXY.USERNAME;
|
|
|
|
socksOptions.password = config.SOCKS5PROXY.PASSWORD;
|
|
|
|
}
|
2022-03-23 16:11:22 +01:00
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
const agent = new SocksProxyAgent(socksOptions);
|
2022-03-23 16:11:22 +01:00
|
|
|
|
2022-04-22 10:03:08 +02:00
|
|
|
logger.info(`Downloading external asset ${fileName} over the Tor network...`);
|
|
|
|
return axios.get(url, {
|
|
|
|
httpAgent: agent,
|
|
|
|
httpsAgent: agent,
|
|
|
|
responseType: 'stream',
|
|
|
|
timeout: 30000
|
|
|
|
}).then(function (response) {
|
|
|
|
const writer = fs.createWriteStream(PATH + fileName);
|
|
|
|
writer.on('finish', () => {
|
|
|
|
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
|
|
|
resolve(0);
|
|
|
|
});
|
|
|
|
response.data.pipe(writer);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.info(`Downloading external asset ${fileName} over clearnet...`);
|
|
|
|
return axios.get(url, {
|
|
|
|
responseType: 'stream',
|
|
|
|
timeout: 30000
|
|
|
|
}).then(function (response) {
|
|
|
|
const writer = fs.createWriteStream(PATH + fileName);
|
|
|
|
writer.on('finish', () => {
|
|
|
|
logger.info(`External asset ${fileName} saved to ${PATH + fileName}`);
|
|
|
|
resolve(0);
|
|
|
|
});
|
|
|
|
response.data.pipe(writer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (e: any) {
|
|
|
|
reject(e);
|
2022-03-23 16:11:22 +01:00
|
|
|
}
|
2022-04-22 10:03:08 +02:00
|
|
|
});
|
2021-12-20 23:00:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new SyncAssets();
|