wifidog 源碼初分析(二)

上一篇分析了接入設備的首次瀏覽器訪問請求如何通過 防火牆過濾規則 重定向到 wifidog 的 HTTP 服務中,本篇主要分析了 wifidog 在接收到 接入設備的 HTTP 訪問請求後,如何將此 HTTP 請求重定向到 認證服務器(auth-server) 上。

通過上面的防火牆規則,會將通過上面的防火牆規則,會將HTTP請求的外部IP地址和端口通過NAT方式重定向至本地wifidog內嵌HTTP服務器的地址和端口上,並由內嵌HTTP服務器進行服務,而內嵌HTTP服務器的路徑和回調處理如下:

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,如下:

/** The 404 handler is also responsible for redirecting to the auth server */
void http_callback_404(httpd *webserver, request *r)
{
    char        tmp_url[MAX_BUF],
        *url;
    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.
     */
    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()) {
        /* The internet connection is down at the moment  - apologize and do not redirect anywhere */
        char * buf;
        safe_asprintf(&buf, 
            "<p>We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable.</p>"
            "<p>If at all possible, please notify the owners of this hotspot that the internet connection is out of service.</p>"
            "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>"
            "<p>In a while please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);

        send_http_page(r, "Uh oh! Internet access unavailable!", buf);
        free(buf);
        debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr);
    }
    else if (!is_auth_online()) {
        /* The auth server is down at the moment - apologize and do not redirect anywhere */
        char * buf;
        safe_asprintf(&buf, 
            "<p>We apologize, but it seems that we are currently unable to re-direct you to the login screen.</p>"
            "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>"
        "<p>In a couple of minutes please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);

        send_http_page(r, "Uh oh! Login screen unavailable!", buf);
        free(buf);
        debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr);
    }
    else {
        /* Re-direct them to auth server */
        char *urlFragment;
        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);
        debug(LOG_INFO, "Captured %s requesting [%s] and re-directing them to login page", r->clientAddr, url);
        http_send_redirect_to_auth(r, urlFragment, "Redirect to login page");
        free(urlFragment);
    }
    free(url);
}
上面代碼基本不用解釋,具體重定向至auth server的消息在下面的 http_send_redirect_to_auth 函數中實現:

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
gw_address,路由器的LAN地址

gw_port:爲wifidog的監聽端口

gw_id:路由器的標識名

mac:客戶端設備的MAC地址

url:爲客戶端訪問的原URL(以便於重定向)




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章