wifidog 源碼初分析(2)-轉

來源:互聯網
上載者:User

標籤:blog   code   http   ext   com   get   

上一篇分析了接入裝置的首次瀏覽器訪問請求如何通過 防火牆過濾規則 重新導向到 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);  

上面代碼基本不用解釋,具體重新導向至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);  

具體的重新導向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(以便於重新導向)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.