From 3342c8e7b4f645c75470d3d893d09037a672cfd2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 14:49:16 +0530 Subject: Bug fixes: reset_auth clears sessions, port 80 /usage shows real data, metric defaults to milliseconds, fix sys_evt stack overflow --- main/captive_portal.c | 33 +++++++++++++++++++++++++++++---- main/config.c | 2 +- main/nip04.c | 1 + main/tollgate_main.c | 16 ++++++++++------ 4 files changed, 41 insertions(+), 11 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 @@ #include "captive_portal.h" #include "firewall.h" +#include "session.h" #include "config.h" #include "esp_log.h" #include "esp_wifi.h" #include "cJSON.h" #include "lwip/sockets.h" #include "lwip/netdb.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" #include #include @@ -224,11 +227,32 @@ static esp_err_t whoami_handler(httpd_req_t *req) static esp_err_t usage_handler(httpd_req_t *req) { uint32_t client_ip; - char resp[32]; - if (get_client_ip(req, &client_ip) == ESP_OK && firewall_is_client_allowed(client_ip)) { - snprintf(resp, sizeof(resp), "0/0"); + if (get_client_ip(req, &client_ip) != ESP_OK) { + httpd_resp_set_type(req, "text/plain"); + httpd_resp_send(req, "-1/-1", 5); + return ESP_OK; + } + + session_t *session = session_find_by_ip(client_ip); + if (!session || !session->active) { + httpd_resp_set_type(req, "text/plain"); + httpd_resp_send(req, "-1/-1", 5); + return ESP_OK; + } + + const tollgate_config_t *cfg = tollgate_config_get(); + bool is_bytes = (strcmp(cfg->metric, "bytes") == 0); + + char resp[64]; + if (is_bytes) { + int64_t remaining = (int64_t)session->allotment_bytes - (int64_t)session->bytes_consumed; + if (remaining < 0) remaining = 0; + snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_bytes); } else { - snprintf(resp, sizeof(resp), "-1/-1"); + int64_t elapsed = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS - session->start_time_ms; + int64_t remaining = session->allotment_ms - elapsed; + if (remaining < 0) remaining = 0; + snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_ms); } httpd_resp_set_type(req, "text/plain"); httpd_resp_send(req, resp, strlen(resp)); @@ -237,6 +261,7 @@ static esp_err_t usage_handler(httpd_req_t *req) static esp_err_t reset_auth_handler(httpd_req_t *req) { + session_revoke_all(); firewall_revoke_all(); const char *resp = "{\"status\":\"reset\"}"; httpd_resp_set_type(req, "application/json"); diff --git a/main/config.c b/main/config.c index 2ee8637..e937fb3 100644 --- a/main/config.c +++ b/main/config.c @@ -21,7 +21,7 @@ esp_err_t tollgate_config_init(void) g_config.price_per_step = 21; g_config.step_size_ms = 60000; g_config.step_size_bytes = 22020096; - strncpy(g_config.metric, "bytes", sizeof(g_config.metric) - 1); + strncpy(g_config.metric, "milliseconds", sizeof(g_config.metric) - 1); g_config.persist_threshold_sats = 1; g_config.nostr_publish_interval_s = 21600; g_config.client_enabled = false; diff --git a/main/nip04.c b/main/nip04.c index 5526d4f..40cf98c 100644 --- a/main/nip04.c +++ b/main/nip04.c @@ -1,6 +1,7 @@ #include "nip04.h" #include "esp_log.h" #include "esp_system.h" +#include "esp_random.h" #include "mbedtls/aes.h" #include #include diff --git a/main/tollgate_main.c b/main/tollgate_main.c index 6adb0ec..2670f05 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c @@ -79,6 +79,12 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, } } +static void services_start_task(void *pvParameters) +{ + start_services(); + vTaskDelete(NULL); +} + static void ip_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { @@ -88,16 +94,11 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, s_retry_count = 0; xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); - const tollgate_config_t *cfg = tollgate_config_get(); - nucula_wallet_init(cfg->mint_url); - - lightning_payout_init(&cfg->payout); - char gw_ip_str[16]; snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw)); tollgate_client_on_sta_connected(gw_ip_str); - start_services(); + xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { ESP_LOGW(TAG, "Lost IP address"); xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); @@ -141,6 +142,9 @@ static void start_services(void) session_manager_init(); const tollgate_config_t *cfg = tollgate_config_get(); + nucula_wallet_init(cfg->mint_url); + lightning_payout_init(&cfg->payout); + dns_server_start(ap_ip_info.ip, upstream_dns); captive_portal_start(cfg->ap_ip_str); tollgate_api_start(); -- cgit v1.2.3