📜 ⬆️ ⬇️

PHP linux server incoming / outgoing traffic balancer

As I found out some time ago on my own experience, there are still providers left to receive an unlimited tariff from which it is required to withstand a certain ratio of incoming and outgoing traffic.
In my case, when hosting a server for colocation in one Moscow data center, the outgoing should have been at least 4 times more than the incoming one (sorry for such a turn). The ratio is calculated both per day and total per month. Violation of any of them is a fine.

By itself, the ratio was not maintained due to periodic volume uploads of backups to the server. Manually (gig sent, 4 gig downloaded) - tired.


')
Then I remembered that once upon a time a primitive script was seen on some site in a couple of lines on a sh-script that did this balancing itself. Then I did not save, now I could not find it. I had to write myself. Result and expose for everyone to see - maybe someone will come in handy.

The script was tested only under Ubuntu, but also under other Linux should work.

Unlike the primitive on sh, my script is able to withstand both daily and monthly ratios, logs, and is carefully encrypted from its detection (fake traffic is done by transferring files of random size via SSH, spacing of random length is done between stuffing). Another difference (but already with a minus sign) is required response node with unlimited traffic, to which you can raise the ssh connection by key. I use my home channel and router on it.

Put your values ​​in the $ interface, $ logfile and $ server variables.
The $ server variable actually specifies the parameters for the ssh command.
That is, in my case, running “ssh my-host.no-ip.org -p 1022 -i /home/user/.ssh/my-key” in the console, I connect without entering a password to my response node (these are conditional , of course).
Calling this script must be done at system startup (for example from /etc/rc.local) or configured as a service.

<?php $interface="eth0"; //    $server="my-host.no-ip.org -p 1022 -i /home/user/.ssh/my-key"; //    $logfile="/usr/local/logs/trafic-balancer.log"; //       ,   logrotate   $curdate=0; function echolog($str) { global $logfile; file_put_contents($logfile, $str, FILE_APPEND); echo($str); } echolog("\n".date('r')." \t\n"); while(true) { $f=@fopen("/proc/net/dev", "r"); $stat=null; while($str=fgets($f)) { if(strpos($str, $interface)!==false) { $stat=$str; break; } } fclose($f); if($stat) { $stat=preg_replace("/\s+/", " ", $stat); $nums=explode(" ", $stat); //       $rx_g=round($nums[2]/1024/1024); $tx_g=round($nums[10]/1024/1024); $delta_g=$rx_g*4-$tx_g; //   if($curdate!=date('j')) //    { if($curdate) echolog("\n".date('r')." \t  $curdate : RX:$rx_d Mb, TX:$tx_d Mb, : $delta_d Mb\n"); $curdate=date('j'); $rx_d=0; $tx_d=0; $rx_s=$rx_g; $tx_s=$tx_g; } $rx_d=$rx_g-$rx_s; $tx_d=$tx_g-$tx_s; $delta_d=$rx_d*4-$tx_d; $delta=max($delta_g, $delta_d); if(($delta>0)&&(date('G')>8)) { if($delta>1000) //   1,     $size=round(rand(1000, min($delta/2, 5000))); //       ,     else $size=round(rand($delta*2, $delta*3)); //          echolog("\n".date('r')." \t:  : $delta_g Mb,  : $delta_d Mb,  $size Mb\n"); passthru("dd if=/dev/zero bs=1M count=$size | ssh $server 'cat > /dev/null'"); } else echolog('.'); } else echolog("\n".date('r')." \t  !\n"); sleep(round(rand(10, 600))); } ?> 

Source: https://habr.com/ru/post/140697/


All Articles