upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorYour Name <you@example.com>2026-05-19 19:55:33 +0530
committerYour Name <you@example.com>2026-05-19 20:33:03 +0530
commitc89ab319fd520c9914b015264a26b581e2103954 (patch)
tree5fa6396c9d3f68b300339c28a0b5ead9c2019304 /main
parent08d7df158acf92399acdbb8a527620a6b1a94f16 (diff)
fix: E2E test stability — socket exhaustion, auto-grant, HTTP robustness
Root causes discovered: - RC-0: LWIP socket exhaustion (CONFIG_LWIP_MAX_SOCKETS=10, need 14) - Two HTTP servers (5 sockets each) + DNS + DoT + wifistr WS = 14 > 10 - Fix: increase to 16, reduce max_open_sockets to 2 on both servers - RC-1: Port 80 captive portal crashes under load - Fix: Connection: close on all handlers, stack 16384 - RC-2: Owner auto-grant makes tests non-deterministic - Fix: remove tollgate_core_fw_grant() from client_connected() Also adds: - /grant_access and /reset_authentication on API server (port 2121) - /portal-config endpoint for future JS-based portal config - Error-checked URI handler registration - Connection: close on captive portal handlers E2E test fixes: - dig +short instead of nslookup for DNS checks - port 2121 for grant/reset/usage/whoami in all tests - pre-mint tokens before blocking internet - increased timeouts and sleeps for reliability
Diffstat (limited to 'main')
-rw-r--r--main/captive_portal.c3
-rw-r--r--main/tollgate_api.c71
2 files changed, 66 insertions, 8 deletions
diff --git a/main/captive_portal.c b/main/captive_portal.c
index 98dc637..fc21d09 100644
--- a/main/captive_portal.c
+++ b/main/captive_portal.c
@@ -115,6 +115,7 @@ static esp_err_t portal_handler(httpd_req_t *req)
115{ 115{
116 ESP_LOGI(TAG, "GET %s from client", req->uri); 116 ESP_LOGI(TAG, "GET %s from client", req->uri);
117 httpd_resp_set_type(req, "text/html"); 117 httpd_resp_set_type(req, "text/html");
118 httpd_resp_set_hdr(req, "Connection", "close");
118 119
119 const tollgate_config_t *cfg = tollgate_config_get(); 120 const tollgate_config_t *cfg = tollgate_config_get();
120 char price_str[16]; 121 char price_str[16];
@@ -310,7 +311,9 @@ esp_err_t captive_portal_start(const char *ap_ip_str)
310 strncpy(s_ap_ip_str, ap_ip_str, sizeof(s_ap_ip_str) - 1); 311 strncpy(s_ap_ip_str, ap_ip_str, sizeof(s_ap_ip_str) - 1);
311 312
312 httpd_config_t config = HTTPD_DEFAULT_CONFIG(); 313 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
314 config.stack_size = 16384;
313 config.max_uri_handlers = 20; 315 config.max_uri_handlers = 20;
316 config.max_open_sockets = 2;
314 config.uri_match_fn = httpd_uri_match_wildcard; 317 config.uri_match_fn = httpd_uri_match_wildcard;
315 318
316 esp_err_t ret = httpd_start(&s_server, &config); 319 esp_err_t ret = httpd_start(&s_server, &config);
diff --git a/main/tollgate_api.c b/main/tollgate_api.c
index 62f75ee..9b4612d 100644
--- a/main/tollgate_api.c
+++ b/main/tollgate_api.c
@@ -1,5 +1,8 @@
1#include "tollgate_api.h" 1#include "tollgate_api.h"
2#include "tollgate_core.h" 2#include "tollgate_core.h"
3#include "tollgate_core_firewall.h"
4#include "tollgate_core_session.h"
5#include "tollgate_core_cashu.h"
3#include "config.h" 6#include "config.h"
4#include "nucula_wallet.h" 7#include "nucula_wallet.h"
5#include "esp_log.h" 8#include "esp_log.h"
@@ -333,6 +336,41 @@ static esp_err_t api_post_wallet_send(httpd_req_t *req)
333 return ESP_OK; 336 return ESP_OK;
334} 337}
335 338
339static esp_err_t api_grant_access(httpd_req_t *req)
340{
341 uint32_t client_ip = 0;
342 if (get_client_ip(req, &client_ip) == ESP_OK) {
343 tollgate_core_fw_grant(client_ip);
344 }
345 httpd_resp_set_type(req, "application/json");
346 httpd_resp_send(req, "{\"status\":\"granted\"}", 20);
347 return ESP_OK;
348}
349
350static esp_err_t api_reset_auth(httpd_req_t *req)
351{
352 tollgate_core_session_revoke_all();
353 tollgate_core_fw_revoke_all();
354 httpd_resp_set_type(req, "application/json");
355 httpd_resp_send(req, "{\"status\":\"reset\"}", 18);
356 return ESP_OK;
357}
358
359static esp_err_t api_get_portal_config(httpd_req_t *req)
360{
361 char *cfg_json = tollgate_core_get_config_json();
362 if (cfg_json) {
363 httpd_resp_set_type(req, "application/json");
364 httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
365 httpd_resp_send(req, cfg_json, strlen(cfg_json));
366 cJSON_free(cfg_json);
367 } else {
368 httpd_resp_set_type(req, "application/json");
369 httpd_resp_send(req, "{}", 2);
370 }
371 return ESP_OK;
372}
373
336static const httpd_uri_t uri_discovery = { .uri = "/", .method = HTTP_GET, .handler = api_get_discovery }; 374static const httpd_uri_t uri_discovery = { .uri = "/", .method = HTTP_GET, .handler = api_get_discovery };
337static const httpd_uri_t uri_payment = { .uri = "/", .method = HTTP_POST, .handler = api_post_payment }; 375static const httpd_uri_t uri_payment = { .uri = "/", .method = HTTP_POST, .handler = api_post_payment };
338static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = api_get_usage }; 376static const httpd_uri_t uri_usage = { .uri = "/usage", .method = HTTP_GET, .handler = api_get_usage };
@@ -340,6 +378,9 @@ static const httpd_uri_t uri_whoami = { .uri = "/whoami", .method = HTTP_GET, .h
340static const httpd_uri_t uri_wallet = { .uri = "/wallet", .method = HTTP_GET, .handler = api_get_wallet }; 378static const httpd_uri_t uri_wallet = { .uri = "/wallet", .method = HTTP_GET, .handler = api_get_wallet };
341static const httpd_uri_t uri_wallet_swap = { .uri = "/wallet/swap", .method = HTTP_POST, .handler = api_post_wallet_swap }; 379static const httpd_uri_t uri_wallet_swap = { .uri = "/wallet/swap", .method = HTTP_POST, .handler = api_post_wallet_swap };
342static const httpd_uri_t uri_wallet_send = { .uri = "/wallet/send", .method = HTTP_POST, .handler = api_post_wallet_send }; 380static const httpd_uri_t uri_wallet_send = { .uri = "/wallet/send", .method = HTTP_POST, .handler = api_post_wallet_send };
381static const httpd_uri_t uri_grant = { .uri = "/grant_access", .method = HTTP_GET, .handler = api_grant_access };
382static const httpd_uri_t uri_reset = { .uri = "/reset_authentication", .method = HTTP_GET, .handler = api_reset_auth };
383static const httpd_uri_t uri_portal_config = { .uri = "/portal-config", .method = HTTP_GET, .handler = api_get_portal_config };
343 384
344esp_err_t tollgate_api_start(void) 385esp_err_t tollgate_api_start(void)
345{ 386{
@@ -348,7 +389,8 @@ esp_err_t tollgate_api_start(void)
348 httpd_config_t config = HTTPD_DEFAULT_CONFIG(); 389 httpd_config_t config = HTTPD_DEFAULT_CONFIG();
349 config.server_port = 2121; 390 config.server_port = 2121;
350 config.ctrl_port = 32769; 391 config.ctrl_port = 32769;
351 config.max_uri_handlers = 10; 392 config.max_uri_handlers = 16;
393 config.max_open_sockets = 2;
352 config.stack_size = 16384; 394 config.stack_size = 16384;
353 395
354 esp_err_t ret = httpd_start(&s_api_server, &config); 396 esp_err_t ret = httpd_start(&s_api_server, &config);
@@ -357,13 +399,26 @@ esp_err_t tollgate_api_start(void)
357 return ret; 399 return ret;
358 } 400 }
359 401
360 httpd_register_uri_handler(s_api_server, &uri_discovery); 402 ret = httpd_register_uri_handler(s_api_server, &uri_discovery);
361 httpd_register_uri_handler(s_api_server, &uri_payment); 403 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register discovery: %s", esp_err_to_name(ret));
362 httpd_register_uri_handler(s_api_server, &uri_usage); 404 ret = httpd_register_uri_handler(s_api_server, &uri_payment);
363 httpd_register_uri_handler(s_api_server, &uri_whoami); 405 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register payment: %s", esp_err_to_name(ret));
364 httpd_register_uri_handler(s_api_server, &uri_wallet); 406 ret = httpd_register_uri_handler(s_api_server, &uri_usage);
365 httpd_register_uri_handler(s_api_server, &uri_wallet_swap); 407 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register usage: %s", esp_err_to_name(ret));
366 httpd_register_uri_handler(s_api_server, &uri_wallet_send); 408 ret = httpd_register_uri_handler(s_api_server, &uri_whoami);
409 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register whoami: %s", esp_err_to_name(ret));
410 ret = httpd_register_uri_handler(s_api_server, &uri_wallet);
411 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register wallet: %s", esp_err_to_name(ret));
412 ret = httpd_register_uri_handler(s_api_server, &uri_wallet_swap);
413 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register wallet_swap: %s", esp_err_to_name(ret));
414 ret = httpd_register_uri_handler(s_api_server, &uri_wallet_send);
415 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register wallet_send: %s", esp_err_to_name(ret));
416 ret = httpd_register_uri_handler(s_api_server, &uri_grant);
417 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register grant: %s", esp_err_to_name(ret));
418 ret = httpd_register_uri_handler(s_api_server, &uri_reset);
419 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register reset: %s", esp_err_to_name(ret));
420 ret = httpd_register_uri_handler(s_api_server, &uri_portal_config);
421 if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to register portal_config: %s", esp_err_to_name(ret));
367 422
368 ESP_LOGI(TAG, "TollGate API started on port 2121"); 423 ESP_LOGI(TAG, "TollGate API started on port 2121");
369 return ESP_OK; 424 return ESP_OK;