Validate environment variables at startup. resolves #101

This commit is contained in:
ngosang 2021-04-04 18:02:17 +02:00
parent c48d342b9c
commit 8aa7723f45
3 changed files with 33 additions and 8 deletions

View File

@ -224,12 +224,12 @@ moment there is nothing setup to do so. If this is something you need feel free
Name | Default | Notes Name | Default | Notes
|--|--|--| |--|--|--|
LOG_LEVEL | info | Used to change the verbosity of the logging. Use `LOG_LEVEL=debug` for more information. LOG_LEVEL | info | Verbosity of the logging. Use `LOG_LEVEL=debug` for more information.
LOG_HTML | false | Used for debugging. If `true` all HTML that passes through the proxy will be logged to the console in `debug` level. LOG_HTML | false | Only for debugging. If `true` all HTML that passes through the proxy will be logged to the console in `debug` level.
PORT | 8191 | Change this if you already have a process running on port `8191`. CAPTCHA_SOLVER | none | Captcha solving method. It used when a captcha is encountered. See the Captcha Solvers section.
HOST | 0.0.0.0 | This shouldn't need to be messed with but if you insist, it's here! HEADLESS | true | Only for debugging. To run the web browser in headless mode or visible.
CAPTCHA_SOLVER | none | This is used to select which captcha solving method it used when a captcha is encountered. PORT | 8191 | Listening port. You don't need to change this if you are running on Docker.
HEADLESS | true | This is used to debug the browser by not running it in headless mode. HOST | 0.0.0.0 | Listening interface. You don't need to change this if you are running on Docker.
Environment variables are set differently depending on the operating system. Some examples: Environment variables are set differently depending on the operating system. Some examples:
* Docker: Take a look at the Docker section in this document. Environment variables can be set in the `docker-compose.yml` file or in the Docker CLI command. * Docker: Take a look at the Docker section in this document. Environment variables can be set in the `docker-compose.yml` file or in the Docker CLI command.

View File

@ -30,12 +30,12 @@ export default (): Solver => {
throw Error(`The solver '${method}' is not a valid captcha solving method.`) throw Error(`The solver '${method}' is not a valid captcha solving method.`)
} else { } else {
console.error(e) console.error(e)
throw Error(`An error occured loading the solver '${method}'.`) throw Error(`An error occurred loading the solver '${method}'.`)
} }
} }
} }
log.info(`Using '${method} to solve the captcha.`); log.info(`Using '${method}' to solve the captcha.`);
return captchaSolvers[method] return captchaSolvers[method]
} }

View File

@ -2,11 +2,33 @@ import log from './log'
import { createServer, IncomingMessage, ServerResponse } from 'http'; import { createServer, IncomingMessage, ServerResponse } from 'http';
import { RequestContext } from './types' import { RequestContext } from './types'
import Router, { BaseAPICall } from './routes' import Router, { BaseAPICall } from './routes'
import getCaptchaSolver from "./captcha";
const version: string = "v" + require('../package.json').version const version: string = "v" + require('../package.json').version
const serverPort: number = Number(process.env.PORT) || 8191 const serverPort: number = Number(process.env.PORT) || 8191
const serverHost: string = process.env.HOST || '0.0.0.0' const serverHost: string = process.env.HOST || '0.0.0.0'
function validateEnvironmentVariables() {
// ip and port variables are validated by nodejs
if (process.env.LOG_LEVEL && ['error', 'warn', 'info', 'verbose', 'debug'].indexOf(process.env.LOG_LEVEL) == -1) {
log.error(`The environment variable 'LOG_LEVEL' is wrong. Check the documentation.`);
process.exit(1);
}
if (process.env.LOG_HTML && ['true', 'false'].indexOf(process.env.LOG_HTML) == -1) {
log.error(`The environment variable 'LOG_HTML' is wrong. Check the documentation.`);
process.exit(1);
}
if (process.env.HEADLESS && ['true', 'false'].indexOf(process.env.HEADLESS) == -1) {
log.error(`The environment variable 'HEADLESS' is wrong. Check the documentation.`);
process.exit(1);
}
try {
getCaptchaSolver();
} catch (e) {
log.error(`The environment variable 'CAPTCHA_SOLVER' is wrong. ${e.message}`);
process.exit(1);
}
}
function errorResponse(errorMsg: string, res: ServerResponse, startTimestamp: number) { function errorResponse(errorMsg: string, res: ServerResponse, startTimestamp: number) {
log.error(errorMsg) log.error(errorMsg)
@ -64,6 +86,9 @@ function validateIncomingRequest(ctx: RequestContext, params: BaseAPICall) {
return true return true
} }
// init
validateEnvironmentVariables();
createServer((req: IncomingMessage, res: ServerResponse) => { createServer((req: IncomingMessage, res: ServerResponse) => {
const startTimestamp = Date.now() const startTimestamp = Date.now()