wifidog 源码初分析(2)-转
上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 HTTP 请求重定向到 认证服务器(auth-server) 上。
通过上面的防火墙规则,会将通过上面的防火墙规则,会将HTTP请求的外部IP地址和端口通过NAT方式重定向至本地wifidog内嵌HTTP服务器的地址和端口上,并由内嵌HTTP服务器进行服务,而内嵌HTTP服务器的路径和回调处理如下:
[cpp] view plaincopy
- if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) {
- debug(LOG_ERR, "Could not create web server: %s", strerror(errno));
- exit(1);
- }
- debug(LOG_DEBUG, "Assigning callbacks to web server");
- httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog);
- httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog);
- httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);
- httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);
- httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);
- httpdAddC404Content(webserver, http_callback_404);
客户端首次访问时回调客户端首次访问时回调http_callback_404函数,在该函数中根据获取的客户端信息来配置重定向的URL fragment,如下:
[cpp] view plaincopy
- void
- http_callback_404(httpd *webserver, request *r)
- {
- char tmp_url[MAX_BUF],
- *url,
- *mac;
- s_config *config = config_get_config();
- t_auth_serv *auth_server = get_auth_server();
- memset(tmp_url, 0, sizeof(tmp_url));
- /*
- * XXX Note the code below assumes that the client's request is a plain
- * http request to a standard port. At any rate, this handler is called only
- * if the internet/auth server is down so it's not a huge loss, but still.
- */ /* 用户需要访问的URL */
- snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s%s%s",
- r->request.host,
- r->request.path,
- r->request.query[0] ? "?" : "",
- r->request.query);
- url = httpdUrlEncode(tmp_url);
- if (!is_online()) {
- /* 路由器都接入不到 internet */
- char * buf;
- send_http_page(r, "Uh oh! Internet access unavailable!", buf);
- free(buf);
- }
- else if (!is_auth_online()) {
- /* auth server 挂起 */
- char * buf;
- send_http_page(r, "Uh oh! Login screen unavailable!", buf);
- free(buf);
- }
- else {
- /* 配置重定向到 auth server 的 url 参数 */
- char *urlFragment;
- if (!(mac = arp_get(r->clientAddr))) {
- /* We could not get their MAC address */
- debug(LOG_INFO, "Failed to retrieve MAC address for ip %s, so not putting in the login request", r->clientAddr);
- safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&url=%s",
- auth_server->authserv_login_script_path_fragment,
- config->gw_address,
- config->gw_port,
- config->gw_id,
- url);
- } else {
- debug(LOG_INFO, "Got client MAC address for ip %s: %s", r->clientAddr, mac);
- safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&mac=%s&url=%s",
- auth_server->authserv_login_script_path_fragment,
- config->gw_address,
- config->gw_port,
- config->gw_id,
- mac,
- url);
- }
- /* 调用该函数将用户请求重定向到 auth server 的登录页面 */
- http_send_redirect_to_auth(r, urlFragment, "Redirect to login page");
- free(urlFragment);
- }
- free(url);
- }
上面代码基本不用解释,具体重定向至auth server的消息在下面的 http_send_redirect_to_auth 函数中实现:
[cpp] view plaincopy
- void http_send_redirect_to_auth(request *r, char *urlFragment, char *text)
- {
- char *protocol = NULL;
- int port = 80;
- t_auth_serv *auth_server = get_auth_server();
- if (auth_server->authserv_use_ssl) {
- protocol = "https";
- port = auth_server->authserv_ssl_port;
- } else {
- protocol = "http";
- port = auth_server->authserv_http_port;
- }
- char *url = NULL;
- safe_asprintf(&url, "%s://%s:%d%s%s",
- protocol,
- auth_server->authserv_hostname,
- port,
- auth_server->authserv_path,
- urlFragment
- );
- http_send_redirect(r, url, text);
- free(url);
- }
具体的重定向URL给个实例:
POST /login/?gw_address=192.168.1.1&gw_port=2060&gw_id=default&mac=44:94:fc:ef:28:40&url=http%3A//www.baidu.com/ HTTP/1.1
可以看到这里有这几个参数信息:
2gw_address,路由器的LAN地址
2gw_port:为wifidog的监听端口
2gw_id:路由器的标识名
2mac:客户端设备的MAC地址
2url:为客户端访问的原URL(以便于重定向)
wifidog 源码初分析(2)-转的更多相关文章
- wifidog 源码初分析(4)-转
在上一篇<wifidog 源码处分析(3)>的流程结束后,接入设备的浏览器重定向至 路由器 上 wifidog 的 http 服务(端口 2060) /wifidog/auth 上(且携带 ...
- wifidog 源码初分析(3)-转
上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 HTTP 请求重定向至 auth-server 的流程. 之后 接入设备 的浏览器接收到 ...
- wifidog 源码初分析(1)-转
wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码. 在路由器上启动 wifidog 之后,wifid ...
- wifidog源码分析 - 用户连接过程
引言 之前的文章已经描述wifidog大概的一个工作流程,这里我们具体说说wifidog是怎么把一个新用户重定向到认证服务器中的,它又是怎么对一个已认证的用户实行放行操作的.我们已经知道wifidog ...
- wifidog源码分析 - wifidog原理 tiger
转:http://www.cnblogs.com/tolimit/p/4223644.html wifidog源码分析 - wifidog原理 wifidog是一个用于配合认证服务器实现无线网页认证功 ...
- Hadoop学习笔记(9) ——源码初窥
Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...
- MapReduce的ReduceTask任务的运行源码级分析
MapReduce的MapTask任务的运行源码级分析 这篇文章好不容易恢复了...谢天谢地...这篇文章讲了MapTask的执行流程.咱们这一节讲解ReduceTask的执行流程.ReduceTas ...
- Activity源码简要分析总结
Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...
- MapReduce的MapTask任务的运行源码级分析
TaskTracker任务初始化及启动task源码级分析 这篇文章中分析了任务的启动,每个task都会使用一个进程占用一个JVM来执行,org.apache.hadoop.mapred.Child方法 ...
随机推荐
- CentOS下的apache配置支持php
修改Apache的配置文件httpd.conf(vi /etc/httpd/conf/httpd.conf) DirectoryIndex index.html index.php #添加index. ...
- Fiddler可以支持Websocket抓包了
今天试了一下,Fiddler已经可以支持客户端Websocket抓包了,并且查看的方式也非常方便. websocket作为一个标准的应用层的协议,在CS端程序用起来也比传统的tcp协议方便了,比较常见 ...
- LOG EXPLORER ApexSQL Log
http://www.minisoft.cn/ https://www.apexsql.com/sql_tools_log.aspx
- RPM软件包管理的查询功能 转
RPM软件包管理的查询功能: 命令格式 rpm {-q|--query} [select-options] [query-options] RPM的查询功能是极为强大,是极为重要的功能之一:举几个常用 ...
- 【Android基础篇】TabWidget设置背景和字体
在使用TabHost实现底部导航栏时,底部导航栏的三个导航button无法在布局文件中进行定制.比方设置点击时的颜色.字体的大小及颜色等,这里提供了一个解决的方法.就是在代码里进行定制. 思路是在Ac ...
- C# 其他图片格式转emf
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- JS 判断 undefined 类型
typeof 返回的是字符串,有六种可能:"number"."string"."boolean"."object".&q ...
- .Net中集合排序
public class StockQuantity { public StockQuantity(string status, DateTime dateTime, int quantity) { ...
- Java_集合操作_将元素插入List的指定位置
package test; import java.util.ArrayList; import java.util.List; public class test { public static v ...
- Android之greenDao,一个orm的使用
转自:http://blog.csdn.net/krislight/article/details/9391455 greenDaoMaster的学习研究 分类: 心得笔记 2013-07-20 16 ...