-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
radius.php
439 lines (425 loc) · 20.2 KB
/
radius.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill)
* by https://t.me/ibnux
*
* Authorize
* - Voucher activation
* Authenticate
* - is it allow to login
* Accounting
* - log
**/
header("Content-Type: application/json");
include "init.php";
$action = $_SERVER['HTTP_X_FREERADIUS_SECTION'];
if (empty($action)) {
$action = _get('action');
}
$code = 200;
//debug
// if (!empty($action)) {
// file_put_contents("$action.json", json_encode([
// 'header' => $_SERVER,
// 'get' => $_GET,
// 'post' => $_POST,
// 'time' => time()
// ]));
// }
try {
switch ($action) {
case 'authenticate':
$username = _req('username');
$password = _req('password');
$CHAPassword = _req('CHAPassword');
$CHAPchallenge = _req('CHAPchallenge');
$isCHAP = false;
if (!empty($CHAPassword)) {
$c = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY username = '$username'")->find_one();
if ($c) {
if (Password::chap_verify($c['password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['password'];
$isVoucher = false;
$isCHAP = true;
} else if (!empty($c['pppoe_password']) && Password::chap_verify($c['pppoe_password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['pppoe_password'];
$isVoucher = false;
$isCHAP = true;
} else {
// check if voucher
if (Password::chap_verify($username, $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
// no password is voucher
if (Password::chap_verify('', $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
}
} else {
$c = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY `pppoe_username` = '$username'")->find_one();
if ($c) {
if (Password::chap_verify($c['password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['password'];
$isVoucher = false;
$isCHAP = true;
} else if (!empty($c['pppoe_password']) && Password::chap_verify($c['pppoe_password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['pppoe_password'];
$isVoucher = false;
$isCHAP = true;
} else {
// check if voucher
if (Password::chap_verify($username, $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
// no password is voucher
if (Password::chap_verify('', $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
}
}
}
} else {
if (!empty($username) && empty($password)) {
// Voucher with empty password
$isVoucher = true;
$password = $username;
} else if (empty($username) || empty($password)) {
show_radius_result([
"control:Auth-Type" => "Reject",
"reply:Reply-Message" => 'Login invalid......'
], 401);
}
}
if ($username == $password) {
$username = Text::alphanumeric($username, "-_.,");
$d = ORM::for_table('tbl_voucher')->whereRaw("BINARY code = '$username'")->find_one();
} else {
$d = ORM::for_table('tbl_customers')->whereRaw("BINARY username = '$username'")->find_one();
if ($d['password'] != $password) {
if ($d['pppoe_password'] != $password) {
unset($d);
}
}
}
if ($d) {
header("HTTP/1.1 204 No Content");
die();
} else {
show_radius_result([
"control:Auth-Type" => "Reject",
"reply:Reply-Message" => 'Login invalid......'
], 401);
}
break;
case 'authorize':
$username = _req('username');
$password = _req('password');
$isVoucher = ($username == $password);
$CHAPassword = _req('CHAPassword');
$CHAPchallenge = _req('CHAPchallenge');
$isCHAP = false;
if (!empty($CHAPassword)) {
$c = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY username = '$username'")->find_one();
if ($c) {
if (Password::chap_verify($c['password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['password'];
$isVoucher = false;
$isCHAP = true;
} else if (!empty($c['pppoe_password']) && Password::chap_verify($c['pppoe_password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['pppoe_password'];
$isVoucher = false;
$isCHAP = true;
} else {
// check if voucher
if (Password::chap_verify($username, $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
// no password is voucher
if (Password::chap_verify('', $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
}
} else {
$c = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY `pppoe_username` = '$username'")->find_one();
if ($c) {
if (Password::chap_verify($c['password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['password'];
$isVoucher = false;
$isCHAP = true;
} else if (!empty($c['pppoe_password']) && Password::chap_verify($c['pppoe_password'], $CHAPassword, $CHAPchallenge)) {
$password = $c['pppoe_password'];
$isVoucher = false;
$isCHAP = true;
} else {
// check if voucher
if (Password::chap_verify($username, $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
// no password is voucher
if (Password::chap_verify('', $CHAPassword, $CHAPchallenge)) {
$isVoucher = true;
$password = $username;
} else {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
}
}
}
} else {
if (!empty($username) && empty($password)) {
// Voucher with empty password
$isVoucher = true;
$password = $username;
} else if (empty($username) || empty($password)) {
show_radius_result([
"control:Auth-Type" => "Reject",
"reply:Reply-Message" => 'Login invalid......'
], 401);
}
}
$tur = ORM::for_table('tbl_user_recharges')->whereRaw("BINARY username = '$username'")->find_one();
if ($tur) {
if (!$isVoucher && !$isCHAP) {
$d = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY username = '$username'")->find_one();
if ($d) {
if ($d['password'] != $password) {
if ($d['pppoe_password'] != $password) {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
} else {
$d = ORM::for_table('tbl_customers')->select('password')->select('pppoe_password')->whereRaw("BINARY `pppoe_username` = '$username'")->find_one();
if ($d) {
if ($d['password'] != $password) {
if ($d['pppoe_password'] != $password) {
show_radius_result(['Reply-Message' => 'Username or Password is wrong'], 401);
}
}
}
}
}
process_radiust_rest($tur, $code);
} else {
if ($isVoucher) {
$username = Text::alphanumeric($username, "-_.,");
$v = ORM::for_table('tbl_voucher')->whereRaw("BINARY code = '$username'")->where('routers', 'radius')->find_one();
if ($v) {
if ($v['status'] == 0) {
if (Package::rechargeUser(0, $v['routers'], $v['id_plan'], "Voucher", $username)) {
$v->status = "1";
$v->used_date = date('Y-m-d H:i:s');
$v->save();
$tur = ORM::for_table('tbl_user_recharges')->whereRaw("BINARY username = '$username'")->find_one();
if ($tur) {
process_radiust_rest($tur, $code);
} else {
show_radius_result(['Reply-Message' => 'Voucher activation failed'], 401);
}
} else {
show_radius_result(['Reply-Message' => 'Voucher activation failed.'], 401);
}
} else {
show_radius_result(['Reply-Message' => 'Voucher Expired...'], 401);
}
} else {
show_radius_result(['Reply-Message' => 'Voucher Expired..'], 401);
}
} else {
show_radius_result(['Reply-Message' => 'Internet Plan Expired..'], 401);
}
}
break;
case 'accounting':
$username = _req('username');
if (empty($username)) {
show_radius_result([
"control:Auth-Type" => "Reject",
"reply:Reply-Message" => 'Username empty'
], 200);
die();
}
header("HTTP/1.1 200 ok");
$d = ORM::for_table('rad_acct')
->whereRaw("BINARY username = '$username'")
->where('acctsessionid', _post('acctsessionid'))
->findOne();
if (!$d) {
$d = ORM::for_table('rad_acct')->create();
}
$acctOutputOctets = _post('acctOutputOctets', 0);
$acctInputOctets = _post('acctInputOctets', 0);
if ($acctOutputOctets !== false && $acctInputOctets !== false) {
$d->acctOutputOctets += intval($acctOutputOctets);
$d->acctInputOctets += intval($acctInputOctets);
} else {
$d->acctOutputOctets = 0;
$d->acctInputOctets = 0;
}
$d->acctSessionId = _post('acctSessionId');
$d->username = $username;
$d->realm = _post('realm');
$d->nasipaddress = _post('nasip');
$d->nasid = _post('nasid');
$d->nasportid = _post('nasPortId');
$d->nasporttype = _post('nasPortType');
$d->framedipaddress = _post('framedIPAddress');
$d->acctstatustype = _post('acctStatusType');
$d->macaddr = _post('macAddr');
$d->dateAdded = date('Y-m-d H:i:s');
$d->save();
if (_post('acctStatusType') == 'Start') {
$tur = ORM::for_table('tbl_user_recharges')->whereRaw("BINARY username = '$username'")->where('status', 'on')->where('routers', 'radius')->find_one();
$plan = ORM::for_table('tbl_plans')->where('id', $tur['plan_id'])->find_one();
if ($plan['limit_type'] == "Data_Limit" || $plan['limit_type'] == "Both_Limit") {
$totalUsage = $d['acctOutputOctets'] + $d['acctInputOctets'];
$attrs['reply:Mikrotik-Total-Limit'] = Text::convertDataUnit($plan['data_limit'], $plan['data_unit']) - $totalUsage;
if ($attrs['reply:Mikrotik-Total-Limit'] < 0) {
$attrs['reply:Mikrotik-Total-Limit'] = 0;
show_radius_result(["control:Auth-Type" => "Accept", 'Reply-Message' => 'You have exceeded your data limit.'], 401);
}
}
}
show_radius_result([
"control:Auth-Type" => "Accept",
"reply:Reply-Message" => 'Saved'
], 200);
break;
}
die();
} catch (Throwable $e) {
Message::sendTelegram(
"Sistem Error.\n" .
$e->getMessage() . "\n" .
$e->getTraceAsString()
);
show_radius_result(['Reply-Message' => 'Command Failed : ' . $action], 401);
} catch (Exception $e) {
Message::sendTelegram(
"Sistem Error.\n" .
$e->getMessage() . "\n" .
$e->getTraceAsString()
);
show_radius_result(['Reply-Message' => 'Command Failed : ' . $action], 401);
}
show_radius_result(['Reply-Message' => 'Invalid Command : ' . $action], 401);
function process_radiust_rest($tur, $code)
{
global $config;
$plan = ORM::for_table('tbl_plans')->where('id', $tur['plan_id'])->find_one();
$bw = ORM::for_table("tbl_bandwidth")->find_one($plan['id_bw']);
// Count User Onlines
$USRon = ORM::for_table('rad_acct')
->where('username', $tur['username'])
->where("acctStatusType", 'Start')
->count();
if ($USRon >= $plan['shared_users'] && $plan['type'] == 'Hotspot') {
show_radius_result(["control:Auth-Type" => "Accept", 'Reply-Message' => 'You are already logged in - access denied ('.$USRon.')'], 401);
}
if ($bw['rate_down_unit'] == 'Kbps') {
$unitdown = 'K';
} else {
$unitdown = 'M';
}
if ($bw['rate_up_unit'] == 'Kbps') {
$unitup = 'K';
} else {
$unitup = 'M';
}
$rate = $bw['rate_up'] . $unitup . "/" . $bw['rate_down'] . $unitdown;
$rates = explode('/', $rate);
if (!empty(trim($bw['burst']))) {
$ratos = $rate . ' ' . $bw['burst'];
} else {
$ratos = $rates[0] . '/' . $rates[1];
}
$attrs = [];
$timeexp = strtotime($tur['expiration'] . ' ' . $tur['time']);
$attrs['reply:Reply-Message'] = 'success';
$attrs['Simultaneous-Use'] = $plan['shared_users'];
$attrs['reply:Mikrotik-Wireless-Comment'] = $plan['name_plan'] . ' | ' . $tur['expiration'] . ' ' . $tur['time'];
$attrs['reply:Ascend-Data-Rate'] = str_replace('M', '000000', str_replace('K', '000', $rates[1]));
$attrs['reply:Ascend-Xmit-Rate'] = str_replace('M', '000000', str_replace('K', '000', $rates[0]));
$attrs['reply:Mikrotik-Rate-Limit'] = $ratos;
$attrs['reply:WISPr-Bandwidth-Max-Up'] = str_replace('M', '000000', str_replace('K', '000', $rates[0]));
$attrs['reply:WISPr-Bandwidth-Max-Down'] = str_replace('M', '000000', str_replace('K', '000', $rates[1]));
$attrs['reply:expiration'] = date('d M Y H:i:s', $timeexp);
$attrs['reply:WISPr-Session-Terminate-Time'] = date('Y-m-d', $timeexp) . 'T' . date('H:i:sP', $timeexp);
if ($plan['type'] == 'PPPOE') {
$attrs['reply:Framed-Pool'] = $plan['pool'];
}
if ($plan['typebp'] == "Limited") {
if ($plan['limit_type'] == "Data_Limit" || $plan['limit_type'] == "Both_Limit") {
$raddact = ORM::for_table('rad_acct')->whereRaw("BINARY username = '$tur[username]'")->where('acctstatustype', 'Start')->find_one();
$totalUsage = intval($raddact['acctOutputOctets']) + intval($raddact['acctInputOctets']);
$attrs['reply:Mikrotik-Total-Limit'] = Text::convertDataUnit($plan['data_limit'], $plan['data_unit']) - $totalUsage;
if ($attrs['reply:Mikrotik-Total-Limit'] < 0) {
$attrs['reply:Mikrotik-Total-Limit'] = 0;
show_radius_result(["control:Auth-Type" => "Accept", 'Reply-Message' => 'You have exceeded your data limit.'], 401);
}
}
if ($plan['limit_type'] == "Time_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60;
else
$timelimit = $plan['time_limit'] * 60;
$attrs['reply:Max-All-Session'] = $timelimit;
$attrs['reply:Expire-After'] = $timelimit;
} else if ($plan['limit_type'] == "Data_Limit") {
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
$attrs['reply:Max-Data'] = $datalimit;
$attrs['reply:Mikrotik-Recv-Limit-Gigawords'] = $datalimit;
$attrs['reply:Mikrotik-Xmit-Limit-Gigawords'] = $datalimit;
} else if ($plan['limit_type'] == "Both_Limit") {
if ($plan['time_unit'] == 'Hrs')
$timelimit = $plan['time_limit'] * 60 * 60;
else
$timelimit = $plan['time_limit'] * 60;
if ($plan['data_unit'] == 'GB')
$datalimit = $plan['data_limit'] . "000000000";
else
$datalimit = $plan['data_limit'] . "000000";
$attrs['reply:Max-All-Session'] = $timelimit;
$attrs['reply:Max-Data'] = $datalimit;
$attrs['reply:Mikrotik-Recv-Limit-Gigawords'] = $datalimit;
$attrs['reply:Mikrotik-Xmit-Limit-Gigawords'] = $datalimit;
}
}
$result = array_merge([
"control:Auth-Type" => "Accept",
"reply" => ["Reply-Message" => ['value' => 'success']]
], $attrs);
show_radius_result($result, $code);
}
function show_radius_result($array, $code = 200)
{
if ($code == 401) {
header("HTTP/1.1 401 Unauthorized");
} else if ($code == 200) {
header("HTTP/1.1 200 OK");
} else if ($code == 204) {
header("HTTP/1.1 204 No Content");
die();
}
echo json_encode($array);
die();
}