//Simple cgi that redirects the user int ICACHE_FLASH_ATTR cgi_redirect(http_connection *connData) { http_response_REDIRECT(connData, (char*)connData->cgi.argument); return HTTPD_CGI_DONE; }
//Cgi that check the request has the correct HOST header //Using it we can ensure our server has a domain of our choice int ICACHE_FLASH_ATTR cgi_check_host(http_connection *connData) { http_server_config *config = (http_server_config*)connData->cgi.argument; if(config==NULL) return HTTPD_CGI_NEXT_RULE; if(config->host_domain==NULL) return HTTPD_CGI_NEXT_RULE; if(connData->state==HTTPD_STATE_ON_URL){ http_set_save_header(connData,HTTP_HOST); return HTTPD_CGI_NEXT_RULE; } if(connData->state==HTTPD_STATE_HEADERS_END){ header *hostHeader = http_get_header(connData,HTTP_HOST); if(hostHeader==NULL){ NODE_DBG("Host header not found"); http_response_BAD_REQUEST(connData); return HTTPD_CGI_DONE; } const char * domain = config->host_domain; NODE_DBG("Host header found: %s, domain: %s",hostHeader->value,domain); if(os_strncmp(hostHeader->value,domain,strlen(domain))==0) //compare ignoring http:// and last / { NODE_DBG("Hosts match"); return HTTPD_CGI_NEXT_RULE; } else{ NODE_DBG("Hosts don't match"); if(config->enable_captive){ //to enable a captive portal we should redirect here char * redirectUrl = (char *)os_zalloc(strlen(domain)+9); // domain lenght + http:// + / + \0 os_strcpy(redirectUrl,"http://"); os_strcat(redirectUrl,domain); os_strcat(redirectUrl,"/"); http_response_REDIRECT(connData, redirectUrl); os_free(redirectUrl); } else{ //bad request http_response_BAD_REQUEST(connData); } return HTTPD_CGI_DONE; } } return HTTPD_CGI_NEXT_RULE; }
//Cgi that check the request has the correct HOST header //Using it we can ensure our server has a domain of our choice int cgi_check_host(http_connection *connData) { http_server_config *config = (http_server_config*)connData->cgi.argument; if(config==NULL) return HTTPD_CGI_NEXT_RULE; if(config->host_domain==NULL) return HTTPD_CGI_NEXT_RULE; if(connData->state==HTTPD_STATE_ON_URL) { http_set_save_header(connData,HTTP_HOST); return HTTPD_CGI_NEXT_RULE; } if(connData->state==HTTPD_STATE_HEADERS_END) { header *hostHeader = http_get_header(connData,HTTP_HOST); if(hostHeader==NULL) { NODE_ERR("Host header not found\n"); http_response_BAD_REQUEST(connData); return HTTPD_CGI_DONE; } const char * domain = config->host_domain; HTTP_CGI_DBG("Host header: %s, domain: %s\n",hostHeader->value,domain); if(strncmp(hostHeader->value,domain,strlen(domain))==0) //compare ignoring http:// and last / { HTTP_CGI_DBG("Domain match\n"); return HTTPD_CGI_NEXT_RULE; } else{ uint8_t op = wifi_get_opmode(); char ipaddrstr[17]; os_bzero(ipaddrstr, sizeof(ipaddrstr)); struct ip_info ipConfig; switch (op) { case STATIONAP_MODE: { wifi_get_ip_info(SOFTAP_IF,&ipConfig); //0x01 ipaddr_ntoa_r(&ipConfig.ip,ipaddrstr, sizeof(ipaddrstr)); if(strncmp(hostHeader->value,ipaddrstr,strlen(ipaddrstr))==0) { HTTP_CGI_DBG("SoftAp ip match"); return HTTPD_CGI_NEXT_RULE; } } case STATION_MODE: { os_bzero(ipaddrstr, sizeof(ipaddrstr)); wifi_get_ip_info(STATION_IF,&ipConfig); //0x00 ipaddr_ntoa_r(&ipConfig.ip,ipaddrstr, sizeof(ipaddrstr)); if(strncmp(hostHeader->value,ipaddrstr,strlen(ipaddrstr))==0) { HTTP_CGI_DBG("Station ip match"); return HTTPD_CGI_NEXT_RULE; } } } HTTP_CGI_DBG("Hosts don't match\n"); if(config->enable_captive) { //to enable a captive portal we should redirect here char * redirectUrl = (char *)os_zalloc(strlen(domain)+9); // domain length + http:// + / + \0 strcpy(redirectUrl,"http://"); os_strcat(redirectUrl,domain); os_strcat(redirectUrl,"/"); http_response_REDIRECT(connData, redirectUrl); os_free(redirectUrl); HTTP_CGI_DBG("Redirect URL = %s\n", redirectUrl); } else { //bad request else http_response_BAD_REQUEST(connData); } return HTTPD_CGI_DONE; } } return HTTPD_CGI_NEXT_RULE; }