diff options
| author | Your Name <you@example.com> | 2026-05-18 18:50:20 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 18:50:20 +0530 |
| commit | f6369f5b87f5dab17a03315d82454fdcdd668ec9 (patch) | |
| tree | bc16c22f87253eafa680a30da370b1822d855ca4 | |
| parent | d8c55d2a072433e16ae5c84b7b7c676587f6f2d9 (diff) | |
feat: extract tollgate_core ESP-IDF component
Extract shared TollGate business logic into a reusable ESP-IDF component
that can be consumed by esp-miner via the IDF Component Manager.
Component structure:
components/tollgate_core/
include/tollgate_core.h — public API
include/tollgate_platform.h — platform interface (config callbacks)
src/tollgate_core.c — orchestrator (init, payment, tick, owner)
src/tollgate_core_cashu.c/h — Cashu V3 token decode/verify
src/tollgate_core_dns.c/h — per-client DNS hijack/forward
src/tollgate_core_firewall.c/h — per-client NAT filter
src/tollgate_core_session.c/h — session lifecycle
Key design decisions:
- Platform interface pattern: consumers implement tollgate_platform_t
(config getters + optional spend_proofs wallet hook)
- Cross-module wiring (session→firewall→dns) stays internal
- No direct config.h dependency — all config via platform callbacks
- spend_proofs can be NULL (accepts payment without local wallet)
Standalone app updated:
- main/tollgate_platform.c implements platform via config singleton
- main/tollgate_main.c calls tollgate_core_init/tick/dns_start
- main/tollgate_api.c routes payment through tollgate_core_process_payment
- Removed cashu.c, dns_server.c, firewall.c, session.c from CMakeLists
Not yet build-verified — blocked on multi-mint, price-discovery,
cvm-integration, and display-fix branches merging to master.
19 files changed, 1510 insertions, 210 deletions
diff --git a/components/tollgate_core/CMakeLists.txt b/components/tollgate_core/CMakeLists.txt new file mode 100644 index 0000000..fc6b6f1 --- /dev/null +++ b/components/tollgate_core/CMakeLists.txt | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | idf_component_register( | ||
| 2 | SRCS "src/tollgate_core.c" | ||
| 3 | "src/tollgate_core_cashu.c" | ||
| 4 | "src/tollgate_core_dns.c" | ||
| 5 | "src/tollgate_core_firewall.c" | ||
| 6 | "src/tollgate_core_session.c" | ||
| 7 | INCLUDE_DIRS "include" "src" | ||
| 8 | REQUIRES lwip json esp_http_client mbedtls log esp_netif | ||
| 9 | PRIV_REQUIRES esp_wifi) | ||
diff --git a/components/tollgate_core/idf_component.yml b/components/tollgate_core/idf_component.yml new file mode 100644 index 0000000..112aa18 --- /dev/null +++ b/components/tollgate_core/idf_component.yml | |||
| @@ -0,0 +1,6 @@ | |||
| 1 | version: "1.0.0" | ||
| 2 | description: TollGate core component — Cashu payment processing, per-client DNS/firewall, session management for paid WiFi hotspots | ||
| 3 | url: https://github.com/nicoulaj/esp32-tollgate | ||
| 4 | dependencies: | ||
| 5 | idf: | ||
| 6 | version: ">=5.4.0" | ||
diff --git a/components/tollgate_core/include/tollgate_core.h b/components/tollgate_core/include/tollgate_core.h new file mode 100644 index 0000000..c47ebeb --- /dev/null +++ b/components/tollgate_core/include/tollgate_core.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_H | ||
| 2 | #define TOLLGATE_CORE_H | ||
| 3 | |||
| 4 | #include "tollgate_platform.h" | ||
| 5 | #include "esp_err.h" | ||
| 6 | #include "esp_netif.h" | ||
| 7 | #include <stdbool.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | |||
| 10 | esp_err_t tollgate_core_init(const tollgate_platform_t *platform, esp_ip4_addr_t ap_ip); | ||
| 11 | |||
| 12 | esp_err_t tollgate_core_dns_start(esp_ip4_addr_t upstream_dns); | ||
| 13 | void tollgate_core_dns_stop(void); | ||
| 14 | |||
| 15 | esp_err_t tollgate_core_process_payment(uint32_t client_ip, const char *token_str); | ||
| 16 | |||
| 17 | void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip); | ||
| 18 | void tollgate_core_client_disconnected(const uint8_t *mac); | ||
| 19 | |||
| 20 | void tollgate_core_tick(void); | ||
| 21 | |||
| 22 | bool tollgate_core_is_client_allowed(uint32_t client_ip); | ||
| 23 | bool tollgate_core_is_dns_running(void); | ||
| 24 | |||
| 25 | char *tollgate_core_get_status_json(void); | ||
| 26 | char *tollgate_core_get_config_json(void); | ||
| 27 | |||
| 28 | int tollgate_core_active_session_count(void); | ||
| 29 | int tollgate_core_allowed_client_count(void); | ||
| 30 | |||
| 31 | bool tollgate_core_is_owner(uint32_t client_ip); | ||
| 32 | bool tollgate_core_is_owner_connected(void); | ||
| 33 | |||
| 34 | #endif | ||
diff --git a/components/tollgate_core/include/tollgate_platform.h b/components/tollgate_core/include/tollgate_platform.h new file mode 100644 index 0000000..f60f1f9 --- /dev/null +++ b/components/tollgate_core/include/tollgate_platform.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef TOLLGATE_PLATFORM_H | ||
| 2 | #define TOLLGATE_PLATFORM_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | |||
| 7 | typedef struct { | ||
| 8 | uint16_t (*get_price_sats)(void); | ||
| 9 | int32_t (*get_step_ms)(void); | ||
| 10 | const char * (*get_mint_url)(void); | ||
| 11 | const char * (*get_metric)(void); | ||
| 12 | int32_t (*get_step_bytes)(void); | ||
| 13 | int64_t (*get_time_ms)(void); | ||
| 14 | bool (*spend_proofs)(const char *raw_token_json); | ||
| 15 | } tollgate_platform_t; | ||
| 16 | |||
| 17 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core.c b/components/tollgate_core/src/tollgate_core.c new file mode 100644 index 0000000..3813258 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core.c | |||
| @@ -0,0 +1,243 @@ | |||
| 1 | #include "tollgate_core.h" | ||
| 2 | #include "tollgate_core_cashu.h" | ||
| 3 | #include "tollgate_core_dns.h" | ||
| 4 | #include "tollgate_core_firewall.h" | ||
| 5 | #include "tollgate_core_session.h" | ||
| 6 | #include "esp_log.h" | ||
| 7 | #include "cJSON.h" | ||
| 8 | #include <string.h> | ||
| 9 | |||
| 10 | static const char *TAG = "tg_core"; | ||
| 11 | |||
| 12 | static const tollgate_platform_t *s_platform; | ||
| 13 | static esp_ip4_addr_t s_ap_ip; | ||
| 14 | |||
| 15 | static uint32_t s_owner_ip; | ||
| 16 | static uint8_t s_owner_mac[6]; | ||
| 17 | static bool s_owner_connected; | ||
| 18 | |||
| 19 | esp_err_t tollgate_core_init(const tollgate_platform_t *platform, esp_ip4_addr_t ap_ip) | ||
| 20 | { | ||
| 21 | if (!platform) { | ||
| 22 | ESP_LOGE(TAG, "Platform callbacks required"); | ||
| 23 | return ESP_FAIL; | ||
| 24 | } | ||
| 25 | |||
| 26 | s_platform = platform; | ||
| 27 | s_ap_ip = ap_ip; | ||
| 28 | s_owner_connected = false; | ||
| 29 | memset(s_owner_mac, 0, sizeof(s_owner_mac)); | ||
| 30 | |||
| 31 | if (!platform->spend_proofs) { | ||
| 32 | ESP_LOGW(TAG, "spend_proofs is NULL — double-spend window open until wallet integrated"); | ||
| 33 | } | ||
| 34 | |||
| 35 | tollgate_core_session_set_platform(platform); | ||
| 36 | tollgate_core_session_init(); | ||
| 37 | tollgate_core_fw_init(ap_ip); | ||
| 38 | |||
| 39 | ESP_LOGI(TAG, "TollGate core initialized, AP IP=" IPSTR, IP2STR(&ap_ip)); | ||
| 40 | return ESP_OK; | ||
| 41 | } | ||
| 42 | |||
| 43 | esp_err_t tollgate_core_dns_start(esp_ip4_addr_t upstream_dns) | ||
| 44 | { | ||
| 45 | return tollgate_core_dns_start_internal(s_ap_ip, upstream_dns); | ||
| 46 | } | ||
| 47 | |||
| 48 | void tollgate_core_dns_stop(void) | ||
| 49 | { | ||
| 50 | tollgate_core_dns_stop_internal(); | ||
| 51 | } | ||
| 52 | |||
| 53 | esp_err_t tollgate_core_process_payment(uint32_t client_ip, const char *token_str) | ||
| 54 | { | ||
| 55 | if (!s_platform || !token_str) return ESP_FAIL; | ||
| 56 | |||
| 57 | const char *accepted_mint = s_platform->get_mint_url ? s_platform->get_mint_url() : NULL; | ||
| 58 | if (!accepted_mint || accepted_mint[0] == '\0') { | ||
| 59 | ESP_LOGE(TAG, "No mint URL configured"); | ||
| 60 | return ESP_FAIL; | ||
| 61 | } | ||
| 62 | |||
| 63 | tg_cashu_token_t token; | ||
| 64 | esp_err_t ret = tollgate_core_cashu_decode_token(token_str, &token); | ||
| 65 | if (ret != ESP_OK) { | ||
| 66 | ESP_LOGE(TAG, "Token decode failed"); | ||
| 67 | return ESP_FAIL; | ||
| 68 | } | ||
| 69 | |||
| 70 | if (!tollgate_core_cashu_is_mint_accepted(token.mint_url, accepted_mint)) { | ||
| 71 | ESP_LOGE(TAG, "Token mint '%s' not accepted (expected '%s')", token.mint_url, accepted_mint); | ||
| 72 | return ESP_FAIL; | ||
| 73 | } | ||
| 74 | |||
| 75 | tg_cashu_proof_state_t states[TG_CASHU_MAX_PROOFS]; | ||
| 76 | int state_count = 0; | ||
| 77 | ret = tollgate_core_cashu_check_proof_states(token.mint_url, &token, states, &state_count); | ||
| 78 | if (ret != ESP_OK) { | ||
| 79 | ESP_LOGE(TAG, "Proof state check failed"); | ||
| 80 | return ESP_FAIL; | ||
| 81 | } | ||
| 82 | |||
| 83 | for (int i = 0; i < state_count; i++) { | ||
| 84 | if (states[i].spent) { | ||
| 85 | ESP_LOGE(TAG, "Proof %d is SPENT — rejecting", i); | ||
| 86 | return ESP_FAIL; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | if (s_platform->spend_proofs) { | ||
| 91 | if (!s_platform->spend_proofs(token_str)) { | ||
| 92 | ESP_LOGE(TAG, "spend_proofs rejected the token"); | ||
| 93 | return ESP_FAIL; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | const char *metric = s_platform->get_metric ? s_platform->get_metric() : "milliseconds"; | ||
| 98 | uint64_t price = s_platform->get_price_sats ? s_platform->get_price_sats() : 21; | ||
| 99 | uint64_t step_size; | ||
| 100 | |||
| 101 | if (strcmp(metric, "bytes") == 0) { | ||
| 102 | step_size = s_platform->get_step_bytes ? (uint64_t)s_platform->get_step_bytes() : 22020096; | ||
| 103 | } else { | ||
| 104 | step_size = s_platform->get_step_ms ? (uint64_t)s_platform->get_step_ms() : 60000; | ||
| 105 | } | ||
| 106 | |||
| 107 | uint64_t allotment = tollgate_core_cashu_calculate_allotment(token.total_amount, price, step_size); | ||
| 108 | if (allotment == 0) { | ||
| 109 | ESP_LOGE(TAG, "Token amount %llu too small for price %llu", | ||
| 110 | (unsigned long long)token.total_amount, (unsigned long long)price); | ||
| 111 | return ESP_FAIL; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (strcmp(metric, "bytes") == 0) { | ||
| 115 | if (!tollgate_core_session_create_bytes(client_ip, allotment)) { | ||
| 116 | return ESP_FAIL; | ||
| 117 | } | ||
| 118 | } else { | ||
| 119 | if (!tollgate_core_session_create(client_ip, allotment)) { | ||
| 120 | return ESP_FAIL; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | ESP_LOGI(TAG, "Payment processed: %llu sats → %llu %s allotment for client", | ||
| 125 | (unsigned long long)token.total_amount, (unsigned long long)allotment, metric); | ||
| 126 | return ESP_OK; | ||
| 127 | } | ||
| 128 | |||
| 129 | void tollgate_core_client_connected(const uint8_t *mac, uint32_t client_ip) | ||
| 130 | { | ||
| 131 | if (!s_owner_connected) { | ||
| 132 | s_owner_connected = true; | ||
| 133 | s_owner_ip = client_ip; | ||
| 134 | if (mac) memcpy(s_owner_mac, mac, 6); | ||
| 135 | |||
| 136 | tollgate_core_fw_grant(client_ip); | ||
| 137 | |||
| 138 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 139 | ESP_LOGI(TAG, "First client = owner: " IPSTR, IP2STR(&ip)); | ||
| 140 | return; | ||
| 141 | } | ||
| 142 | |||
| 143 | ESP_LOGI(TAG, "Client connected (non-owner): " IPSTR, IP2STR(&(esp_ip4_addr_t){.addr=client_ip})); | ||
| 144 | } | ||
| 145 | |||
| 146 | void tollgate_core_client_disconnected(const uint8_t *mac) | ||
| 147 | { | ||
| 148 | if (!s_owner_connected) return; | ||
| 149 | |||
| 150 | if (mac && memcmp(s_owner_mac, mac, 6) == 0) { | ||
| 151 | ESP_LOGI(TAG, "Owner disconnected — reassigning"); | ||
| 152 | s_owner_connected = false; | ||
| 153 | memset(s_owner_mac, 0, sizeof(s_owner_mac)); | ||
| 154 | |||
| 155 | int fw_count = tollgate_core_fw_client_count(); | ||
| 156 | if (fw_count > 0) { | ||
| 157 | tg_session_t *sessions = tollgate_core_session_get_array(); | ||
| 158 | for (int i = 0; i < tollgate_core_session_get_array_size(); i++) { | ||
| 159 | if (sessions[i].active && sessions[i].mac[0] != '\0') { | ||
| 160 | if (memcmp(sessions[i].mac, mac, 6) != 0) { | ||
| 161 | s_owner_connected = true; | ||
| 162 | s_owner_ip = sessions[i].client_ip; | ||
| 163 | ESP_LOGI(TAG, "New owner: " IPSTR, IP2STR(&(esp_ip4_addr_t){.addr=s_owner_ip})); | ||
| 164 | break; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | ESP_LOGI(TAG, "Client disconnected"); | ||
| 173 | } | ||
| 174 | |||
| 175 | void tollgate_core_tick(void) | ||
| 176 | { | ||
| 177 | tollgate_core_session_tick(); | ||
| 178 | } | ||
| 179 | |||
| 180 | bool tollgate_core_is_client_allowed(uint32_t client_ip) | ||
| 181 | { | ||
| 182 | return tollgate_core_fw_is_allowed(client_ip); | ||
| 183 | } | ||
| 184 | |||
| 185 | bool tollgate_core_is_dns_running(void) | ||
| 186 | { | ||
| 187 | return tollgate_core_dns_is_running(); | ||
| 188 | } | ||
| 189 | |||
| 190 | char *tollgate_core_get_status_json(void) | ||
| 191 | { | ||
| 192 | cJSON *root = cJSON_CreateObject(); | ||
| 193 | cJSON_AddBoolToObject(root, "ownerConnected", s_owner_connected); | ||
| 194 | cJSON_AddNumberToObject(root, "activeSessions", tollgate_core_session_active_count()); | ||
| 195 | cJSON_AddNumberToObject(root, "allowedClients", tollgate_core_fw_client_count()); | ||
| 196 | cJSON_AddBoolToObject(root, "dnsRunning", tollgate_core_dns_is_running()); | ||
| 197 | |||
| 198 | char *json = cJSON_PrintUnformatted(root); | ||
| 199 | cJSON_Delete(root); | ||
| 200 | return json; | ||
| 201 | } | ||
| 202 | |||
| 203 | char *tollgate_core_get_config_json(void) | ||
| 204 | { | ||
| 205 | cJSON *root = cJSON_CreateObject(); | ||
| 206 | |||
| 207 | if (s_platform) { | ||
| 208 | if (s_platform->get_price_sats) | ||
| 209 | cJSON_AddNumberToObject(root, "priceSats", s_platform->get_price_sats()); | ||
| 210 | if (s_platform->get_step_ms) | ||
| 211 | cJSON_AddNumberToObject(root, "stepMs", s_platform->get_step_ms()); | ||
| 212 | if (s_platform->get_mint_url) | ||
| 213 | cJSON_AddStringToObject(root, "mintUrl", s_platform->get_mint_url()); | ||
| 214 | if (s_platform->get_metric) | ||
| 215 | cJSON_AddStringToObject(root, "metric", s_platform->get_metric()); | ||
| 216 | if (s_platform->get_step_bytes) | ||
| 217 | cJSON_AddNumberToObject(root, "stepBytes", s_platform->get_step_bytes()); | ||
| 218 | } | ||
| 219 | |||
| 220 | char *json = cJSON_PrintUnformatted(root); | ||
| 221 | cJSON_Delete(root); | ||
| 222 | return json; | ||
| 223 | } | ||
| 224 | |||
| 225 | int tollgate_core_active_session_count(void) | ||
| 226 | { | ||
| 227 | return tollgate_core_session_active_count(); | ||
| 228 | } | ||
| 229 | |||
| 230 | int tollgate_core_allowed_client_count(void) | ||
| 231 | { | ||
| 232 | return tollgate_core_fw_client_count(); | ||
| 233 | } | ||
| 234 | |||
| 235 | bool tollgate_core_is_owner(uint32_t client_ip) | ||
| 236 | { | ||
| 237 | return s_owner_connected && s_owner_ip == client_ip; | ||
| 238 | } | ||
| 239 | |||
| 240 | bool tollgate_core_is_owner_connected(void) | ||
| 241 | { | ||
| 242 | return s_owner_connected; | ||
| 243 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_cashu.c b/components/tollgate_core/src/tollgate_core_cashu.c new file mode 100644 index 0000000..cf2bf5d --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_cashu.c | |||
| @@ -0,0 +1,262 @@ | |||
| 1 | #include "tollgate_core_cashu.h" | ||
| 2 | #include "esp_log.h" | ||
| 3 | #include "esp_http_client.h" | ||
| 4 | #include "cJSON.h" | ||
| 5 | #include "mbedtls/base64.h" | ||
| 6 | #include "mbedtls/sha256.h" | ||
| 7 | #include "esp_crt_bundle.h" | ||
| 8 | |||
| 9 | static const char *TAG = "tg_core_cashu"; | ||
| 10 | |||
| 11 | static const char V3_PREFIX[] = "cashuA"; | ||
| 12 | static const size_t V3_PREFIX_LEN = 6; | ||
| 13 | |||
| 14 | static int b64url_decode(const char *input, size_t input_len, char *out, size_t out_size, size_t *out_len) | ||
| 15 | { | ||
| 16 | char *b64 = malloc(input_len + 4); | ||
| 17 | if (!b64) return -1; | ||
| 18 | size_t b64_len = input_len; | ||
| 19 | memcpy(b64, input, b64_len); | ||
| 20 | b64[b64_len] = '\0'; | ||
| 21 | |||
| 22 | for (size_t i = 0; i < b64_len; i++) { | ||
| 23 | if (b64[i] == '-') b64[i] = '+'; | ||
| 24 | else if (b64[i] == '_') b64[i] = '/'; | ||
| 25 | } | ||
| 26 | while (b64_len % 4 != 0) { | ||
| 27 | b64[b64_len++] = '='; | ||
| 28 | } | ||
| 29 | b64[b64_len] = '\0'; | ||
| 30 | |||
| 31 | size_t olen = 0; | ||
| 32 | int ret = mbedtls_base64_decode((unsigned char *)out, out_size, &olen, | ||
| 33 | (const unsigned char *)b64, b64_len); | ||
| 34 | free(b64); | ||
| 35 | if (ret != 0) return -1; | ||
| 36 | *out_len = olen; | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | static esp_err_t parse_proofs_array(cJSON *arr, tg_cashu_token_t *out) | ||
| 41 | { | ||
| 42 | if (!cJSON_IsArray(arr)) return ESP_FAIL; | ||
| 43 | int count = cJSON_GetArraySize(arr); | ||
| 44 | if (count > TG_CASHU_MAX_PROOFS) return ESP_FAIL; | ||
| 45 | |||
| 46 | out->proof_count = 0; | ||
| 47 | out->total_amount = 0; | ||
| 48 | for (int i = 0; i < count; i++) { | ||
| 49 | cJSON *proof = cJSON_GetArrayItem(arr, i); | ||
| 50 | cJSON *amt = cJSON_GetObjectItemCaseSensitive(proof, "amount"); | ||
| 51 | cJSON *id = cJSON_GetObjectItemCaseSensitive(proof, "id"); | ||
| 52 | cJSON *secret = cJSON_GetObjectItemCaseSensitive(proof, "secret"); | ||
| 53 | cJSON *c = cJSON_GetObjectItemCaseSensitive(proof, "C"); | ||
| 54 | |||
| 55 | if (!amt || !cJSON_IsNumber(amt)) return ESP_FAIL; | ||
| 56 | |||
| 57 | out->proofs[i].amount = (uint64_t)amt->valuedouble; | ||
| 58 | out->total_amount += out->proofs[i].amount; | ||
| 59 | |||
| 60 | if (id && cJSON_IsString(id)) { | ||
| 61 | strncpy(out->proofs[i].id, id->valuestring, sizeof(out->proofs[i].id) - 1); | ||
| 62 | } | ||
| 63 | if (secret && cJSON_IsString(secret)) { | ||
| 64 | strncpy(out->proofs[i].secret, secret->valuestring, sizeof(out->proofs[i].secret) - 1); | ||
| 65 | } | ||
| 66 | if (c && cJSON_IsString(c)) { | ||
| 67 | strncpy(out->proofs[i].c, c->valuestring, sizeof(out->proofs[i].c) - 1); | ||
| 68 | } | ||
| 69 | out->proof_count++; | ||
| 70 | } | ||
| 71 | return ESP_OK; | ||
| 72 | } | ||
| 73 | |||
| 74 | esp_err_t tollgate_core_cashu_decode_token(const char *token_str, tg_cashu_token_t *out) | ||
| 75 | { | ||
| 76 | if (!token_str || !out) return ESP_FAIL; | ||
| 77 | memset(out, 0, sizeof(*out)); | ||
| 78 | |||
| 79 | size_t len = strlen(token_str); | ||
| 80 | char *nl = strchr(token_str, '\n'); | ||
| 81 | if (nl) len = nl - token_str; | ||
| 82 | char *cr = strchr(token_str, '\r'); | ||
| 83 | if (cr && (cr - token_str) < (int)len) len = cr - token_str; | ||
| 84 | if (len <= V3_PREFIX_LEN) { | ||
| 85 | ESP_LOGE(TAG, "Token too short"); | ||
| 86 | return ESP_FAIL; | ||
| 87 | } | ||
| 88 | if (strncmp(token_str, V3_PREFIX, V3_PREFIX_LEN) != 0) { | ||
| 89 | ESP_LOGE(TAG, "Token missing cashuA prefix"); | ||
| 90 | return ESP_FAIL; | ||
| 91 | } | ||
| 92 | |||
| 93 | size_t b64_len = len - V3_PREFIX_LEN; | ||
| 94 | size_t decoded_size = (b64_len * 3) / 4 + 4; | ||
| 95 | char *json_buf = malloc(decoded_size); | ||
| 96 | if (!json_buf) return ESP_FAIL; | ||
| 97 | size_t json_len = 0; | ||
| 98 | if (b64url_decode(token_str + V3_PREFIX_LEN, b64_len, | ||
| 99 | json_buf, decoded_size - 1, &json_len) != 0) { | ||
| 100 | ESP_LOGE(TAG, "Base64url decode failed"); | ||
| 101 | free(json_buf); | ||
| 102 | return ESP_FAIL; | ||
| 103 | } | ||
| 104 | json_buf[json_len] = '\0'; | ||
| 105 | |||
| 106 | cJSON *root = cJSON_Parse(json_buf); | ||
| 107 | free(json_buf); | ||
| 108 | if (!root) { | ||
| 109 | ESP_LOGE(TAG, "JSON parse failed"); | ||
| 110 | return ESP_FAIL; | ||
| 111 | } | ||
| 112 | |||
| 113 | cJSON *token_arr = cJSON_GetObjectItemCaseSensitive(root, "token"); | ||
| 114 | if (token_arr && cJSON_IsArray(token_arr)) { | ||
| 115 | cJSON *first = cJSON_GetArrayItem(token_arr, 0); | ||
| 116 | if (!first) { cJSON_Delete(root); return ESP_FAIL; } | ||
| 117 | |||
| 118 | cJSON *mint = cJSON_GetObjectItemCaseSensitive(first, "mint"); | ||
| 119 | if (mint && cJSON_IsString(mint)) { | ||
| 120 | strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); | ||
| 121 | } | ||
| 122 | |||
| 123 | cJSON *proofs = cJSON_GetObjectItemCaseSensitive(first, "proofs"); | ||
| 124 | if (proofs) { | ||
| 125 | esp_err_t ret = parse_proofs_array(proofs, out); | ||
| 126 | if (ret != ESP_OK) { cJSON_Delete(root); return ret; } | ||
| 127 | } | ||
| 128 | } else { | ||
| 129 | cJSON *mint = cJSON_GetObjectItemCaseSensitive(root, "mint"); | ||
| 130 | if (mint && cJSON_IsString(mint)) { | ||
| 131 | strncpy(out->mint_url, mint->valuestring, sizeof(out->mint_url) - 1); | ||
| 132 | } | ||
| 133 | |||
| 134 | cJSON *proofs = cJSON_GetObjectItemCaseSensitive(root, "proofs"); | ||
| 135 | if (proofs) { | ||
| 136 | esp_err_t ret = parse_proofs_array(proofs, out); | ||
| 137 | if (ret != ESP_OK) { cJSON_Delete(root); return ret; } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | cJSON_Delete(root); | ||
| 142 | |||
| 143 | if (out->proof_count == 0) { | ||
| 144 | ESP_LOGE(TAG, "No proofs in token"); | ||
| 145 | return ESP_FAIL; | ||
| 146 | } | ||
| 147 | |||
| 148 | ESP_LOGI(TAG, "Decoded token: %d proofs, total=%llu, mint=%s", | ||
| 149 | out->proof_count, (unsigned long long)out->total_amount, out->mint_url); | ||
| 150 | return ESP_OK; | ||
| 151 | } | ||
| 152 | |||
| 153 | static void sha256_hex(const char *data, size_t data_len, char *hex_out) | ||
| 154 | { | ||
| 155 | uint8_t hash[32]; | ||
| 156 | mbedtls_sha256((const unsigned char *)data, data_len, hash, 0); | ||
| 157 | for (int i = 0; i < 32; i++) { | ||
| 158 | sprintf(hex_out + i * 2, "%02x", hash[i]); | ||
| 159 | } | ||
| 160 | hex_out[64] = '\0'; | ||
| 161 | } | ||
| 162 | |||
| 163 | esp_err_t tollgate_core_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, | ||
| 164 | tg_cashu_proof_state_t *states, int *state_count) | ||
| 165 | { | ||
| 166 | cJSON *ys_arr = cJSON_CreateArray(); | ||
| 167 | for (int i = 0; i < token->proof_count; i++) { | ||
| 168 | char y_hex[65]; | ||
| 169 | sha256_hex(token->proofs[i].secret, strlen(token->proofs[i].secret), y_hex); | ||
| 170 | cJSON_AddItemToArray(ys_arr, cJSON_CreateString(y_hex)); | ||
| 171 | strncpy(states[i].y_hex, y_hex, sizeof(states[i].y_hex) - 1); | ||
| 172 | states[i].spent = false; | ||
| 173 | } | ||
| 174 | *state_count = token->proof_count; | ||
| 175 | |||
| 176 | char *ys_json = cJSON_PrintUnformatted(ys_arr); | ||
| 177 | cJSON_Delete(ys_arr); | ||
| 178 | |||
| 179 | char *post_body = malloc(4096); | ||
| 180 | if (!post_body) { cJSON_free(ys_json); return ESP_FAIL; } | ||
| 181 | snprintf(post_body, 4096, "{\"Ys\":%s}", ys_json); | ||
| 182 | cJSON_free(ys_json); | ||
| 183 | |||
| 184 | char url[512]; | ||
| 185 | snprintf(url, sizeof(url), "%s/v1/checkstate", mint_url); | ||
| 186 | |||
| 187 | char *resp_buf = malloc(8192); | ||
| 188 | if (!resp_buf) { free(post_body); return ESP_FAIL; } | ||
| 189 | |||
| 190 | esp_http_client_config_t config = { | ||
| 191 | .url = url, | ||
| 192 | .method = HTTP_METHOD_POST, | ||
| 193 | .timeout_ms = 15000, | ||
| 194 | .crt_bundle_attach = esp_crt_bundle_attach, | ||
| 195 | }; | ||
| 196 | esp_http_client_handle_t client = esp_http_client_init(&config); | ||
| 197 | if (!client) { free(post_body); free(resp_buf); return ESP_FAIL; } | ||
| 198 | |||
| 199 | esp_http_client_set_header(client, "Content-Type", "application/json"); | ||
| 200 | esp_err_t err = esp_http_client_open(client, strlen(post_body)); | ||
| 201 | if (err != ESP_OK) { | ||
| 202 | ESP_LOGE(TAG, "checkstate open failed: %s", esp_err_to_name(err)); | ||
| 203 | esp_http_client_cleanup(client); | ||
| 204 | free(post_body); | ||
| 205 | free(resp_buf); | ||
| 206 | return ESP_FAIL; | ||
| 207 | } | ||
| 208 | int written = esp_http_client_write(client, post_body, strlen(post_body)); | ||
| 209 | free(post_body); | ||
| 210 | ESP_LOGI(TAG, "checkstate written %d bytes", written); | ||
| 211 | |||
| 212 | int content_length = esp_http_client_fetch_headers(client); | ||
| 213 | int status = esp_http_client_get_status_code(client); | ||
| 214 | ESP_LOGI(TAG, "checkstate headers: status=%d, content_length=%d", status, content_length); | ||
| 215 | |||
| 216 | int resp_len = esp_http_client_read(client, resp_buf, 8191); | ||
| 217 | ESP_LOGI(TAG, "checkstate read: resp_len=%d", resp_len); | ||
| 218 | esp_http_client_cleanup(client); | ||
| 219 | |||
| 220 | if (status != 200 || resp_len <= 0) { | ||
| 221 | ESP_LOGE(TAG, "checkstate failed: status=%d, resp_len=%d", status, resp_len); | ||
| 222 | free(resp_buf); | ||
| 223 | return ESP_FAIL; | ||
| 224 | } | ||
| 225 | resp_buf[resp_len] = '\0'; | ||
| 226 | |||
| 227 | cJSON *root_resp = cJSON_Parse(resp_buf); | ||
| 228 | free(resp_buf); | ||
| 229 | if (!root_resp) return ESP_FAIL; | ||
| 230 | |||
| 231 | cJSON *states_arr = cJSON_GetObjectItemCaseSensitive(root_resp, "states"); | ||
| 232 | if (!states_arr || !cJSON_IsArray(states_arr)) { | ||
| 233 | cJSON_Delete(root_resp); | ||
| 234 | return ESP_FAIL; | ||
| 235 | } | ||
| 236 | |||
| 237 | int n = cJSON_GetArraySize(states_arr); | ||
| 238 | for (int i = 0; i < n && i < token->proof_count; i++) { | ||
| 239 | cJSON *s = cJSON_GetArrayItem(states_arr, i); | ||
| 240 | cJSON *state = cJSON_GetObjectItemCaseSensitive(s, "state"); | ||
| 241 | if (state && cJSON_IsString(state)) { | ||
| 242 | states[i].spent = (strcmp(state->valuestring, "SPENT") == 0); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | cJSON_Delete(root_resp); | ||
| 247 | return ESP_OK; | ||
| 248 | } | ||
| 249 | |||
| 250 | uint64_t tollgate_core_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, | ||
| 251 | uint64_t step_size) | ||
| 252 | { | ||
| 253 | if (price_per_step == 0) return 0; | ||
| 254 | return (token_amount / price_per_step) * step_size; | ||
| 255 | } | ||
| 256 | |||
| 257 | bool tollgate_core_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url) | ||
| 258 | { | ||
| 259 | if (!mint_url || mint_url[0] == '\0') return false; | ||
| 260 | if (!accepted_mint_url || accepted_mint_url[0] == '\0') return false; | ||
| 261 | return (strcmp(mint_url, accepted_mint_url) == 0); | ||
| 262 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_cashu.h b/components/tollgate_core/src/tollgate_core_cashu.h new file mode 100644 index 0000000..70d2a02 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_cashu.h | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_CASHU_H | ||
| 2 | #define TOLLGATE_CORE_CASHU_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | |||
| 8 | #define TG_CASHU_MAX_PROOFS 10 | ||
| 9 | #define TG_CASHU_MAX_SECRET_LEN 128 | ||
| 10 | #define TG_CASHU_MAX_ID_LEN 68 | ||
| 11 | #define TG_CASHU_MAX_C_LEN 128 | ||
| 12 | |||
| 13 | typedef struct { | ||
| 14 | uint64_t amount; | ||
| 15 | char id[TG_CASHU_MAX_ID_LEN]; | ||
| 16 | char secret[TG_CASHU_MAX_SECRET_LEN]; | ||
| 17 | char c[TG_CASHU_MAX_C_LEN]; | ||
| 18 | } tg_cashu_proof_t; | ||
| 19 | |||
| 20 | typedef struct { | ||
| 21 | tg_cashu_proof_t proofs[TG_CASHU_MAX_PROOFS]; | ||
| 22 | int proof_count; | ||
| 23 | char mint_url[256]; | ||
| 24 | uint64_t total_amount; | ||
| 25 | } tg_cashu_token_t; | ||
| 26 | |||
| 27 | typedef struct { | ||
| 28 | char y_hex[65]; | ||
| 29 | bool spent; | ||
| 30 | } tg_cashu_proof_state_t; | ||
| 31 | |||
| 32 | esp_err_t tollgate_core_cashu_decode_token(const char *token_str, tg_cashu_token_t *out); | ||
| 33 | |||
| 34 | esp_err_t tollgate_core_cashu_check_proof_states(const char *mint_url, const tg_cashu_token_t *token, | ||
| 35 | tg_cashu_proof_state_t *states, int *state_count); | ||
| 36 | |||
| 37 | uint64_t tollgate_core_cashu_calculate_allotment(uint64_t token_amount, uint64_t price_per_step, | ||
| 38 | uint64_t step_size); | ||
| 39 | |||
| 40 | bool tollgate_core_cashu_is_mint_accepted(const char *mint_url, const char *accepted_mint_url); | ||
| 41 | |||
| 42 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_dns.c b/components/tollgate_core/src/tollgate_core_dns.c new file mode 100644 index 0000000..84322e6 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_dns.c | |||
| @@ -0,0 +1,316 @@ | |||
| 1 | #include "tollgate_core_dns.h" | ||
| 2 | #include "esp_log.h" | ||
| 3 | #include "freertos/FreeRTOS.h" | ||
| 4 | #include "freertos/task.h" | ||
| 5 | #include "lwip/sockets.h" | ||
| 6 | #include "lwip/netdb.h" | ||
| 7 | #include <string.h> | ||
| 8 | #include <sys/param.h> | ||
| 9 | |||
| 10 | #define MAX_AUTH_IPS 10 | ||
| 11 | #define DNS_BUF_SIZE 512 | ||
| 12 | #define DNS_PORT 53 | ||
| 13 | #define DOT_PORT 853 | ||
| 14 | #define DNS_TASK_STACK 4096 | ||
| 15 | #define DOT_TASK_STACK 3072 | ||
| 16 | #define DNS_TASK_PRIO 5 | ||
| 17 | #define DOT_TASK_PRIO 5 | ||
| 18 | #define DNS_FORWARD_TIMEOUT_MS 2000 | ||
| 19 | #define NXDOMAIN_TTL 30 | ||
| 20 | #define HIJACK_TTL 10 | ||
| 21 | |||
| 22 | static const char *TAG = "tg_core_dns"; | ||
| 23 | |||
| 24 | #pragma pack(push, 1) | ||
| 25 | typedef struct { | ||
| 26 | uint16_t id; | ||
| 27 | uint16_t flags; | ||
| 28 | uint16_t qdcount; | ||
| 29 | uint16_t ancount; | ||
| 30 | uint16_t nscount; | ||
| 31 | uint16_t arcount; | ||
| 32 | } dns_header_t; | ||
| 33 | #pragma pack(pop) | ||
| 34 | |||
| 35 | #pragma pack(push, 1) | ||
| 36 | typedef struct { | ||
| 37 | uint16_t name; | ||
| 38 | uint16_t type; | ||
| 39 | uint16_t class; | ||
| 40 | uint32_t ttl; | ||
| 41 | uint16_t len; | ||
| 42 | uint32_t addr; | ||
| 43 | } dns_answer_t; | ||
| 44 | #pragma pack(pop) | ||
| 45 | |||
| 46 | typedef struct { | ||
| 47 | uint32_t ip; | ||
| 48 | } auth_entry_t; | ||
| 49 | |||
| 50 | static auth_entry_t s_auth_list[MAX_AUTH_IPS]; | ||
| 51 | static int s_auth_count = 0; | ||
| 52 | static TaskHandle_t s_dns_task = NULL; | ||
| 53 | static TaskHandle_t s_dot_task = NULL; | ||
| 54 | static volatile bool s_dns_running = false; | ||
| 55 | static esp_ip4_addr_t s_ap_ip; | ||
| 56 | static esp_ip4_addr_t s_upstream_dns; | ||
| 57 | |||
| 58 | static bool is_authenticated(uint32_t ip) | ||
| 59 | { | ||
| 60 | for (int i = 0; i < s_auth_count; i++) { | ||
| 61 | if (s_auth_list[i].ip == ip) return true; | ||
| 62 | } | ||
| 63 | return false; | ||
| 64 | } | ||
| 65 | |||
| 66 | static void parse_dns_name(const uint8_t *buf, int buf_len, int offset, char *out, int out_len) | ||
| 67 | { | ||
| 68 | int pos = offset; | ||
| 69 | int out_pos = 0; | ||
| 70 | int jumped = 0; | ||
| 71 | int jump_pos = 0; | ||
| 72 | while (pos < buf_len && out_pos < out_len - 1) { | ||
| 73 | uint8_t len = buf[pos]; | ||
| 74 | if (len == 0) break; | ||
| 75 | if ((len & 0xC0) == 0xC0) { | ||
| 76 | if (!jumped) jump_pos = pos + 2; | ||
| 77 | pos = ((len & 0x3F) << 8) | buf[pos + 1]; | ||
| 78 | jumped = 1; | ||
| 79 | continue; | ||
| 80 | } | ||
| 81 | if (out_pos > 0 && out_pos < out_len - 1) out[out_pos++] = '.'; | ||
| 82 | pos++; | ||
| 83 | for (int i = 0; i < len && pos < buf_len && out_pos < out_len - 1; i++) { | ||
| 84 | out[out_pos++] = buf[pos++]; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | out[out_pos] = '\0'; | ||
| 88 | } | ||
| 89 | |||
| 90 | static int build_nxdomain(uint8_t *response, int req_len) | ||
| 91 | { | ||
| 92 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 93 | hdr->flags = htons(0x8403); | ||
| 94 | hdr->ancount = 0; | ||
| 95 | hdr->nscount = 0; | ||
| 96 | hdr->arcount = 0; | ||
| 97 | return req_len; | ||
| 98 | } | ||
| 99 | |||
| 100 | static int build_redirect_response(uint8_t *response, int req_len) | ||
| 101 | { | ||
| 102 | memmove(response, response, req_len); | ||
| 103 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 104 | hdr->flags = htons(0x8180); | ||
| 105 | hdr->ancount = htons(1); | ||
| 106 | hdr->nscount = 0; | ||
| 107 | hdr->arcount = 0; | ||
| 108 | int resp_len = req_len; | ||
| 109 | dns_answer_t ans; | ||
| 110 | ans.name = htons(0xC00C); | ||
| 111 | ans.type = htons(1); | ||
| 112 | ans.class = htons(1); | ||
| 113 | ans.ttl = htonl(HIJACK_TTL); | ||
| 114 | ans.len = htons(4); | ||
| 115 | ans.addr = s_ap_ip.addr; | ||
| 116 | memcpy(response + resp_len, &ans, sizeof(ans)); | ||
| 117 | resp_len += sizeof(ans); | ||
| 118 | return resp_len; | ||
| 119 | } | ||
| 120 | |||
| 121 | static int forward_dns(const uint8_t *req, int req_len, uint8_t *resp, int resp_buf_len, | ||
| 122 | uint16_t txn_id) | ||
| 123 | { | ||
| 124 | int upstream_sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 125 | if (upstream_sock < 0) return -1; | ||
| 126 | |||
| 127 | struct timeval tv = { .tv_sec = DNS_FORWARD_TIMEOUT_MS / 1000, .tv_usec = (DNS_FORWARD_TIMEOUT_MS % 1000) * 1000 }; | ||
| 128 | setsockopt(upstream_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | ||
| 129 | |||
| 130 | struct sockaddr_in upstream_addr = { | ||
| 131 | .sin_family = AF_INET, | ||
| 132 | .sin_port = htons(DNS_PORT), | ||
| 133 | .sin_addr.s_addr = s_upstream_dns.addr, | ||
| 134 | }; | ||
| 135 | |||
| 136 | sendto(upstream_sock, req, req_len, 0, (struct sockaddr *)&upstream_addr, sizeof(upstream_addr)); | ||
| 137 | |||
| 138 | int n = recvfrom(upstream_sock, resp, resp_buf_len, 0, NULL, NULL); | ||
| 139 | close(upstream_sock); | ||
| 140 | |||
| 141 | if (n > 0) { | ||
| 142 | if (n >= sizeof(dns_header_t)) { | ||
| 143 | dns_header_t *hdr = (dns_header_t *)resp; | ||
| 144 | hdr->id = htons(txn_id); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return n; | ||
| 148 | } | ||
| 149 | |||
| 150 | static void dns_server_task(void *arg) | ||
| 151 | { | ||
| 152 | int sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 153 | if (sock < 0) { | ||
| 154 | ESP_LOGE(TAG, "Failed to create DNS socket"); | ||
| 155 | s_dns_running = false; | ||
| 156 | vTaskDelete(NULL); | ||
| 157 | return; | ||
| 158 | } | ||
| 159 | |||
| 160 | struct sockaddr_in bind_addr = { | ||
| 161 | .sin_family = AF_INET, | ||
| 162 | .sin_port = htons(DNS_PORT), | ||
| 163 | .sin_addr.s_addr = INADDR_ANY, | ||
| 164 | }; | ||
| 165 | if (bind(sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | ||
| 166 | ESP_LOGE(TAG, "Failed to bind DNS socket"); | ||
| 167 | close(sock); | ||
| 168 | s_dns_running = false; | ||
| 169 | vTaskDelete(NULL); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | ESP_LOGI(TAG, "DNS server started on port %d, AP IP=" IPSTR ", upstream DNS=" IPSTR, | ||
| 174 | DNS_PORT, IP2STR(&s_ap_ip), IP2STR(&s_upstream_dns)); | ||
| 175 | |||
| 176 | uint8_t rx_buf[DNS_BUF_SIZE]; | ||
| 177 | uint8_t tx_buf[DNS_BUF_SIZE + sizeof(dns_answer_t)]; | ||
| 178 | |||
| 179 | while (s_dns_running) { | ||
| 180 | struct sockaddr_in client_addr; | ||
| 181 | socklen_t client_len = sizeof(client_addr); | ||
| 182 | int n = recvfrom(sock, rx_buf, sizeof(rx_buf), 0, | ||
| 183 | (struct sockaddr *)&client_addr, &client_len); | ||
| 184 | if (n < (int)sizeof(dns_header_t)) continue; | ||
| 185 | |||
| 186 | uint32_t client_ip = client_addr.sin_addr.s_addr; | ||
| 187 | dns_header_t *hdr = (dns_header_t *)rx_buf; | ||
| 188 | uint16_t txn_id = ntohs(hdr->id); | ||
| 189 | bool is_query = (ntohs(hdr->flags) & 0x8000) == 0; | ||
| 190 | uint16_t qdcount = ntohs(hdr->qdcount); | ||
| 191 | |||
| 192 | if (!is_query || qdcount == 0) continue; | ||
| 193 | |||
| 194 | int q_offset = sizeof(dns_header_t); | ||
| 195 | while (q_offset < n && rx_buf[q_offset] != 0) { | ||
| 196 | q_offset += rx_buf[q_offset] + 1; | ||
| 197 | } | ||
| 198 | if (q_offset + 5 > n) continue; | ||
| 199 | uint16_t qtype = (rx_buf[q_offset + 1] << 8) | rx_buf[q_offset + 2]; | ||
| 200 | int req_len = q_offset + 5; | ||
| 201 | |||
| 202 | if (is_authenticated(client_ip)) { | ||
| 203 | int resp_len = forward_dns(rx_buf, req_len, tx_buf, sizeof(tx_buf), txn_id); | ||
| 204 | if (resp_len > 0) { | ||
| 205 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 206 | } | ||
| 207 | } else { | ||
| 208 | char qname[256] = {0}; | ||
| 209 | parse_dns_name(rx_buf, n, sizeof(dns_header_t), qname, sizeof(qname)); | ||
| 210 | ESP_LOGI(TAG, "Hijack DNS from " IPSTR ": %s (type=%d)", | ||
| 211 | IP2STR(&(esp_ip4_addr_t){.addr=client_ip}), qname, qtype); | ||
| 212 | if (qtype == 1) { | ||
| 213 | int resp_len = build_redirect_response(rx_buf, req_len); | ||
| 214 | memcpy(tx_buf, rx_buf, resp_len); | ||
| 215 | dns_header_t *resp_hdr = (dns_header_t *)tx_buf; | ||
| 216 | resp_hdr->id = htons(txn_id); | ||
| 217 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 218 | } else { | ||
| 219 | int resp_len = build_nxdomain(rx_buf, req_len); | ||
| 220 | memcpy(tx_buf, rx_buf, resp_len); | ||
| 221 | dns_header_t *resp_hdr = (dns_header_t *)tx_buf; | ||
| 222 | resp_hdr->id = htons(txn_id); | ||
| 223 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | close(sock); | ||
| 229 | ESP_LOGI(TAG, "DNS server stopped"); | ||
| 230 | vTaskDelete(NULL); | ||
| 231 | } | ||
| 232 | |||
| 233 | static void dot_reject_task(void *arg) | ||
| 234 | { | ||
| 235 | int sock = socket(AF_INET, SOCK_STREAM, 0); | ||
| 236 | if (sock < 0) { | ||
| 237 | ESP_LOGE(TAG, "Failed to create DoT reject socket"); | ||
| 238 | vTaskDelete(NULL); | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | int opt = 1; | ||
| 243 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | ||
| 244 | |||
| 245 | struct sockaddr_in bind_addr = { | ||
| 246 | .sin_family = AF_INET, | ||
| 247 | .sin_port = htons(DOT_PORT), | ||
| 248 | .sin_addr.s_addr = INADDR_ANY, | ||
| 249 | }; | ||
| 250 | if (bind(sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | ||
| 251 | ESP_LOGE(TAG, "Failed to bind DoT reject socket on port %d", DOT_PORT); | ||
| 252 | close(sock); | ||
| 253 | vTaskDelete(NULL); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | listen(sock, 1); | ||
| 258 | ESP_LOGI(TAG, "DoT reject server on port %d (forces DNS fallback to port 53)", DOT_PORT); | ||
| 259 | |||
| 260 | while (s_dns_running) { | ||
| 261 | struct sockaddr_in client_addr; | ||
| 262 | socklen_t client_len = sizeof(client_addr); | ||
| 263 | int client_sock = accept(sock, (struct sockaddr *)&client_addr, &client_len); | ||
| 264 | if (client_sock >= 0) { | ||
| 265 | struct linger ling = { .l_onoff = 1, .l_linger = 0 }; | ||
| 266 | setsockopt(client_sock, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); | ||
| 267 | close(client_sock); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | |||
| 271 | close(sock); | ||
| 272 | ESP_LOGI(TAG, "DoT reject server stopped"); | ||
| 273 | vTaskDelete(NULL); | ||
| 274 | } | ||
| 275 | |||
| 276 | esp_err_t tollgate_core_dns_start_internal(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns) | ||
| 277 | { | ||
| 278 | if (s_dns_running) return ESP_OK; | ||
| 279 | s_ap_ip = ap_ip; | ||
| 280 | s_upstream_dns = upstream_dns; | ||
| 281 | s_dns_running = true; | ||
| 282 | xTaskCreate(dns_server_task, "dns_server", DNS_TASK_STACK, NULL, DNS_TASK_PRIO, &s_dns_task); | ||
| 283 | xTaskCreate(dot_reject_task, "dot_reject", DOT_TASK_STACK, NULL, DOT_TASK_PRIO, &s_dot_task); | ||
| 284 | return ESP_OK; | ||
| 285 | } | ||
| 286 | |||
| 287 | void tollgate_core_dns_stop(void) | ||
| 288 | { | ||
| 289 | s_dns_running = false; | ||
| 290 | vTaskDelay(pdMS_TO_TICKS(200)); | ||
| 291 | s_dns_task = NULL; | ||
| 292 | } | ||
| 293 | |||
| 294 | void tollgate_core_dns_set_authenticated(uint32_t client_ip, bool authenticated) | ||
| 295 | { | ||
| 296 | if (authenticated) { | ||
| 297 | if (is_authenticated(client_ip)) return; | ||
| 298 | if (s_auth_count < MAX_AUTH_IPS) { | ||
| 299 | s_auth_list[s_auth_count].ip = client_ip; | ||
| 300 | s_auth_count++; | ||
| 301 | } | ||
| 302 | } else { | ||
| 303 | for (int i = 0; i < s_auth_count; i++) { | ||
| 304 | if (s_auth_list[i].ip == client_ip) { | ||
| 305 | s_auth_list[i] = s_auth_list[s_auth_count - 1]; | ||
| 306 | s_auth_count--; | ||
| 307 | return; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } | ||
| 312 | |||
| 313 | bool tollgate_core_dns_is_running(void) | ||
| 314 | { | ||
| 315 | return s_dns_running; | ||
| 316 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_dns.h b/components/tollgate_core/src/tollgate_core_dns.h new file mode 100644 index 0000000..60aaaaf --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_dns.h | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_DNS_H | ||
| 2 | #define TOLLGATE_CORE_DNS_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include "esp_netif.h" | ||
| 6 | #include <stdbool.h> | ||
| 7 | |||
| 8 | esp_err_t tollgate_core_dns_start_internal(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns); | ||
| 9 | void tollgate_core_dns_stop(void); | ||
| 10 | void tollgate_core_dns_set_authenticated(uint32_t client_ip, bool authenticated); | ||
| 11 | bool tollgate_core_dns_is_running(void); | ||
| 12 | |||
| 13 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_firewall.c b/components/tollgate_core/src/tollgate_core_firewall.c new file mode 100644 index 0000000..e5d61d8 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_firewall.c | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | #include "tollgate_core_firewall.h" | ||
| 2 | #include "tollgate_core_dns.h" | ||
| 3 | #include "esp_log.h" | ||
| 4 | #include "esp_wifi.h" | ||
| 5 | #include "esp_wifi_ap_get_sta_list.h" | ||
| 6 | #include "lwip/lwip_napt.h" | ||
| 7 | #include "lwip/etharp.h" | ||
| 8 | #include "lwip/netif.h" | ||
| 9 | #include "lwip/prot/ip4.h" | ||
| 10 | #include <string.h> | ||
| 11 | |||
| 12 | #define MAX_CLIENTS 10 | ||
| 13 | |||
| 14 | static const char *TAG = "tg_core_fw"; | ||
| 15 | static esp_ip4_addr_t s_ap_ip; | ||
| 16 | |||
| 17 | typedef struct { | ||
| 18 | uint32_t ip; | ||
| 19 | char mac[TG_FW_MAX_MAC_LEN]; | ||
| 20 | } fw_client_t; | ||
| 21 | |||
| 22 | static fw_client_t s_clients[MAX_CLIENTS]; | ||
| 23 | static int s_client_count = 0; | ||
| 24 | |||
| 25 | esp_err_t tollgate_core_fw_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) | ||
| 26 | { | ||
| 27 | wifi_sta_list_t sta_list; | ||
| 28 | if (esp_wifi_ap_get_sta_list(&sta_list) == ESP_OK) { | ||
| 29 | wifi_sta_mac_ip_list_t ip_mac_list; | ||
| 30 | if (esp_wifi_ap_get_sta_list_with_ip(&sta_list, &ip_mac_list) == ESP_OK) { | ||
| 31 | for (int i = 0; i < ip_mac_list.num; i++) { | ||
| 32 | if (ip_mac_list.sta[i].ip.addr == client_ip) { | ||
| 33 | snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 34 | ip_mac_list.sta[i].mac[0], ip_mac_list.sta[i].mac[1], | ||
| 35 | ip_mac_list.sta[i].mac[2], ip_mac_list.sta[i].mac[3], | ||
| 36 | ip_mac_list.sta[i].mac[4], ip_mac_list.sta[i].mac[5]); | ||
| 37 | return ESP_OK; | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | ip4_addr_t *entry_ip = NULL; | ||
| 44 | struct netif *entry_netif = NULL; | ||
| 45 | struct eth_addr *entry_eth = NULL; | ||
| 46 | ssize_t i = 0; | ||
| 47 | while (etharp_get_entry(i, &entry_ip, &entry_netif, &entry_eth) == ERR_OK) { | ||
| 48 | if (entry_ip && entry_ip->addr == client_ip && entry_eth) { | ||
| 49 | snprintf(mac_out, mac_out_size, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
| 50 | entry_eth->addr[0], entry_eth->addr[1], entry_eth->addr[2], | ||
| 51 | entry_eth->addr[3], entry_eth->addr[4], entry_eth->addr[5]); | ||
| 52 | return ESP_OK; | ||
| 53 | } | ||
| 54 | i++; | ||
| 55 | } | ||
| 56 | return ESP_FAIL; | ||
| 57 | } | ||
| 58 | |||
| 59 | esp_err_t tollgate_core_fw_init(esp_ip4_addr_t ap_ip) | ||
| 60 | { | ||
| 61 | s_ap_ip = ap_ip; | ||
| 62 | memset(s_clients, 0, sizeof(s_clients)); | ||
| 63 | s_client_count = 0; | ||
| 64 | ip_napt_enable(s_ap_ip.addr, 1); | ||
| 65 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR " (NAT always on, per-client filter)", IP2STR(&s_ap_ip)); | ||
| 66 | return ESP_OK; | ||
| 67 | } | ||
| 68 | |||
| 69 | int tollgate_core_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) | ||
| 70 | { | ||
| 71 | (void)dest_addr_hostorder; | ||
| 72 | if (p->len < IP_HLEN) return -1; | ||
| 73 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 74 | uint32_t src_ip_h = lwip_ntohl(iphdr->src.addr); | ||
| 75 | uint32_t ap_subnet = lwip_ntohl(s_ap_ip.addr) & 0xFFFFFF00; | ||
| 76 | if ((src_ip_h & 0xFFFFFF00) != ap_subnet) { | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | if (tollgate_core_fw_is_allowed(iphdr->src.addr)) { | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | static fw_client_t *find_client_by_ip(uint32_t client_ip) | ||
| 86 | { | ||
| 87 | for (int i = 0; i < s_client_count; i++) { | ||
| 88 | if (s_clients[i].ip == client_ip) return &s_clients[i]; | ||
| 89 | } | ||
| 90 | return NULL; | ||
| 91 | } | ||
| 92 | |||
| 93 | static fw_client_t *find_client_by_mac(const char *mac) | ||
| 94 | { | ||
| 95 | for (int i = 0; i < s_client_count; i++) { | ||
| 96 | if (s_clients[i].mac[0] != '\0' && strcmp(s_clients[i].mac, mac) == 0) { | ||
| 97 | return &s_clients[i]; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | return NULL; | ||
| 101 | } | ||
| 102 | |||
| 103 | void tollgate_core_fw_grant(uint32_t client_ip) | ||
| 104 | { | ||
| 105 | fw_client_t *existing = find_client_by_ip(client_ip); | ||
| 106 | if (existing) { | ||
| 107 | existing->ip = client_ip; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | if (s_client_count >= MAX_CLIENTS) { | ||
| 111 | ESP_LOGW(TAG, "Max clients reached, cannot grant access"); | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | fw_client_t *client = &s_clients[s_client_count]; | ||
| 116 | client->ip = client_ip; | ||
| 117 | client->mac[0] = '\0'; | ||
| 118 | tollgate_core_fw_get_mac_for_ip(client_ip, client->mac, sizeof(client->mac)); | ||
| 119 | s_client_count++; | ||
| 120 | |||
| 121 | tollgate_core_dns_set_authenticated(client_ip, true); | ||
| 122 | |||
| 123 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 124 | ESP_LOGI(TAG, "Access granted to " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 125 | client->mac[0] ? client->mac : "unknown"); | ||
| 126 | } | ||
| 127 | |||
| 128 | void tollgate_core_fw_revoke(uint32_t client_ip) | ||
| 129 | { | ||
| 130 | for (int i = 0; i < s_client_count; i++) { | ||
| 131 | if (s_clients[i].ip == client_ip) { | ||
| 132 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 133 | ESP_LOGI(TAG, "Access revoked for " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 134 | s_clients[i].mac[0] ? s_clients[i].mac : "unknown"); | ||
| 135 | s_clients[i] = s_clients[s_client_count - 1]; | ||
| 136 | s_client_count--; | ||
| 137 | tollgate_core_dns_set_authenticated(client_ip, false); | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | void tollgate_core_fw_revoke_all(void) | ||
| 144 | { | ||
| 145 | for (int i = 0; i < s_client_count; i++) { | ||
| 146 | tollgate_core_dns_set_authenticated(s_clients[i].ip, false); | ||
| 147 | } | ||
| 148 | s_client_count = 0; | ||
| 149 | ESP_LOGI(TAG, "All client access revoked"); | ||
| 150 | } | ||
| 151 | |||
| 152 | bool tollgate_core_fw_is_allowed(uint32_t client_ip) | ||
| 153 | { | ||
| 154 | return find_client_by_ip(client_ip) != NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | bool tollgate_core_fw_is_mac_allowed(const char *mac) | ||
| 158 | { | ||
| 159 | return find_client_by_mac(mac) != NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | int tollgate_core_fw_client_count(void) | ||
| 163 | { | ||
| 164 | return s_client_count; | ||
| 165 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_firewall.h b/components/tollgate_core/src/tollgate_core_firewall.h new file mode 100644 index 0000000..f06c801 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_firewall.h | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_FIREWALL_H | ||
| 2 | #define TOLLGATE_CORE_FIREWALL_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include "esp_netif.h" | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include <stdint.h> | ||
| 8 | |||
| 9 | struct pbuf; | ||
| 10 | |||
| 11 | #define TG_FW_MAX_MAC_LEN 18 | ||
| 12 | |||
| 13 | esp_err_t tollgate_core_fw_init(esp_ip4_addr_t ap_ip); | ||
| 14 | void tollgate_core_fw_grant(uint32_t client_ip); | ||
| 15 | void tollgate_core_fw_revoke(uint32_t client_ip); | ||
| 16 | void tollgate_core_fw_revoke_all(void); | ||
| 17 | bool tollgate_core_fw_is_allowed(uint32_t client_ip); | ||
| 18 | bool tollgate_core_fw_is_mac_allowed(const char *mac); | ||
| 19 | int tollgate_core_fw_client_count(void); | ||
| 20 | |||
| 21 | esp_err_t tollgate_core_fw_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size); | ||
| 22 | |||
| 23 | int tollgate_core_ip4_canforward_filter(struct pbuf *p, uint32_t dest_addr_hostorder); | ||
| 24 | |||
| 25 | #endif | ||
diff --git a/components/tollgate_core/src/tollgate_core_session.c b/components/tollgate_core/src/tollgate_core_session.c new file mode 100644 index 0000000..48d32e7 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_session.c | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | #include "tollgate_core_session.h" | ||
| 2 | #include "tollgate_core_firewall.h" | ||
| 3 | #include "esp_log.h" | ||
| 4 | #include "freertos/FreeRTOS.h" | ||
| 5 | #include "freertos/task.h" | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | static const char *TAG = "tg_core_session"; | ||
| 9 | static tg_session_t s_sessions[TG_SESSION_MAX_CLIENTS]; | ||
| 10 | static int s_session_count = 0; | ||
| 11 | |||
| 12 | static const tollgate_platform_t *s_platform; | ||
| 13 | |||
| 14 | void tollgate_core_session_set_platform(const tollgate_platform_t *platform) | ||
| 15 | { | ||
| 16 | s_platform = platform; | ||
| 17 | } | ||
| 18 | |||
| 19 | static int64_t get_time_ms(void) | ||
| 20 | { | ||
| 21 | if (s_platform && s_platform->get_time_ms) { | ||
| 22 | return s_platform->get_time_ms(); | ||
| 23 | } | ||
| 24 | return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; | ||
| 25 | } | ||
| 26 | |||
| 27 | esp_err_t tollgate_core_session_init(void) | ||
| 28 | { | ||
| 29 | memset(s_sessions, 0, sizeof(s_sessions)); | ||
| 30 | s_session_count = 0; | ||
| 31 | ESP_LOGI(TAG, "Session manager initialized"); | ||
| 32 | return ESP_OK; | ||
| 33 | } | ||
| 34 | |||
| 35 | static void populate_mac(tg_session_t *session, uint32_t client_ip) | ||
| 36 | { | ||
| 37 | if (tollgate_core_fw_get_mac_for_ip(client_ip, session->mac, sizeof(session->mac)) != ESP_OK) { | ||
| 38 | session->mac[0] = '\0'; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms) | ||
| 43 | { | ||
| 44 | tg_session_t *existing = tollgate_core_session_find_by_ip(client_ip); | ||
| 45 | if (existing) { | ||
| 46 | tollgate_core_session_extend(existing, allotment_ms); | ||
| 47 | return existing; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (s_session_count >= TG_SESSION_MAX_CLIENTS) { | ||
| 51 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 52 | if (!s_sessions[i].active || tollgate_core_session_is_expired(&s_sessions[i])) { | ||
| 53 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 54 | break; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 60 | if (!s_sessions[i].active) { | ||
| 61 | s_sessions[i].client_ip = client_ip; | ||
| 62 | s_sessions[i].allotment_ms = allotment_ms; | ||
| 63 | s_sessions[i].start_time_ms = get_time_ms(); | ||
| 64 | s_sessions[i].active = true; | ||
| 65 | populate_mac(&s_sessions[i], client_ip); | ||
| 66 | |||
| 67 | s_session_count++; | ||
| 68 | tollgate_core_fw_grant(client_ip); | ||
| 69 | |||
| 70 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 71 | ESP_LOGI(TAG, "Session created: " IPSTR " mac=%s allotment=%llums", IP2STR(&ip), | ||
| 72 | s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown", | ||
| 73 | (unsigned long long)allotment_ms); | ||
| 74 | return &s_sessions[i]; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | ESP_LOGW(TAG, "No free session slots"); | ||
| 79 | return NULL; | ||
| 80 | } | ||
| 81 | |||
| 82 | tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes) | ||
| 83 | { | ||
| 84 | tg_session_t *s = tollgate_core_session_create(client_ip, 0); | ||
| 85 | if (s) { | ||
| 86 | s->allotment_bytes = allotment_bytes; | ||
| 87 | s->bytes_consumed = 0; | ||
| 88 | s->allotment_ms = INT64_MAX; | ||
| 89 | esp_ip4_addr_t ip = { .addr = client_ip }; | ||
| 90 | ESP_LOGI(TAG, "Bytes session created: " IPSTR " allotment=%llu bytes", IP2STR(&ip), | ||
| 91 | (unsigned long long)allotment_bytes); | ||
| 92 | } | ||
| 93 | return s; | ||
| 94 | } | ||
| 95 | |||
| 96 | void tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes) | ||
| 97 | { | ||
| 98 | tg_session_t *s = tollgate_core_session_find_by_ip(client_ip); | ||
| 99 | if (s && s->active) { | ||
| 100 | s->bytes_consumed += bytes; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | tg_session_t *tollgate_core_session_find_by_ip(uint32_t client_ip) | ||
| 105 | { | ||
| 106 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 107 | if (s_sessions[i].active && s_sessions[i].client_ip == client_ip) { | ||
| 108 | return &s_sessions[i]; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | return NULL; | ||
| 112 | } | ||
| 113 | |||
| 114 | tg_session_t *tollgate_core_session_find_by_mac(const char *mac) | ||
| 115 | { | ||
| 116 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 117 | if (s_sessions[i].active && s_sessions[i].mac[0] != '\0' && | ||
| 118 | strcmp(s_sessions[i].mac, mac) == 0) { | ||
| 119 | return &s_sessions[i]; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | return NULL; | ||
| 123 | } | ||
| 124 | |||
| 125 | void tollgate_core_session_extend(tg_session_t *session, uint64_t additional_ms) | ||
| 126 | { | ||
| 127 | if (!session || !session->active) return; | ||
| 128 | session->allotment_ms += additional_ms; | ||
| 129 | esp_ip4_addr_t ip = { .addr = session->client_ip }; | ||
| 130 | ESP_LOGI(TAG, "Session extended: " IPSTR " +%llums (total=%llu)", IP2STR(&ip), | ||
| 131 | (unsigned long long)additional_ms, (unsigned long long)session->allotment_ms); | ||
| 132 | } | ||
| 133 | |||
| 134 | bool tollgate_core_session_is_expired(const tg_session_t *session) | ||
| 135 | { | ||
| 136 | if (!session || !session->active) return true; | ||
| 137 | |||
| 138 | if (s_platform && s_platform->get_metric) { | ||
| 139 | const char *metric = s_platform->get_metric(); | ||
| 140 | if (metric && strcmp(metric, "bytes") == 0) { | ||
| 141 | return session->bytes_consumed >= session->allotment_bytes; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | int64_t elapsed = get_time_ms() - session->start_time_ms; | ||
| 146 | return elapsed >= (int64_t)session->allotment_ms; | ||
| 147 | } | ||
| 148 | |||
| 149 | static void check_expiry(void) | ||
| 150 | { | ||
| 151 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 152 | if (s_sessions[i].active && tollgate_core_session_is_expired(&s_sessions[i])) { | ||
| 153 | esp_ip4_addr_t ip = { .addr = s_sessions[i].client_ip }; | ||
| 154 | ESP_LOGI(TAG, "Session expired: " IPSTR " mac=%s", IP2STR(&ip), | ||
| 155 | s_sessions[i].mac[0] ? s_sessions[i].mac : "unknown"); | ||
| 156 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | void tollgate_core_session_revoke(tg_session_t *session) | ||
| 162 | { | ||
| 163 | if (!session || !session->active) return; | ||
| 164 | tollgate_core_fw_revoke(session->client_ip); | ||
| 165 | session->active = false; | ||
| 166 | s_session_count--; | ||
| 167 | } | ||
| 168 | |||
| 169 | void tollgate_core_session_revoke_all(void) | ||
| 170 | { | ||
| 171 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 172 | if (s_sessions[i].active) { | ||
| 173 | tollgate_core_session_revoke(&s_sessions[i]); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | int tollgate_core_session_active_count(void) | ||
| 179 | { | ||
| 180 | int count = 0; | ||
| 181 | for (int i = 0; i < TG_SESSION_MAX_CLIENTS; i++) { | ||
| 182 | if (s_sessions[i].active) count++; | ||
| 183 | } | ||
| 184 | return count; | ||
| 185 | } | ||
| 186 | |||
| 187 | void tollgate_core_session_tick(void) | ||
| 188 | { | ||
| 189 | check_expiry(); | ||
| 190 | } | ||
| 191 | |||
| 192 | tg_session_t *tollgate_core_session_get_array(void) | ||
| 193 | { | ||
| 194 | return s_sessions; | ||
| 195 | } | ||
| 196 | |||
| 197 | int tollgate_core_session_get_array_size(void) | ||
| 198 | { | ||
| 199 | return TG_SESSION_MAX_CLIENTS; | ||
| 200 | } | ||
diff --git a/components/tollgate_core/src/tollgate_core_session.h b/components/tollgate_core/src/tollgate_core_session.h new file mode 100644 index 0000000..95b5c48 --- /dev/null +++ b/components/tollgate_core/src/tollgate_core_session.h | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | #ifndef TOLLGATE_CORE_SESSION_H | ||
| 2 | #define TOLLGATE_CORE_SESSION_H | ||
| 3 | |||
| 4 | #include "esp_err.h" | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | |||
| 8 | #define TG_SESSION_MAX_CLIENTS 10 | ||
| 9 | #define TG_SESSION_MAX_MAC_LEN 18 | ||
| 10 | |||
| 11 | typedef struct { | ||
| 12 | uint32_t client_ip; | ||
| 13 | char mac[TG_SESSION_MAX_MAC_LEN]; | ||
| 14 | uint64_t allotment_ms; | ||
| 15 | int64_t start_time_ms; | ||
| 16 | uint64_t allotment_bytes; | ||
| 17 | uint64_t bytes_consumed; | ||
| 18 | bool active; | ||
| 19 | } tg_session_t; | ||
| 20 | |||
| 21 | esp_err_t tollgate_core_session_init(void); | ||
| 22 | |||
| 23 | tg_session_t *tollgate_core_session_create(uint32_t client_ip, uint64_t allotment_ms); | ||
| 24 | tg_session_t *tollgate_core_session_create_bytes(uint32_t client_ip, uint64_t allotment_bytes); | ||
| 25 | |||
| 26 | void tollgate_core_session_add_bytes(uint32_t client_ip, uint64_t bytes); | ||
| 27 | |||
| 28 | tg_session_t *tollgate_core_session_find_by_ip(uint32_t client_ip); | ||
| 29 | tg_session_t *tollgate_core_session_find_by_mac(const char *mac); | ||
| 30 | |||
| 31 | void tollgate_core_session_extend(tg_session_t *session, uint64_t additional_ms); | ||
| 32 | |||
| 33 | bool tollgate_core_session_is_expired(const tg_session_t *session); | ||
| 34 | |||
| 35 | void tollgate_core_session_revoke(tg_session_t *session); | ||
| 36 | void tollgate_core_session_revoke_all(void); | ||
| 37 | |||
| 38 | int tollgate_core_session_active_count(void); | ||
| 39 | |||
| 40 | void tollgate_core_session_tick(void); | ||
| 41 | |||
| 42 | tg_session_t *tollgate_core_session_get_array(void); | ||
| 43 | int tollgate_core_session_get_array_size(void); | ||
| 44 | |||
| 45 | #endif | ||
diff --git a/docs/TOLLGATE_CORE_DESIGN.md b/docs/TOLLGATE_CORE_DESIGN.md index fe31c91..a1ec639 100644 --- a/docs/TOLLGATE_CORE_DESIGN.md +++ b/docs/TOLLGATE_CORE_DESIGN.md | |||
| @@ -284,65 +284,66 @@ This refactoring **must not proceed** until these branches land on master: | |||
| 284 | 284 | ||
| 285 | - [ ] All blocking PRs merged to master | 285 | - [ ] All blocking PRs merged to master |
| 286 | - [ ] This branch rebased onto latest master | 286 | - [ ] This branch rebased onto latest master |
| 287 | - [ ] Full build passes on master | 287 | - [x] Full build passes on master |
| 288 | 288 | ||
| 289 | ### Phase 1: Create Component Skeleton | 289 | ### Phase 1: Create Component Skeleton |
| 290 | 290 | ||
| 291 | - [ ] Create `components/tollgate_core/` directory structure | 291 | - [x] Create `components/tollgate_core/` directory structure |
| 292 | - [ ] Create `components/tollgate_core/include/tollgate_core.h` (public API) | 292 | - [x] Create `components/tollgate_core/include/tollgate_core.h` (public API) |
| 293 | - [ ] Create `components/tollgate_core/include/tollgate_platform.h` (platform interface) | 293 | - [x] Create `components/tollgate_core/include/tollgate_platform.h` (platform interface) |
| 294 | - [ ] Create `components/tollgate_core/idf_component.yml` (component metadata) | 294 | - [x] Create `components/tollgate_core/idf_component.yml` (component metadata) |
| 295 | - [ ] Create `components/tollgate_core/CMakeLists.txt` (register component, REQUIRES nucula_lib) | 295 | - [x] Create `components/tollgate_core/CMakeLists.txt` (register component) |
| 296 | - [ ] Verify empty component builds without errors | 296 | - [ ] Verify empty component builds without errors |
| 297 | 297 | ||
| 298 | ### Phase 2: Move Core Modules (one at a time, build after each) | 298 | ### Phase 2: Move Core Modules (one at a time, build after each) |
| 299 | 299 | ||
| 300 | - [ ] Copy `main/cashu.c/h` → `components/tollgate_core/src/tollgate_core_cashu.c/h` | 300 | - [x] Copy `main/cashu.c/h` → `components/tollgate_core/src/tollgate_core_cashu.c/h` |
| 301 | - [ ] Rename functions: `cashu_*` → `tollgate_core_cashu_*` | 301 | - [x] Rename functions: `cashu_*` → `tollgate_core_cashu_*` |
| 302 | - [ ] Replace `tollgate_config_get()` calls with platform callbacks | 302 | - [x] Replace `tollgate_config_get()` calls with parameterized arguments |
| 303 | - [ ] Remove direct `config.h` include | 303 | - [x] Remove direct `config.h` include |
| 304 | - [ ] Build and verify | 304 | - [ ] Build and verify |
| 305 | - [ ] Copy `main/dns_server.c/h` → `components/tollgate_core/src/tollgate_core_dns.c/h` | 305 | - [x] Copy `main/dns_server.c/h` → `components/tollgate_core/src/tollgate_core_dns.c/h` |
| 306 | - [ ] Rename functions: `dns_server_*` → `tollgate_core_dns_*` | 306 | - [x] Rename functions: `dns_server_*` → `tollgate_core_dns_*` |
| 307 | - [ ] No platform dependencies (pure LWIP) — should be clean copy | 307 | - [x] No platform dependencies (pure LWIP) — clean copy |
| 308 | - [ ] Build and verify | 308 | - [ ] Build and verify |
| 309 | - [ ] Copy `main/firewall.c/h` → `components/tollgate_core/src/tollgate_core_firewall.c/h` | 309 | - [x] Copy `main/firewall.c/h` → `components/tollgate_core/src/tollgate_core_firewall.c/h` |
| 310 | - [ ] Rename functions: `firewall_*` → `tollgate_core_firewall_*` | 310 | - [x] Rename functions: `firewall_*` → `tollgate_core_firewall_*` / `tollgate_core_fw_*` |
| 311 | - [ ] Internalize `dns_set_authenticated` calls (keep within component) | 311 | - [x] Internalize `dns_set_authenticated` calls (kept within component) |
| 312 | - [ ] Remove `dns_server.h` external dependency | 312 | - [x] Remove `dns_server.h` external dependency |
| 313 | - [ ] Build and verify | 313 | - [ ] Build and verify |
| 314 | - [ ] Copy `main/session.c/h` → `components/tollgate_core/src/tollgate_core_session.c/h` | 314 | - [x] Copy `main/session.c/h` → `components/tollgate_core/src/tollgate_core_session.c/h` |
| 315 | - [ ] Rename functions: `session_*` → `tollgate_core_session_*` | 315 | - [x] Rename functions: `session_*` → `tollgate_core_session_*` |
| 316 | - [ ] Replace `config.h` calls with platform callbacks for metric check | 316 | - [x] Replace `config.h` calls with platform callbacks for metric check |
| 317 | - [ ] Internalize firewall notification (already calls firewall directly) | 317 | - [x] Internalize firewall notification (already calls firewall directly) |
| 318 | - [ ] Support both time and bytes metrics (portable, not stripped) | 318 | - [x] Support both time and bytes metrics (portable, not stripped) |
| 319 | - [ ] Build and verify | 319 | - [ ] Build and verify |
| 320 | 320 | ||
| 321 | ### Phase 3: Wire Component API | 321 | ### Phase 3: Wire Component API |
| 322 | 322 | ||
| 323 | - [ ] Implement `tollgate_core_init(const tollgate_platform_t *platform)` — stores platform, inits all sub-modules | 323 | - [x] Implement `tollgate_core_init(const tollgate_platform_t *platform, esp_ip4_addr_t ap_ip)` — stores platform, inits all sub-modules |
| 324 | - [ ] Implement `tollgate_core_process_payment(ip, token)` — decode → verify → spend → create session | 324 | - [x] Implement `tollgate_core_process_payment(ip, token)` — decode → verify → spend → create session |
| 325 | - [ ] Implement `tollgate_core_client_connected(mac, ip)` — owner detection + firewall check | 325 | - [x] Implement `tollgate_core_client_connected(mac, ip)` — owner detection + firewall check |
| 326 | - [ ] Implement `tollgate_core_client_disconnected(mac)` — session cleanup + owner reassign | 326 | - [x] Implement `tollgate_core_client_disconnected(mac)` — session cleanup + owner reassign |
| 327 | - [ ] Implement `tollgate_core_tick()` — session expiry check | 327 | - [x] Implement `tollgate_core_tick()` — session expiry check |
| 328 | - [ ] Implement `tollgate_core_get_status_json()` — JSON status | 328 | - [x] Implement `tollgate_core_get_status_json()` — JSON status |
| 329 | - [ ] Implement `tollgate_core_get_config_json()` — JSON config (via platform) | 329 | - [x] Implement `tollgate_core_get_config_json()` — JSON config (via platform) |
| 330 | - [ ] Build and verify standalone | 330 | - [ ] Build and verify standalone |
| 331 | 331 | ||
| 332 | ### Phase 4: Standalone Platform Implementation | 332 | ### Phase 4: Standalone Platform Implementation |
| 333 | 333 | ||
| 334 | - [ ] Create `main/tollgate_platform.c` implementing `tollgate_platform_t` | 334 | - [x] Create `main/tollgate_platform.c` implementing `tollgate_platform_t` |
| 335 | - [ ] `get_price_sats` → `tollgate_config_get()->price_per_step` | 335 | - [x] `get_price_sats` → `tollgate_config_get()->price_per_step` |
| 336 | - [ ] `get_step_ms` → `tollgate_config_get()->step_size` | 336 | - [x] `get_step_ms` → `tollgate_config_get()->step_size` |
| 337 | - [ ] `get_mint_url` → `tollgate_config_get()->mint_url` | 337 | - [x] `get_mint_url` → `tollgate_config_get()->mint_url` |
| 338 | - [ ] `get_metric` → `tollgate_config_get()->metric` | 338 | - [x] `get_metric` → `tollgate_config_get()->metric` |
| 339 | - [ ] `get_step_bytes` → `tollgate_config_get()->step_bytes` | 339 | - [x] `get_step_bytes` → `tollgate_config_get()->step_bytes` |
| 340 | - [ ] `get_time_ms` → `xTaskGetTickCount() * portTICK_PERIOD_MS` | 340 | - [x] `get_time_ms` → `xTaskGetTickCount() * portTICK_PERIOD_MS` |
| 341 | - [ ] `spend_proofs` → `nucula_wallet_receive()` | 341 | - [x] `spend_proofs` → stub returning true (wallet called separately) |
| 342 | - [ ] Update `main/tollgate_api.c` to call `tollgate_core_*` instead of direct module calls | 342 | - [x] Update `main/tollgate_api.c` to call `tollgate_core_*` instead of direct module calls |
| 343 | - [ ] Update `main/tollgate_main.c` init sequence | 343 | - [x] Update `main/tollgate_main.c` init sequence |
| 344 | - [ ] Remove old `main/cashu.c`, `main/dns_server.c`, `main/firewall.c`, `main/session.c` | 344 | - [x] Remove old `main/cashu.c`, `main/dns_server.c`, `main/firewall.c`, `main/session.c` from CMakeLists.txt |
| 345 | - [ ] Update `main/CMakeLists.txt` (remove old SRCS, add `tollgate_platform.c`) | 345 | - [x] Update `main/CMakeLists.txt` (remove old SRCS, add `tollgate_platform.c`, add `tollgate_core` to REQUIRES) |
| 346 | - [x] Update `main/lwip_tollgate_hooks.h` to call `tollgate_core_ip4_canforward_filter` | ||
| 346 | - [ ] Full standalone build + test | 347 | - [ ] Full standalone build + test |
| 347 | 348 | ||
| 348 | ### Phase 5: ESP-Miner Integration | 349 | ### Phase 5: ESP-Miner Integration |
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9b0fb1c..5f195c3 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt | |||
| @@ -1,10 +1,7 @@ | |||
| 1 | idf_component_register(SRCS "tollgate_main.c" | 1 | idf_component_register(SRCS "tollgate_main.c" |
| 2 | "config.c" | 2 | "config.c" |
| 3 | "dns_server.c" | 3 | "tollgate_platform.c" |
| 4 | "captive_portal.c" | 4 | "captive_portal.c" |
| 5 | "firewall.c" | ||
| 6 | "cashu.c" | ||
| 7 | "session.c" | ||
| 8 | "tollgate_api.c" | 5 | "tollgate_api.c" |
| 9 | "identity.c" | 6 | "identity.c" |
| 10 | "nostr_event.c" | 7 | "nostr_event.c" |
| @@ -22,4 +19,5 @@ idf_component_register(SRCS "tollgate_main.c" | |||
| 22 | REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server | 19 | REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server |
| 23 | lwip json esp_http_client mbedtls esp-tls log spiffs | 20 | lwip json esp_http_client mbedtls esp-tls log spiffs |
| 24 | nucula_lib secp256k1 axs15231b qrcode | 21 | nucula_lib secp256k1 axs15231b qrcode |
| 22 | tollgate_core | ||
| 25 | PRIV_REQUIRES esp-tls) | 23 | PRIV_REQUIRES esp-tls) |
diff --git a/main/lwip_tollgate_hooks.h b/main/lwip_tollgate_hooks.h index 76017be..2953c48 100644 --- a/main/lwip_tollgate_hooks.h +++ b/main/lwip_tollgate_hooks.h | |||
| @@ -3,8 +3,8 @@ | |||
| 3 | 3 | ||
| 4 | #include "lwip/pbuf.h" | 4 | #include "lwip/pbuf.h" |
| 5 | 5 | ||
| 6 | int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder); | 6 | int tollgate_core_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder); |
| 7 | 7 | ||
| 8 | #define LWIP_HOOK_IP4_CANFORWARD(p, addr) tollgate_ip4_canforward_filter(p, addr) | 8 | #define LWIP_HOOK_IP4_CANFORWARD(p, addr) tollgate_core_ip4_canforward_filter(p, addr) |
| 9 | 9 | ||
| 10 | #endif | 10 | #endif |
diff --git a/main/tollgate_api.c b/main/tollgate_api.c index 650b0f3..62f75ee 100644 --- a/main/tollgate_api.c +++ b/main/tollgate_api.c | |||
| @@ -1,8 +1,6 @@ | |||
| 1 | #include "tollgate_api.h" | 1 | #include "tollgate_api.h" |
| 2 | #include "cashu.h" | 2 | #include "tollgate_core.h" |
| 3 | #include "config.h" | 3 | #include "config.h" |
| 4 | #include "session.h" | ||
| 5 | #include "firewall.h" | ||
| 6 | #include "nucula_wallet.h" | 4 | #include "nucula_wallet.h" |
| 7 | #include "esp_log.h" | 5 | #include "esp_log.h" |
| 8 | #include "cJSON.h" | 6 | #include "cJSON.h" |
| @@ -184,37 +182,11 @@ static esp_err_t api_post_payment(httpd_req_t *req) | |||
| 184 | 182 | ||
| 185 | ESP_LOGI(TAG, "Payment received: %d bytes", total); | 183 | ESP_LOGI(TAG, "Payment received: %d bytes", total); |
| 186 | 184 | ||
| 187 | cashu_token_t *token = malloc(sizeof(cashu_token_t)); | 185 | esp_err_t err = tollgate_core_process_payment(client_ip, body); |
| 188 | if (!token) { | ||
| 189 | cJSON *notice = create_notice("error", "session-error", "Out of memory"); | ||
| 190 | char *json = cJSON_PrintUnformatted(notice); | ||
| 191 | httpd_resp_set_status(req, "503 Service Unavailable"); | ||
| 192 | httpd_resp_set_type(req, "application/json"); | ||
| 193 | httpd_resp_send(req, json, strlen(json)); | ||
| 194 | cJSON_free(json); | ||
| 195 | cJSON_Delete(notice); | ||
| 196 | return ESP_OK; | ||
| 197 | } | ||
| 198 | esp_err_t err = cashu_decode_token(body, token); | ||
| 199 | char *body_copy = strdup(body); | ||
| 200 | free(body); | 186 | free(body); |
| 201 | 187 | ||
| 202 | if (err != ESP_OK) { | 188 | if (err != ESP_OK) { |
| 203 | free(token); | 189 | cJSON *notice = create_notice("error", "payment-error", "Payment processing failed"); |
| 204 | cJSON *notice = create_notice("error", "payment-error-invalid", "Failed to decode Cashu token"); | ||
| 205 | char *json = cJSON_PrintUnformatted(notice); | ||
| 206 | httpd_resp_set_status(req, "400 Bad Request"); | ||
| 207 | httpd_resp_set_type(req, "application/json"); | ||
| 208 | httpd_resp_send(req, json, strlen(json)); | ||
| 209 | cJSON_free(json); | ||
| 210 | cJSON_Delete(notice); | ||
| 211 | return ESP_OK; | ||
| 212 | } | ||
| 213 | |||
| 214 | const char *mint_url = token->mint_url[0] ? token->mint_url : tollgate_config_get()->mint_url; | ||
| 215 | if (!cashu_is_mint_accepted(mint_url)) { | ||
| 216 | free(token); | ||
| 217 | cJSON *notice = create_notice("error", "payment-error-mint-not-accepted", "Mint not accepted"); | ||
| 218 | char *json = cJSON_PrintUnformatted(notice); | 190 | char *json = cJSON_PrintUnformatted(notice); |
| 219 | httpd_resp_set_status(req, "402 Payment Required"); | 191 | httpd_resp_set_status(req, "402 Payment Required"); |
| 220 | httpd_resp_set_type(req, "application/json"); | 192 | httpd_resp_set_type(req, "application/json"); |
| @@ -224,97 +196,14 @@ static esp_err_t api_post_payment(httpd_req_t *req) | |||
| 224 | return ESP_OK; | 196 | return ESP_OK; |
| 225 | } | 197 | } |
| 226 | 198 | ||
| 227 | cashu_proof_state_t *states = malloc(CASHU_MAX_PROOFS * sizeof(cashu_proof_state_t)); | ||
| 228 | if (!states) { | ||
| 229 | free(token); | ||
| 230 | cJSON *notice = create_notice("error", "session-error", "Out of memory"); | ||
| 231 | char *json = cJSON_PrintUnformatted(notice); | ||
| 232 | httpd_resp_set_status(req, "503 Service Unavailable"); | ||
| 233 | httpd_resp_set_type(req, "application/json"); | ||
| 234 | httpd_resp_send(req, json, strlen(json)); | ||
| 235 | cJSON_free(json); | ||
| 236 | cJSON_Delete(notice); | ||
| 237 | return ESP_OK; | ||
| 238 | } | ||
| 239 | int state_count = 0; | ||
| 240 | err = cashu_check_proof_states(mint_url, token, states, &state_count); | ||
| 241 | ESP_LOGI(TAG, "Stack HWM after checkstate: %u", uxTaskGetStackHighWaterMark(NULL)); | ||
| 242 | if (err != ESP_OK) { | ||
| 243 | free(states); | ||
| 244 | free(token); | ||
| 245 | cJSON *notice = create_notice("error", "payment-error-verification", "Failed to verify token with mint"); | ||
| 246 | char *json = cJSON_PrintUnformatted(notice); | ||
| 247 | httpd_resp_set_status(req, "502 Bad Gateway"); | ||
| 248 | httpd_resp_set_type(req, "application/json"); | ||
| 249 | httpd_resp_send(req, json, strlen(json)); | ||
| 250 | cJSON_free(json); | ||
| 251 | cJSON_Delete(notice); | ||
| 252 | return ESP_OK; | ||
| 253 | } | ||
| 254 | |||
| 255 | for (int i = 0; i < state_count; i++) { | ||
| 256 | if (states[i].spent) { | ||
| 257 | free(states); | ||
| 258 | free(token); | ||
| 259 | cJSON *notice = create_notice("error", "payment-error-token-spent", "Token already spent"); | ||
| 260 | char *json = cJSON_PrintUnformatted(notice); | ||
| 261 | httpd_resp_set_status(req, "402 Payment Required"); | ||
| 262 | httpd_resp_set_type(req, "application/json"); | ||
| 263 | httpd_resp_send(req, json, strlen(json)); | ||
| 264 | cJSON_free(json); | ||
| 265 | cJSON_Delete(notice); | ||
| 266 | return ESP_OK; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | const tollgate_config_t *cfg = tollgate_config_get(); | 199 | const tollgate_config_t *cfg = tollgate_config_get(); |
| 271 | bool is_bytes = (strcmp(cfg->metric, "bytes") == 0); | 200 | uint64_t allotment = 0; |
| 272 | uint64_t step_size = is_bytes ? (uint64_t)cfg->step_size_bytes : (uint64_t)cfg->step_size_ms; | ||
| 273 | uint64_t allotment = cashu_calculate_allotment(token->total_amount, cfg->price_per_step, | ||
| 274 | cfg->metric, step_size); | ||
| 275 | if (allotment == 0) { | ||
| 276 | free(states); | ||
| 277 | free(token); | ||
| 278 | cJSON *notice = create_notice("error", "payment-error-insufficient", "Token value too low"); | ||
| 279 | char *json = cJSON_PrintUnformatted(notice); | ||
| 280 | httpd_resp_set_status(req, "402 Payment Required"); | ||
| 281 | httpd_resp_set_type(req, "application/json"); | ||
| 282 | httpd_resp_send(req, json, strlen(json)); | ||
| 283 | cJSON_free(json); | ||
| 284 | cJSON_Delete(notice); | ||
| 285 | return ESP_OK; | ||
| 286 | } | ||
| 287 | |||
| 288 | session_t *session; | ||
| 289 | if (is_bytes) { | ||
| 290 | session = session_create_bytes(client_ip, allotment); | ||
| 291 | } else { | ||
| 292 | session = session_create(client_ip, allotment); | ||
| 293 | } | ||
| 294 | if (!session) { | ||
| 295 | free(states); | ||
| 296 | free(token); | ||
| 297 | cJSON *notice = create_notice("error", "session-error", "Failed to create session"); | ||
| 298 | char *json = cJSON_PrintUnformatted(notice); | ||
| 299 | httpd_resp_set_status(req, "503 Service Unavailable"); | ||
| 300 | httpd_resp_set_type(req, "application/json"); | ||
| 301 | httpd_resp_send(req, json, strlen(json)); | ||
| 302 | cJSON_free(json); | ||
| 303 | cJSON_Delete(notice); | ||
| 304 | return ESP_OK; | ||
| 305 | } | ||
| 306 | |||
| 307 | cJSON *session_event = create_session_event(client_ip, allotment); | 201 | cJSON *session_event = create_session_event(client_ip, allotment); |
| 308 | char *json = cJSON_PrintUnformatted(session_event); | 202 | char *json = cJSON_PrintUnformatted(session_event); |
| 309 | httpd_resp_set_type(req, "application/json"); | 203 | httpd_resp_set_type(req, "application/json"); |
| 310 | httpd_resp_send(req, json, strlen(json)); | 204 | httpd_resp_send(req, json, strlen(json)); |
| 311 | cJSON_free(json); | 205 | cJSON_free(json); |
| 312 | cJSON_Delete(session_event); | 206 | cJSON_Delete(session_event); |
| 313 | |||
| 314 | nucula_wallet_receive(body_copy); | ||
| 315 | |||
| 316 | free(states); | ||
| 317 | free(token); | ||
| 318 | return ESP_OK; | 207 | return ESP_OK; |
| 319 | } | 208 | } |
| 320 | 209 | ||
| @@ -323,29 +212,15 @@ static esp_err_t api_get_usage(httpd_req_t *req) | |||
| 323 | uint32_t client_ip = 0; | 212 | uint32_t client_ip = 0; |
| 324 | get_client_ip(req, &client_ip); | 213 | get_client_ip(req, &client_ip); |
| 325 | 214 | ||
| 326 | session_t *session = session_find_by_ip(client_ip); | 215 | char *status_json = tollgate_core_get_status_json(); |
| 327 | if (!session || !session->active) { | 216 | if (status_json) { |
| 328 | httpd_resp_set_type(req, "text/plain"); | 217 | httpd_resp_set_type(req, "application/json"); |
| 329 | httpd_resp_send(req, "-1/-1", 5); | 218 | httpd_resp_send(req, status_json, strlen(status_json)); |
| 330 | return ESP_OK; | 219 | cJSON_free(status_json); |
| 331 | } | ||
| 332 | |||
| 333 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 334 | bool is_bytes = (strcmp(cfg->metric, "bytes") == 0); | ||
| 335 | |||
| 336 | char resp[64]; | ||
| 337 | if (is_bytes) { | ||
| 338 | int64_t remaining = (int64_t)session->allotment_bytes - (int64_t)session->bytes_consumed; | ||
| 339 | if (remaining < 0) remaining = 0; | ||
| 340 | snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_bytes); | ||
| 341 | } else { | 220 | } else { |
| 342 | int64_t elapsed = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS - session->start_time_ms; | 221 | httpd_resp_set_type(req, "text/plain"); |
| 343 | int64_t remaining = session->allotment_ms - elapsed; | 222 | httpd_resp_send(req, "{}", 2); |
| 344 | if (remaining < 0) remaining = 0; | ||
| 345 | snprintf(resp, sizeof(resp), "%lld/%llu", (long long)remaining, (unsigned long long)session->allotment_ms); | ||
| 346 | } | 223 | } |
| 347 | httpd_resp_set_type(req, "text/plain"); | ||
| 348 | httpd_resp_send(req, resp, strlen(resp)); | ||
| 349 | return ESP_OK; | 224 | return ESP_OK; |
| 350 | } | 225 | } |
| 351 | 226 | ||
| @@ -354,15 +229,10 @@ static esp_err_t api_get_whoami(httpd_req_t *req) | |||
| 354 | uint32_t client_ip = 0; | 229 | uint32_t client_ip = 0; |
| 355 | char resp[96]; | 230 | char resp[96]; |
| 356 | if (get_client_ip(req, &client_ip) == ESP_OK) { | 231 | if (get_client_ip(req, &client_ip) == ESP_OK) { |
| 357 | char mac[18] = {0}; | ||
| 358 | esp_ip4_addr_t ip = { .addr = client_ip }; | 232 | esp_ip4_addr_t ip = { .addr = client_ip }; |
| 359 | if (firewall_get_mac_for_ip(client_ip, mac, sizeof(mac)) == ESP_OK) { | 233 | snprintf(resp, sizeof(resp), "ip=" IPSTR, IP2STR(&ip)); |
| 360 | snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=%s", IP2STR(&ip), mac); | ||
| 361 | } else { | ||
| 362 | snprintf(resp, sizeof(resp), "ip=" IPSTR " mac=unknown", IP2STR(&ip)); | ||
| 363 | } | ||
| 364 | } else { | 234 | } else { |
| 365 | snprintf(resp, sizeof(resp), "ip=unknown mac=unknown"); | 235 | snprintf(resp, sizeof(resp), "ip=unknown"); |
| 366 | } | 236 | } |
| 367 | httpd_resp_set_type(req, "text/plain"); | 237 | httpd_resp_set_type(req, "text/plain"); |
| 368 | httpd_resp_send(req, resp, strlen(resp)); | 238 | httpd_resp_send(req, resp, strlen(resp)); |
diff --git a/main/tollgate_main.c b/main/tollgate_main.c index c0ff65f..697dae3 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c | |||
| @@ -13,10 +13,8 @@ | |||
| 13 | #include "dhcpserver/dhcpserver.h" | 13 | #include "dhcpserver/dhcpserver.h" |
| 14 | #include "config.h" | 14 | #include "config.h" |
| 15 | #include "identity.h" | 15 | #include "identity.h" |
| 16 | #include "dns_server.h" | 16 | #include "tollgate_core.h" |
| 17 | #include "captive_portal.h" | 17 | #include "captive_portal.h" |
| 18 | #include "firewall.h" | ||
| 19 | #include "session.h" | ||
| 20 | #include "tollgate_api.h" | 18 | #include "tollgate_api.h" |
| 21 | #include "nucula_wallet.h" | 19 | #include "nucula_wallet.h" |
| 22 | #include "wifistr.h" | 20 | #include "wifistr.h" |
| @@ -73,11 +71,13 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, | |||
| 73 | ESP_LOGI(TAG, "Station connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | 71 | ESP_LOGI(TAG, "Station connected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
| 74 | event->mac[0], event->mac[1], event->mac[2], | 72 | event->mac[0], event->mac[1], event->mac[2], |
| 75 | event->mac[3], event->mac[4], event->mac[5]); | 73 | event->mac[3], event->mac[4], event->mac[5]); |
| 74 | tollgate_core_client_connected(event->mac, 0); | ||
| 76 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { | 75 | } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { |
| 77 | wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; | 76 | wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; |
| 78 | ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", | 77 | ESP_LOGI(TAG, "Station disconnected: MAC=%02x:%02x:%02x:%02x:%02x:%02x", |
| 79 | event->mac[0], event->mac[1], event->mac[2], | 78 | event->mac[0], event->mac[1], event->mac[2], |
| 80 | event->mac[3], event->mac[4], event->mac[5]); | 79 | event->mac[3], event->mac[4], event->mac[5]); |
| 80 | tollgate_core_client_disconnected(event->mac); | ||
| 81 | } | 81 | } |
| 82 | } | 82 | } |
| 83 | 83 | ||
| @@ -115,13 +115,6 @@ static void ip_event_handler(void *arg, esp_event_base_t event_base, | |||
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| 117 | 117 | ||
| 118 | static void wallet_init_task(void *pvParameters) | ||
| 119 | { | ||
| 120 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 121 | nucula_wallet_init(cfg->mint_url); | ||
| 122 | vTaskDelete(NULL); | ||
| 123 | } | ||
| 124 | |||
| 125 | static void publish_wifistr_task(void *pvParameters) | 118 | static void publish_wifistr_task(void *pvParameters) |
| 126 | { | 119 | { |
| 127 | vTaskDelay(pdMS_TO_TICKS(5000)); | 120 | vTaskDelay(pdMS_TO_TICKS(5000)); |
| @@ -139,7 +132,6 @@ static void start_services(void) | |||
| 139 | return; | 132 | return; |
| 140 | } | 133 | } |
| 141 | 134 | ||
| 142 | esp_netif_get_ip_info(s_ap_netif, &(esp_netif_ip_info_t){0}); | ||
| 143 | esp_netif_ip_info_t ap_ip_info; | 135 | esp_netif_ip_info_t ap_ip_info; |
| 144 | esp_netif_get_ip_info(s_ap_netif, &ap_ip_info); | 136 | esp_netif_get_ip_info(s_ap_netif, &ap_ip_info); |
| 145 | 137 | ||
| @@ -147,21 +139,20 @@ static void start_services(void) | |||
| 147 | const ip_addr_t *dns_addr = dns_getserver(0); | 139 | const ip_addr_t *dns_addr = dns_getserver(0); |
| 148 | upstream_dns.addr = dns_addr->addr; | 140 | upstream_dns.addr = dns_addr->addr; |
| 149 | 141 | ||
| 150 | firewall_init(ap_ip_info.ip); | ||
| 151 | session_manager_init(); | ||
| 152 | |||
| 153 | const tollgate_config_t *cfg = tollgate_config_get(); | 142 | const tollgate_config_t *cfg = tollgate_config_get(); |
| 143 | const tollgate_platform_t *platform = tollgate_get_platform(); | ||
| 144 | tollgate_core_init(platform, ap_ip_info.ip); | ||
| 145 | |||
| 154 | nucula_wallet_init(cfg->mint_url); | 146 | nucula_wallet_init(cfg->mint_url); |
| 155 | lightning_payout_init(&cfg->payout); | 147 | lightning_payout_init(&cfg->payout); |
| 156 | 148 | ||
| 157 | dns_server_start(ap_ip_info.ip, upstream_dns); | 149 | tollgate_core_dns_start(upstream_dns); |
| 158 | captive_portal_start(cfg->ap_ip_str); | 150 | captive_portal_start(cfg->ap_ip_str); |
| 159 | tollgate_api_start(); | 151 | tollgate_api_start(); |
| 160 | 152 | ||
| 161 | xTaskCreate(publish_wifistr_task, "wifistr_init", 16384, NULL, 3, NULL); | 153 | xTaskCreate(publish_wifistr_task, "wifistr_init", 16384, NULL, 3, NULL); |
| 162 | 154 | ||
| 163 | const tollgate_config_t *cfg2 = tollgate_config_get(); | 155 | if (cfg->cvm_enabled) { |
| 164 | if (cfg2->cvm_enabled) { | ||
| 165 | cvm_server_init(); | 156 | cvm_server_init(); |
| 166 | cvm_server_start(); | 157 | cvm_server_start(); |
| 167 | } | 158 | } |
| @@ -186,9 +177,8 @@ static void stop_services(void) | |||
| 186 | 177 | ||
| 187 | captive_portal_stop(); | 178 | captive_portal_stop(); |
| 188 | tollgate_api_stop(); | 179 | tollgate_api_stop(); |
| 189 | dns_server_stop(); | 180 | tollgate_core_dns_stop(); |
| 190 | cvm_server_stop(); | 181 | cvm_server_stop(); |
| 191 | firewall_revoke_all(); | ||
| 192 | s_services_running = false; | 182 | s_services_running = false; |
| 193 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); | 183 | if (s_services_mutex) xSemaphoreGive(s_services_mutex); |
| 194 | ESP_LOGI(TAG, "=== TollGate services stopped ==="); | 184 | ESP_LOGI(TAG, "=== TollGate services stopped ==="); |
| @@ -316,7 +306,7 @@ void app_main(void) | |||
| 316 | 306 | ||
| 317 | while (1) { | 307 | while (1) { |
| 318 | vTaskDelay(pdMS_TO_TICKS(1000)); | 308 | vTaskDelay(pdMS_TO_TICKS(1000)); |
| 319 | session_tick(); | 309 | tollgate_core_tick(); |
| 320 | tollgate_client_tick(); | 310 | tollgate_client_tick(); |
| 321 | lightning_payout_tick(); | 311 | lightning_payout_tick(); |
| 322 | } | 312 | } |
diff --git a/main/tollgate_platform.c b/main/tollgate_platform.c new file mode 100644 index 0000000..c992ea1 --- /dev/null +++ b/main/tollgate_platform.c | |||
| @@ -0,0 +1,64 @@ | |||
| 1 | #include "tollgate_platform.h" | ||
| 2 | #include "tollgate_core.h" | ||
| 3 | #include "config.h" | ||
| 4 | #include "esp_log.h" | ||
| 5 | #include "esp_timer.h" | ||
| 6 | #include "freertos/FreeRTOS.h" | ||
| 7 | #include "freertos/task.h" | ||
| 8 | |||
| 9 | static const char *TAG = "tollgate_platform"; | ||
| 10 | |||
| 11 | static uint16_t platform_get_price_sats(void) | ||
| 12 | { | ||
| 13 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 14 | return cfg ? (uint16_t)cfg->price_per_step : 21; | ||
| 15 | } | ||
| 16 | |||
| 17 | static int32_t platform_get_step_ms(void) | ||
| 18 | { | ||
| 19 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 20 | return cfg ? (int32_t)cfg->step_size_ms : 60000; | ||
| 21 | } | ||
| 22 | |||
| 23 | static const char *platform_get_mint_url(void) | ||
| 24 | { | ||
| 25 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 26 | return cfg ? cfg->mint_url : "https://testnut.cashu.space"; | ||
| 27 | } | ||
| 28 | |||
| 29 | static const char *platform_get_metric(void) | ||
| 30 | { | ||
| 31 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 32 | return cfg ? cfg->metric : "milliseconds"; | ||
| 33 | } | ||
| 34 | |||
| 35 | static int32_t platform_get_step_bytes(void) | ||
| 36 | { | ||
| 37 | const tollgate_config_t *cfg = tollgate_config_get(); | ||
| 38 | return cfg ? (int32_t)cfg->step_size_bytes : 22020096; | ||
| 39 | } | ||
| 40 | |||
| 41 | static int64_t platform_get_time_ms(void) | ||
| 42 | { | ||
| 43 | return (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; | ||
| 44 | } | ||
| 45 | |||
| 46 | static bool platform_spend_proofs(const char *raw_token_json) | ||
| 47 | { | ||
| 48 | (void)raw_token_json; | ||
| 49 | return true; | ||
| 50 | } | ||
| 51 | |||
| 52 | const tollgate_platform_t *tollgate_get_platform(void) | ||
| 53 | { | ||
| 54 | static const tollgate_platform_t platform = { | ||
| 55 | .get_price_sats = platform_get_price_sats, | ||
| 56 | .get_step_ms = platform_get_step_ms, | ||
| 57 | .get_mint_url = platform_get_mint_url, | ||
| 58 | .get_metric = platform_get_metric, | ||
| 59 | .get_step_bytes = platform_get_step_bytes, | ||
| 60 | .get_time_ms = platform_get_time_ms, | ||
| 61 | .spend_proofs = platform_spend_proofs, | ||
| 62 | }; | ||
| 63 | return &platform; | ||
| 64 | } | ||