也有现成的一些插件,不过感觉不好用,自己用PHP简单写了一个。
openvpn server需要配置
C++代码
- tmp-dir /tmp
- auth-user-pass-verify /etc/openvpn/verifyvpn.php via-env #验证
- client-cert-not-required #设置客户端不使用证书,可选
- username-as-common-name
- client-connect /etc/openvpn/clientconnected.php #记录登录,可选
- client-disconnect /etc/openvpn/clientdisconnected.php #记录退出,可选
openvpn client需要配置
C++代码
- auth-user-pass
使用这些PHP脚本文件
mysqlconfig.inc.php
PHP代码
- <?php
- $server = '127.0.0.1';
- $username = 'openvpn';
- $password = 'password';
- $database = 'openvpn';
verifyvpn.php
PHP代码
- #!/usr/bin/env php
- <?php
- include_once 'mysqlconfig.inc.php';
- $mysqli = new mysqli($server,$username,$password,$database);
- if (mysqli_connect_errno()) {
- exit(1);
- }
- $user = $mysqli->real_escape_string($_ENV["username"]);
- $pass = $mysqli->real_escape_string($_ENV["password"]);
- $rs = $mysqli->query("select id from user where name = '$user' and password = '$pass' and canlogin = 1 and expiretime > now()");
- $mysqli->close();
- if ($rs->num_rows == 1) {
- exit(0);
- } else {
- exit(1);
- }
clientconnected.php
PHP代码
- #!/usr/bin/env php
- <?php
- include_once 'mysqlconfig.inc.php';
- $mysqli = new mysqli($server,$username,$password,$database);
- if (mysqli_connect_errno()) {
- exit(1);
- }
- $user = $mysqli->real_escape_string($_ENV["common_name"]);
- $ip = $mysqli->real_escape_string($_ENV["trusted_ip"]);
- $rs = $mysqli->query("select id from user where name = '$user'");
- $id = -1;
- if($rs->num_rows == 1){
- $id = $rs->fetch_object()->id;
- }else{
- $mysqli->close();
- exit(1);
- }
- $logintime = date('Y-m-d H:i:s');
- $mysqli->query("update user set logincount = logincount +1, lastlogintime = '$logintime', lastloginip = '$ip', isonline = 1 where id = $id");
- if ($mysqli->affected_rows != 1) {
- $mysqli->close();
- exit(1);
- }
- $mysqli->query("insert into log (userid, logintime, loginip) values ($id, '$logintime', '$ip')");
- if ($mysqli->affected_rows != 1) {
- $mysqli->close();
- exit(1);
- }
- $mysqli->close();
- exit(0);
clientdisconnected.php
PHP代码
- #!/usr/bin/env php
- <?php
- include_once 'mysqlconfig.inc.php';
- $mysqli = new mysqli($server,$username,$password,$database);
- if (mysqli_connect_errno()) {
- exit(1);
- }
- $user = $mysqli->real_escape_string($_ENV["common_name"]);
- $br = $mysqli->real_escape_string($_ENV["bytes_received"]);
- $bt = $mysqli->real_escape_string($_ENV["bytes_sent"]);
- $rs = $mysqli->query("select id,lastlogintime,lastloginip from user where name = '$user'");
- $id = -1;
- $logintime = '';
- $ip = '';
- if($rs->num_rows == 1){
- $userinfo = $rs->fetch_object();
- $id = $userinfo->id;
- $logintime = $userinfo->lastlogintime;
- $ip = $userinfo->lastloginip;
- }else{
- $mysqli->close();
- exit(1);
- }
- $logouttime = date('Y-m-d H:i:s');
- $mysqli->query("update user set isonline = 0, bytes_received = bytes_received + $br, bytes_sent = bytes_sent + $bt where id = $id");
- if ($mysqli->affected_rows != 1) {
- $mysqli->close();
- exit(1);
- }
- $mysqli->query("update log set logouttime = '$logouttime', bytes_received = $br, bytes_sent = $bt where userid = $id and logintime = '$logintime' and loginip = '$ip'");
- if ($mysqli->affected_rows != 1) {
- $mysqli->close();
- exit(1);
- }
- $mysqli->close();
- exit(0);
数据库结构
SQL代码
- -- phpMyAdmin SQL Dump
- -- version 3.2.5
- -- http://www.phpmyadmin.net
- --
- -- 主机: 127.0.0.1
- -- 生成日期: 2010 年 02 月 16 日 16:58
- -- 服务器版本: 5.0.77
- -- PHP 版本: 5.3.1
- SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
- --
- -- 数据库: `openvpn`
- --
- -- --------------------------------------------------------
- --
- -- 表的结构 `log`
- --
- CREATE TABLE IF NOT EXISTS `log` (
- `id` int(11) NOT NULL auto_increment,
- `userid` int(11) NOT NULL,
- `logintime` datetime default NULL,
- `loginip` char(46) default NULL,
- `logouttime` datetime default NULL,
- `bytes_received` bigint(20) NOT NULL,
- `bytes_sent` bigint(20) NOT NULL,
- PRIMARY KEY (`id`),
- KEY `userid` (`userid`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- -- --------------------------------------------------------
- --
- -- 表的结构 `user`
- --
- CREATE TABLE IF NOT EXISTS `user` (
- `id` int(11) NOT NULL auto_increment,
- `name` varchar(32) NOT NULL,
- `password` char(32) NOT NULL,
- `canlogin` tinyint(1) NOT NULL,
- `logincount` int(11) NOT NULL,
- `isonline` tinyint(1) NOT NULL,
- `expiretime` datetime NOT NULL,
- `lastlogintime` datetime default NULL,
- `lastloginip` char(46) default NULL,
- `bytes_received` bigint(20) NOT NULL,
- `bytes_sent` bigint(20) NOT NULL,
- PRIMARY KEY (`id`),
- UNIQUE KEY `name` (`name`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
管理的话直接使用phpMyAdmin吧,呵呵,前台和后台现在还没有做。
openvpn其它设置与一般的一样。
#1
