[转载]wifidog 认证 php - 逆雪寒的天坑 - 博客频道 - CSDN.NET

[转载]wifidog 认证 php – 逆雪寒的天坑 – 博客频道 – CSDN.NET.

<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">1.首先简单说说wifidog认证的过程


客户端首次连接到wifi后,浏览器请求将会被重定向到:


login/?gw_address=%s&gw_port=%d&gw_id=%s&url=%s


验证通过后,客户端被重定向到网关,url格式如下:


http://网关地址:网关端口/wifidog/auth?token=


wifidong会启动一个线程周期性地报告每一个用户的状态信息,并通过如下地址发送给认证


服务器:


auth_server:/auth/?stage=


ip=


mac=


token=


incoming=


outgoing=


认证服务器根据该状态信息决定是否允许该用户继续连接,并回复网关,回复格式为:Auth:状态码,


如:Auth:1


常用状态码:


0:AUTH_DENIED,表示拒绝


1:AUTH_ALLOWED,验证通过


验证通过后,将重定向到如下地址:


portal/?gw_id=%s


wifidog的ping协议


wifidog通过ping协议将当前状态信息发送给认证服务器,发送地址为:


http://auth_sever/ping/?


gw_id=%s


sys_uptime=%lu


sys_memfree=%u


sys_load=%.2f


wifidog_uptime=%lu


认证服务器须返回一个“Pong”作为回应。


具体php实现代码如下


<pre code_snippet_id="335795" snippet_file_name="blog_20140509_1_6007550" name="code">public function auth()
&nbsp; &nbsp; {
&nbsp; &nbsp; <span style="white-space:pre">	</span>//响应客户端的定时认证,可在此处做各种统计、<a title="计费" href="index.php?c=search&amp;key=%E8%AE%A1%E8%B4%B9" target="_blank">计费</a>等等
&nbsp; &nbsp; <span style="white-space:pre">	</span>/*
&nbsp; &nbsp; <span style="white-space:pre">		</span>wifidog 会通过这个接口传递连接客户端的信息,然后根据返回,对客户端做<a title="开通" href="index.php?c=search&amp;key=%E5%BC%80%E9%80%9A" target="_blank">开通</a>、断开等处理,具体返回值可以看wifidog的文档
&nbsp; &nbsp; <span style="white-space:pre">	</span>wifidog主要提交如下参数
&nbsp; &nbsp; <span style="white-space:pre">	</span>1.ip
&nbsp; &nbsp; <span style="white-space:pre">	</span>2. mac
&nbsp; &nbsp; <span style="white-space:pre">	</span>3. token(login页面<a title="下发" href="index.php?c=search&amp;key=%E4%B8%8B%E5%8F%91" target="_blank">下发</a>的token)
&nbsp; &nbsp; <span style="white-space:pre">	</span>4.incoming 下载流量
&nbsp; &nbsp; <span style="white-space:pre">	</span>5.outgoing 上传流量
&nbsp; &nbsp; <span style="white-space:pre">	</span>6.stage &nbsp;认证阶段,就两种 login 和 counters
&nbsp; &nbsp; <span style="white-space:pre">	</span>*/
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; <span style="white-space:pre">	</span>$stage = $_GET['stage'] == 'counters'?'counters':'login';
&nbsp; &nbsp; <span style="white-space:pre">	</span>if($stage == 'login')
&nbsp; &nbsp; <span style="white-space:pre">	</span>{
&nbsp; &nbsp; <span style="white-space:pre">		</span>//XXXX跳过login 阶段的处理XXXX不能随便跳过的
&nbsp; &nbsp; <span style="white-space:pre">		</span>//默认返回 允许
&nbsp; &nbsp; <span style="white-space:pre">		</span>echo "Auth: 1";
&nbsp; &nbsp; <span style="white-space:pre">	</span>}
&nbsp; &nbsp; <span style="white-space:pre">	</span>else if($stage == 'counters')
&nbsp; &nbsp; <span style="white-space:pre">	</span>{
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; <span style="white-space:pre">		</span>//做一个简单的流量判断验证,下载流量超值时,返回<a title="下线" href="index.php?c=search&amp;key=%E4%B8%8B%E7%BA%BF" target="_blank">下线</a>通知,否则保持在线
&nbsp; &nbsp; <span style="white-space:pre">		</span>if(!empty($_GET['incoming']) and $_GET['incoming'] &gt; 10000000)
&nbsp; &nbsp; <span style="white-space:pre">		</span>{
&nbsp; &nbsp; <span style="white-space:pre">			</span>echo "Auth: 0";
&nbsp; &nbsp; <span style="white-space:pre">		</span>}else{
&nbsp; &nbsp; <span style="white-space:pre">			</span>echo "Auth: 1\n";
&nbsp; &nbsp; <span style="white-space:pre">		</span>}
&nbsp; &nbsp; <span style="white-space:pre">	</span>}
&nbsp; &nbsp; <span style="white-space:pre">	</span>else
&nbsp; &nbsp; <span style="white-space:pre">		</span>echo "Auth: 0"; //其他情况都返回拒绝
&nbsp; &nbsp; <span style="white-space:pre">		</span>
&nbsp; &nbsp; <span style="white-space:pre">		</span> <span style="white-space:pre">	</span>
&nbsp; &nbsp; <span style="white-space:pre">	</span>/*
&nbsp; &nbsp; <span style="white-space:pre">		</span>返回值:主要有这两种就够了
&nbsp; &nbsp; <span style="white-space:pre">	</span>0 - 拒绝
&nbsp; &nbsp; <span style="white-space:pre">	</span>1 - <a title="放行" href="index.php?c=search&amp;key=%E6%94%BE%E8%A1%8C" target="_blank">放行</a>
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; <span style="white-space:pre">	</span>官方文档如下
&nbsp; &nbsp; <span style="white-space:pre">	</span>0 - AUTH_DENIED - User firewall users are deleted and the user removed.
&nbsp; &nbsp; <span style="white-space:pre">	</span>6 - AUTH_VALIDATION_FAILED - User email validation timeout has occured and user/firewall is deleted(用户邮件验证超时,防火墙关闭该用户)
&nbsp; &nbsp; <span style="white-space:pre">	</span>1 - AUTH_ALLOWED - User was valid, add firewall rules if not present
&nbsp; &nbsp; <span style="white-space:pre">	</span>5 - AUTH_VALIDATION - Permit user access to email to get validation email under default rules (用户邮件验证时,向用户开放email)
&nbsp; &nbsp; <span style="white-space:pre">	</span>-1 - AUTH_ERROR - An error occurred during the validation process
&nbsp; &nbsp; <span style="white-space:pre">	</span>*/
&nbsp; &nbsp; }
&nbsp; &nbsp; public function portal()
&nbsp; &nbsp; {
&nbsp; &nbsp; <span style="white-space:pre">	</span>/*
&nbsp; &nbsp; <span style="white-space:pre">	</span> wifidog 带过来的参数 如下
&nbsp; &nbsp; <span style="white-space:pre">	</span>1. gw_id
&nbsp; &nbsp; <span style="white-space:pre">	</span>*/
&nbsp; &nbsp; <span style="white-space:pre">	</span>//重定到指定网站 或者 显示splash广告页面
&nbsp; &nbsp; <span style="white-space:pre">	</span>redirect('http://www.baidu.com', 'location', 302);
&nbsp; &nbsp; <span style="white-space:pre">		</span>
&nbsp; &nbsp; }
&nbsp; &nbsp; public function ping()
&nbsp; &nbsp; {
&nbsp; &nbsp; <span style="white-space:pre">	</span>//url请求 "gw_id=$gw_id&amp;sys_uptime=$sys_uptime&amp;sys_memfree=$sys_memfree&amp;sys_load=$sys_load&amp;wifidog_uptime=$wifidog_uptime";
&nbsp; &nbsp; <span style="white-space:pre">	</span>//log_message($this-&gt;config-&gt;item('MY_log_threshold'), __CLASS__.':'.__FUNCTION__.':'.debug_printarray($_GET));
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; <span style="white-space:pre">	</span>//判断各种参数是否为空
&nbsp; &nbsp; <span style="white-space:pre">	</span>if( !(isset($_GET['gw_id']) and isset($_GET['sys_uptime']) and isset($_GET['sys_memfree']) and isset($_GET['sys_load']) and isset($_GET['wifidog_uptime']) ) )
&nbsp; &nbsp; <span style="white-space:pre">	</span>{
&nbsp; &nbsp; <span style="white-space:pre">		</span>echo '{"error":"2"}';
&nbsp; &nbsp; <span style="white-space:pre">		</span>return;
&nbsp; &nbsp; <span style="white-space:pre">	</span>}
&nbsp; &nbsp; <span style="white-space:pre">	</span>//添加<a title="心跳" href="index.php?c=search&amp;key=%E5%BF%83%E8%B7%B3" target="_blank">心跳</a>日志处理功能
&nbsp; &nbsp; <span style="white-space:pre">	</span>/*
&nbsp; &nbsp; <span style="white-space:pre">		</span>此处可获取 wififog提供的 如下参数
&nbsp; &nbsp; <span style="white-space:pre">	</span>1.gw_id &nbsp;来自wifidog 配置文件中,用来区分不同的路由设备
&nbsp; &nbsp; <span style="white-space:pre">	</span>2.sys_uptime 路由器的系统启动时间
&nbsp; &nbsp; <span style="white-space:pre">	</span>3.sys_memfree 系统内存使用百分比
&nbsp; &nbsp; <span style="white-space:pre">	</span>4.wifidog_uptime wifidog持续运行时间(这个数据经常会有问题)
&nbsp; &nbsp; <span style="white-space:pre">	</span>*/
&nbsp; &nbsp;&nbsp;
&nbsp; &nbsp; <span style="white-space:pre">	</span>//返回值
&nbsp; &nbsp; <span style="white-space:pre">	</span>echo 'Pong';
&nbsp; &nbsp; }
&nbsp; &nbsp; /**
&nbsp; &nbsp; &nbsp;* wifidog 的gw_message 接口,信息提示页面
&nbsp; &nbsp; &nbsp;*/
&nbsp; &nbsp; function gw_message()
&nbsp; &nbsp; {
&nbsp; &nbsp; <span style="white-space:pre">	</span>if (isset($_REQUEST["message"])) {
&nbsp; &nbsp; <span style="white-space:pre">		</span>switch ($_REQUEST["message"]) {
&nbsp; &nbsp; <span style="white-space:pre">			</span>case 'failed_validation':
&nbsp; &nbsp; <span style="white-space:pre">				</span>//auth的stage为login时,被服务器返回AUTH_VALIDATION_FAILED时,<a title="来到" href="index.php?c=search&amp;key=%E6%9D%A5%E5%88%B0" target="_blank">来到</a><a title="该处" href="index.php?c=search&amp;key=%E8%AF%A5%E5%A4%84" target="_blank">该处</a>处理
&nbsp; &nbsp; <span style="white-space:pre">				</span>//认证失败,请重新认证
&nbsp; &nbsp; <span style="white-space:pre">				</span>break;
&nbsp; &nbsp; <span style="white-space:pre">			</span>case 'denied':
&nbsp; &nbsp; <span style="white-space:pre">				</span>//auth的stage为login时,被服务器返回AUTH_DENIED时,来到该处处理
&nbsp; &nbsp; <span style="white-space:pre">				</span>//认证被拒
&nbsp; &nbsp; <span style="white-space:pre">				</span>break;
&nbsp; &nbsp; <span style="white-space:pre">			</span>case 'activate':
&nbsp; &nbsp; <span style="white-space:pre">				</span>//auth的stage为login时,被服务器返回AUTH_VALIDATION时,来到该处处理
&nbsp; &nbsp; <span style="white-space:pre">				</span>//待激活
&nbsp; &nbsp; <span style="white-space:pre">				</span>break;
&nbsp; &nbsp; <span style="white-space:pre">			</span>default:
&nbsp; &nbsp; <span style="white-space:pre">				</span>break;
&nbsp; &nbsp; <span style="white-space:pre">		</span>}
&nbsp; &nbsp; <span style="white-space:pre">	</span>}else{
&nbsp; &nbsp; <span style="white-space:pre">		</span>//不回显任何信息
&nbsp; &nbsp; <span style="white-space:pre">	</span>}
&nbsp; &nbsp; }</pre>
</p>
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏