首页 > WEB互联网 > openvpn控流系统计费系统web管理界面流量控制天数控制 开源
2016
06-09

openvpn控流系统计费系统web管理界面流量控制天数控制 开源

openvpn云免,控流系统,计费系统-转载请注明来源

openvpn控流系统计费系统web管理界面流量控制天数控制 开源 - 第1张  | OuYang-Blog

那次事件后,openvpn成为热点,博主也专门研究了一下,现在把控流脚本和web管理源码分享出来。

web功能有:批量添加用户,单个用户管理,添加用户重第一次登陆时间开始计算,可以控制天数和总流量值等等,但是流量统计和天数统计必须得用户断开后才能进行统计,博主还没研究出时时统计,听说radius可以实现,但是因为没时间,所以没有去研究radius,有时间了发radius的教程吧。

自行先安装好mysql,php,apache或其他的。

新建sql文件:

— MySQL dump 10.13 Distrib 5.1.73, for redhat-linux-gnu (x86_64)

— Host: localhost Database: openvpn

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE=’+00:00′ */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=’NO_AUTO_VALUE_ON_ZERO’ */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

DROP TABLE IF EXISTS log;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE log (
username varchar(32) COLLATE utf8_unicode_ci NOT NULL,
start_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
end_time timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
trusted_ip varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
trusted_port int(10) DEFAULT NULL,
protocol varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
remote_ip varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
remote_netmask varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
bytes_received bigint(20) DEFAULT ‘0’,
bytes_sent bigint(20) DEFAULT ‘0’,
total_used bigint(20) DEFAULT ‘0’,
status int(11) NOT NULL DEFAULT ‘1’,
KEY idx_username (username),
KEY idx_start_time (start_time),
KEY idx_end_time (end_time)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
DROP TABLE IF EXISTS stat;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE stat (
username char(32) NOT NULL,
origin_time timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’,
total_used bigint(20) DEFAULT ‘0’,
PRIMARY KEY (username)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

DROP TABLE IF EXISTS user;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE user (
username char(32) COLLATE utf8_unicode_ci NOT NULL,
password char(128) COLLATE utf8_unicode_ci DEFAULT NULL,
active int(10) NOT NULL DEFAULT ‘1’,
creation timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
note text COLLATE utf8_unicode_ci,
quota_cycle int(10) NOT NULL DEFAULT ’30’,
use_cycle int(11) NOT NULL DEFAULT ‘0’,
surplus_cycle int(11) NOT NULL DEFAULT ‘0’,
quota_bytes bigint(20) NOT NULL DEFAULT ‘1024’,
used_quota bigint(20) NOT NULL DEFAULT ‘0’,
left_quota bigint(20) NOT NULL DEFAULT ‘0’,
enabled int(10) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (username),
KEY idx_active (active),
KEY enabled (enabled)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

openvpn控流系统计费系统web管理界面流量控制天数控制 开源 - 第2张  | OuYang-Blog

进入数据库:

mysql

设置密码
use mysql;
update user set password=password(‘密码’) where user=’root’;
flush privileges;

新建数据库

CREATE DATABASE openvpn;
USE openvpn;
source /刚刚新建的路径/文件名.sql

导入后即可。

下面是控流安装教程,安装好openvpn后,再server.conf里加入

auth-user-pass-verify /etc/openvpn/login.sh via-env
client-disconnect /etc/openvpn/disconnect.sh
client-connect /etc/openvpn/connect.sh

这里用户认证是采用读取数据库导出文本格式认证,6.X到7.X均可实现,6.X还可以用PAM认证,方法差不多。

如果有重复的认证段请删除掉。

修改好service后,新建一个

login.sh

#!/bin/sh
user=$username
rp=$password
mysql -hIP地址 -u账号 -p密码 -e “use 数据库名;SELECT password FROM user WHERE username=’$user’;”>>log.txt
mysql -hIP地址 -uIP地址 -pIP地址 -e “use 数据库名;SELECT active FROM user WHERE username=’$user’;”>>log.txt
pass=$(sed -n 2p log.txt)
i=$(sed -n 4p log.txt)
rm -rf log.txt
#if [ “$rp” == “$pass” ] && [ “$i” == “1” ] && [ “$[$recv+$sent]” -lt “$all” ];
if [ “$rp” == “$pass” ] && [ “$i” == “1” ];
then
echo $(date +%Y年%m月%d日%k时%M分) “用户登录成功” “账号:”${username} “密码:”${password}>>login.log
exit 0
else
echo $(date +%Y年%m月%d日%k时%M分) “用户登录失败” “账号:”${username} “密码:”${password}>>login.log
exit 1
fi

 

connect.sh

#!/bin/bash
DB=’数据库名’
DBADMIN=’账号’
DBPASSWD=’密码’

mysql -u$DBADMIN -p$DBPASSWD -e “INSERT into log(username,start_time,trusted_ip,trusted_port,protocol,remote_ip,remote_netmask,status) VALUES(‘$common_name’,now(),’$trusted_ip’,$trusted_port,’$proto_1′,’$ifconfig_pool_remote_ip’,’$route_netmask_1′,1)” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “INSERT INTO stat (username) VALUES (‘$common_name’) ON DUPLICATE KEY UPDATE username=username;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE stat SET origin_time=now() where origin_time=’0000-00-00 00:00:00′ and username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET creation=(select origin_time from stat where stat.username=’$common_name’) WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET use_cycle=(TO_DAYS(NOW()) – TO_DAYS(creation)) WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET surplus_cycle=(quota_cycle – use_cycle) WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET enabled=1 WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET active=0 WHERE (quota_cycle – use_cycle) <= 0 AND username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET active=0 WHERE user.username IN (SELECT username FROM (SELECT log.username AS username,quota_bytes,left_quota,stat.origin_time FROM user,log,stat WHERE log.username=’$common_name’ AND log.username=user.username AND log.status=0 AND TO_DAYS(NOW())-TO_DAYS(stat.origin_time)<=quota_cycle GROUP BY log.username HAVING left_quota<=0) as username);” $DB

 

disconnect.sh

#!/bin/bash
DB=’数据库名’
DBADMIN=’账号’
DBPASSWD=’密码’

mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE log SET end_time=now(),bytes_received=’$bytes_received’,bytes_sent=’$bytes_sent’,total_used=(select sum(bytes_sent)+sum(bytes_received)),status=0 WHERE trusted_ip=’$trusted_ip’ AND trusted_port=$trusted_port AND remote_ip=’$ifconfig_pool_remote_ip’ AND username=’$common_name’ AND status=1;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “update stat set total_used=(select sum(total_used) from log where log.username=’$common_name’) WHERE stat.username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET used_quota=(select total_used from stat where stat.username=’$common_name’),left_quota=quota_bytes-used_quota WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET use_cycle=(TO_DAYS(NOW()) – TO_DAYS(creation)) WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET surplus_cycle=(quota_cycle – use_cycle) WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET enabled=0 WHERE username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET active=0 WHERE (quota_cycle – use_cycle) <= 0 AND username=’$common_name’;” $DB
mysql -u$DBADMIN -p$DBPASSWD -e “UPDATE user SET active=0 WHERE user.username IN (SELECT username FROM (SELECT log.username AS username,quota_bytes,left_quota,stat.origin_time FROM user,log,stat WHERE log.username=’$common_name’ AND log.username=user.username AND log.status=0 AND TO_DAYS(NOW())-TO_DAYS(stat.origin_time)<=quota_cycle GROUP BY log.username HAVING left_quota<=0) as username);” $DB

新建好三个文件后,赋予权限755.

这三个脚本可不少折腾博主,对于登陆才开始计时,达到周期后自动锁定登陆和流量统计,这些功能的脚本足足折腾了博主一天。

然后重启openvpn即可。

安装web管理界面,修改Angus下的config.php的数据库地址和密码,数据库名,把源码上传到服务器,即可。

下载是WEB源码下载链接:http://pan.baidu.com/s/1qYIhCKG 密码:t3r2

 

最后编辑:
作者:欧阳
欧阳
设计界的一只猿|平面设计|创意|WEB安全|互联网

openvpn控流系统计费系统web管理界面流量控制天数控制 开源》有 9 条评论

  1. 66 说:

    你好,这个新建的sql文件在phpmyadmin里SQL查询和导入都不可以,具体怎么操作可以更详细点吗?

  2. 说:

    有没有视频教程

  3. Hyun 说:

    下载 失效了。。。。

  4. 说:

    下载地址 已失效了。。。。

  5. charles 说:

    有git的地址吗,下载不了了

留下一个回复

你的email不会被公开。