-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
41 lines (32 loc) · 946 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import log from "../services/log";
export enum CaptchaType {
re = 'reCaptcha',
h = 'hCaptcha'
}
export interface SolverOptions {
url: string
sitekey: string
type: CaptchaType
}
export type Solver = (options: SolverOptions) => Promise<string>
const captchaSolvers: { [key: string]: Solver } = {}
export default (): Solver => {
const method = process.env.CAPTCHA_SOLVER
if (!method || method.toLowerCase() == 'none') {
return null;
}
if (!(method in captchaSolvers)) {
try {
captchaSolvers[method] = require('./' + method).default as Solver
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
throw Error(`The solver '${method}' is not a valid captcha solving method.`)
} else {
console.error(e)
throw Error(`An error occurred loading the solver '${method}'.`)
}
}
}
log.info(`Using '${method}' to solve the captcha.`);
return captchaSolvers[method]
}