Answer the question
In order to leave comments, you need to log in
Editing a PHP script, in an Asus router, for a fee?
Habravchane, I'm in a desperate state, I need help as quickly as possible, and they won't help anywhere as quickly as possible.
The situation is this:
There is an RT-N16 router with enthusiast firmware, a script is running on it that counts user traffic. Link to the description of the script goo.gl/be2bE
Recently, it was necessary to limit the user limit. To do this, the script has additional functionality in the form of a php script that checks for violations by users of the limit and disables them.
(as a person who doesn’t understand PHP AT ALL and hardly understands Linux, I can’t describe in more detail)
The problem is that the script works for monthly limits, and I need the same thing, but only a daily limit ...
Who can help is ready to pay the n-th amount to a Qiwi or WM wallet.
Skype contact lfatal1ty
Please help!
Answer the question
In order to leave comments, you need to log in
So the solution was found and the work was paid for, unfortunately the person is not even a Habravchan (it's still amazing what a huge community can help, given that many just read, and not just write.)
public function getUserTrafByThisDay(&$totalDay, &$totalIP) {
$thisdate = date('Y-m-d');
$handle1 = fopen($this->world.$thisdate, "r");
$handle2 = fopen($this->city.$thisdate, "r");
$sum['win'] = '0';
$sum['wout'] = '0';
$sum['cin'] = '0';
$sum['cout'] = '0';
while ($userinfo = fscanf($handle1, "%s\t%s\t%s\n")) {
$userByIp[$userinfo[0]]['win'] = $userinfo[1];
$userByIp[$userinfo[0]]['wout'] = $userinfo[2];
$sum['win'] = bcadd($sum['win'], $userinfo[1]);
$sum['wout'] = bcadd($sum['wout'], $userinfo[2]);
if (isset($totalIP[$userinfo[0]])) {
$totalIP[$userinfo[0]]['win'] = bcadd($totalIP[$userinfo[0]]['win'], $userinfo[1]);
$totalIP[$userinfo[0]]['wout'] = bcadd($totalIP[$userinfo[0]]['wout'], $userinfo[2]);
}
else {
$totalIP[$userinfo[0]]['win'] = $userinfo[1];
$totalIP[$userinfo[0]]['wout'] = $userinfo[2];
$totalIP[$userinfo[0]]['cin'] = '0';
$totalIP[$userinfo[0]]['cout'] = '0';
}
}
while ($userinfo = fscanf($handle2, "%s\t%s\t%s\n")) {
$userByIp[$userinfo[0]]['cin'] = $userinfo[1];
$userByIp[$userinfo[0]]['cout'] = $userinfo[2];
$sum['cin'] = bcadd($sum['cin'], $userinfo[1]);
$sum['cout'] = bcadd($sum['cout'], $userinfo[2]);
if (isset($totalIP[$userinfo[0]])) {
$totalIP[$userinfo[0]]['cin'] = bcadd($totalIP[$userinfo[0]]['cin'], $userinfo[1]);
$totalIP[$userinfo[0]]['cout'] = bcadd($totalIP[$userinfo[0]]['cout'], $userinfo[2]);
}
else {
$totalIP[$userinfo[0]]['cin'] = $userinfo[1];
$totalIP[$userinfo[0]]['cout'] = $userinfo[2];
}
}
fclose($handle1);
fclose($handle2);
$trafByDay[$thisdate] = $userByIp;
$totalDay[$thisdate] = $sum;
unset ($userByIp);
unset ($sum);
return $trafByDay;
}
<?php
class Limit {
private $limitFile = "billing/limit";
private $users = "/usr/local/etc/ethers";
private $userMAC;
private $userLimit;
private function getLimit() {
$handle = fopen($this->limitFile, "r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\t%s\n")) {
$this->userLimit[$userinfo[0]]['in'] = $userinfo[1];
$this->userLimit[$userinfo[0]]['out'] = $userinfo[2];
$this->userLimit[$userinfo[0]]['total'] = $userinfo[3];
}
fclose($handle);
}
private function getUserMAC() {
$handle = fopen($this->users, "r");
while ($userinfo = fscanf($handle, "%s\t%s\n")) {
$this->userMAC[$userinfo[1]] = $userinfo[0];
}
fclose($handle);
}
public function disableUsers() {
$this->getLimit();
$this->getUserMAC();
include_once("FileExtractor.php");
$fe = new FileExtractor();
$trafByDay = $fe->getUserTrafByDay($totalDay, $totalIP);
foreach ($this->userLimit as $ip => $limit) {
if (isset ($this->userMAC[$ip]) && isset ($totalIP[$ip]) &&
( bccomp(bcdiv($totalIP[$ip]['win'], '1048576'), $limit['in']) == 1
|| bccomp(bcdiv($totalIP[$ip]['wout'], '1048576'), $limit['out']) == 1
|| bccomp(bcdiv(bcadd($totalIP[$ip]['win'], $totalIP[$ip]['wout']), '1048576'), $limit['total']) == 1
)
){
shell_exec('iptables -D MAC_IP -s '.$ip.' -m mac --mac-source '.$this->userMAC[$ip].' -j RETURN');
}
}
}
}
$limit = new Limit();
$limit->disableUsers();
?>
The script itself does not need to be changed. It is enough to change the lock reset time. Read the article to the point
All limits are set as integers.
the user is given 1750MB of incoming, 275MB of outgoing traffic and 2010MB of total traffic (incoming + outgoing). When the user reaches at least one of the limits, it will be disabled.
You can not enter all users in this file if we do not want to set a limit for them.
+ It is necessary to configure
crons: Add to crontab:
*/5 * * * * root cd /opt/share/www/statistic/ && /opt/bin/php ./limit.php > /dev/null 2>&1 1 0 1 * * root /user/local/sbin/makefilter
1 0 1 * * root /user/local/sbin/makefilter
0 0 * * * root /user/local/sbin/makefilter
Give the script in a readable form. Wrap the formatted source with the "source" tag. To understand the porridge of letters is too lazy
Judging by the scripts, the removal of the blocking occurs by deleting the billing files, so try changing the lines
in the file codebilling.php
$yesterday = date("Y-m", time()-300);
if (date("Y-m") != $yesterday) {
$yesterday = date("Y-m-d", time()-300);
if (date("Y-m-d") != $yesterday) {
if
deletes files with data about the used traffic. In their absence, the blocking should be removed. You can get to this block if only 5 minutes ago it was not the same month and year as now (the next month has come). Therefore, to unlock daily, you need to check another day. That is, "Y-m"
you need to replace it with "Y-m-d"
, which will allow you to delete files every day. Well, my previous answer about cron should also be taken into account.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question