forked from StepanNakonechnyi/vivabill_agent_freebsd
/
fn_ipfw.php
261 lines (192 loc) · 6.49 KB
/
fn_ipfw.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
<?php
function ipfw_start($router, $ch, $connect_url, $connect_api) {
include("config.php");
if ( ipfw_syncro($router, $ch, $connect_url, $connect_api) == "0" ) {
ipfw_syncro_recovery();
}
}
function ipfw_syncro_recovery() {
include("config.php");
$file_ipaddr=$data_dir."ipfw_ipaddr_bi.json";
$file_tables=$data_dir."ipfw_tables_bi.json";
if (file_exists($file_ipaddr)) {
$data = file_get_contents ($file_ipaddr);
$ipaddr_bi = json_decode($data, true);
} else {
$ipaddr_bi = array();
}
if (file_exists($file_tables)) {
$data = file_get_contents ($file_tables);
$tables_bi = json_decode($data, true);
} else {
$tables_bi = array();
}
$ipaddr_os = ipfw_ipaddr_os($tables_bi);
ipfw_ipaddr_comparer($ipaddr_bi, $ipaddr_os);
}
function ipfw_syncro($router, $ch, $connect_url, $connect_api) {
include("config.php");
$sync_success="0";
$tables_bi = ipfw_tables_bi($router, $ch, $connect_url, $connect_api);
if ( $tables_bi > 0 ) {
$ipaddr_os = ipfw_ipaddr_os($tables_bi);
$ipaddr_bi = ipfw_ipaddr_bi($router, $ch, $connect_url, $connect_api);
if ( $ipaddr_bi > 0 ) {
ipfw_ipaddr_comparer($ipaddr_bi, $ipaddr_os);
ipfw_ipaddr_savedata($ipaddr_bi);
ipfw_tables_savedata($tables_bi);
$sync_success="1";
}
}
return $sync_success;
}
function ipfw_ipaddr_os($tables_bi) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
$arr_os = array();
$cou_os = 0;
//Start table cross
foreach ($tables_bi as $num_tables => $num_table) {
unset ($data);
exec($ipfw." table ".$num_table." list", $data);
foreach ($data as $row) {
$rows=explode("/",$row);
$ip=$rows[0];
array_push($arr_os , $num_table.":".$ip);
}
$cou_data=count($data);
if ( $cou_data > 0 ) {
echo message_addlog ($func_role, "Check table ".$num_table.". Avaliable ".$cou_data." IP");
$cou_os = $cou_os+$cou_data;
}
}
$cou_os = count($arr_os);
if ( $cou_os > 0 ) {
echo message_addlog ($func_role, "Get ".$cou_os." IP from OS");
} else {
echo message_addlog ($func_role, "IP list from OS is empty");
}
return $arr_os;
}
// Get list of tables which use in billing -----------------------------------
function ipfw_tables_bi($router, $ch, $connect_url, $connect_api) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
$config_tables = array();
curl_setopt($ch, CURLOPT_URL, $connect_url."get_ipfw_tables");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_router\":\"".$router."\",\"".$connect_api."\":\"1\"}");
$return=curl_exec($ch);
if(curl_errno($ch)) {
echo system_addlog($func_role,"1","Curl error: ". curl_error($ch) );
} else {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode == "200") {
$config_tables = json_decode($return,true);
} else {
$last_url=curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo system_addlog ($func_role,"1","Error [".$httpcode."] ".$last_url);
}
}
$cou_tables = count($config_tables);
if ( $cou_tables > 0 ) {
echo message_addlog ($func_role, "Get ".$cou_tables." Tables from Billing");
} else {
echo message_addlog ($func_role, "Tables list from Billing is empty");
}
return $config_tables;
}
function ipfw_ipaddr_savedata($arr_bi) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
// Save data to OS
$fp = fopen($data_dir."ipfw_ipaddr_bi.json", "w");
fwrite($fp, json_encode($arr_bi));
fclose($fp);
echo message_addlog ($func_role, "Data saved to ipfw_ipaddr_bi.json");
}
function ipfw_tables_savedata($arr_bi) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
// Save data to OS
$fp = fopen($data_dir."ipfw_tables_bi.json", "w");
fwrite($fp, json_encode($arr_bi));
fclose($fp);
echo message_addlog ($func_role, "Data saved to ipfw_tables_bi.json");
}
function ipfw_ipaddr_comparer($arr_bi, $arr_os) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
if ( count($arr_bi)>0 ) {
// Find ip addresess for delete
foreach ($arr_os as $row) {
if (!in_array($row, $arr_bi)) {
$vrow=explode(chr(10),$row);
$srow=explode(":", $vrow[0]);
if ( count($srow) == 2 ) {
echo system_exec_addlog ($func_role, $ipfw." -q -f table ".$srow[0]." delete ".$srow[1]);
}
}
}
// Find ip addresess for add
foreach ($arr_bi as $row) {
if (!in_array($row, $arr_os)) {
$vrow=explode(chr(10),$row);
$srow=explode(":", $vrow[0]);
if ( count($srow) == 2 ) {
echo system_exec_addlog (basename(__FILE__)." ".__FUNCTION__, $ipfw." -q -f table ".$srow[0]." add ".$srow[1]);
}
}
}
echo system_addlog ($func_role, "0", "Compare successfully completed");
} else {
echo message_addlog ($func_role, "Data from Billing is empty");
}
}
function ipfw_ipaddr_bi($router, $ch, $connect_url, $connect_api) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
$config_ipaddr = array();
curl_setopt($ch, CURLOPT_URL, $connect_url."get_ipfw_ipaddr");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_router\":\"".$router."\",\"".$connect_api."\":\"1\"}");
$return=curl_exec($ch);
if(curl_errno($ch)) {
echo system_addlog($func_role,"1","Curl error: ". curl_error($ch) );
} else {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode == "200") {
$config_ipaddr = json_decode($return,true);
} else {
$last_url=curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo system_addlog ($func_role,"1","Error [".$httpcode."] ".$last_url);
}
}
$cou_ipaddr=count($config_ipaddr);
if ( count($cou_ipaddr > 0 )) {
echo message_addlog ($func_role, "Get ".$cou_ipaddr." IP from Billing");
} else {
echo message_addlog ($func_role, "IP list from Billing is empty");
}
return $config_ipaddr;
}
function ipfw_update($router, $ch, $connect_url, $connect_api) {
include("config.php");
$func_role = basename(__FILE__)." ".__FUNCTION__ ;
curl_setopt($ch, CURLOPT_URL, $connect_url."get_ipfw_update");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"id_router\":\"".$router."\",\"".$connect_api."\":\"1\"}");
$return=curl_exec($ch);
if(curl_errno($ch)) {
echo system_addlog($func_role,"1","Curl error: ". curl_error($ch) );
} else {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode == "200") {
$config_ipfw = json_decode($return,true);
foreach ($config_ipfw as $line_num => $line) {
echo system_exec_addlog ($func_role, $ipfw." -q -f table ".$line);
}
} else {
$last_url=curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
echo system_addlog ($func_role,"1","Error [".$httpcode."] ".$last_url);
}
}
}
?>