wifidog 认证 php

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”作为回应。

2.具体php实现代码如下

<pre code_snippet_id="335795" snippet_file_name="blog_20140509_1_6007550" name="code">public function auth()  
    {  
    <span style="white-space:pre">  </span>//响应客户端的定时认证,可在此处做各种统计、<a title="计费" href="index.php?c=search&amp;key=%E8%AE%A1%E8%B4%B9" target="_blank">计费</a>等等  
    <span style="white-space:pre">  </span>/*  
    <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的文档  
    <span style="white-space:pre">  </span>wifidog主要提交如下参数  
    <span style="white-space:pre">  </span>1.ip  
    <span style="white-space:pre">  </span>2. mac  
    <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)  
    <span style="white-space:pre">  </span>4.incoming 下载流量  
    <span style="white-space:pre">  </span>5.outgoing 上传流量  
    <span style="white-space:pre">  </span>6.stage  认证阶段,就两种 login 和 counters  
    <span style="white-space:pre">  </span>*/  


    <span style="white-space:pre">  </span>$stage = $_GET['stage'] == 'counters'?'counters':'login';  
    <span style="white-space:pre">  </span>if($stage == 'login')  
    <span style="white-space:pre">  </span>{  
    <span style="white-space:pre">      </span>//XXXX跳过login 阶段的处理XXXX不能随便跳过的  
    <span style="white-space:pre">      </span>//默认返回 允许  
    <span style="white-space:pre">      </span>echo "Auth: 1";  
    <span style="white-space:pre">  </span>}  
    <span style="white-space:pre">  </span>else if($stage == 'counters')  
    <span style="white-space:pre">  </span>{  

    <span style="white-space:pre">      </span>//做一个简单的流量判断验证,下载流量超值时,返回<a title="下线" href="index.php?c=search&amp;key=%E4%B8%8B%E7%BA%BF" target="_blank">下线</a>通知,否则保持在线  
    <span style="white-space:pre">      </span>if(!empty($_GET['incoming']) and $_GET['incoming'] &gt; 10000000)  
    <span style="white-space:pre">      </span>{  
    <span style="white-space:pre">          </span>echo "Auth: 0";  
    <span style="white-space:pre">      </span>}else{  
    <span style="white-space:pre">          </span>echo "Auth: 1\n";  
    <span style="white-space:pre">      </span>}  
    <span style="white-space:pre">  </span>}  
    <span style="white-space:pre">  </span>else  
    <span style="white-space:pre">      </span>echo "Auth: 0"; //其他情况都返回拒绝  
    <span style="white-space:pre">      </span>  
    <span style="white-space:pre">      </span> <span style="white-space:pre">  </span>  
    <span style="white-space:pre">  </span>/*  
    <span style="white-space:pre">      </span>返回值:主要有这两种就够了  
    <span style="white-space:pre">  </span>0 - 拒绝  
    <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>  

    <span style="white-space:pre">  </span>官方文档如下  
    <span style="white-space:pre">  </span>0 - AUTH_DENIED - User firewall users are deleted and the user removed.  
    <span style="white-space:pre">  </span>6 - AUTH_VALIDATION_FAILED - User email validation timeout has occured and user/firewall is deleted(用户邮件验证超时,防火墙关闭该用户)  
    <span style="white-space:pre">  </span>1 - AUTH_ALLOWED - User was valid, add firewall rules if not present  
    <span style="white-space:pre">  </span>5 - AUTH_VALIDATION - Permit user access to email to get validation email under default rules (用户邮件验证时,向用户开放email)  
    <span style="white-space:pre">  </span>-1 - AUTH_ERROR - An error occurred during the validation process  
    <span style="white-space:pre">  </span>*/  
    }  
    public function portal()  
    {  
    <span style="white-space:pre">  </span>/*  
    <span style="white-space:pre">  </span> wifidog 带过来的参数 如下  
    <span style="white-space:pre">  </span>1. gw_id  
    <span style="white-space:pre">  </span>*/  
    <span style="white-space:pre">  </span>//重定到指定网站 或者 显示splash广告页面  
    <span style="white-space:pre">  </span>redirect('http://www.baidu.com', 'location', 302);  
    <span style="white-space:pre">      </span>  
    }  
    public function ping()  
    {  
    <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";  
    <span style="white-space:pre">  </span>//log_message($this-&gt;config-&gt;item('MY_log_threshold'), __CLASS__.':'.__FUNCTION__.':'.debug_printarray($_GET));  

    <span style="white-space:pre">  </span>//判断各种参数是否为空  
    <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']) ) )  
    <span style="white-space:pre">  </span>{  
    <span style="white-space:pre">      </span>echo '{"error":"2"}';  
    <span style="white-space:pre">      </span>return;  
    <span style="white-space:pre">  </span>}  
    <span style="white-space:pre">  </span>//添加<a title="心跳" href="index.php?c=search&amp;key=%E5%BF%83%E8%B7%B3" target="_blank">心跳</a>日志处理功能  
    <span style="white-space:pre">  </span>/*  
    <span style="white-space:pre">      </span>此处可获取 wififog提供的 如下参数  
    <span style="white-space:pre">  </span>1.gw_id  来自wifidog 配置文件中,用来区分不同的路由设备  
    <span style="white-space:pre">  </span>2.sys_uptime 路由器的系统启动时间  
    <span style="white-space:pre">  </span>3.sys_memfree 系统内存使用百分比  
    <span style="white-space:pre">  </span>4.wifidog_uptime wifidog持续运行时间(这个数据经常会有问题)  
    <span style="white-space:pre">  </span>*/  

    <span style="white-space:pre">  </span>//返回值  
    <span style="white-space:pre">  </span>echo 'Pong';  
    }  
    /**  
     * wifidog 的gw_message 接口,信息提示页面  
     */  
    function gw_message()  
    {  
    <span style="white-space:pre">  </span>if (isset($_REQUEST["message"])) {  
    <span style="white-space:pre">      </span>switch ($_REQUEST["message"]) {  
    <span style="white-space:pre">          </span>case 'failed_validation':  
    <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>处理  
    <span style="white-space:pre">              </span>//认证失败,请重新认证  
    <span style="white-space:pre">              </span>break;  
    <span style="white-space:pre">          </span>case 'denied':  
    <span style="white-space:pre">              </span>//auth的stage为login时,被服务器返回AUTH_DENIED时,来到该处处理  
    <span style="white-space:pre">              </span>//认证被拒  
    <span style="white-space:pre">              </span>break;  
    <span style="white-space:pre">          </span>case 'activate':  
    <span style="white-space:pre">              </span>//auth的stage为login时,被服务器返回AUTH_VALIDATION时,来到该处处理  
    <span style="white-space:pre">              </span>//待激活  
    <span style="white-space:pre">              </span>break;  
    <span style="white-space:pre">          </span>default:  
    <span style="white-space:pre">              </span>break;  
    <span style="white-space:pre">      </span>}  
    <span style="white-space:pre">  </span>}else{  
    <span style="white-space:pre">      </span>//不回显任何信息  
    <span style="white-space:pre">  </span>}  
    }</pre>  
</p>  

本文章由 http://www.wifidog.pro/2015/01/28/wifidog-php-1.html 整理编辑,转载请注明出处

标签: wifidog认证 wifidog安装 wifidog原理 wifidog分析 wifidog配置 wifidog流程 wifidog服务器 wifidog-ddwrt wifidog openwrt