-
Notifications
You must be signed in to change notification settings - Fork 972
/
api.js
executable file
·443 lines (404 loc) · 15.6 KB
/
api.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
const http = require('http');
const cluster = require('cluster');
const formidable = require('formidable');
const os = require('os');
const countlyConfig = require('./config', 'dont-enclose');
const plugins = require('../plugins/pluginManager.js');
const jobs = require('./parts/jobs');
const log = require('./utils/log.js')('core:api');
const common = require('./utils/common.js');
const {processRequest} = require('./utils/requestProcessor');
const frontendConfig = require('../frontend/express/config.js');
const {CacheMaster, CacheWorker} = require('./parts/data/cache.js');
const {WriteBatcher, ReadBatcher, InsertBatcher} = require('./parts/data/batcher.js');
const pack = require('../package.json');
const versionInfo = require('../frontend/express/version.info.js');
const moment = require("moment");
var t = ["countly:", "api"];
common.processRequest = processRequest;
if (cluster.isMaster) {
console.log("Starting Countly", "version", versionInfo.version, "package", pack.version);
if (!common.checkDatabaseConfigMatch(countlyConfig.mongodb, frontendConfig.mongodb)) {
log.w('API AND FRONTEND DATABASE CONFIGS ARE DIFFERENT');
}
t.push("master");
t.push("node");
t.push(process.argv[1]);
}
else {
t.push("worker");
t.push("node");
}
// Finaly set the visible title
process.title = t.join(' ');
plugins.connectToAllDatabases().then(function() {
common.writeBatcher = new WriteBatcher(common.db);
common.readBatcher = new ReadBatcher(common.db);
common.insertBatcher = new InsertBatcher(common.db);
if (common.drillDb) {
common.drillReadBatcher = new ReadBatcher(common.drillDb);
}
let workers = [];
/**
* Set Max Sockets
*/
http.globalAgent.maxSockets = countlyConfig.api.max_sockets || 1024;
/**
* Set Plugins APIs Config
*/
plugins.setConfigs("api", {
domain: "",
safe: false,
session_duration_limit: 86400,
country_data: true,
city_data: true,
event_limit: 500,
event_segmentation_limit: 100,
event_segmentation_value_limit: 1000,
array_list_limit: 10,
metric_limit: 1000,
sync_plugins: false,
session_cooldown: 15,
request_threshold: 30,
total_users: true,
export_limit: 10000,
prevent_duplicate_requests: true,
metric_changes: true,
offline_mode: false,
reports_regenerate_interval: 3600,
send_test_email: "",
//data_retention_period: 0,
batch_processing: true,
//batch_on_master: false,
batch_period: 10,
batch_read_processing: true,
//batch_read_on_master: false,
batch_read_ttl: 600,
batch_read_period: 60,
user_merge_paralel: 1,
trim_trailing_ending_spaces: false
});
/**
* Set Plugins APPs Config
*/
plugins.setConfigs("apps", {
country: "TR",
timezone: "Europe/Istanbul",
category: "6"
});
/**
* Set Plugins Security Config
*/
plugins.setConfigs("security", {
login_tries: 3,
login_wait: 5 * 60,
password_min: 8,
password_char: true,
password_number: true,
password_symbol: true,
password_expiration: 0,
password_rotation: 3,
password_autocomplete: true,
robotstxt: "User-agent: *\nDisallow: /",
dashboard_additional_headers: "X-Frame-Options:deny\nX-XSS-Protection:1; mode=block\nStrict-Transport-Security:max-age=31536000 ; includeSubDomains\nX-Content-Type-Options: nosniff",
api_additional_headers: "X-Frame-Options:deny\nX-XSS-Protection:1; mode=block\nAccess-Control-Allow-Origin:*",
dashboard_rate_limit_window: 60,
dashboard_rate_limit_requests: 500,
proxy_hostname: "",
proxy_port: "",
proxy_username: "",
proxy_password: "",
proxy_type: "https"
});
/**
* Set Plugins Logs Config
*/
plugins.setConfigs('logs', {
debug: (countlyConfig.logging && countlyConfig.logging.debug) ? countlyConfig.logging.debug.join(', ') : '',
info: (countlyConfig.logging && countlyConfig.logging.info) ? countlyConfig.logging.info.join(', ') : '',
warn: (countlyConfig.logging && countlyConfig.logging.warn) ? countlyConfig.logging.warn.join(', ') : '',
error: (countlyConfig.logging && countlyConfig.logging.error) ? countlyConfig.logging.error.join(', ') : '',
default: (countlyConfig.logging && countlyConfig.logging.default) ? countlyConfig.logging.default : 'warn',
}, undefined, () => {
const cfg = plugins.getConfig('logs'), msg = {
cmd: 'log',
config: cfg
};
if (process.send) {
process.send(msg);
}
require('./utils/log.js').ipcHandler(msg);
});
/**
* Initialize Plugins
*/
plugins.init();
/**
* Trying to gracefully handle the batch state
* @param {number} code - error code
*/
async function storeBatchedData(code) {
try {
await common.writeBatcher.flushAll();
await common.insertBatcher.flushAll();
console.log("Successfully stored batch state");
}
catch (ex) {
console.log("Could not store batch state");
}
process.exit(code);
}
/**
* Handle before exit for gracefull close
*/
process.on('beforeExit', (code) => {
console.log('Received exit, trying to save batch state: ', code);
storeBatchedData(code);
});
/**
* Handle exit events for gracefull close
*/
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
'SIGBUS', 'SIGFPE', 'SIGSEGV', 'SIGTERM',
].forEach(function(sig) {
process.on(sig, async function() {
storeBatchedData(sig);
console.log('Got signal: ' + sig);
});
});
/**
* Uncaught Exception Handler
*/
process.on('uncaughtException', (err) => {
console.log('Caught exception: %j', err, err.stack);
if (log && log.e) {
log.e('Logging caught exception');
}
console.trace();
storeBatchedData(1);
});
/**
* Unhandled Rejection Handler
*/
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled rejection for %j with reason %j stack ', p, reason, reason ? reason.stack : undefined);
if (log && log.e) {
log.e('Logging unhandled rejection');
}
console.trace();
});
/**
* Pass To Master
* @param {cluster.Worker} worker - worker thatw as spawned by master
*/
const passToMaster = (worker) => {
worker.on('message', (msg) => {
if (msg.cmd === 'log') {
workers.forEach((w) => {
if (w !== worker) {
w.send({
cmd: 'log',
config: msg.config
});
}
});
require('./utils/log.js').ipcHandler(msg);
}
else if (msg.cmd === "checkPlugins") {
plugins.checkPluginsMaster();
}
else if (msg.cmd === "startPlugins") {
plugins.startSyncing();
}
else if (msg.cmd === "endPlugins") {
plugins.stopSyncing();
}
else if (msg.cmd === "batch_insert") {
const {collection, doc, db} = msg.data;
common.insertBatcher.insert(collection, doc, db);
}
else if (msg.cmd === "batch_write") {
const {collection, id, operation, db} = msg.data;
common.writeBatcher.add(collection, id, operation, db);
}
else if (msg.cmd === "batch_read") {
const {collection, query, projection, multi, msgId} = msg.data;
common.readBatcher.get(collection, query, projection, multi).then((data) => {
worker.send({ cmd: "batch_read", data: {msgId, data} });
})
.catch((err) => {
worker.send({ cmd: "batch_read", data: {msgId, err} });
});
}
else if (msg.cmd === "batch_invalidate") {
const {collection, query, projection, multi} = msg.data;
common.readBatcher.invalidate(collection, query, projection, multi);
}
else if (msg.cmd === "dispatchMaster" && msg.event) {
plugins.dispatch(msg.event, msg.data);
}
else if (msg.cmd === "dispatch" && msg.event) {
workers.forEach((w) => {
w.send(msg);
});
}
});
};
if (cluster.isMaster) {
plugins.installMissingPlugins(common.db);
common.runners = require('./parts/jobs/runner');
common.cache = new CacheMaster(common.db);
common.cache.start().then(() => {
setImmediate(() => {
plugins.dispatch('/cache/init', {});
});
}, e => {
console.log(e);
process.exit(1);
});
const workerCount = (countlyConfig.api.workers)
? countlyConfig.api.workers
: os.cpus().length;
for (let i = 0; i < workerCount; i++) {
// there's no way to define inspector port of a worker in the code. So if we don't
// pick a unique port for each worker, they conflict with each other.
let nodeOptions = {};
if (countlyConfig?.symlinked !== true) { // countlyConfig.symlinked is passed when running in a symlinked setup
const inspectorPort = i + 1 + (common?.config?.masterInspectorPort || 9229);
nodeOptions = { NODE_OPTIONS: "--inspect-port=" + inspectorPort };
}
const worker = cluster.fork(nodeOptions);
workers.push(worker);
}
workers.forEach(passToMaster);
cluster.on('exit', (worker) => {
workers = workers.filter((w) => {
return w !== worker;
});
const newWorker = cluster.fork();
workers.push(newWorker);
passToMaster(newWorker);
});
plugins.dispatch("/master", {});
// Allow configs to load & scanner to find all jobs classes
setTimeout(() => {
jobs.job('api:topEvents').replace().schedule('at 00:01 am ' + 'every 1 day');
jobs.job('api:ping').replace().schedule('every 1 day');
jobs.job('api:clear').replace().schedule('every 1 day');
jobs.job('api:clearTokens').replace().schedule('every 1 day');
jobs.job('api:clearAutoTasks').replace().schedule('every 1 day');
jobs.job('api:task').replace().schedule('every 5 minutes');
jobs.job('api:userMerge').replace().schedule('every 10 minutes');
//jobs.job('api:appExpire').replace().schedule('every 1 day');
}, 10000);
//Record as restarted
var utcMoment = moment.utc();
var incObj = {};
incObj.r = 1;
incObj[`d.${utcMoment.format("D")}.${utcMoment.format("H")}.r`] = 1;
common.db.collection("diagnostic").updateOne({"_id": "no-segment_" + utcMoment.format("YYYY:M")}, {"$set": {"m": utcMoment.format("YYYY:M")}, "$inc": incObj}, {"upsert": true}, function(err) {
if (err) {
log.e(err);
}
});
}
else {
console.log("Starting worker", process.pid, "parent:", process.ppid);
const taskManager = require('./utils/taskmanager.js');
common.cache = new CacheWorker(common.db);
common.cache.start();
//since process restarted mark running tasks as errored
taskManager.errorResults({db: common.db});
process.on('message', common.log.ipcHandler);
process.on('message', (msg) => {
if (msg.cmd === 'log') {
common.log.ipcHandler(msg);
}
else if (msg.cmd === "dispatch" && msg.event) {
plugins.dispatch(msg.event, msg.data || {});
}
});
process.on('exit', () => {
console.log('Exiting due to master exited');
});
plugins.dispatch("/worker", {common: common});
http.Server((req, res) => {
const params = {
qstring: {},
res: res,
req: req
};
if (req.method.toLowerCase() === 'post') {
const formidableOptions = {};
if (countlyConfig.api.maxUploadFileSize) {
formidableOptions.maxFileSize = countlyConfig.api.maxUploadFileSize;
}
const form = new formidable.IncomingForm(formidableOptions);
if (/crash_symbols\/(add_symbol|upload_symbol)/.test(req.url)) {
req.body = [];
req.on('data', (data) => {
req.body.push(data);
});
}
else {
req.body = '';
req.on('data', (data) => {
req.body += data;
});
}
let multiFormData = false;
// Check if we have 'multipart/form-data'
if (req.headers['content-type']?.startsWith('multipart/form-data')) {
multiFormData = true;
}
form.parse(req, (err, fields, files) => {
//handle bakcwards compatability with formiddble v1
for (let i in files) {
if (files[i].filepath) {
files[i].path = files[i].filepath;
}
if (files[i].mimetype) {
files[i].type = files[i].mimetype;
}
if (files[i].originalFilename) {
files[i].name = files[i].originalFilename;
}
}
params.files = files;
if (multiFormData) {
let formDataUrl = [];
for (const i in fields) {
params.qstring[i] = fields[i];
formDataUrl.push(`${i}=${fields[i]}`);
}
params.formDataUrl = formDataUrl.join('&');
}
else {
for (const i in fields) {
params.qstring[i] = fields[i];
}
}
if (!params.apiPath) {
processRequest(params);
}
});
}
else if (req.method.toLowerCase() === 'options') {
const headers = {};
headers["Access-Control-Allow-Origin"] = "*";
headers["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS";
headers["Access-Control-Allow-Headers"] = "countly-token, Content-Type";
res.writeHead(200, headers);
res.end();
}
//attempt process GET request
else if (req.method.toLowerCase() === 'get') {
processRequest(params);
}
else {
common.returnMessage(params, 405, "Method not allowed");
}
}).listen(common.config.api.port, common.config.api.host || '').timeout = common.config.api.timeout || 120000;
plugins.loadConfigs(common.db);
}
});