-
Notifications
You must be signed in to change notification settings - Fork 0
/
Raspberry_bot.php
275 lines (222 loc) · 7.36 KB
/
Raspberry_bot.php
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
<?php
/*----------DEFINE-----------*/
define("IP_DELAY", 4);
define("TEMP_DELAY", 15);
define("TEMP_SOGLIA", 60);
define("DDOS_DELAY", 120);
define("POLLING_DELAY", 15);
define("START_DELAY",5);
/*---------TELEGRAM---------*/
$TOKEN = ""; /*your telegram token here*/
$API_URL = "https://api.telegram.org/bot";
$WHITELIST_ID[""] = TRUE; /*your id here (it's possible to add more than one)*/
$CHAT_ID = ; /*your chat id here*/
/*------------VARIABLES---------*/
$current_t0 = 0;
$current_t1 = 0;
$OFFSET = -1;
$maxtemp = -20;
$mintemp = 100;
$temp_flag = FALSE;
$error_counter = 0;
/*------------Start delay--------*/
echo "Starting Raspberry bot";
for($i=0; $i<START_DELAY ; $i++){
if( !($i % 3))
echo ".";
sleep(1);
}
echo "STARTED !\n";
/*-------------------------------*/
/*main loop*/
while(1){
/*wait for incoming messages*/
$json_message = file_get_contents($API_URL.$TOKEN."/getupdates?timeout=".POLLING_DELAY."&offset=$OFFSET");
/*check if there are errors*/
if($json_message == FALSE){
echo "\n[ ERRORE ] Errore di connessione con il server\n";
$error_counter++;
if( $error_counter > 5 )
sleep(120);
else
sleep(6);
}
else
$error_counter = 0;
/*decode the jeson output to string*/
$message = json_decode($json_message, TRUE);
/*If the message isn't empty continue, else wait again*/
if( isset($message["result"][0]) ){
/*Reset user counter after DDOS_DELAY seconds*/
$USER_ID = $message["result"][0]["message"]["from"]["id"];
if( isset($USER_TIMER[$USER_ID]) && time() > $USER_TIMER[$USER_ID]+DDOS_DELAY ){
unset($USER_TIMER[$USER_ID]);
unset($USER_COUNTER[$USER_ID]);
}
$flag = ( isset($USER_COUNTER[$USER_ID]) && $USER_COUNTER[$USER_ID] < 6 );
/*If user made less than 6 request then continue*/
if( $flag || !isset($USER_COUNTER[$USER_ID]) ){
/*read and decode the incoming text message*/
read_incoming_text();
/*If user id is in whitelist then continue*/
if( isset($WHITELIST_ID[$USER_ID]) ){
echo "[AUTORIZZATO] Utente $USER_FIRST_NAME, ID: $USER_ID\n";
echo "[AUTORIZZATO] Comando: ";
select_function();
}
/*else send warnings*/
else {
/*send messages for logs*/
echo "[ NEGATO ] Utente $USER_FIRST_NAME , ID: $USER_ID\n";
echo "[ NEGATO ] Testo inserito: $TEXT\n";
/*allocate the counter and increment it*/
if( isset($USER_COUNTER[$USER_ID]) )
$USER_COUNTER[$USER_ID]++;
else
$USER_COUNTER[$USER_ID] = 1;
switch($USER_COUNTER[$USER_ID]){
case 1:
curl_post("<b>ACCESSO NEGATO</b>",$USER_CHAT_ID);
$USER_TIMER[$USER_ID] = time();
break;
case 2:
curl_post("Cosa di \"<b>ACCESSO NEGATO</b>\" non ti è chiaro?",$USER_CHAT_ID);
break;
case 3:
curl_post("Forse non capisci l'italiano, te lo ripeto in inglese.
\n<b>ACCESS DENIED</b>",$USER_CHAT_ID);
break;
case 4:
curl_post("Ok, proviamo in zulu.
\n<b>ukufinyelela kunqatshelwe</b>",$USER_CHAT_ID);
break;
case 5:
curl_post("Senti $USER_FIRST_NAME , io ci rinuncio, fa quello che vuoi...", $USER_CHAT_ID);
break;
default:
curl_post("Hai esagerato, sei stato <b>bloccato</b> e <b>segnalato</b> all'amministratore", $USER_CHAT_ID);
echo "[ WARNING ] L'utente $USER_FIRST_NAME, ID:$USER_ID, e' stato bloccato";
}
}
unset($USER_CHAT_ID);
unset($USER_FIRST_NAME);
unset($USER_USERNAME);
unset($TEXT);
} else {
/*update the offset when ignoring messages*/
$OFFSET = $message["result"][0]["update_id"] + 1;
}
unset($USER_ID);
}
/*check the system temperature and update the max/min if necessary*/
check_temp($current_t1);
}
/*Selects the right function from given command*/
function select_function(){
global $USER_CHAT_ID, $external_IP, $TEXT, $current_t0, $maxtemp, $mintemp;
switch($TEXT){
case "/ip":
getip($external_IP , $internal_IP, $current_t0);
curl_post("IP esterno: $external_IP\nIP interno: $internal_IP" , $USER_CHAT_ID);
echo "IP\n";
break;
case "/temp":
$temp = gettemp();
curl_post("Temperatura attuale CPU: $temp °C\n".
"Temperatura massima CPU: $maxtemp °C\n".
"Temperatura minima CPU: $mintemp °C\n" , $USER_CHAT_ID);
echo "Temperature\n";
break;
case "/riavvia":
curl_post("Riavvio..." , $USER_CHAT_ID);
echo "Restart\n";
exec("sudo shutdown -r");
break;
case "/spegni":
curl_post("Spengo..." , $USER_CHAT_ID);
echo "Shutdown\n";
exec("sudo shutdown now");
break;
case "/start":
curl_post("Bot avviato" , $USER_CHAT_ID);
echo "Bot started\n";
break;
case "/stato":
curl_post("Tutto bene, grazie" , $USER_CHAT_ID);
echo "Status\n";
break;
default:
curl_post("Comando non riconosciuto" , $USER_CHAT_ID);
echo "Unknow\n";
}
}
/*Sends a text message via POST method using curl*/
function curl_post($text = "", $chat_ID = 0){
global $API_URL, $TOKEN;
$data = array("chat_id" => "$chat_ID", "text" => "$text", "parse_mode" => "HTML");
$curl = curl_init($API_URL.$TOKEN."/sendmessage");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);
}
/*Reads an incoming text message, and updates the offset*/
function read_incoming_text(){
/* set the global variables */
global $OFFSET, $USER_ID, $USER_CHAT_ID, $USER_FIRST_NAME, $USER_USERNAME;
global $TEXT, $message;
/* read and update the offset */
$OFFSET = $message["result"][0]["update_id"] + 1;
/* read the user ID */
$USER_ID = $message["result"][0]["message"]["from"]["id"];
/* read the user chat id*/
$USER_CHAT_ID = $message["result"][0]["message"]["chat"]["id"];
/* read the first name */
$USER_FIRST_NAME = $message["result"][0]["message"]["from"]["first_name"];
/* check if username it's present */
if( isset($message["result"][0]["message"]["from"]["username"]) )
/*if yes save it*/
$USER_USERNAME = $message["result"][0]["message"]["from"]["username"];
/* check a text message it's present */
if( isset($message["result"][0]["message"]["text"]) )
/*if yes save it*/
$TEXT = $message["result"][0]["message"]["text"];
}
/*Update the min/max temp and send an sms in case of overheat*/
function check_temp(&$t1 = 0){
global $mintemp, $maxtemp, $CHAT_ID, $temp_flag;
if( time() >= $t1+TEMP_DELAY ) {
$t1 = time();
$temp = gettemp();
if( $mintemp > $temp )
$mintemp = $temp;
elseif( $maxtemp < $temp ){
$maxtemp = $temp;
if($maxtemp >= TEMP_SOGLIA){
echo "[ WARNING ] Temperatura massima superata ($maxtemp °C)\n";
curl_post("<b>Temperatura massima superata! ($maxtemp °C)</b>" , $CHAT_ID);
$temp_flag = TRUE;
}
}
if( $temp_flag && $temp < TEMP_SOGLIA){
echo "[ OK ] Temperatura normale ($temp °C)\n";
curl_post("Temperatura normale <b>($temp °C)</b>" , $CHAT_ID);
$temp_flag = FALSE;
}
}
}
/*take the local and external IPV4*/
function getip(&$e_IP = NULL , &$i_IP = NULL, &$t0 = 0){
if( time() >= $t0+IP_DELAY ) {
$t0 = time();
$e_IP = exec("curl -s ifconfig.co");
}
$i_IP = exec(' ifconfig | grep -A1 "eth0" | grep -o "inet addr:[0-9]\{1,\}.[0-9]\{1,\}.[0-9]\{1,\}.[0-9]\{1,\}" | grep -o "[0-9]\{1,\}.[0-9]\{1,\}.[0-9]\{1,\}.[0-9]\{1,\}"');
}
/*Get the temperature of CPU and return it as float number*/
function gettemp(){
return exec('vcgencmd measure_temp | grep -o "[0-9]\{1,\}.[0-9]\{1,\}" ');
}
?>
Gabriele Russo 2017