上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 HTTP 请求重定向到 认证服务器(auth-server) 上。

通过上面的防火墙规则,会将通过上面的防火墙规则,会将HTTP请求的外部IP地址和端口通过NAT方式重定向至本地wifidog内嵌HTTP服务器的地址和端口上,并由内嵌HTTP服务器进行服务,而内嵌HTTP服务器的路径和回调处理如下:

[cpp] view plaincopy

  1. if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) {  
  2. debug(LOG_ERR, "Could not create web server: %s", strerror(errno));  
  3. exit(1);  
  4. }  
  5. debug(LOG_DEBUG, "Assigning callbacks to web server");  
  6. httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog);  
  7. httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog);  
  8. httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);  
  9. httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);  
  10. httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);  
  11. httpdAddC404Content(webserver, http_callback_404);

客户端首次访问时回调客户端首次访问时回调http_callback_404函数,在该函数中根据获取的客户端信息来配置重定向的URL fragment,如下:

[cpp] view plaincopy

  1. void
  2. http_callback_404(httpd *webserver, request *r)  
  3. {  
  4. char tmp_url[MAX_BUF],  
  5. *url,  
  6. *mac;  
  7. s_config    *config = config_get_config();  
  8. t_auth_serv *auth_server = get_auth_server();  
  9. memset(tmp_url, 0, sizeof(tmp_url));  
  10. /*
  11. * XXX Note the code below assumes that the client's request is a plain
  12. * http request to a standard port. At any rate, this handler is called only
  13. * if the internet/auth server is down so it's not a huge loss, but still.
  14. */ /* 用户需要访问的URL */
  15. snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s%s%s",  
  16. r->request.host,  
  17. r->request.path,  
  18. r->request.query[0] ? "?" : "",  
  19. r->request.query);  
  20. url = httpdUrlEncode(tmp_url);  
  21. if (!is_online()) {  
  22. /* 路由器都接入不到 internet */
  23. char * buf;  
  24. send_http_page(r, "Uh oh! Internet access unavailable!", buf);  
  25. free(buf);  
  26. }  
  27. else if (!is_auth_online()) {  
  28. /* auth server 挂起 */
  29. char * buf;  
  30. send_http_page(r, "Uh oh! Login screen unavailable!", buf);  
  31. free(buf);  
  32. }  
  33. else {  
  34. /* 配置重定向到 auth server 的 url 参数 */
  35. char *urlFragment;  
  36. if (!(mac = arp_get(r->clientAddr))) {  
  37. /* We could not get their MAC address */
  38. debug(LOG_INFO, "Failed to retrieve MAC address for ip %s, so not putting in the login request", r->clientAddr);  
  39. safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&url=%s",  
  40. auth_server->authserv_login_script_path_fragment,  
  41. config->gw_address,  
  42. config->gw_port,  
  43. config->gw_id,  
  44. url);  
  45. } else {            
  46. debug(LOG_INFO, "Got client MAC address for ip %s: %s", r->clientAddr, mac);  
  47. safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&mac=%s&url=%s",  
  48. auth_server->authserv_login_script_path_fragment,  
  49. config->gw_address,  
  50. config->gw_port,  
  51. config->gw_id,  
  52. mac,  
  53. url);  
  54. }  
  55. /* 调用该函数将用户请求重定向到 auth server 的登录页面 */
  56. http_send_redirect_to_auth(r, urlFragment, "Redirect to login page");  
  57. free(urlFragment);  
  58. }  
  59. free(url);  
  60. }

上面代码基本不用解释,具体重定向至auth server的消息在下面的 http_send_redirect_to_auth 函数中实现:

[cpp] view plaincopy

  1. void http_send_redirect_to_auth(request *r, char *urlFragment, char *text)  
  2. {  
  3. char *protocol = NULL;  
  4. int port = 80;  
  5. t_auth_serv *auth_server = get_auth_server();  
  6. if (auth_server->authserv_use_ssl) {  
  7. protocol = "https";  
  8. port = auth_server->authserv_ssl_port;  
  9. } else {  
  10. protocol = "http";  
  11. port = auth_server->authserv_http_port;  
  12. }  
  13. char *url = NULL;  
  14. safe_asprintf(&url, "%s://%s:%d%s%s",  
  15. protocol,  
  16. auth_server->authserv_hostname,  
  17. port,  
  18. auth_server->authserv_path,  
  19. urlFragment  
  20. );  
  21. http_send_redirect(r, url, text);  
  22. free(url);  
  23. }

具体的重定向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)-转的更多相关文章

  1. wifidog 源码初分析(4)-转

    在上一篇<wifidog 源码处分析(3)>的流程结束后,接入设备的浏览器重定向至 路由器 上 wifidog 的 http 服务(端口 2060) /wifidog/auth 上(且携带 ...

  2. wifidog 源码初分析(3)-转

    上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 HTTP 请求重定向至 auth-server 的流程. 之后 接入设备 的浏览器接收到 ...

  3. wifidog 源码初分析(1)-转

    wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码. 在路由器上启动 wifidog 之后,wifid ...

  4. wifidog源码分析 - 用户连接过程

    引言 之前的文章已经描述wifidog大概的一个工作流程,这里我们具体说说wifidog是怎么把一个新用户重定向到认证服务器中的,它又是怎么对一个已认证的用户实行放行操作的.我们已经知道wifidog ...

  5. wifidog源码分析 - wifidog原理 tiger

    转:http://www.cnblogs.com/tolimit/p/4223644.html wifidog源码分析 - wifidog原理 wifidog是一个用于配合认证服务器实现无线网页认证功 ...

  6. Hadoop学习笔记(9) ——源码初窥

    Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...

  7. MapReduce的ReduceTask任务的运行源码级分析

    MapReduce的MapTask任务的运行源码级分析 这篇文章好不容易恢复了...谢天谢地...这篇文章讲了MapTask的执行流程.咱们这一节讲解ReduceTask的执行流程.ReduceTas ...

  8. Activity源码简要分析总结

    Activity源码简要分析总结 摘自参考书籍,只列一下结论: 1. Activity的顶层View是DecorView,而我们在onCreate()方法中通过setContentView()设置的V ...

  9. MapReduce的MapTask任务的运行源码级分析

    TaskTracker任务初始化及启动task源码级分析 这篇文章中分析了任务的启动,每个task都会使用一个进程占用一个JVM来执行,org.apache.hadoop.mapred.Child方法 ...

随机推荐

  1. HDU 4813 Hard Code(水题,2013年长春现场赛A题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4813 签到题. 把一个字符串按照格式输出就可以了,很水 #include <stdio.h> ...

  2. Java IO:同步、非堵塞式IO(NIO)

    转载请注明出处:jiq•钦's technical Blog 引言 JDK1.4中引入了NIO,即New IO,目的在于提高IO速度.特别注意JavaNIO不全然是非堵塞式IO(No-Blocking ...

  3. LPC-LINK 2 Board IO

  4. Using Dtrace OEL 6.X

    http://www.hhutzler.de/blog/using-dtrace/ http://docs.oracle.com/cd/E37670_01/E38608/html/dt_sdtparg ...

  5. 让AngularJS的controllers之间共享数据

    如何让controller之间共享数据呢?大致是让不同controller中的变量指向同一个实例. 通过service创建一个存放共享数据的对象. .service("greeting&qu ...

  6. 利用进程ID获取主线程ID

    利用进程ID获取主线程ID,仅适用于单线程.多线程应区分哪个是主线程,区分方法待验证 (1)好像可以用StartTime最早的,不过通过线程执行时间不一定可靠,要是在最开始就CreateThread了 ...

  7. Android中Service类onStartCommand的返回值问题

    Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...

  8. 网易游戏2015年暑期实习生面试经历-游戏研发project师

    首先,我还是先介绍一下网易游戏吧.引用别人的一段话 作者:王选易.出处: http://www.cnblogs.com/neverdie/ 欢迎转载 .也请保留这段声明.假设你喜欢这篇文章,请点[推荐 ...

  9. ASP.NET MVC:模块化/插件式文章汇总

    方案 Shazwazza | Developing a plugin framework in ASP.NET MVC with medium trust 基于ASP.NET MVC3 Razor的模 ...

  10. iOS开发UI篇章 15-项目中的常见文件

    iOS开发UI篇-常见的项目文件介绍 一.项目文件结构示意图 二.文件介绍 1.products目录:主要用于mac电脑开发的可运行文件.ios开发用不到这个文件 2.frameworks目录主要用来 ...