-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
327 lines (256 loc) · 16.9 KB
/
index.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
const userAgents = require('random-useragent');
const rateLimit = require("express-rate-limit");
const request = require('request');
const mysql = require('mysql2');
var app = require('express')();
var md5 = require('md5');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.use(function(req, res, next){
var data = "";
req.on('data', function(chunk){ data += chunk})
req.on('end', function(){
req.rawBody = data;
next();
})
})
const limiter = rateLimit({
windowMs: 1000,
max: 3,
message: JSON.stringify({"status" : "error","message": "Вы слишком часто пользуйтесь нашими услугами"})
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const connection = mysql.createConnection({
host : "server_ip",
user : "username",
password : "password",
database : "db_name"
});
const proxyPreset = [
"http://w3bkCQ:TNSQtY@193.31.102.29:9373",
"http://w3bkCQ:TNSQtY@193.31.102.24:9750",
"http://w3bkCQ:TNSQtY@193.31.102.157:9407",
"http://w3bkCQ:TNSQtY@193.31.103.202:9133",
"http://w3bkCQ:TNSQtY@193.31.101.253:9919",
"http://w3bkCQ:TNSQtY@193.31.103.185:9942",
"http://w3bkCQ:TNSQtY@193.31.100.112:9318",
"http://w3bkCQ:TNSQtY@193.31.103.98:9924",
"http://w3bkCQ:TNSQtY@193.31.102.240:9723",
"http://w3bkCQ:TNSQtY@193.31.101.126:9578",
"http://hXESFu:T0GtGm@213.166.73.100:9119",
"http://2jt1k4:WynYDZ@213.166.75.230:9619",
"http://gzYkf9:WgvWbb@91.188.243.144:9290",
"http://fd1Kux:fKJ2cY@46.161.20.80:8000"
]
app.get('/', async function(req, res){
res.sendFile(__dirname + '/index.html');
});
// +----------------------------+
// | Генерация рандомной строки |
// +----------------------------+
function randomString(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
// +---------------------------------------------------------------+
// | Регистрация ключа пользователя для дальнейшего взаимодействия |
// +---------------------------------------------------------------+
app.get('/register_local_user', async function(req, res) {
res.type('json');
var user_agent = req.header('user-agent');
var salt = randomString(50);
var localToken = md5(user_agent + salt);
var user_local_data = {}
connection.query("SELECT * FROM local_tokens WHERE local_tokens.t_value = '" + localToken + "'", async function(err, results, fields) {
if (err) throw err;
if (typeof results !== 'undefined' || results.length == 0) {
connection.query("INSERT INTO `local_tokens` (`t_id`, `t_value`, `t_ua`, `t_date`) VALUES (NULL, '" + localToken + "', '" + user_agent + "', CURRENT_TIMESTAMP);", function(err, results, fields) {
if (err) { console.log(error); } else {
user_local_data['status'] = 'success';
user_local_data['localToken'] = localToken;
res.send(user_local_data);
}})} else {
user_local_data['status'] = 'error';
user_local_data['msg'] = 'Произошла внутренняя ошибка. Локальный токен уже зарегистрирован'
res.send(user_local_data);
} }) });
// +-------------------------------------------------------+
// | Получение сообщений пользователя через локальный ключ |
// +-------------------------------------------------------+
app.get('/get_user_messages', async function(req, res) {
res.type('json');
var user_agent = req.header('user-agent');
var localToken = req.header('localtoken');
var response = {}
connection.query("SELECT * FROM local_tokens WHERE local_tokens.t_value = '" + localToken + "'", async function(err, results, fields) {
if (err) throw err;
if (typeof results !== 'undefined' && results.length > 0) {
connection.query("SELECT * FROM messages WHERE messages.m_user_token = '" + localToken + "'", function(err, results, fields) {
if (err) { console.log(error); } else {
response['status'] = 'success';
response['localToken'] = localToken;
response['messages'] = [];
results.forEach(function(item, i, arr) {
response['messages'].push(item);
});
res.send(response);
}})} else {
response['status'] = 'error';
response['msg'] = 'Произошла внутренняя ошибка. Локальный токен уже зарегистрирован или его никогда не существовало'
res.send(response);
}})});
// +--------------------------------------+
// | Получение синонимов по каждому слову |
// +--------------------------------------+
async function getSynonyms(word) {
return new Promise(async function(resolve) {
var options = {
method : 'POST',
url : 'https://synonymonline.ru/assets/json/synonyms.json',
body : 'word=' + encodeURI(word),
proxy : proxyPreset[Math.floor(Math.random() * proxyPreset.length)],
headers : {
'authority' : 'synonymonline.ru',
'accept' : '*/*',
'x-requested-with' : 'XMLHttpRequest',
'user-agent' : userAgents.getRandom()['userAgent'],
'content-type' : 'application/x-www-form-urlencoded; charset=UTF-8',
'origin' : 'https://synonymonline.ru',
'sec-fetch-site' : 'same-origin',
'sec-fetch-mode' : 'cors',
'sec-fetch-dest' : 'empty',
'referer' : 'https://synonymonline.ru/',
'accept-language' : 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7'
} }
request(options, async function(error, response) {
if (error) {
console.log('ERROR', error);
} else {
return resolve(JSON.parse(response.body)['synonyms']);
}})})}
async function saveMessageDB(content,localToken,timestamp,from) {
connection.query("INSERT INTO `messages` (`m_id`, `m_content`, `m_user_token`, `m_timestamp`, `m_from`) VALUES (NULL, '" + JSON.stringify(content) + "', '" + localToken + "', '" + timestamp + "', '" + from + "')", function(err, results, fields) {
if (err) { console.log(error); }
});
}
// +--------------------------------------+
// | Получение синонимов по каждому слову |
// +--------------------------------------+
async function fetchSynonyms(data) {
var keywords = '';
return new Promise(async function(resolve) {
data.forEach(async function(item, i, arr) {
keywords += (await getSynonyms(item)).join('|');
})
console.log(keywords);
return resolve(keywords);
})}
// +---------------------------------------+
// | Получение к сокетам (прием сообщений) |
// +---------------------------------------+
io.on('connection', async function(socket){
async function EmptyBotResponse(localToken, unix) {
var bot_response = {
'from' : 'system',
'message' : 'Переключаем ваш диалог на оператора. Мы уже занимаемся вашим вопросом, ответим в ближайшее время',
}
saveMessageDB(bot_response, localToken, unix, 'system');
io.to(socket.id).emit('chat message', bot_response);
}
socket.on('chat message', async function(msg,localToken,callback){
if (callback == null || callback == 'undefined' || callback == '') {
callback = null;
}
var unix = Math.round(+new Date() / 1000);
var user_response = {
'from' : 'own',
'message' : msg,
'hash' : md5(randomString(20)),
'time' : new Date().toTimeString().replace(/:[0-9]{2,2} .*/, ''),
'buttons' : null,
'images' : null
}
io.to(socket.id).emit('chat message', user_response);
saveMessageDB(user_response, localToken, unix, 'user');
if (msg.includes('Что ты умеешь') || msg.includes('умеешь') || msg.includes('могешь') || msg.includes('способен')) {
var bot_response = {
'from' : 'bot',
'message' : 'Здравствуйте.У вас возникли вопросы? Мы с удовольствием ответим!',
'hash' : md5(randomString(20)),
'time' : new Date().toTimeString().replace(/:[0-9]{2,2} .*/, ''),
'username' : 'Поддержка',
'avatar' : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAA1VBMVEXx9PZ4AP//TxLy9vZtAP/n4vfx+Pv2wbX/QgD0+fb4//VzAP//RgD7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BDCPTNdnrrhODXhGyNUAzh38GwNcLh6dn2pj9eCaULh4eHp2cWjJgqDIDHTRuIacIQeGwFMUUYLdgMfvDfxWRhBAw3bPbhV0wUxo9o4Gv2+/3RNTYxSSjfwdjn+++uoR/UBGEAbDZjX9/3/V7v/Qdk4qpQAme+Vqvz8RMwcUU4HB1K30j62u0uMnFZOByNln3t7v7+Di5xSRgA+6u+/UZj9zkqcUEoJHDq60x9DWDivFACI18v9nWlr1GroRLnhBFwNPIfDpg5Xw2W+CAUw/OI5yfsVwMmzoQB8NxP3Q+YOBXGwN46HyhRCiNgb/GCWPFhEmOhGI7HOXyQxEgYA5cuwCQfIjES3ozHrSVfGhCPGArF506+/TCJ4X/+6Kw9QNGJ3o340m7FA+bywRHdO3HbngNm+9CIzmX9a3cGzOVDI3p39W9dRSAW0XEntw1VIBax4n6/31UFYhEd78fPXzvq/d70B1fIdY/+/H2i3D+cFYMdtzXa9IdmjDHGGGOMMcYYY4wxxhhjjDHGGGOMMcYYy+4/RSRpe6TUOMMAAAAASUVORK5CYII=',
'buttons' : [
{'title' : 'Расскажите о бонусах'},
{'title' : 'У меня технический вопрос'},
{'title' : 'У меня вопрос по оплате'},
],
'images' : null
}
io.to(socket.id).emit('chat message', bot_response);
saveMessageDB(bot_response, localToken, unix, 'bot');
} else if (msg.includes('Картинка') || msg.includes('Картинка') || msg.includes('Покажи картинку')) {
var bot_response = {
'from' : 'bot',
'message' : 'Держи картинку',
'hash' : md5(randomString(20)),
'time' : new Date().toTimeString().replace(/:[0-9]{2,2} .*/, ''),
'username' : 'Поддержка',
'avatar' : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAA1VBMVEXx9PZ4AP//TxLy9vZtAP/n4vfx+Pv2wbX/QgD0+fb4//VzAP//RgD7TBHy8/a0HQbcNwzyRhDDp/qHNf7oPw7j3PezHADJKwnfOQ3uRA/RMAvl3/ixGwa7IQeHNv6GMP6AB+azCQDBT0O+IwD8c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0wUxo9o4Gv2+/3RNTYxSSjfwdjn+++uoR/UBGEAbDZjX9/3/V7v/Qdk4qpQAme+Vqvz8RMwcUU4HB1K30j62u0uMnFZOByNln3t7v7+Di5xSRgA+6u+/UZj9zkqcUEoJHDq60x9DWDivFACI18v9nWlr1GroRLnhBFwNPIfDpg5Xw2W+CAUw/OI5yfsVwMmzoQB8NxP3Q+YOBXGwN46HyhRCiNgb/GCWPFhEmOhGI7HOXyQxEgYA5cuwCQfIjES3ozHrSVfGhCPGArF506+/TCJ4X/+6Kw9QNGJ3o340m7FA+bywRHdO3HbngNm+9CIzmX9a3cGzOVDI3p39W9dRSAW0XEntw1VIBax4n6/31UFYhEd78fPXzvq/d70B1fIdY/+/H2i3D+cFYMdtzXa9IdmjDHGGGOMMcYYY4wxxhhjjDHGGGOMMcYYy+4/RSRpe6TUOMMAAAAASUVORK5CYII=',
'buttons' : null,
'images' : [
{'thumb' : 'https://sun9-48.userapi.com/VgbLFWry3gFL0I121CojxFo2K7J3kwMRWAYhXw/ZFWfiNNvSAw.jpg' }
]
}
saveMessageDB(bot_response, localToken, unix, 'bot');
io.to(socket.id).emit('chat message', bot_response);
} else {
var user_msg = msg.trim().split(' ');
await fetchSynonyms(user_msg);
var keywordsData = msg.trim().replace(/ /g, '|');
console.log(keywordsData);
// var db_request = 'SELECT * FROM faq WHERE faq.f_question REGEXP "' + keywordsData + '"';
console.log(callback);
if (callback !== null) {
var db_request = 'SELECT * FROM faq WHERE faq.f_id = ' + callback;
} else {
var db_request = 'SELECT * FROM faq WHERE faq.f_question LIKE "%' + msg + '%" AND faq.f_bot_access = 1';
}
connection.query(db_request,
async function(err, results, fields) {
if (err) throw err;
if (typeof results !== 'undefined' && results.length > 0) {
console.log(results);
var bot_response = {
'from' : 'bot',
'message' : results[0]['f_answer'],
'hash' : md5(randomString(20)),
'time' : new Date().toTimeString().replace(/:[0-9]{2,2} .*/, ''),
'username' : 'Поддержка',
'avatar' : 'data:image/png;base64,iVBORw0KGgoAAA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Oyy3EuyZkeYWY72BYTiHgPTgtnxD0kInKJcR9RCv5hLCHTFQOIcQ32+llC5HfwbBMIfQ7GJYlBL4mZBlC9Ee0kiXEXzBDCPTNdnrrhODXhGyNUAzh38GwNcLh6dn2pj9eCaULh4eHp2cWjJgqDIDHTRuIacIQeGwFMUUYLdgMfvDfxWRhBAw3bPbhV0wUxo9o4Gv2+/3RNTYxSSjfwdjn+++uoR/UBGEAbDZjX9/3/V7v/Qdk4qpQAme+Vqvz8RMwcUU4HB1K30j62u0uMnFZOByNln3t7v7+Di5xSRgA+6u+/UZj9zkqcUEoJHDq60x9DWDivFACI18v9nWlr1GroRLnhBFwNPIfDpg5Xw2W+CAUw/OI5yfsVwMmzoQB8NxP3Q+YOBXGwN46HyhRCiNgb/GCWPFhEmOhGI7HOXyQxEgYA5cuwCQfIjES3ozHrSVfGhCPGArF506+/TCJ4X/+6Kw9QNGJ3o340m7FA+bywRHdO3HbngNm+9CIzmX9a3cGzOVDI3p39W9dRSAW0XEntw1VIBax4n6/31UFYhEd78fPXzvq/d70B1fIdY/+/H2i3D+cFYMdtzXa9IdmjDHGGGOMMcYYY4wxxhhjjDHGGGOMMcYYy+4/RSRpe6TUOMMAAAAASUVORK5CYII=',
'buttons' : JSON.parse(results[0]['f_buttons']),
'images' : JSON.parse(results[0]['f_images'])
}
io.to(socket.id).emit('chat message', bot_response);
saveMessageDB(bot_response, localToken, unix, 'bot');
} else { EmptyBotResponse(localToken, unix); }
});
}
});
});
// +----------------+
// | Запуск сервера |
// +----------------+
http.listen(port, async function(){
console.log('Messanger started at port: ' + port);
});