From be1953b2069fce72969904c1bc18055df73f4b6b Mon Sep 17 00:00:00 2001 From: Mark Dietzer Date: Sun, 13 Dec 2020 13:37:54 -0800 Subject: [PATCH] feat(proxy): fallback to a global proxy list (#1388) --- docs/reference/application.md | 2 +- docs/reference/proxy.md | 20 ++++++++++++++++++++ src/config.ts | 20 +++++++++++++++----- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 docs/reference/proxy.md diff --git a/docs/reference/application.md b/docs/reference/application.md index 6ad6a93..fe32307 100644 --- a/docs/reference/application.md +++ b/docs/reference/application.md @@ -22,7 +22,7 @@ | `WEB_PORT` | Starts a webserver to be able to control the bot while it is running. Setting this value starts this service. | ???+ info - You can also have a list of proxies that are rotated while searching stores. Proxies can be read from a file named `STORENAME.proxies` in the format of `socks5://username:password@ip`; one per line. In this case, there is no need to use the `PROXY_*` environments. + There is more information on proxy settings in the [Proxy documentation](proxy.md). ???+ tip - You can also have a list of proxies that are rotated while searching stores. Proxies can be read from a file named `STORENAME.proxies` in the format of `socks5://username:password@ip`; one per line. diff --git a/docs/reference/proxy.md b/docs/reference/proxy.md new file mode 100644 index 0000000..181503e --- /dev/null +++ b/docs/reference/proxy.md @@ -0,0 +1,20 @@ +# Proxy + +## Filename + +Proxy configuration can be set either per store in a file called `storename.proxies` or globally in `global.proxies` in the streetmerchant root directory. + +If both exist, the store specific file will take precedence. + +## Format + +The format is one proxy per line with the following structure: +`protocol://[user:password@]ip[:port]` + +Supported protocols are `http` and `socks5`. + +Valid examples include: +- `socks5://1.2.3.4:3180` +- `socks5://abcd:efgh@1.2.3.4:5678` +- `http://1.2.3.4:80` +- `http://abcd:efgh@1.2.3.4:8080` diff --git a/src/config.ts b/src/config.ts index 31f8249..9c31848 100644 --- a/src/config.ts +++ b/src/config.ts @@ -147,6 +147,14 @@ function envOrNumberMax( return number ?? 0; } +function loadProxyList(filename: string) { + return readFileSync(`${filename}.proxies`) + .toString() + .trim() + .split('\n') + .map((x) => x.trim()); +} + const browser = { isHeadless: envOrBoolean(process.env.HEADLESS), isIncognito: envOrBoolean(process.env.INCOGNITO, false), @@ -384,13 +392,15 @@ const store = { let proxyList; try { - proxyList = readFileSync(`${name}.proxies`) - .toString() - .trim() - .split('\n') - .map((x) => x.trim()); + proxyList = loadProxyList(name); } catch {} + if (!proxyList) { + try { + proxyList = loadProxyList('global'); + } catch {} + } + return { maxPageSleep: envOrNumberMax( minPageSleep,