Для того что бы Ubilling начал работать с Mikrotik NAS необходимо заменить стандартные скрипты OnConnect, OnDisconnect и т.д., и тут мы столкнулись с проблемой, после замены этих файлов Ubilling начинает работать с Mikrotik NAS, но перестаёт работать с Local NAS. Это связано с тем, что мы используем ОС Debian, а не FreeBSD под которую рассчитан Ubilling.
В этой теме я не буду описывать саму настройку Mikrotik и Ubilling, подробную инструкцию Вы можете посмотреть на Ubilling wiki, будем считать что мы всё сделали по инструкции и дальше я дам пример как заставить Ubilling работать с Mikrotik NAS и Local NAS на ОС Debian.
Первым делом отредактируем файл /etc/stargazer/system/executer/rscriptd.drv:
<?php if ( !defined('ENVIRONMENT') ) exit('Only STG can run script!' . "
");
class Executer {
// Recived data from `ubilling.cls`:
private $log;
private $config;
private $database;
// Constructor:
public function __construct($data) {
// Put all recived data to specified vars:
foreach ($data as $key => $value) {
$this->$key = $value;
}
// Write log message, that class is loaded:
$this->log->message(__CLASS__, "RScriptD driver loaded", 'debug');
// Run:
switch ( ENVIRONMENT ) {
case OnConnect:
case OnDisconnect:
$script = constant('ENVIRONMENT');
$this->$script();
break;
}
}
// 1. OnConnect:
public function OnConnect() {
// User's TX & RX:
$rate = $this->database->get_user_rate();
//Execute OnConnect script
shell_exec("/bin/bash /etc/stargazer/system/executer/OnConnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac());
// Write log message:
$this->log->message(__CLASS__, "/bin/bash OnConnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac(), 'debug');
// ARP:
//shell_exec($this->config['arpcmd'] . ' -S ' . IP . ' ' . $this->database->get_user_mac());
// Speed control:
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 101) . " config bw " . $rate['tx'] . $this->config['rate_val'] . " queue 32Kbytes");
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 8101) . " config bw " . $rate['rx'] . $this->config['rate_val'] . " queue 32Kbytes");
// Shaper:
//shell_exec($this->config['fwcmd'] . " table 3 add " . IP . " " . (ID + 101));
//shell_exec($this->config['fwcmd'] . " table 4 add " . IP . " " . (ID + 8101));
//shell_exec($this->config['fwcmd'] . " table 47 delete " . IP);
// Day/Night switcher:
//file_put_contents(BASEPATH . "dn/" . LOGIN, $rate['rx'] . ":" . (ID + 8101), LOCK_EX);
//shell_exec("/bin/chmod 777 " . BASEPATH . "dn/" . LOGIN);
$this->log->message(__CLASS__, "Creation of firewall rules done", 'success');
}
// 2. OnDisconnect:
public function OnDisconnect() {
// User's TX & RX:
$rate = $this->database->get_user_rate();
//Execute OnConnect script
shell_exec("/bin/bash /etc/stargazer/system/executer/OnDisconnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac());
// Write log message:
$this->log->message(__CLASS__, "/bin/bash OnDisconnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac(), 'debug');
// Delete old pipes:
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 101) . " delete");
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 8101) . " delete");
// Delete from shaper:
//shell_exec($this->config['fwcmd'] . " table 3 delete " . IP . " " . (ID + 101));
//shell_exec($this->config['fwcmd'] . " table 4 delete " . IP . " " . (ID + 8101));
//shell_exec($this->config['fwcmd'] . " table 47 add " . IP);
// Day/Night switcher:
//shell_exec("/bin/rm " . BASEPATH . "dn/" . LOGIN);
$this->log->message(__CLASS__, "Removing of firewall rules done", 'success');
}
}
?>
class Executer {
// Recived data from `ubilling.cls`:
private $log;
private $config;
private $database;
// Constructor:
public function __construct($data) {
// Put all recived data to specified vars:
foreach ($data as $key => $value) {
$this->$key = $value;
}
// Write log message, that class is loaded:
$this->log->message(__CLASS__, "RScriptD driver loaded", 'debug');
// Run:
switch ( ENVIRONMENT ) {
case OnConnect:
case OnDisconnect:
$script = constant('ENVIRONMENT');
$this->$script();
break;
}
}
// 1. OnConnect:
public function OnConnect() {
// User's TX & RX:
$rate = $this->database->get_user_rate();
//Execute OnConnect script
shell_exec("/bin/bash /etc/stargazer/system/executer/OnConnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac());
// Write log message:
$this->log->message(__CLASS__, "/bin/bash OnConnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac(), 'debug');
// ARP:
//shell_exec($this->config['arpcmd'] . ' -S ' . IP . ' ' . $this->database->get_user_mac());
// Speed control:
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 101) . " config bw " . $rate['tx'] . $this->config['rate_val'] . " queue 32Kbytes");
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 8101) . " config bw " . $rate['rx'] . $this->config['rate_val'] . " queue 32Kbytes");
// Shaper:
//shell_exec($this->config['fwcmd'] . " table 3 add " . IP . " " . (ID + 101));
//shell_exec($this->config['fwcmd'] . " table 4 add " . IP . " " . (ID + 8101));
//shell_exec($this->config['fwcmd'] . " table 47 delete " . IP);
// Day/Night switcher:
//file_put_contents(BASEPATH . "dn/" . LOGIN, $rate['rx'] . ":" . (ID + 8101), LOCK_EX);
//shell_exec("/bin/chmod 777 " . BASEPATH . "dn/" . LOGIN);
$this->log->message(__CLASS__, "Creation of firewall rules done", 'success');
}
// 2. OnDisconnect:
public function OnDisconnect() {
// User's TX & RX:
$rate = $this->database->get_user_rate();
//Execute OnConnect script
shell_exec("/bin/bash /etc/stargazer/system/executer/OnDisconnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac());
// Write log message:
$this->log->message(__CLASS__, "/bin/bash OnDisconnect " . LOGIN . " " . IP . " " . CASH . " " . ID . " " . $rate['tx'] . " " . $rate['rx'] . " " . $this->database->get_user_mac(), 'debug');
// Delete old pipes:
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 101) . " delete");
//shell_exec($this->config['fwcmd'] . " pipe " . (ID + 8101) . " delete");
// Delete from shaper:
//shell_exec($this->config['fwcmd'] . " table 3 delete " . IP . " " . (ID + 101));
//shell_exec($this->config['fwcmd'] . " table 4 delete " . IP . " " . (ID + 8101));
//shell_exec($this->config['fwcmd'] . " table 47 add " . IP);
// Day/Night switcher:
//shell_exec("/bin/rm " . BASEPATH . "dn/" . LOGIN);
$this->log->message(__CLASS__, "Removing of firewall rules done", 'success');
}
}
?>
Для большей наглядности что именно мы изменили в этом файле, старый код я не удалял с файла, а закомментировал знаком "//".
Далее в папке /etc/stargazer/system/executer/ создадим файлы OnConnect и OnDisconnect и напишем в них следующий код:
Файл OnConnect:
Код PHP:
Файл OnDisconnect:
#!/bin/bash #OnConnect IFUP="eth0" IFDOWN="eth1" LOGIN=$1 IP=$2 CASH=$3 ID=$4 SPEED=$5 UPSPEED=$6 MAC=$7 SCOUNT="Kbit" IPT="/sbin/iptables" tc="/sbin/tc" arpcmd="/usr/sbin/arp" # DELETE RULEZ ###################################################################### $IPT -t filter -D INPUT -s $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D INPUT -s $IP -j ACCEPT done $IPT -t filter -D FORWARD -s $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D FORWARD -s $IP -j ACCEPT done $IPT -t filter -D FORWARD -d $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D FORWARD -d $IP -j ACCEPT done $IPT -t filter -D OUTPUT -d $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D OUTPUT -d $IP -j ACCEPT done ####################################################################### # ADD RULEZ ####################################################################### $IPT -t filter -A INPUT -s $IP -j ACCEPT $IPT -t filter -A FORWARD -s $IP -j ACCEPT $IPT -t filter -A FORWARD -d $IP -j ACCEPT $IPT -t filter -A OUTPUT -d $IP -j ACCEPT ############################ # fix user mac to ip $arpcmd -d $IP $MAC while [ $? -eq 0 ] do $arpcmd -d $IP $MAC done $arpcmd -s $IP $MAC ############################ declare -i mark=$ID+101 declare -i mark1=$ID+4101 $IPT -t mangle -A FORWARD -d $IP -j MARK --set-mark $mark $IPT -t mangle -A FORWARD -s $IP -j MARK --set-mark $mark1 #Download $tc class add dev $IFDOWN parent 1:1 classid 1:$mark htb rate $SPEED$SCOUNT $tc filter add dev $IFDOWN parent 1: protocol ip prio 3 handle $mark fw classid 1:$mark #Upload $tc class add dev $IFUP parent 1:1 classid 1:$mark1 htb rate $UPSPEED$SCOUNT $tc filter add dev $IFUP parent 1: protocol ip prio 3 handle $mark1 fw classid 1:$mark1 ########################################################################################### # DAY/NIGHT switcher /bin/echo $SPEED:`expr $ID + 4101` > /etc/stargazer/dn/$LOGIN # ADD TO LOG
Код PHP:
Пути логов отредактируйте под себя.
#!/bin/bash # OnDisconnect IFUP="eth0" IFDOWN="eth1" LOGIN=$1 IP=$2 CASH=$3 ID=$4 SPEED=$5 UPSPEED=$6 MAC=$7 SCOUNT="Kbit" IPT="/sbin/iptables" tc="/sbin/tc" # TIME FORMAT # DELETE RULEZ IPT ###################################################################### $IPT -t filter -D INPUT -s $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D INPUT -s $IP -j ACCEPT done $IPT -t filter -D FORWARD -s $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D FORWARD -s $IP -j ACCEPT done $IPT -t filter -D FORWARD -d $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D FORWARD -d $IP -j ACCEPT done $IPT -t filter -D OUTPUT -d $IP -j ACCEPT while [ $? -eq 0 ] do $IPT -t filter -D OUTPUT -d $IP -j ACCEPT done ######################### declare -i mark=$ID+101 declare -i mark1=$ID+4101 if [ -n "$mark" ] then $IPT -t mangle -D FORWARD -d $IP -j MARK --set-mark $mark while [ $? = 0 ] do $IPT -t mangle -D FORWARD -d $IP -j MARK --set-mark $mark done fi if [ -n "$mark1" ] then $IPT -t mangle -D FORWARD -s $IP -j MARK --set-mark $mark1 while [ $? = 0 ] do $IPT -t mangle -D FORWARD -s $IP -j MARK --set-mark $mark1 done fi #Download $tc filter del dev $IFDOWN parent 1: protocol ip prio 3 handle $mark fw classid 1:$mark $tc class del dev $IFDOWN parent 1:1 classid 1:$mark htb rate $SPEED$SCOUNT #Upload $tc filter del dev $IFUP parent 1: protocol ip prio 3 handle $mark1 fw classid 1:$mark1 $tc class del dev $IFUP parent 1:1 classid 1:$mark1 htb rate $UPSPEED$SCOUNT ############################################################################ # DAY/NIGHT switcher /bin/rm /etc/stargazer/dn/$LOGIN
Теперь наш Ubilling на ОС Debian работает одновременно и с Mikrotik NAS и с Local NAS.