-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
131 lines (115 loc) · 4.8 KB
/
app.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { createPuppeteerMetrics } = require('./helpers/meter'); // Essential to put it first
const express = require('express');
const puppeteer = require('puppeteer-extra')
const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha')
const StealthPlugin = require('puppeteer-extra-plugin-stealth')
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const AsyncLock = require('async-lock');
const indexRouter = require('./routes/index');
const composeRouter = require('./routes/compose');
const healthCheckRouter = require('./routes/health_check');
const gotoRouter = require('./routes/goto');
const backRouter = require('./routes/goback');
const forwardRouter = require('./routes/goforward');
const clickRouter = require('./routes/click');
const actionRouter = require('./routes/action');
const scrollRouter = require('./routes/scroll');
const screenshotRouter = require('./routes/screenshot');
const recaptchaSolverRouter = require('./routes/recaptcha_solver')
const mhtmlRouter = require('./routes/mhtml');
const harRouter = require('./routes/har');
const fillFormRouter = require('./routes/fill_form');
const closeContextRouter = require('./routes/close_context');
const middlewares = require('./helpers/middlewares');
const timeoutContext = require('./helpers/timeout_context');
const limitContext = require('./helpers/limit_context');
const loggers = require("./helpers/loggers");
const app = express();
const LOG_LEVEL = process.env.LOG_LEVEL || "http";
const LOG_FILE = process.env.LOG_FILE;
const LOGSTASH_HOST = process.env.LOGSTASH_HOST;
const LOGSTASH_PORT = process.env.LOGSTASH_PORT;
const HEADLESS = (process.env.HEADLESS || "true").toLowerCase() === "true";
const CONNECT_TIMEOUT = parseInt(process.env.CONNECT_TIMEOUT) || 180000;
const VIEWPORT_WIDTH = parseInt(process.env.VIEWPORT_WIDTH) || 1280;
const VIEWPORT_HEIGHT = parseInt(process.env.VIEWPORT_HEIGHT) || 720;
const TOKEN_2CAPTCHA = process.env.TOKEN_2CAPTCHA;
const STEALTH_BROWSING = (process.env.STEALTH_BROWSING || "true").toLowerCase() === "true";
const MAX_CONCURRENT_CONTEXTS = process.env.MAX_CONCURRENT_CONTEXTS === "Infinity" ? Infinity : parseInt(process.env.MAX_CONCURRENT_CONTEXTS);
const CONTEXT_TIMEOUT = parseInt(process.env.CONTEXT_TIMEOUT) || 600000; // 10 minutes
async function setupBrowser() {
try {
if (TOKEN_2CAPTCHA) { // If token is given then RecaptchaPlugin is activated
puppeteer.use(
RecaptchaPlugin({
provider: {
id: '2captcha',
token: TOKEN_2CAPTCHA
}
})
)
}
} catch (error) {
console.error('Failed to proceed 2captcha token:', error);
process.exit(1);
}
try {
if (STEALTH_BROWSING) { // Activate or not StealthPlugin
puppeteer.use(StealthPlugin());
}
} catch (error) {
console.error('Failed to enable StealthPlugin:', error);
process.exit(1);
}
try {
//TODO add more params for puppeteer launch
const browser = await puppeteer.launch(
{
headless: HEADLESS,
defaultViewport: { width: VIEWPORT_WIDTH, height: VIEWPORT_HEIGHT },
timeout: CONNECT_TIMEOUT,
args: [
"--no-sandbox",
]
}
);
browser.on('disconnected', setupBrowser);
app.set('browser', browser);
} catch (error) {
process.exit(1);
}
createPuppeteerMetrics(app); // TODO: to check if we can move it to services initialization part
}
// App initialization
(async () => {
await setupBrowser();
app.set('lock', new AsyncLock());
})();
// Services initialization
timeoutContext.initTimeoutContext(CONTEXT_TIMEOUT);
limitContext.initContextCounter(app, MAX_CONCURRENT_CONTEXTS);
loggers.initLogger(LOG_LEVEL, LOG_FILE, LOGSTASH_HOST, LOGSTASH_PORT);
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(middlewares.logHTTPMiddleware());
app.use(bodyParser.raw({ inflate: true, limit: '200kb', type: 'application/javascript' }));
app.use(cookieParser());
app.use('/', indexRouter);
app.use('/compose', composeRouter);
app.use('/health_check', healthCheckRouter);
app.use('/goto', gotoRouter);
app.use('/back', backRouter);
app.use('/forward', forwardRouter);
app.use('/click', clickRouter);
app.use('/action', actionRouter);
app.use('/scroll', scrollRouter);
app.use('/screenshot', screenshotRouter);
app.use('/recaptcha_solver', recaptchaSolverRouter);
app.use('/mhtml', mhtmlRouter);
app.use('/har', harRouter);
app.use('/fill_form', fillFormRouter);
app.use('/close_context', closeContextRouter);
app.use(middlewares.processExceptionMiddleware);
app.use(middlewares.logExceptionMiddleware);
module.exports = app;