diff options
Diffstat (limited to 'main/captive_portal.c')
| -rw-r--r-- | main/captive_portal.c | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/main/captive_portal.c b/main/captive_portal.c index 7d6c885..1a3d5ce 100644 --- a/main/captive_portal.c +++ b/main/captive_portal.c | |||
| @@ -1,11 +1,14 @@ | |||
| 1 | #include "captive_portal.h" | 1 | #include "captive_portal.h" |
| 2 | #include "firewall.h" | 2 | #include "firewall.h" |
| 3 | #include "session.h" | ||
| 3 | #include "config.h" | 4 | #include "config.h" |
| 4 | #include "esp_log.h" | 5 | #include "esp_log.h" |
| 5 | #include "esp_wifi.h" | 6 | #include "esp_wifi.h" |
| 6 | #include "cJSON.h" | 7 | #include "cJSON.h" |
| 7 | #include "lwip/sockets.h" | 8 | #include "lwip/sockets.h" |
| 8 | #include "lwip/netdb.h" | 9 | #include "lwip/netdb.h" |
| 10 | #include "freertos/FreeRTOS.h" | ||
| 11 | #include "freertos/task.h" | ||
| 9 | #include <string.h> | 12 | #include <string.h> |
| 10 | #include <sys/param.h> | 13 | #include <sys/param.h> |
| 11 | 14 | ||
| @@ -224,11 +227,32 @@ static esp_err_t whoami_handler(httpd_req_t *req) | |||
| 224 | static esp_err_t usage_handler(httpd_req_t *req) | 227 | static esp_err_t usage_handler(httpd_req_t *req) |
| 225 | { | 228 | { |
| 226 | uint32_t client_ip; | 229 | uint32_t client_ip; |
| 227 | char resp[32]; | 230 | if (get_client_ip(req, &client_ip) != ESP_OK) { |
| 228 | if (get_client_ip(req, &client_ip) == ESP_OK && firewall_is_client_allowed(client_ip)) { | 231 | httpd_resp_set_type(req, "text/plain"); |
| 229 | snprintf(resp, sizeof(resp), "0/0"); | 232 | httpd_resp_send(req, "-1/-1", 5); |
| 233 | return ESP_OK; | ||
| 234 | } | ||
| 235 | |||
| 236 | session_t *session = session_find_by_ip(client_ip); | ||
| 237 | if (!session || !session->active) { | ||
| 238 | httpd_resp_set_type(req, "text/plain"); | ||
| 239 | httpd_resp_send(req, "-1/-1", 5); | ||
| 240 | return ESP_OK; | ||
| 241 | } | ||
| 242 | |||
| 243 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 244 | bool is_bytes = (strcmp(cfg->metric, "bytes") == 0); | ||
| 245 | |||
| 246 | char resp[64]; | ||
| 247 | if (is_bytes) { | ||
| 248 | int64_t remaining = (int64_t)session->allotment_bytes - (int64_t)session->bytes_consumed; | ||
| 249 | if (remaining < 0) remaining = 0; | ||
| 250 | snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_bytes); | ||
| 230 | } else { | 251 | } else { |
| 231 | snprintf(resp, sizeof(resp), "-1/-1"); | 252 | int64_t elapsed = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS - session->start_time_ms; |
| 253 | int64_t remaining = session->allotment_ms - elapsed; | ||
| 254 | if (remaining < 0) remaining = 0; | ||
| 255 | snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_ms); | ||
| 232 | } | 256 | } |
| 233 | httpd_resp_set_type(req, "text/plain"); | 257 | httpd_resp_set_type(req, "text/plain"); |
| 234 | httpd_resp_send(req, resp, strlen(resp)); | 258 | httpd_resp_send(req, resp, strlen(resp)); |
| @@ -237,6 +261,7 @@ static esp_err_t usage_handler(httpd_req_t *req) | |||
| 237 | 261 | ||
| 238 | static esp_err_t reset_auth_handler(httpd_req_t *req) | 262 | static esp_err_t reset_auth_handler(httpd_req_t *req) |
| 239 | { | 263 | { |
| 264 | session_revoke_all(); | ||
| 240 | firewall_revoke_all(); | 265 | firewall_revoke_all(); |
| 241 | const char *resp = "{\"status\":\"reset\"}"; | 266 | const char *resp = "{\"status\":\"reset\"}"; |
| 242 | httpd_resp_set_type(req, "application/json"); | 267 | httpd_resp_set_type(req, "application/json"); |