Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Add SOCKS proxy support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Danilenko committed Mar 21, 2022
1 parent ce994b9 commit 235e91e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"socks-proxy-agent": "^6.2.0-beta.0",
"user-agents": "^1.0.951"
},
"devDependencies": {
Expand Down
27 changes: 22 additions & 5 deletions src/service/random.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosProxyConfig, AxiosRequestConfig } from 'axios';
import https from 'https';
import { sample as _sample } from 'lodash';
import { lastValueFrom } from 'rxjs';
import { SocksProxyAgent } from 'socks-proxy-agent';
import UserAgent from 'user-agents';
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
Expand All @@ -26,20 +27,36 @@ export class RandomService {
* Returns axios config for making request to certain url with certain proxy.
*/
buildRequestConfig(url: string, proxy: AxiosProxyConfig, config: AxiosRequestConfig = {}) {
return {
const requestConfig: AxiosRequestConfig = {
...config,
url,
proxy,
headers: this.randomHeaders(),
// Accept invalid certificates.
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
// Status code validation callback. Valid codes do not throw errors.
validateStatus: (statusCode) => {
// If >500: server is down. We are happy.
// If <300: server responded. We are happy.
return statusCode >= 500 || statusCode < 300;
},
} as AxiosRequestConfig;
};

switch (proxy.protocol) {
case 'socks4':
case 'socks5':
requestConfig.httpsAgent = requestConfig.httpAgent = new SocksProxyAgent(
`${proxy.protocol}://${proxy.host}:${proxy.port}`,
{
timeout: 2000, // 2 seconds is less than total request timeout.
},
);
break;

default:
requestConfig.proxy = proxy;
// Accept invalid certificates.
requestConfig.httpsAgent = new https.Agent({ rejectUnauthorized: false });
}

return requestConfig;
}

randomInt(number: number): number {
Expand Down
29 changes: 13 additions & 16 deletions src/service/uashield.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,19 @@ export class UashieldService {
this.axios.get<Array<ProxyInterface>>(this.config.endpoints.proxy, this.config.axios),
);
// Transform response object to axios-compatible object.
this.axiosProxies = data
// Use only HTTP/HTTPS proxies.
// @TODO: Add support for SOCKS4 & SOCKS5 proxy types.
.filter((proxy) => !proxy.scheme || ['http', 'https'].includes(proxy.scheme.toLowerCase()))
.map((proxy) => {
const [username, password] = proxy.auth?.split(':', 2) ?? [];
const [host, port] = proxy.ip.split(':', 2);
const axiosProxy: AxiosProxyConfig = {
host,
port: port ? parseInt(port) : 443,
};
if (username && password) {
axiosProxy.auth = { username, password };
}
return axiosProxy;
});
this.axiosProxies = data.map((proxy) => {
const [username, password] = proxy.auth?.split(':', 2) ?? [];
const [host, port] = proxy.ip.split(':', 2);
const axiosProxy: AxiosProxyConfig = {
host,
port: +port || 8080,
protocol: proxy.scheme || 'http',
};
if (username && password) {
axiosProxy.auth = { username, password };
}
return axiosProxy;
});
this.logger.debug(`Fetched ${this.axiosProxies.length} proxies from ${this.config.endpoints.proxy}`);
}

Expand Down
35 changes: 34 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ acorn@^8.2.4, acorn@^8.4.1, acorn@^8.5.0, acorn@^8.7.0:
resolved "https://registry.yarnpkg.com/acorn/-/acorn-8.7.0.tgz#90951fde0f8f09df93549481e5fc141445b791cf"
integrity sha512-V/LGr1APy+PXIwKebEWrkZPwoeoF+w1jiOBUmuxuiUIaOHtob8Qc9BTrYo7VuI5fR8tqsy+buA2WFooR5olqvQ==

agent-base@6:
agent-base@6, agent-base@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-6.0.2.tgz#49fff58577cfee3f37176feab4c22e00f86d7f77"
integrity sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==
Expand Down Expand Up @@ -1706,6 +1706,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.1.2.tgz#8b0beeb98605adf1b128fa4386403c009e0221a5"
integrity sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==

cacheable-lookup@~6.0.4:
version "6.0.4"
resolved "https://registry.yarnpkg.com/cacheable-lookup/-/cacheable-lookup-6.0.4.tgz#65c0e51721bb7f9f2cb513aed6da4a1b93ad7dc8"
integrity sha512-mbcDEZCkv2CZF4G01kr8eBd/5agkt9oCqz75tJMSIsquvRZ2sL6Hi5zGVKi/0OSC9oO1GHfJ2AV0ZIOY9vye0A==

call-bind@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/call-bind/-/call-bind-1.0.2.tgz#b1d4e89e688119c3c9a903ad30abb2f6a919be3c"
Expand Down Expand Up @@ -2984,6 +2989,11 @@ interpret@^1.0.0:
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.4.0.tgz#665ab8bc4da27a774a40584e812e3e0fa45b1a1e"
integrity sha512-agE4QfB2Lkp9uICn7BAqoscw4SZP9kTE2hxiFI3jBPmXJfdqiahTbUuKGsMoN2GtqL9AxhYioAcVvgsb1HvRbA==

ip@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/ip/-/ip-1.1.5.tgz#bdded70114290828c0a039e72ef25f5aaec4354a"
integrity sha1-vd7XARQpCCjAoDnnLvJfWq7ENUo=

[email protected]:
version "1.9.1"
resolved "https://registry.yarnpkg.com/ipaddr.js/-/ipaddr.js-1.9.1.tgz#bff38543eeb8984825079ff3a2a8e6cbd46781b3"
Expand Down Expand Up @@ -4614,6 +4624,29 @@ slash@^3.0.0:
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
integrity sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==

smart-buffer@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/smart-buffer/-/smart-buffer-4.2.0.tgz#6e1d71fa4f18c05f7d0ff216dd16a481d0e8d9ae"
integrity sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==

socks-proxy-agent@^6.2.0-beta.0:
version "6.2.0-beta.0"
resolved "https://registry.yarnpkg.com/socks-proxy-agent/-/socks-proxy-agent-6.2.0-beta.0.tgz#f099f38025ce2f0d18a6faf2cf7e0bc2ebb3b79c"
integrity sha512-vJVDGsyaBh7cP8BfynQV1sSqiZ045FkNTyaWLz1g4Ut3sCIuO52sxK0ix8yvqf5n0teDyY1Pw4NRclRiuGTV+w==
dependencies:
agent-base "^6.0.2"
cacheable-lookup "~6.0.4"
debug "^4.3.3"
socks "^2.6.2"

socks@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/socks/-/socks-2.6.2.tgz#ec042d7960073d40d94268ff3bb727dc685f111a"
integrity sha512-zDZhHhZRY9PxRruRMR7kMhnf3I8hDs4S3f9RecfnGxvcBHQcKcIH/oUcEWffsfl1XxdYlA7nnlGbbTvPz9D8gA==
dependencies:
ip "^1.1.5"
smart-buffer "^4.2.0"

[email protected], source-map-support@^0.5.20, source-map-support@^0.5.6, source-map-support@~0.5.20:
version "0.5.21"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.21.tgz#04fe7c7f9e1ed2d662233c28cb2b35b9f63f6e4f"
Expand Down

0 comments on commit 235e91e

Please sign in to comment.