diff options
| author | Your Name <you@example.com> | 2026-05-23 04:50:26 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-23 04:50:26 +0530 |
| commit | 9330b01c28aebc865a3c0a51df8730196cb4152e (patch) | |
| tree | 60c3ae35b2550c737c228e81fd342c8d07af11af /main | |
| parent | 851801f50f1282ab1db5f8a241dbd245f59e447e (diff) | |
Phase 6a-6d: Consolidate and clean up
6a: Delete dead standalone tollgate_core/ (12 files, never compiled)
6b: Rewrite dns_server.c as thin shim to component's tollgate_core_dns
- Fix component DNS to bind to AP IP instead of INADDR_ANY
6c: Rewrite stratum_proxy.c as thin shim with struct cast to component types
6d: Move sandbox logic into component's tollgate_core_firewall
- Add tollgate_core_fw_set_sandbox_ports/set_sandbox_mint_access
- Add is_sandbox_allowed() to component's ip4_canforward_filter
- Clean main/firewall.c to delegate filter to component
- Remove redundant DNS auth double-calls from main firewall
Add ROADMAP.md with full extraction plan and checklists
All 21 unit tests pass. ESP-IDF build passes.
Diffstat (limited to 'main')
| -rw-r--r-- | main/dns_server.c | 304 | ||||
| -rw-r--r-- | main/firewall.c | 72 | ||||
| -rw-r--r-- | main/stratum_proxy.c | 145 |
3 files changed, 17 insertions, 504 deletions
diff --git a/main/dns_server.c b/main/dns_server.c index b84a4cf..5b1bdc3 100644 --- a/main/dns_server.c +++ b/main/dns_server.c | |||
| @@ -1,316 +1,22 @@ | |||
| 1 | #include "dns_server.h" | 1 | #include "dns_server.h" |
| 2 | #include "esp_log.h" | 2 | #include "tollgate_core_dns.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 MAX_PENDING 50 | ||
| 12 | #define DNS_BUF_SIZE 512 | ||
| 13 | #define DNS_PORT 53 | ||
| 14 | #define DOT_PORT 853 | ||
| 15 | #define DNS_TASK_STACK 4096 | ||
| 16 | #define DOT_TASK_STACK 3072 | ||
| 17 | #define DNS_TASK_PRIO 5 | ||
| 18 | #define DOT_TASK_PRIO 5 | ||
| 19 | #define DNS_FORWARD_TIMEOUT_MS 2000 | ||
| 20 | #define NXDOMAIN_TTL 30 | ||
| 21 | #define HIJACK_TTL 10 | ||
| 22 | |||
| 23 | static const char *TAG = "dns_server"; | ||
| 24 | |||
| 25 | #pragma pack(push, 1) | ||
| 26 | typedef struct { | ||
| 27 | uint16_t id; | ||
| 28 | uint16_t flags; | ||
| 29 | uint16_t qdcount; | ||
| 30 | uint16_t ancount; | ||
| 31 | uint16_t nscount; | ||
| 32 | uint16_t arcount; | ||
| 33 | } dns_header_t; | ||
| 34 | #pragma pack(pop) | ||
| 35 | |||
| 36 | #pragma pack(push, 1) | ||
| 37 | typedef struct { | ||
| 38 | uint16_t name; | ||
| 39 | uint16_t type; | ||
| 40 | uint16_t class; | ||
| 41 | uint32_t ttl; | ||
| 42 | uint16_t len; | ||
| 43 | uint32_t addr; | ||
| 44 | } dns_answer_t; | ||
| 45 | #pragma pack(pop) | ||
| 46 | |||
| 47 | typedef struct { | ||
| 48 | uint32_t ip; | ||
| 49 | } auth_entry_t; | ||
| 50 | |||
| 51 | static auth_entry_t s_auth_list[MAX_AUTH_IPS]; | ||
| 52 | static int s_auth_count = 0; | ||
| 53 | static TaskHandle_t s_dns_task = NULL; | ||
| 54 | static TaskHandle_t s_dot_task = NULL; | ||
| 55 | static volatile bool s_dns_running = false; | ||
| 56 | static esp_ip4_addr_t s_ap_ip; | ||
| 57 | static esp_ip4_addr_t s_upstream_dns; | ||
| 58 | |||
| 59 | static bool is_authenticated(uint32_t ip) | ||
| 60 | { | ||
| 61 | for (int i = 0; i < s_auth_count; i++) { | ||
| 62 | if (s_auth_list[i].ip == ip) return true; | ||
| 63 | } | ||
| 64 | return false; | ||
| 65 | } | ||
| 66 | |||
| 67 | static void parse_dns_name(const uint8_t *buf, int buf_len, int offset, char *out, int out_len) | ||
| 68 | { | ||
| 69 | int pos = offset; | ||
| 70 | int out_pos = 0; | ||
| 71 | int jumped = 0; | ||
| 72 | int jump_pos = 0; | ||
| 73 | while (pos < buf_len && out_pos < out_len - 1) { | ||
| 74 | uint8_t len = buf[pos]; | ||
| 75 | if (len == 0) break; | ||
| 76 | if ((len & 0xC0) == 0xC0) { | ||
| 77 | if (!jumped) jump_pos = pos + 2; | ||
| 78 | pos = ((len & 0x3F) << 8) | buf[pos + 1]; | ||
| 79 | jumped = 1; | ||
| 80 | continue; | ||
| 81 | } | ||
| 82 | if (out_pos > 0 && out_pos < out_len - 1) out[out_pos++] = '.'; | ||
| 83 | pos++; | ||
| 84 | for (int i = 0; i < len && pos < buf_len && out_pos < out_len - 1; i++) { | ||
| 85 | out[out_pos++] = buf[pos++]; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | out[out_pos] = '\0'; | ||
| 89 | } | ||
| 90 | |||
| 91 | static int build_nxdomain(uint8_t *response, int req_len) | ||
| 92 | { | ||
| 93 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 94 | hdr->flags = htons(0x8403); | ||
| 95 | hdr->ancount = 0; | ||
| 96 | hdr->nscount = 0; | ||
| 97 | hdr->arcount = 0; | ||
| 98 | return req_len; | ||
| 99 | } | ||
| 100 | |||
| 101 | static int build_redirect_response(uint8_t *response, int req_len) | ||
| 102 | { | ||
| 103 | memmove(response, response, req_len); | ||
| 104 | dns_header_t *hdr = (dns_header_t *)response; | ||
| 105 | hdr->flags = htons(0x8180); | ||
| 106 | hdr->ancount = htons(1); | ||
| 107 | hdr->nscount = 0; | ||
| 108 | hdr->arcount = 0; | ||
| 109 | int resp_len = req_len; | ||
| 110 | dns_answer_t ans; | ||
| 111 | ans.name = htons(0xC00C); | ||
| 112 | ans.type = htons(1); | ||
| 113 | ans.class = htons(1); | ||
| 114 | ans.ttl = htonl(HIJACK_TTL); | ||
| 115 | ans.len = htons(4); | ||
| 116 | ans.addr = s_ap_ip.addr; | ||
| 117 | memcpy(response + resp_len, &ans, sizeof(ans)); | ||
| 118 | resp_len += sizeof(ans); | ||
| 119 | return resp_len; | ||
| 120 | } | ||
| 121 | |||
| 122 | static int forward_dns(const uint8_t *req, int req_len, uint8_t *resp, int resp_buf_len, | ||
| 123 | const struct sockaddr_in *client_addr, uint16_t txn_id) | ||
| 124 | { | ||
| 125 | int upstream_sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 126 | if (upstream_sock < 0) return -1; | ||
| 127 | |||
| 128 | struct timeval tv = { .tv_sec = DNS_FORWARD_TIMEOUT_MS / 1000, .tv_usec = (DNS_FORWARD_TIMEOUT_MS % 1000) * 1000 }; | ||
| 129 | setsockopt(upstream_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); | ||
| 130 | |||
| 131 | struct sockaddr_in upstream_addr = { | ||
| 132 | .sin_family = AF_INET, | ||
| 133 | .sin_port = htons(DNS_PORT), | ||
| 134 | .sin_addr.s_addr = s_upstream_dns.addr, | ||
| 135 | }; | ||
| 136 | |||
| 137 | sendto(upstream_sock, req, req_len, 0, (struct sockaddr *)&upstream_addr, sizeof(upstream_addr)); | ||
| 138 | |||
| 139 | int n = recvfrom(upstream_sock, resp, resp_buf_len, 0, NULL, NULL); | ||
| 140 | close(upstream_sock); | ||
| 141 | |||
| 142 | if (n > 0) { | ||
| 143 | if (n >= sizeof(dns_header_t)) { | ||
| 144 | dns_header_t *hdr = (dns_header_t *)resp; | ||
| 145 | hdr->id = htons(txn_id); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | return n; | ||
| 149 | } | ||
| 150 | |||
| 151 | static void dns_server_task(void *arg) | ||
| 152 | { | ||
| 153 | int sock = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 154 | if (sock < 0) { | ||
| 155 | ESP_LOGE(TAG, "Failed to create DNS socket"); | ||
| 156 | s_dns_running = false; | ||
| 157 | vTaskDelete(NULL); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | struct sockaddr_in bind_addr = { | ||
| 162 | .sin_family = AF_INET, | ||
| 163 | .sin_port = htons(DNS_PORT), | ||
| 164 | .sin_addr.s_addr = s_ap_ip.addr, | ||
| 165 | }; | ||
| 166 | if (bind(sock, (struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) { | ||
| 167 | ESP_LOGE(TAG, "Failed to bind DNS socket"); | ||
| 168 | close(sock); | ||
| 169 | s_dns_running = false; | ||
| 170 | vTaskDelete(NULL); | ||
| 171 | return; | ||
| 172 | } | ||
| 173 | |||
| 174 | ESP_LOGI(TAG, "DNS server started on port %d, AP IP=" IPSTR ", upstream DNS=" IPSTR, | ||
| 175 | DNS_PORT, IP2STR(&s_ap_ip), IP2STR(&s_upstream_dns)); | ||
| 176 | |||
| 177 | uint8_t rx_buf[DNS_BUF_SIZE]; | ||
| 178 | uint8_t tx_buf[DNS_BUF_SIZE + sizeof(dns_answer_t)]; | ||
| 179 | |||
| 180 | while (s_dns_running) { | ||
| 181 | struct sockaddr_in client_addr; | ||
| 182 | socklen_t client_len = sizeof(client_addr); | ||
| 183 | int n = recvfrom(sock, rx_buf, sizeof(rx_buf), 0, | ||
| 184 | (struct sockaddr *)&client_addr, &client_len); | ||
| 185 | if (n < (int)sizeof(dns_header_t)) continue; | ||
| 186 | |||
| 187 | uint32_t client_ip = client_addr.sin_addr.s_addr; | ||
| 188 | dns_header_t *hdr = (dns_header_t *)rx_buf; | ||
| 189 | uint16_t txn_id = ntohs(hdr->id); | ||
| 190 | bool is_query = (ntohs(hdr->flags) & 0x8000) == 0; | ||
| 191 | uint16_t qdcount = ntohs(hdr->qdcount); | ||
| 192 | |||
| 193 | if (!is_query || qdcount == 0) continue; | ||
| 194 | |||
| 195 | int q_offset = sizeof(dns_header_t); | ||
| 196 | while (q_offset < n && rx_buf[q_offset] != 0) { | ||
| 197 | q_offset += rx_buf[q_offset] + 1; | ||
| 198 | } | ||
| 199 | if (q_offset + 5 > n) continue; | ||
| 200 | uint16_t qtype = (rx_buf[q_offset + 1] << 8) | rx_buf[q_offset + 2]; | ||
| 201 | int req_len = q_offset + 5; | ||
| 202 | |||
| 203 | if (is_authenticated(client_ip)) { | ||
| 204 | int resp_len = forward_dns(rx_buf, req_len, tx_buf, sizeof(tx_buf), &client_addr, txn_id); | ||
| 205 | if (resp_len > 0) { | ||
| 206 | sendto(sock, tx_buf, resp_len, 0, (struct sockaddr *)&client_addr, client_len); | ||
| 207 | } | ||
| 208 | } else { | ||
| 209 | char qname[256] = {0}; | ||
| 210 | parse_dns_name(rx_buf, n, sizeof(dns_header_t), qname, sizeof(qname)); | ||
| 211 | ESP_LOGI(TAG, "Hijack DNS from " IPSTR ": %s (type=%d)", 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 | 3 | ||
| 276 | esp_err_t dns_server_start(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns) | 4 | esp_err_t dns_server_start(esp_ip4_addr_t ap_ip, esp_ip4_addr_t upstream_dns) |
| 277 | { | 5 | { |
| 278 | if (s_dns_running) return ESP_OK; | 6 | return tollgate_core_dns_start_internal(ap_ip, upstream_dns); |
| 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 | } | 7 | } |
| 286 | 8 | ||
| 287 | void dns_server_stop(void) | 9 | void dns_server_stop(void) |
| 288 | { | 10 | { |
| 289 | s_dns_running = false; | 11 | tollgate_core_dns_stop(); |
| 290 | vTaskDelay(pdMS_TO_TICKS(200)); | ||
| 291 | s_dns_task = NULL; | ||
| 292 | } | 12 | } |
| 293 | 13 | ||
| 294 | void dns_server_set_client_authenticated(uint32_t client_ip, bool authenticated) | 14 | void dns_server_set_client_authenticated(uint32_t client_ip, bool authenticated) |
| 295 | { | 15 | { |
| 296 | if (authenticated) { | 16 | tollgate_core_dns_set_authenticated(client_ip, 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 | } | 17 | } |
| 312 | 18 | ||
| 313 | bool dns_server_is_running(void) | 19 | bool dns_server_is_running(void) |
| 314 | { | 20 | { |
| 315 | return s_dns_running; | 21 | return tollgate_core_dns_is_running(); |
| 316 | } | 22 | } |
diff --git a/main/firewall.c b/main/firewall.c index 077d16c..5ffdee0 100644 --- a/main/firewall.c +++ b/main/firewall.c | |||
| @@ -1,36 +1,28 @@ | |||
| 1 | #include "firewall.h" | 1 | #include "firewall.h" |
| 2 | #include "dns_server.h" | ||
| 3 | #include "tollgate_core.h" | 2 | #include "tollgate_core.h" |
| 4 | #include "tollgate_core_firewall.h" | 3 | #include "tollgate_core_firewall.h" |
| 5 | #include "esp_log.h" | 4 | #include "esp_log.h" |
| 6 | #include "lwip/netif.h" | ||
| 7 | #include "lwip/lwip_napt.h" | ||
| 8 | #include "lwip/prot/ip4.h" | 5 | #include "lwip/prot/ip4.h" |
| 9 | #include "lwip/prot/tcp.h" | ||
| 10 | #include "lwip/prot/ip.h" | ||
| 11 | #include <string.h> | 6 | #include <string.h> |
| 12 | 7 | ||
| 13 | static const char *TAG = "firewall"; | 8 | static const char *TAG = "firewall"; |
| 14 | static esp_ip4_addr_t s_ap_ip; | 9 | static esp_ip4_addr_t s_ap_ip; |
| 15 | static uint16_t s_mining_port = 3333; | ||
| 16 | static bool s_sandbox_mint_access = false; | ||
| 17 | 10 | ||
| 18 | esp_err_t firewall_init(esp_ip4_addr_t ap_ip) | 11 | esp_err_t firewall_init(esp_ip4_addr_t ap_ip) |
| 19 | { | 12 | { |
| 20 | s_ap_ip = ap_ip; | 13 | s_ap_ip = ap_ip; |
| 21 | ip_napt_enable(s_ap_ip.addr, 1); | 14 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR, IP2STR(&s_ap_ip)); |
| 22 | ESP_LOGI(TAG, "Firewall initialized with AP IP=" IPSTR " (NAT always on, per-client filter)", IP2STR(&s_ap_ip)); | ||
| 23 | return ESP_OK; | 15 | return ESP_OK; |
| 24 | } | 16 | } |
| 25 | 17 | ||
| 26 | void firewall_set_mining_port(uint16_t port) | 18 | void firewall_set_mining_port(uint16_t port) |
| 27 | { | 19 | { |
| 28 | s_mining_port = port; | 20 | tollgate_core_fw_set_sandbox_ports(port); |
| 29 | } | 21 | } |
| 30 | 22 | ||
| 31 | void firewall_set_sandbox_mint_access(bool enabled) | 23 | void firewall_set_sandbox_mint_access(bool enabled) |
| 32 | { | 24 | { |
| 33 | s_sandbox_mint_access = enabled; | 25 | tollgate_core_fw_set_sandbox_mint_access(enabled); |
| 34 | } | 26 | } |
| 35 | 27 | ||
| 36 | esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) | 28 | esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_out_size) |
| @@ -38,80 +30,24 @@ esp_err_t firewall_get_mac_for_ip(uint32_t client_ip, char *mac_out, size_t mac_ | |||
| 38 | return tollgate_core_fw_get_mac_for_ip(client_ip, mac_out, mac_out_size); | 30 | return tollgate_core_fw_get_mac_for_ip(client_ip, mac_out, mac_out_size); |
| 39 | } | 31 | } |
| 40 | 32 | ||
| 41 | static bool is_sandbox_allowed(struct pbuf *p) | ||
| 42 | { | ||
| 43 | if (p->len < IP_HLEN) return false; | ||
| 44 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 45 | uint32_t dest_ip_h = lwip_ntohl(iphdr->dest.addr); | ||
| 46 | uint32_t ap_ip_h = lwip_ntohl(s_ap_ip.addr); | ||
| 47 | |||
| 48 | if (dest_ip_h == ap_ip_h) { | ||
| 49 | if (iphdr->_proto == IP_PROTO_TCP) { | ||
| 50 | uint16_t dst_port = 0; | ||
| 51 | if (p->len >= IP_HLEN + TCP_HLEN) { | ||
| 52 | struct tcp_hdr *tcphdr = (struct tcp_hdr *)((uint8_t *)p->payload + IP_HLEN); | ||
| 53 | dst_port = lwip_ntohs(tcphdr->dest); | ||
| 54 | } | ||
| 55 | if (dst_port == 80 || dst_port == 2121 || dst_port == s_mining_port) { | ||
| 56 | return true; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | if (iphdr->_proto == IP_PROTO_UDP) { | ||
| 60 | return true; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | if (s_sandbox_mint_access && iphdr->_proto == IP_PROTO_TCP) { | ||
| 65 | return true; | ||
| 66 | } | ||
| 67 | |||
| 68 | return false; | ||
| 69 | } | ||
| 70 | |||
| 71 | int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) | 33 | int tollgate_ip4_canforward_filter(struct pbuf *p, u32_t dest_addr_hostorder) |
| 72 | { | 34 | { |
| 73 | (void)dest_addr_hostorder; | 35 | return tollgate_core_ip4_canforward_filter(p, dest_addr_hostorder); |
| 74 | if (p->len < IP_HLEN) return -1; | ||
| 75 | struct ip_hdr *iphdr = (struct ip_hdr *)p->payload; | ||
| 76 | uint32_t src_ip_h = lwip_ntohl(iphdr->src.addr); | ||
| 77 | uint32_t ap_subnet = lwip_ntohl(s_ap_ip.addr) & 0xFFFFFF00; | ||
| 78 | if ((src_ip_h & 0xFFFFFF00) != ap_subnet) { | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | if (firewall_is_client_allowed(iphdr->src.addr)) { | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | if (is_sandbox_allowed(p)) { | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | return 0; | ||
| 88 | } | 36 | } |
| 89 | 37 | ||
| 90 | void firewall_grant_access(uint32_t client_ip) | 38 | void firewall_grant_access(uint32_t client_ip) |
| 91 | { | 39 | { |
| 92 | tollgate_core_fw_grant(client_ip); | 40 | tollgate_core_fw_grant(client_ip); |
| 93 | dns_server_set_client_authenticated(client_ip, true); | ||
| 94 | |||
| 95 | char mac[18] = {0}; | ||
| 96 | tollgate_core_fw_get_mac_for_ip(client_ip, mac, sizeof(mac)); | ||
| 97 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 98 | ESP_LOGI(TAG, "Access granted to " IPSTR " mac=%s", IP2STR(&ip_addr), | ||
| 99 | mac[0] ? mac : "unknown"); | ||
| 100 | } | 41 | } |
| 101 | 42 | ||
| 102 | void firewall_revoke_access(uint32_t client_ip) | 43 | void firewall_revoke_access(uint32_t client_ip) |
| 103 | { | 44 | { |
| 104 | tollgate_core_fw_revoke(client_ip); | 45 | tollgate_core_fw_revoke(client_ip); |
| 105 | dns_server_set_client_authenticated(client_ip, false); | ||
| 106 | |||
| 107 | esp_ip4_addr_t ip_addr = { .addr = client_ip }; | ||
| 108 | ESP_LOGI(TAG, "Access revoked for " IPSTR, IP2STR(&ip_addr)); | ||
| 109 | } | 46 | } |
| 110 | 47 | ||
| 111 | void firewall_revoke_all(void) | 48 | void firewall_revoke_all(void) |
| 112 | { | 49 | { |
| 113 | tollgate_core_fw_revoke_all(); | 50 | tollgate_core_fw_revoke_all(); |
| 114 | ESP_LOGI(TAG, "All client access revoked"); | ||
| 115 | } | 51 | } |
| 116 | 52 | ||
| 117 | bool firewall_is_client_allowed(uint32_t client_ip) | 53 | bool firewall_is_client_allowed(uint32_t client_ip) |
diff --git a/main/stratum_proxy.c b/main/stratum_proxy.c index 288c633..53909f0 100644 --- a/main/stratum_proxy.c +++ b/main/stratum_proxy.c | |||
| @@ -1,160 +1,31 @@ | |||
| 1 | #include "stratum_proxy.h" | 1 | #include "stratum_proxy.h" |
| 2 | #include "mining_payment.h" | 2 | #include "tollgate_core_stratum_proxy.h" |
| 3 | #include "esp_log.h" | ||
| 4 | #include "lwip/sockets.h" | ||
| 5 | #include "freertos/FreeRTOS.h" | ||
| 6 | #include "freertos/task.h" | ||
| 7 | #include <string.h> | 3 | #include <string.h> |
| 8 | 4 | ||
| 9 | static const char *TAG = "stratum_proxy"; | 5 | _Static_assert(sizeof(stratum_job_t) == sizeof(tollgate_stratum_job_t), "job struct size mismatch"); |
| 10 | static uint16_t s_port = 3333; | 6 | _Static_assert(sizeof(stratum_proxy_stats_t) == sizeof(tollgate_stratum_proxy_stats_t), "stats struct size mismatch"); |
| 11 | static bool s_running = false; | ||
| 12 | static TaskHandle_t s_task_handle = NULL; | ||
| 13 | static int s_server_fd = -1; | ||
| 14 | |||
| 15 | static stratum_job_t s_current_job = {0}; | ||
| 16 | static stratum_proxy_stats_t s_stats = {0}; | ||
| 17 | |||
| 18 | static void proxy_client_handler(void *arg) | ||
| 19 | { | ||
| 20 | int client_fd = (int)(intptr_t)arg; | ||
| 21 | struct sockaddr_in client_addr; | ||
| 22 | socklen_t addr_len = sizeof(client_addr); | ||
| 23 | getpeername(client_fd, (struct sockaddr *)&client_addr, &addr_len); | ||
| 24 | uint32_t client_ip = client_addr.sin_addr.s_addr; | ||
| 25 | |||
| 26 | ESP_LOGI(TAG, "Miner connected from 0x%08lx", (unsigned long)client_ip); | ||
| 27 | |||
| 28 | if (s_current_job.valid) { | ||
| 29 | char job_json[512]; | ||
| 30 | snprintf(job_json, sizeof(job_json), | ||
| 31 | "{\"id\":1,\"method\":\"mining.notify\",\"params\":[\"%lu\",\"%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx\",\"\",\"\",\"\",\"%08lx\",\"%08lx\",\"%08lx\",true]}\n", | ||
| 32 | (unsigned long)s_current_job.job_id, | ||
| 33 | (unsigned long)0, (unsigned long)0, (unsigned long)0, (unsigned long)0, | ||
| 34 | (unsigned long)0, (unsigned long)0, (unsigned long)0, (unsigned long)0, | ||
| 35 | (unsigned long)s_current_job.nbits, (unsigned long)s_current_job.ntime, | ||
| 36 | (unsigned long)s_current_job.version); | ||
| 37 | send(client_fd, job_json, strlen(job_json), 0); | ||
| 38 | } | ||
| 39 | |||
| 40 | char buf[1024]; | ||
| 41 | while (s_running) { | ||
| 42 | int len = recv(client_fd, buf, sizeof(buf) - 1, 0); | ||
| 43 | if (len <= 0) break; | ||
| 44 | buf[len] = '\0'; | ||
| 45 | |||
| 46 | ESP_LOGI(TAG, "Received from miner: %s", buf); | ||
| 47 | s_stats.total_shares++; | ||
| 48 | s_stats.total_accepted++; | ||
| 49 | } | ||
| 50 | |||
| 51 | ESP_LOGI(TAG, "Miner disconnected from 0x%08lx", (unsigned long)client_ip); | ||
| 52 | close(client_fd); | ||
| 53 | vTaskDelete(NULL); | ||
| 54 | } | ||
| 55 | |||
| 56 | static void proxy_server_task(void *arg) | ||
| 57 | { | ||
| 58 | struct sockaddr_in server_addr; | ||
| 59 | memset(&server_addr, 0, sizeof(server_addr)); | ||
| 60 | server_addr.sin_family = AF_INET; | ||
| 61 | server_addr.sin_addr.s_addr = INADDR_ANY; | ||
| 62 | server_addr.sin_port = htons(s_port); | ||
| 63 | |||
| 64 | s_server_fd = socket(AF_INET, SOCK_STREAM, 0); | ||
| 65 | if (s_server_fd < 0) { | ||
| 66 | ESP_LOGE(TAG, "Failed to create socket"); | ||
| 67 | vTaskDelete(NULL); | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | int opt = 1; | ||
| 72 | setsockopt(s_server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | ||
| 73 | |||
| 74 | if (bind(s_server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0) { | ||
| 75 | ESP_LOGE(TAG, "Failed to bind to port %u", (unsigned)s_port); | ||
| 76 | close(s_server_fd); | ||
| 77 | s_server_fd = -1; | ||
| 78 | vTaskDelete(NULL); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (listen(s_server_fd, 5) != 0) { | ||
| 83 | ESP_LOGE(TAG, "Failed to listen"); | ||
| 84 | close(s_server_fd); | ||
| 85 | s_server_fd = -1; | ||
| 86 | vTaskDelete(NULL); | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | ESP_LOGI(TAG, "Stratum proxy listening on port %u", (unsigned)s_port); | ||
| 91 | |||
| 92 | while (s_running) { | ||
| 93 | struct sockaddr_in client_addr; | ||
| 94 | socklen_t client_len = sizeof(client_addr); | ||
| 95 | int client_fd = accept(s_server_fd, (struct sockaddr *)&client_addr, &client_len); | ||
| 96 | if (client_fd < 0) continue; | ||
| 97 | |||
| 98 | s_stats.active_miners++; | ||
| 99 | char task_name[20]; | ||
| 100 | snprintf(task_name, sizeof(task_name), "miner_%d", client_fd); | ||
| 101 | xTaskCreate(proxy_client_handler, task_name, 4096, (void *)(intptr_t)client_fd, 3, NULL); | ||
| 102 | } | ||
| 103 | |||
| 104 | close(s_server_fd); | ||
| 105 | s_server_fd = -1; | ||
| 106 | vTaskDelete(NULL); | ||
| 107 | } | ||
| 108 | 7 | ||
| 109 | esp_err_t stratum_proxy_init(uint16_t port) | 8 | esp_err_t stratum_proxy_init(uint16_t port) |
| 110 | { | 9 | { |
| 111 | s_port = port; | 10 | return tollgate_core_stratum_proxy_init(port); |
| 112 | memset(&s_current_job, 0, sizeof(s_current_job)); | ||
| 113 | memset(&s_stats, 0, sizeof(s_stats)); | ||
| 114 | s_running = true; | ||
| 115 | |||
| 116 | BaseType_t ret = xTaskCreate(proxy_server_task, "stratum_proxy", 4096, NULL, 4, &s_task_handle); | ||
| 117 | if (ret != pdPASS) { | ||
| 118 | ESP_LOGE(TAG, "Failed to create proxy task"); | ||
| 119 | s_running = false; | ||
| 120 | return ESP_FAIL; | ||
| 121 | } | ||
| 122 | |||
| 123 | ESP_LOGI(TAG, "Stratum proxy initialized on port %u", (unsigned)port); | ||
| 124 | return ESP_OK; | ||
| 125 | } | 11 | } |
| 126 | 12 | ||
| 127 | void stratum_proxy_set_job(const stratum_job_t *job) | 13 | void stratum_proxy_set_job(const stratum_job_t *job) |
| 128 | { | 14 | { |
| 129 | if (job) { | 15 | tollgate_core_stratum_proxy_set_job((const tollgate_stratum_job_t *)job); |
| 130 | memcpy(&s_current_job, job, sizeof(stratum_job_t)); | ||
| 131 | s_stats.nbits = job->nbits; | ||
| 132 | s_stats.current_hashprice = mining_get_current_hashprice(); | ||
| 133 | } | ||
| 134 | } | 16 | } |
| 135 | 17 | ||
| 136 | const stratum_job_t *stratum_proxy_get_current_job(void) | 18 | const stratum_job_t *stratum_proxy_get_current_job(void) |
| 137 | { | 19 | { |
| 138 | return &s_current_job; | 20 | return (const stratum_job_t *)tollgate_core_stratum_proxy_get_current_job(); |
| 139 | } | 21 | } |
| 140 | 22 | ||
| 141 | void stratum_proxy_get_stats(stratum_proxy_stats_t *stats) | 23 | void stratum_proxy_get_stats(stratum_proxy_stats_t *stats) |
| 142 | { | 24 | { |
| 143 | if (stats) { | 25 | tollgate_core_stratum_proxy_get_stats((tollgate_stratum_proxy_stats_t *)stats); |
| 144 | *stats = s_stats; | ||
| 145 | stats->current_hashprice = mining_get_current_hashprice(); | ||
| 146 | } | ||
| 147 | } | 26 | } |
| 148 | 27 | ||
| 149 | void stratum_proxy_stop(void) | 28 | void stratum_proxy_stop(void) |
| 150 | { | 29 | { |
| 151 | s_running = false; | 30 | tollgate_core_stratum_proxy_stop(); |
| 152 | if (s_server_fd >= 0) { | ||
| 153 | close(s_server_fd); | ||
| 154 | s_server_fd = -1; | ||
| 155 | } | ||
| 156 | if (s_task_handle) { | ||
| 157 | vTaskDelay(pdMS_TO_TICKS(500)); | ||
| 158 | s_task_handle = NULL; | ||
| 159 | } | ||
| 160 | } | 31 | } |