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

Commit

Permalink
Add UASHIELD_TARGETS variable that overrides list of UAShield target …
Browse files Browse the repository at this point in the history
…sites.
  • Loading branch information
Alexander Danilenko committed Mar 18, 2022
1 parent 4fb2bc5 commit 8885f68
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ LOG_RESPONSE_ERROR='false'

# UAShield settings.
UASHIELD_USE_PROXY='true'
## Override target URLs. Needs to be valid JSON array with URLs.
#UASHIELD_TARGETS='["https://kremlin.ru", "https://www.sberbank.ru"]'
UASHIELD_URLS='https://raw.githubusercontent.com/opengs/uashieldtargets/v2/sites.json'
UASHIELD_PROXIES='https://raw.githubusercontent.com/opengs/uashieldtargets/v2/proxy.json'
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ services:
LOG_RESPONSE_ERROR: 'false'
# UAShield settings.
UASHIELD_USE_PROXY: 'true'
## Override target URLs. Needs to be valid JSON array with URLs.
#UASHIELD_TARGETS: '["https://kremlin.ru", "https://www.sberbank.ru"]'
UASHIELD_URLS: 'https://raw.githubusercontent.com/opengs/uashieldtargets/v2/sites.json'
UASHIELD_PROXIES: 'https://raw.githubusercontent.com/opengs/uashieldtargets/v2/proxy.json'
```
Expand Down
2 changes: 2 additions & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ services:
LOG_RESPONSE_ERROR: 'false'
# UAShield settings.
UASHIELD_USE_PROXY: 'true'
## Override target URLs. Needs to be valid JSON array with URLs.
#UASHIELD_TARGETS: '["https://kremlin.ru", "https://www.sberbank.ru"]'
UASHIELD_URLS: 'https://raw.githubusercontent.com/opengs/uashieldtargets/v2/sites.json'
UASHIELD_PROXIES: 'https://raw.githubusercontent.com/opengs/uashieldtargets/v2/proxy.json'
17 changes: 17 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ export function mainConfigValidationSchema() {
UASHIELD_USE_PROXY: Joi.string().valid('true', 'false').default('true'),
UASHIELD_URLS: Joi.string().uri().required(),
UASHIELD_PROXIES: Joi.string().uri().required(),
UASHIELD_TARGETS: Joi.string().custom((value, helpers) => {
const errorMessage: Joi.LanguageMessages = {
custom: 'Needs to be valid JSON string with array of URLs as strings.',
};
try {
// Try to parse json string.
const parsed = JSON.parse(value);
// If not an array.
if (!Array.isArray(parsed)) return helpers.message(errorMessage);
return value;
} catch (e) {
// Catch JSON.parse() errors.
return helpers.message(errorMessage);
}
}),
LOG_SUMMARY_TABLE: Joi.string().valid('true', 'false').default('true'),
LOG_CLEAR: Joi.string().valid('true', 'false').default('false'),
LOG_RESPONSE_SUCCESS: Joi.string().valid('true', 'false').default('false'),
Expand All @@ -42,6 +57,7 @@ export function config() {
urls: process.env.UASHIELD_URLS,
proxy: process.env.UASHIELD_PROXIES,
},
targets: process.env.UASHIELD_TARGETS ? JSON.parse(process.env.UASHIELD_TARGETS) : [],
useProxy: process.env.UASHIELD_USE_PROXY === 'true',
axios: {
timeout: +process.env.UASHIELD_REQUEST_TIMEOUT,
Expand Down Expand Up @@ -76,6 +92,7 @@ export interface UashieldConfigInterface {
urls: string;
proxy: string;
};
targets: Array<string>;
useProxy: boolean;
axios: AxiosRequestConfig;
}
7 changes: 7 additions & 0 deletions src/service/uashield.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export class UashieldService {
*/
@Cron('0 */10 * * * *')
protected async fetchUrls() {
// If custom targets are defined.
if (this.config.targets?.length) {
this.urls = new Set(this.config.targets);
this.logger.debug(`Used ${this.urls.size} URLs from "UASHIELD_TARGETS" variable`);
return;
}

// Retrieve list of hosts from API.
const { data: hostsList } = await lastValueFrom(
this.axios.get<Array<SiteInterface>>(this.config.endpoints.urls, this.config.axios),
Expand Down

0 comments on commit 8885f68

Please sign in to comment.