-
Notifications
You must be signed in to change notification settings - Fork 0
/
omni.js
executable file
·205 lines (192 loc) · 8.38 KB
/
omni.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
'use strict';
const network = require('./middleware/network'),
bash = require('./middleware/bash'),
dev = require('./middleware/dev'),
private_info = require('./private_info'),
TelegramBot = require('node-telegram-bot-api'),
omni = new TelegramBot(private_info.token, { polling: true });
let onbanID = new Map(),
bannedID = [];
// BOT function ///////////////////////////////////////////////////////////////////////////////////
let checkID = (hostname, msg) => {
let userID = msg.from.id.toString();
if (userID == private_info.ownerID) { return true }
else if (!onbanID.has(userID)) {
let isBan = false;
for (let id of bannedID){
if (userID == id) { isBan = true }
}
if (isBan) {
console.log(`${hostname} Request from banned user ${userID}`);
omni.sendMessage(msg.chat.id, 'You was bannedID')
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
return false;
} else if (!isBan) {
onbanID.set(userID, 1);
console.log(`${hostname} Unauthorized request from user ID ${userID} !!!`);
omni.sendMessage(private_info.ownerID, `${hostname}\nUnauthorized request from user ID ${userID} !!!`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
omni.sendMessage(msg.chat.id, `Access denied!!!`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
return false;
}
} else if (onbanID.has(userID)) {
let count = parseInt(onbanID.get(userID));
if (count < 3) {
onbanID.set(userID, count+1);
console.log(`${hostname} Unauthorized request from user ID ${userID} !!!`);
omni.sendMessage(private_info.ownerID, `${hostname}\nUnauthorized request from user ID ${userID} !!!`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
omni.sendMessage(msg.chat.id, `Access denied!!!`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
return false;
} else {
onbanID.delete(userID);
bannedID.push(userID);
omni.sendMessage(msg.chat.id, 'You was bannedID')
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
return false;
}
}
}
// getmyid ////////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getmyid/, (msg) => {
network.getHostname()
.then((hostname) => {
console.log(`${hostname} Send user ID ${msg.from.id}`);
if (msg.from.id == private_info.ownerID) {
omni.sendMessage( msg.from.id, `${hostname}\nYour user ID: ${msg.from.id}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
} else {
omni.sendMessage( msg.from.id, `Your user ID: ${msg.from.id}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getip //////////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getip/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
network.updateIP()
.then(ip => {
console.log(`${hostname} Send ip to user: ${msg.from.id}`);
omni.sendMessage(msg.chat.id, `${hostname}\nCurrent ip: ${ip}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch((err) => { console.log(`${hostname} UpdateIP error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// gettop /////////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/gettop/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
bash.getTop()
.then(top => {
console.log(`${hostname} Send top info ${top.replace(/\r?\n/g, "")}`);
omni.sendMessage( private_info.ownerID, `${hostname}\n${top}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch((err) => { console.log(`GetTop error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getuptime //////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getuptime/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
bash.getUptime()
.then(uptime => {
console.log(`${hostname} Send uptime info ${uptime.replace(/\r?\n/g, "")}`);
omni.sendMessage( private_info.ownerID, `${hostname}\nUptime info: ${uptime}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch((err) => { console.log(`GetUptime error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getdisk ////////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getdisk/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
bash.getDisk()
.then(df => {
console.log(`${hostname} Send uptime info ${df.replace(/\r?\n/g, "")}`);
omni.sendMessage( private_info.ownerID, `${hostname}\nDisk info:\n${df}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch((err) => { console.log(`GetUptime error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getfail2ban ////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getfail2ban/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
bash.fail2ban()
.then(banned => {
console.log(`${hostname} Send fail2ban banned IP info: ${banned.replace(/\r?\n/g, "")}`);
omni.sendMessage( private_info.ownerID, `${hostname}\nBanned IP list:\n${banned}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch((err) => { console.log(`GetUptime error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getbanned //////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getbanned/, (msg) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
let banlist = '';
for (let id of bannedID){ banlist += id + '\n' }
console.log(`${hostname} Send banned users info: ${banlist.replace(/\r?\n/g, " ")}`);
omni.sendMessage(msg.chat.id, `${hostname}\nBanned users:\n${banlist}`)
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
// getcam /////////////////////////////////////////////////////////////////////////////////////////
omni.onText(/\/getcam (.+)/, (msg, camera) => {
network.getHostname()
.then((hostname) => {
if (checkID(hostname, msg)) {
dev.getFrame(camera[1])
.then(cam => {
console.log(`${hostname} Send camera ${cam} frame.`);
omni.sendPhoto(msg.chat.id, `./frames/frame${camera[1]}.jpg`,{caption:`${hostname} ${cam}`})
.catch((err) => { console.log(`${hostname} Error: ${err.message}`) });
})
.catch(err => { console.log(`${hostname} Error: ${err}`) });
}
})
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
});
///////////////////////////////////////////////////////////////////////////////////////////////////
// Error handling /////////////////////////////////////////////////////////////////////////////////
omni.on('polling_error', (err) => {
let hostname = '';
network.getHostname()
.then((res) => { hostname = res })
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
console.log(`${hostname} Polling error: ${err.message}`);
});
omni.on('webhook_error', (err) => {
let hostname = '';
network.getHostname()
.then((res) => { hostname = res })
.catch((err) => { console.log(`GetHostname error: ${err.message}`) });
console.log(`${hostname} Webhook error: ${err.message}`);
});