Ubilling на Debian Jessie

Имеем 2 сетевых интерфейса
eth1 100.1.1.1 — интернет
eth0 192.168.10.1 — локальная сеть

Добавляем репозиторий
vim /etc/apt/sources.list:

deb http://ftp.ua.debian.org/debian/ jessie main

aptitude update
aptitude install apache2 bandwidthd expat libapache2-mod-php5 mysql-server libexpat1 libmysqlclient-dev libxmlrpc-c++8 libxmlrpc-c++8-dev php5-cli php5-mysql isc-dhcp-server softflowd sudo wget build-essential openssl iptables-persistent
wget http://ftp.ua.debian.org/debian/pool/main/x/xmlrpc-c/{libxmlrpc-core-c3_1.33.14-0.2_amd64.deb,libxmlrpc-core-c3-dev_1.33.14-0.2_amd64.deb}
dpkg -i libxmlrpc-core-c3_1.33.14-0.2_amd64.deb libxmlrpc-core-c3-dev_1.33.14-0.2_amd64.deb

vim /etc/default/isc-dhcp-server:

INTERFACES="eth0"

service isc-dhcp-server start

vim /etc/bandwidthd/bandwidthd.conf:

htdocs_dir "/var/lib/bandwidthd/htdocs"

vim /etc/apache2/conf-enabled/bandwidthd.conf

Alias /bwd /var/lib/bandwidthd/htdocs/

chown -R www-data:www-data /var/lib/bandwidthd/htdocs/
service bandwidthd restart

visudo

User_Alias BILLING = www-data
BILLING ALL = NOPASSWD: ALL

Устанавливаем шейпер
wget http://sourceforge.net/projects/htbinit/files/HTB.init/0.8.5/htb.init-v0.8.5 -O /etc/init.d/htb
chmod +x /etc/init.d/htb
mkdir /etc/htb
vim /etc/init.d/htb:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          htb init script
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start htb
# Description:       enable traffic control
### END INIT INFO
HTB_PATH=${HTB_PATH:-/etc/htb}

Добавляем htb в автозапуск
systemctl enable htb.service

vim /etc/htb/eth0:

DEFAULT=0
R2Q=100

vim /etc/htb/eth1:

DEFAULT=0
R2Q=100

vim /etc/htb/eth0-2.root:

RATE=100Mbit
CEIL=100Mbit

vim /etc/htb/eth1-2.root:

RATE=100Mbit
CEIL=100Mbit

service htb compile
service htb start

vim /etc/default/softflowd:

INTERFACE="eth0"
OPTIONS="-n 192.168.10.1:42111"

service softflowd restart

Устанавливаем stargazer
Добавляем исходник для iptables без него не скомпилируется stargazer (https://git.netfilter.org/iptables/tree/include/linux/netfilter_ipv4/ip_queue.h)
vim /usr/include/linux/netfilter_ipv4/ip_queue.h:

/*
 * This is a module which is used for queueing IPv4 packets and
 * communicating with userspace via netlink.
 *
 * (C) 2000 James Morris, this code is GPL.
 */
#ifndef _IP_QUEUE_H
#define _IP_QUEUE_H

#ifdef __KERNEL__
#ifdef DEBUG_IPQ
#define QDEBUG(x...) printk(KERN_DEBUG ## x)
#else
#define QDEBUG(x...)
#endif  /* DEBUG_IPQ */
#else
#include 
#endif	/* ! __KERNEL__ */

/* Messages sent from kernel */
typedef struct ipq_packet_msg {
	unsigned long packet_id;	/* ID of queued packet */
	unsigned long mark;		/* Netfilter mark value */
	long timestamp_sec;		/* Packet arrival time (seconds) */
	long timestamp_usec;		/* Packet arrvial time (+useconds) */
	unsigned int hook;		/* Netfilter hook we rode in on */
	char indev_name[IFNAMSIZ];	/* Name of incoming interface */
	char outdev_name[IFNAMSIZ];	/* Name of outgoing interface */
	__be16 hw_protocol;		/* Hardware protocol (network order) */
	unsigned short hw_type;		/* Hardware type */
	unsigned char hw_addrlen;	/* Hardware address length */
	unsigned char hw_addr[8];	/* Hardware address */
	size_t data_len;		/* Length of packet data */
	unsigned char payload[0];	/* Optional packet data */
} ipq_packet_msg_t;

/* Messages sent from userspace */
typedef struct ipq_mode_msg {
	unsigned char value;		/* Requested mode */
	size_t range;			/* Optional range of packet requested */
} ipq_mode_msg_t;

typedef struct ipq_verdict_msg {
	unsigned int value;		/* Verdict to hand to netfilter */
	unsigned long id;		/* Packet ID for this verdict */
	size_t data_len;		/* Length of replacement data */
	unsigned char payload[0];	/* Optional replacement packet */
} ipq_verdict_msg_t;

typedef struct ipq_peer_msg {
	union {
		ipq_verdict_msg_t verdict;
		ipq_mode_msg_t mode;
	} msg;
} ipq_peer_msg_t;

/* Packet delivery modes */
enum {
	IPQ_COPY_NONE,		/* Initial mode, packets are dropped */
	IPQ_COPY_META,		/* Copy metadata */
	IPQ_COPY_PACKET		/* Copy metadata + packet (range) */
};
#define IPQ_COPY_MAX IPQ_COPY_PACKET

/* Types of messages */
#define IPQM_BASE	0x10	/* standard netlink messages below this */
#define IPQM_MODE	(IPQM_BASE + 1)		/* Mode request from peer */
#define IPQM_VERDICT	(IPQM_BASE + 2)		/* Verdict from peer */
#define IPQM_PACKET	(IPQM_BASE + 3)		/* Packet from kernel */
#define IPQM_MAX	(IPQM_BASE + 4)

#endif /*_IP_QUEUE_H*/

cd /usr/src/
wget http://stargazer.net.ua/download/server/2.408/stg-2.408.tar.gz
tar xzf stg-2.408.tar.gz
cd stg-2.408/projects/stargazer
./build
make install
cd ../sgconf
./build
make
make install
cd ../sgconf_xml/
./build
make
make install
cd ../stargazer/plugins/configuration/rpcconfig/
make
make install

Создаем скрипт запуска для stargazer
vim /etc/init.d/stg:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          startgazer init script
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start startgazer
# Description:       enable startgazer daemon on start system
### END INIT INFO

NAME="startgazer daemon"
BINARYNAME="stargazer"
PIDFILE="/var/run/startgazer.pid"

running() {
    if [ -n "`pgrep -f $BINARYNAME`" ]; then
        return 0
    else
        return 1
    fi
}

start() {
    if ! running; then
        echo -n "Starting the $NAME ... "
        start-stop-daemon --start --exec /usr/sbin/stargazer
        pgrep -f $BINARYNAME > $PIDFILE
        if [ -s $PIDFILE ]; then
            echo "Done"
        else
            echo "Failed"
            rm $PIDFILE
        fi
    else
        echo "The $NAME is already started."
    fi
}

stop() {
    if running; then
        echo -n "Stopping the $NAME ... "
        kill `cat $PIDFILE`
        while running; do
            sleep 1
        done
        rm $PIDFILE
        echo "Done"
    else
        echo "The $NAME is already stopped."
    fi
}

case "$1" in
    start)
        start
    ;;
    stop)
        stop
    ;;
    restart)
        stop
        start
    ;;
    status)
        if running; then
            echo "The $NAME is started."
        else
            echo "The $NAME is stopped."
        fi
    ;;
    *)
        echo "Usage: $0 (start|stop|restart|status)"
        exit 1
esac
exit 0

Добавляем в автозапуск stargazer
insserv -d stg

cp /etc/stargazer/conf-available.d/{store_mysql.conf,mod_rpc.conf,mod_cap_nf.conf,mod_remote_script.conf} /etc/stargazer/conf-enabled.d/
vim /etc/stargazer/stargazer.conf:

LogFile = /var/log/stargazer/stargazer.log
# Store module
# Configure the module that works with the database server

# Warning: Only one store module could be used at the same time!

<IncludeFile "conf-enabled.d/store_mysql.conf">
</IncludeFile>


################################################################################
# Other modules

<Modules>

    <IncludeFile "conf-enabled.d/mod_*.conf">
    </IncludeFile>

    <IncludeFile "conf-enabled.d/mod_ia.conf">
    </IncludeFile>
   <IncludeFile "conf-enabled.d/mod_sg.conf">
    </IncludeFile>

    <IncludeFile "conf-enabled.d/mod_cap_nf.conf">
    </IncludeFile>

    <IncludeFile "conf-enabled.d/mod_rpc.conf">
    </IncludeFile>

    <IncludeFile "conf-enabled.d/mod_remote_script.conf">
    </IncludeFile>

</Modules>

mkdir /var/log/stargazer

vim /etc/stargazer/conf-enabled.d/store_mysql.conf:

Database = stargazer
User = stargazer
Password = stargazerpasswd

vim /etc/stargazer/conf-enabled.d/mod_cap_nf.conf:

TCPPort = 42111
UDPPort = 42111

vim /etc/stargazer/conf-enabled.d/mod_rpc.conf:

Port = 8081
vim /etc/stargazer/conf-enabled.d/mod_remote_script.conf:
SubnetFile = /etc/stargazer/subnets

vim /etc/stargazer/subnets:

192.168.10.0/24 100.1.1.1

chmod 777 /etc/stargazer/subnets

vim /etc/stargazer/rules:

ALL     0.0.0.0/0       DIR0

Делаем базу данных для stargazer
mysql -u root -p
CREATE DATABASE stargazer;
GRANT ALL PRIVILEGES ON stargazer.* TO stargazer@localhost IDENTIFIED BY 'stargazerpasswd';
quit

Запускаем stargazer чтобы он создал свои таблицы в базе данных
/etc/init.d/stg start
Проверяем таблицы
mysql -u stargazer -p stargazer -e "SHOW TABLES"

+---------------------+
| Tables_in_stargazer |
+---------------------+
| admins              |
| messages            |
| stat                |
| tariffs             |
| users               |
+---------------------+

/etc/init.d/stg stop

Устанавливаем ubilling
mkdir /var/www/ubilling
wget http://ubilling.net.ua/ub.tgz
tar fxz ub.tgz -C /var/www/ubilling
chown -R www-data:www-data /var/www/ubilling

Создаем таблицы для ubilling
mysql -u stargazer -p stargazer < /var/www/ubilling/docs/test_dump.sql

vim /var/www/ubilling/config/mysql.ini:

username = "stargazer"
password = "stargazerpasswd"
db = "stargazer"

vim /var/www/ubilling/config/billing.ini

STG_LOGIN=admin
STG_PASSWD=adminpasswd
SUDO=/usr/bin/sudo
RC_DHCPD=/etc/init.d/isc-dhcp-server
GREP=/bin/grep
PING=/bin/ping
LANG = ru
TASKBAR_ICON_SIZE = 64
REGRANDOM_MAC=0

ln -fs /etc/dhcp/ /var/www/ubilling/multinet

vim /var/www/ubilling/config/dhcp/global.template:

option domain-name "example.com";
option domain-name-servers 192.168.10.1;

vim /var/www/ubilling/config/dhcp/subnets.template:

option domain-name "example.com";
option routers 192.168.10.1;
include "/etc/dhcp/{HOSTS}";

cp /var/www/ubilling/docs/presets/Linux/etc/* /etc/stargazer/
chmod +x /etc/stargazer/*

vim /etc/stargazer/config:

username = stargazer
password = stargazerpasswd
database = stargazer

vim /etc/stargazer/OnConnect:

IFUP="eth1"
IFDOWN="eth0"
echo "$cur_date $cur_time CONNECT: ID-$ID;LOGIN-$LOGIN;IP-$IP;CASH-$CASH;SPEED-$SPEED;UPSPEED-$UPSPEED,MAC-$MAC" >> /var/log/stargazer/allconnect.log

vim /etc/stargazer/OnDisconnect:

IFUP="eth1"
IFDOWN="eth0"
echo "$cur_date $cur_time DISCONNECT: ID-$ID;LOGIN-$LOGIN;IP-$IP;CASH-$CASH;SPEED-$SPEED;UPSPEED-$UPSPEED,MAC-$MAC" >> /var/log/stargazer/allconnect.log

vim /etc/stargazer/GetMac:

#!/usr/bin/php

vim /etc/stargazer/GetSpeed:

#!/usr/bin/php

vim /etc/stargazer/GetUpSpeed:

#!/usr/bin/php

Даем права ubilling на папку dhcp чтобы он смог сгенерировать конфигурационный файл для dhcp сервера
chown -R www-data:www-data /etc/dhcp

Включаем пересылку сетевых пакетов
vim /etc/sysctl.conf:

net.ipv4.ip_forward=1

sysctl -p
vim /etc/rc.local:

sysctl -p

vim /etc/stargazer/conf-enabled.d/mod_remote_script.conf:

SubnetFile = /var/www/ubilling/remote_nas.conf

Запускаем stargazer
/etc/init.d/stg start

Генерируем самоподписные сертификаты для SSL
cd /etc/ssl/private
openssl genrsa -des3 -out example.com.key 2048
openssl rsa -in server.key -out example.com.key
openssl req -new -days 36500 -key example.com.key -out example.com.csr
openssl x509 -in example.com.csr -out example.com.crt -req -signkey example.com.key -days 3650
chmod 400 example.com.*

Включаем поддержку SSL на нашем сайте
vim /etc/apache2/sites-available/default-ssl.conf:

SSLEngine on
SSLCertificateFile /etc/ssl/private/example.com.crt
SSLCertificateKeyFile /etc/ssl/private/example.com.key

a2ensite default-ssl

Создаем конфигурационный файл для apache
vim /etc/apache2/conf-enabled/ubilling.conf:

Alias /ubil /var/www/ubilling/
<Directory /var/www/ubilling/>
  DirectoryIndex index.php
  Require all granted
</Directory>

service apache2 reload

Включаем NAT и открываем порты
iptables -I POSTROUTING 1 -s 192.168.10.0/24 -o eth1 -j SNAT --to-source 100.1.1.1
iptables -I INPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I INPUT 2 -s 192.168.10.0/24 -d 192.168.10.1 -i eth0 -p tcp -m state --state NEW -m multiport --dports 80,443,5555,8081,9999,42111 -j ACCEPT
iptables -I INPUT 3 -s 192.168.10.0/24 -d 192.168.10.1 -i eth0 -p udp -m multiport --dports 67,5555,42111 -j ACCEPT
service netfilter-persistent save

Заходим в биллинг https://example.com/ubil
По умолчанию логин admin, пароль demo
Внизу в левом углу заходим в "Права администраторов" и изменяем пароль

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *