-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartup.js
82 lines (74 loc) · 2.58 KB
/
startup.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
const os = require('os');
const path = require('path');
const util = require('util');
const fs = require('fs-extra');
const request = util.promisify(require('request'));
const { logger } = require('./utils/logger');
const config = require('./config.global');
const tokenPatch = parseInt(config.INDOCKER) ? path.join(config.PATCH_TOKENS, os.hostname()) : config.PATCH_TOKENS;
if (!fs.existsSync(tokenPatch)) {
try {
fs.mkdirSync(tokenPatch, { recursive: true });
} catch (error) {
logger.error(`- Error creating directory ${tokenPatch}: ${error}`);
}
}
class AllSessions {
static async getAllSessions() {
logger?.info(`- Getting sessions to auto start`);
logger?.info(`- Patch: ${tokenPatch}`);
let startup = [];
try {
const files = await fs.readdir(tokenPatch);
files.forEach(file => {
if (file.includes('.startup.json')) {
startup.push(file.split('.')[0]);
}
});
} catch (error) {
logger.error(`- Error getting sessions: ${error}`);
}
return startup;
}
static async startAllSessions() {
const hostUrl = config.IPV4 == '0.0.0.0' ? '127.0.0.1' : config.IPV4;
const host = config.DOMAIN_SSL == '' ? `http://${hostUrl}:${config.PORT}` : `https://${config.DOMAIN_SSL}`;
const dados = await this.getAllSessions();
if (dados?.length) {
for (let item of dados) {
setTimeout(async () => {
try {
const filePath = path.join(tokenPatch, `${item}.startup.json`);
let result = JSON.parse(await fs.readFile(filePath, 'utf-8'));
const resBody = {
"AuthorizationToken": result?.AuthorizationToken,
"SessionName": result?.SessionName,
"setOnline": result?.setOnline ? result?.setOnline : true,
"wh_connect": result?.wh_connect ? result?.wh_connect : null,
"wh_qrcode": result?.wh_qrcode ? result?.wh_qrcode : null,
"wh_status": result?.wh_status ? result?.wh_status : null,
"wh_message": result?.wh_message ? result?.wh_message : null,
"wh_incomingcall": result?.wh_incomingcall ? result?.wh_incomingcall : null
};
const options = {
method: 'POST',
rejectUnauthorized: false,
headers: {
'Content-Type': 'application/json',
AuthorizationToken: parseInt(config.VALIDATE_MYSQL) ? result?.AuthorizationToken : config.SECRET_KEY
},
json: true,
url: `${host}/instance/Start`,
body: resBody
};
await request(options);
logger?.info(`- Start Session Name: ${item}`);
} catch (err) {
logger?.error(`- Error starting session ${item}: ${err}`);
}
}, 2000);
}
}
}
}
module.exports = AllSessions;