会挽雕弓如满月,西北望,射天狼。 注册 | 登陆

openvpn使用mysql来验证

也有现成的一些插件,不过感觉不好用,自己用PHP简单写了一个。

openvpn server需要配置

C++代码
  1. tmp-dir /tmp   
  2. auth-user-pass-verify /etc/openvpn/verifyvpn.php via-env #验证   
  3.   
  4. client-cert-not-required #设置客户端不使用证书,可选   
  5. username-as-common-name   
  6.   
  7. client-connect /etc/openvpn/clientconnected.php #记录登录,可选   
  8. client-disconnect /etc/openvpn/clientdisconnected.php #记录退出,可选   

openvpn client需要配置

C++代码
  1. auth-user-pass  

使用这些PHP脚本文件

mysqlconfig.inc.php

PHP代码
  1. <?php   
  2. $server         = '127.0.0.1';   
  3. $username           = 'openvpn';   
  4. $password           = 'password';   
  5. $database           = 'openvpn';  

verifyvpn.php

PHP代码
  1. #!/usr/bin/env php   
  2. <?php   
  3. include_once 'mysqlconfig.inc.php';   
  4. $mysqli = new mysqli($server,$username,$password,$database);   
  5. if (mysqli_connect_errno()) {   
  6.     exit(1);   
  7. }   
  8. $user = $mysqli->real_escape_string($_ENV["username"]);   
  9. $pass = $mysqli->real_escape_string($_ENV["password"]);   
  10.   
  11. $rs = $mysqli->query("select id from user where name = '$user' and password = '$pass' and canlogin = 1 and expiretime > now()");   
  12.   
  13. $mysqli->close();   
  14.   
  15. if ($rs->num_rows == 1) {   
  16.     exit(0);   
  17. else {   
  18.     exit(1);   
  19. }    

clientconnected.php

PHP代码
  1. #!/usr/bin/env php   
  2. <?php   
  3. include_once 'mysqlconfig.inc.php';   
  4. $mysqli = new mysqli($server,$username,$password,$database);   
  5. if (mysqli_connect_errno()) {   
  6.     exit(1);   
  7. }   
  8. $user = $mysqli->real_escape_string($_ENV["common_name"]);   
  9. $ip = $mysqli->real_escape_string($_ENV["trusted_ip"]);   
  10.   
  11. $rs = $mysqli->query("select id from user where name = '$user'");   
  12. $id = -1;   
  13. if($rs->num_rows == 1){   
  14.     $id = $rs->fetch_object()->id;   
  15. }else{   
  16.     $mysqli->close();   
  17.     exit(1);   
  18. }   
  19.   
  20. $logintime = date('Y-m-d H:i:s');   
  21. $mysqli->query("update user set logincount = logincount +1, lastlogintime = '$logintime', lastloginip = '$ip', isonline = 1 where id = $id");   
  22.   
  23. if ($mysqli->affected_rows != 1) {   
  24.     $mysqli->close();   
  25.     exit(1);   
  26. }   
  27.   
  28. $mysqli->query("insert into log (userid, logintime, loginip) values ($id, '$logintime', '$ip')");   
  29.   
  30. if ($mysqli->affected_rows != 1) {   
  31.     $mysqli->close();   
  32.     exit(1);   
  33. }   
  34.   
  35. $mysqli->close();   
  36. exit(0);  

clientdisconnected.php

PHP代码
  1. #!/usr/bin/env php   
  2. <?php   
  3. include_once 'mysqlconfig.inc.php';   
  4. $mysqli = new mysqli($server,$username,$password,$database);   
  5. if (mysqli_connect_errno()) {   
  6.     exit(1);   
  7. }   
  8. $user = $mysqli->real_escape_string($_ENV["common_name"]);   
  9. $br = $mysqli->real_escape_string($_ENV["bytes_received"]);   
  10. $bt = $mysqli->real_escape_string($_ENV["bytes_sent"]);   
  11.   
  12. $rs = $mysqli->query("select id,lastlogintime,lastloginip from user where name = '$user'");   
  13. $id = -1;   
  14. $logintime = '';   
  15. $ip = '';   
  16. if($rs->num_rows == 1){   
  17.     $userinfo = $rs->fetch_object();   
  18.     $id = $userinfo->id;   
  19.     $logintime = $userinfo->lastlogintime;   
  20.     $ip = $userinfo->lastloginip;   
  21. }else{   
  22.     $mysqli->close();   
  23.     exit(1);   
  24. }   
  25.   
  26. $logouttime = date('Y-m-d H:i:s');   
  27. $mysqli->query("update user set isonline = 0, bytes_received = bytes_received + $br, bytes_sent = bytes_sent + $bt where id = $id");   
  28.   
  29. if ($mysqli->affected_rows != 1) {   
  30.     $mysqli->close();   
  31.     exit(1);   
  32. }   
  33.   
  34. $mysqli->query("update log set logouttime = '$logouttime', bytes_received = $br, bytes_sent = $bt where userid = $id and logintime = '$logintime' and loginip = '$ip'");   
  35.   
  36. if ($mysqli->affected_rows != 1) {   
  37.     $mysqli->close();   
  38.     exit(1);   
  39. }   
  40.   
  41. $mysqli->close();   
  42. exit(0);  

数据库结构

SQL代码
  1. -- phpMyAdmin SQL Dump   
  2. -- version 3.2.5   
  3. -- http://www.phpmyadmin.net   
  4. --   
  5. -- 主机: 127.0.0.1   
  6. -- 生成日期: 2010 年 02 月 16 日 16:58   
  7. -- 服务器版本: 5.0.77   
  8. -- PHP 版本: 5.3.1   
  9.   
  10. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";   
  11.   
  12. --   
  13. -- 数据库: `openvpn`   
  14. --   
  15.   
  16. -- --------------------------------------------------------   
  17.   
  18. --   
  19. -- 表的结构 `log`   
  20. --   
  21.   
  22. CREATE TABLE IF NOT EXISTS `log` (   
  23.   `id` int(11) NOT NULL auto_increment,   
  24.   `userid` int(11) NOT NULL,   
  25.   `logintime` datetime default NULL,   
  26.   `loginip` char(46) default NULL,   
  27.   `logouttime` datetime default NULL,   
  28.   `bytes_received` bigint(20) NOT NULL,   
  29.   `bytes_sent` bigint(20) NOT NULL,   
  30.   PRIMARY KEY  (`id`),   
  31.   KEY `userid` (`userid`)   
  32. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;   
  33.   
  34. -- --------------------------------------------------------   
  35.   
  36. --   
  37. -- 表的结构 `user`   
  38. --   
  39.   
  40. CREATE TABLE IF NOT EXISTS `user` (   
  41.   `id` int(11) NOT NULL auto_increment,   
  42.   `namevarchar(32) NOT NULL,   
  43.   `passwordchar(32) NOT NULL,   
  44.   `canlogin` tinyint(1) NOT NULL,   
  45.   `logincount` int(11) NOT NULL,   
  46.   `isonline` tinyint(1) NOT NULL,   
  47.   `expiretime` datetime NOT NULL,   
  48.   `lastlogintime` datetime default NULL,   
  49.   `lastloginip` char(46) default NULL,   
  50.   `bytes_received` bigint(20) NOT NULL,   
  51.   `bytes_sent` bigint(20) NOT NULL,   
  52.   PRIMARY KEY  (`id`),   
  53.   UNIQUE KEY `name` (`name`)   
  54. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1;   

管理的话直接使用phpMyAdmin吧,呵呵,前台和后台现在还没有做。

openvpn其它设置与一般的一样。

Tags: openvpn, php, mysql

« 上一篇 | 下一篇 »

只显示5条记录相关文章

从z-blog 1.7到sablog-x 1.6 (浏览: 4998, 评论: 4)
php unescape iconv无效时 (浏览: 1309, 评论: 0)
PHP中MySQL查询语句的转义 (浏览: 4085, 评论: 0)

1条记录访客评论

很好,学习了O(∩_∩)O~

Post by 白癜风 on 2010, August 9, 11:01 AM 引用此文发表评论 #1


发表评论

评论内容 (必填):