File size: 2,257 Bytes
dd7f5fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import log from './services/log'
import {testWebBrowserInstallation} from "./services/sessions";

const app = require("./app");
const version: string = 'v' + require('../package.json').version
const serverPort: number = Number(process.env.PORT) || 8191
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);
  }
  // todo: fix resolvers
  // try {
  //   getCaptchaSolver();
  // } catch (e) {
  //   log.error(`The environment variable 'CAPTCHA_SOLVER' is wrong. ${e.message}`);
  //   process.exit(1);
  // }
}

// Init
log.info(`FlareSolverr ${version}`);
log.debug('Debug log enabled');

process.on('SIGTERM', () => {
  // Capture signal on Docker Stop #158
  log.info("Process interrupted")
  process.exit(0)
})

process.on('uncaughtException', function(err) {
  // Avoid crashing in NodeJS 17 due to UnhandledPromiseRejectionWarning: Unhandled promise rejection.
  log.error(err)
})

validateEnvironmentVariables();

testWebBrowserInstallation().then(() => {
  // Start server
  app.listen(serverPort, serverHost, () => {
    log.info(`Listening on http://${serverHost}:${serverPort}`);
  })
}).catch(function(e) {
  log.error(e);
  const msg: string = "" + e;
  if (msg.includes('while trying to connect to the browser!')) {
    log.error(`It seems that the system is too slow to run FlareSolverr. 
    If you are running with Docker, try to remove CPU limits in the container. 
    If not, try setting the 'BROWSER_TIMEOUT' environment variable and the 'maxTimeout' parameter to higher values.`);
  }
  process.exit(1);
})