diff options
| author | Your Name <you@example.com> | 2026-05-17 14:49:16 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-17 14:49:16 +0530 |
| commit | 3342c8e7b4f645c75470d3d893d09037a672cfd2 (patch) | |
| tree | 0cee003b894525a6097b43ff5ccc0c35c9949900 /main | |
| parent | 4fb44e7aa8f4643f5027a41e81e96c9ca303930d (diff) | |
Bug fixes: reset_auth clears sessions, port 80 /usage shows real data, metric defaults to milliseconds, fix sys_evt stack overflow
Diffstat (limited to 'main')
| -rw-r--r-- | main/captive_portal.c | 33 | ||||
| -rw-r--r-- | main/config.c | 2 | ||||
| -rw-r--r-- | main/nip04.c | 1 | ||||
| -rw-r--r-- | 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 @@ | |||
| 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"); |
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) | |||
| 21 | g_config.price_per_step = 21; | 21 | g_config.price_per_step = 21; |
| 22 | g_config.step_size_ms = 60000; | 22 | g_config.step_size_ms = 60000; |
| 23 | g_config.step_size_bytes = 22020096; | 23 | g_config.step_size_bytes = 22020096; |
| 24 | strncpy(g_config.metric, "bytes", sizeof(g_config.metric) - 1); | 24 | strncpy(g_config.metric, "milliseconds", sizeof(g_config.metric) - 1); |
| 25 | g_config.persist_threshold_sats = 1; | 25 | g_config.persist_threshold_sats = 1; |
| 26 | g_config.nostr_publish_interval_s = 21600; | 26 | g_config.nostr_publish_interval_s = 21600; |
| 27 | g_config.client_enabled = false; | 27 | 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 @@ | |||
| 1 | #include "nip04.h" | 1 | #include "nip04.h" |
| 2 | #include "esp_log.h" | 2 | #include "esp_log.h" |
| 3 | #include "esp_system.h" | 3 | #include "esp_system.h" |
| 4 | #include "esp_random.h" | ||
| 4 | #include "mbedtls/aes.h" | 5 | #include "mbedtls/aes.h" |
| 5 | #include <string.h> | 6 | #include <string.h> |
| 6 | #include <stdlib.h> | 7 | #include <stdlib.h> |
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, | |||
| 79 | } | 79 | } |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | static void services_start_task(void *pvParameters) | ||
| 83 | { | ||
| 84 | start_services(); | ||
| 85 | vTaskDelete(NULL); | ||
| 86 | } | ||
| 87 | |||
| 82 | static void ip_event_handler(void *arg, esp_event_base_t event_base, | 88 | static void ip_event_handler(void *arg, esp_event_base_t event_base, |
| 83 | int32_t event_id, void *event_data) | 89 | int32_t event_id, void *event_data) |
| 84 | { | 90 | { |
| @@ -88,16 +94,11 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 88 | s_retry_count = 0; | 94 | s_retry_count = 0; |
| 89 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | 95 | xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 90 | 96 | ||
| 91 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 92 | nucula_wallet_init(cfg->mint_url); | ||
| 93 | |||
| 94 | lightning_payout_init(&cfg->payout); | ||
| 95 | |||
| 96 | char gw_ip_str[16]; | 97 | char gw_ip_str[16]; |
| 97 | snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw)); | 98 | snprintf(gw_ip_str, sizeof(gw_ip_str), IPSTR, IP2STR(&event->ip_info.gw)); |
| 98 | tollgate_client_on_sta_connected(gw_ip_str); | 99 | tollgate_client_on_sta_connected(gw_ip_str); |
| 99 | 100 | ||
| 100 | start_services(); | 101 | xTaskCreate(services_start_task, "svc_start", 32768, NULL, 5, NULL); |
| 101 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { | 102 | } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_LOST_IP) { |
| 102 | ESP_LOGW(TAG, "Lost IP address"); | 103 | ESP_LOGW(TAG, "Lost IP address"); |
| 103 | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); | 104 | xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| @@ -141,6 +142,9 @@ static void start_services(void) | |||
| 141 | session_manager_init(); | 142 | session_manager_init(); |
| 142 | 143 | ||
| 143 | const tollgate_config_t *cfg = tollgate_config_get(); | 144 | const tollgate_config_t *cfg = tollgate_config_get(); |
| 145 | nucula_wallet_init(cfg->mint_url); | ||
| 146 | lightning_payout_init(&cfg->payout); | ||
| 147 | |||
| 144 | dns_server_start(ap_ip_info.ip, upstream_dns); | 148 | dns_server_start(ap_ip_info.ip, upstream_dns); |
| 145 | captive_portal_start(cfg->ap_ip_str); | 149 | captive_portal_start(cfg->ap_ip_str); |
| 146 | tollgate_api_start(); | 150 | tollgate_api_start(); |