forked from Yara724/api
27 lines
852 B
TypeScript
27 lines
852 B
TypeScript
import { ConfigService } from "@nestjs/config";
|
|
import { HttpModuleOptions } from "@nestjs/axios";
|
|
import { SocksProxyAgent } from "socks-proxy-agent";
|
|
|
|
/**
|
|
* Shared HttpModule.registerAsync factory that applies the SOCKS proxy
|
|
* when SOCKS_PROXY_HOST + SOCKS_PROXY_PORT env vars are set.
|
|
* Used by every module that imports HttpModule.
|
|
*/
|
|
export function createHttpModuleOptions(
|
|
configService: ConfigService,
|
|
): HttpModuleOptions | Promise<HttpModuleOptions> {
|
|
const socksHost = configService.get<string>("SOCKS_PROXY_HOST");
|
|
const socksPort = configService.get<string>("SOCKS_PROXY_PORT");
|
|
|
|
if (socksHost && socksPort) {
|
|
const proxyUrl = `socks5://${socksHost}:${socksPort}`;
|
|
const agent = new SocksProxyAgent(proxyUrl);
|
|
return {
|
|
httpsAgent: agent,
|
|
httpAgent: agent,
|
|
proxy: false,
|
|
};
|
|
}
|
|
return {};
|
|
}
|