show capative portal on connect
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 6m44s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 4m12s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 3m54s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 3m56s
Some checks failed
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 6m44s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 4m12s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 3m54s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 3m56s
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -627,12 +627,25 @@ static const char *get_mime_type(const char *path)
|
||||
esp_err_t api_static_file_handler(httpd_req_t *req)
|
||||
{
|
||||
char filepath[CONFIG_HTTPD_MAX_URI_LEN + 16];
|
||||
const char *uri = req->uri;
|
||||
|
||||
// Default to index.html for root
|
||||
if (strcmp(uri, "/") == 0)
|
||||
const char *uri = req->uri;
|
||||
wifi_mode_t mode = 0;
|
||||
esp_wifi_get_mode(&mode);
|
||||
// Im AP-Modus immer captive.html ausliefern
|
||||
if (mode == WIFI_MODE_AP || mode == WIFI_MODE_APSTA)
|
||||
{
|
||||
uri = "/index.html";
|
||||
if (strcmp(uri, "/") == 0 || strcmp(uri, "/index.html") == 0)
|
||||
{
|
||||
uri = "/captive.html";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Default to index.html for root
|
||||
if (strcmp(uri, "/") == 0)
|
||||
{
|
||||
uri = "/index.html";
|
||||
}
|
||||
}
|
||||
|
||||
const char *base_path = CONFIG_API_SERVER_STATIC_FILES_PATH;
|
||||
@@ -689,10 +702,32 @@ esp_err_t api_captive_portal_handler(httpd_req_t *req)
|
||||
{
|
||||
ESP_LOGI(TAG, "Captive portal detection: %s", req->uri);
|
||||
|
||||
// Redirect to captive portal page
|
||||
httpd_resp_set_status(req, "302 Found");
|
||||
httpd_resp_set_hdr(req, "Location", "/captive.html");
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
// captive.html direkt ausliefern (Status 200, text/html)
|
||||
const char *base_path = CONFIG_API_SERVER_STATIC_FILES_PATH;
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "%s/captive.html", base_path);
|
||||
FILE *f = fopen(filepath, "r");
|
||||
if (!f)
|
||||
{
|
||||
ESP_LOGE(TAG, "captive.html not found: %s", filepath);
|
||||
httpd_resp_set_status(req, "500 Internal Server Error");
|
||||
httpd_resp_sendstr(req, "Captive Portal nicht verfügbar");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
httpd_resp_set_type(req, "text/html");
|
||||
char buf[512];
|
||||
size_t read_bytes;
|
||||
while ((read_bytes = fread(buf, 1, sizeof(buf), f)) > 0)
|
||||
{
|
||||
if (httpd_resp_send_chunk(req, buf, read_bytes) != ESP_OK)
|
||||
{
|
||||
fclose(f);
|
||||
ESP_LOGE(TAG, "Failed to send captive chunk");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
httpd_resp_send_chunk(req, NULL, 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ idf_component_register(SRCS
|
||||
src/ble/ble_connection.c
|
||||
src/ble/ble_scanner.c
|
||||
src/ble_manager.c
|
||||
src/dns_hijack.c
|
||||
src/wifi_manager.c
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
void dns_server_start(const char *ap_ip);
|
||||
void dns_set_ap_ip(const char *ip);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
80
firmware/components/connectivity-manager/src/dns_hijack.c
Normal file
80
firmware/components/connectivity-manager/src/dns_hijack.c
Normal file
@@ -0,0 +1,80 @@
|
||||
// Minimaler DNS-Server für Captive Portal (alle Anfragen auf AP-IP)
|
||||
// Quelle: https://github.com/espressif/esp-idf/blob/master/examples/protocols/sntp/main/dns_server.c (angepasst)
|
||||
#include <arpa/inet.h>
|
||||
#include <esp_log.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <lwip/inet.h>
|
||||
#include <lwip/sockets.h>
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#define DNS_PORT 53
|
||||
#define DNS_MAX_LEN 512
|
||||
static const char *TAG = "dns_hijack";
|
||||
|
||||
static char s_ap_ip[16] = "192.168.4.1"; // Default AP-IP, ggf. dynamisch setzen
|
||||
|
||||
void dns_set_ap_ip(const char *ip)
|
||||
{
|
||||
strncpy(s_ap_ip, ip, sizeof(s_ap_ip) - 1);
|
||||
s_ap_ip[sizeof(s_ap_ip) - 1] = 0;
|
||||
}
|
||||
|
||||
static void dns_server_task(void *pvParameters)
|
||||
{
|
||||
int sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create socket");
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
struct sockaddr_in server_addr = {0};
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(DNS_PORT);
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
|
||||
uint8_t buf[DNS_MAX_LEN];
|
||||
while (1)
|
||||
{
|
||||
struct sockaddr_in from;
|
||||
socklen_t fromlen = sizeof(from);
|
||||
int len = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fromlen);
|
||||
if (len < 0)
|
||||
continue;
|
||||
// DNS Header: 12 bytes, Antwort-Flag setzen
|
||||
buf[2] |= 0x80; // QR=1 (Antwort)
|
||||
buf[3] = 0x80; // RA=1, RCODE=0
|
||||
// Fragen: 1, Antworten: 1
|
||||
buf[7] = 1;
|
||||
// Antwort anhängen (Name Pointer auf Frage)
|
||||
int qlen = len - 12;
|
||||
int pos = len;
|
||||
buf[pos++] = 0xC0;
|
||||
buf[pos++] = 0x0C; // Name pointer
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x01; // Type A
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x01; // Class IN
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x3C; // TTL 60s
|
||||
buf[pos++] = 0x00;
|
||||
buf[pos++] = 0x04; // Data length
|
||||
inet_pton(AF_INET, s_ap_ip, &buf[pos]);
|
||||
pos += 4;
|
||||
sendto(sock, buf, pos, 0, (struct sockaddr *)&from, fromlen);
|
||||
}
|
||||
close(sock);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void dns_server_start(const char *ap_ip)
|
||||
{
|
||||
dns_set_ap_ip(ap_ip);
|
||||
xTaskCreate(dns_server_task, "dns_server", 4096, NULL, 3, NULL);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "wifi_manager.h"
|
||||
#include "dns_hijack.h"
|
||||
|
||||
#include "api_server.h"
|
||||
|
||||
@@ -13,6 +14,7 @@
|
||||
#include <led_status.h>
|
||||
#include <lwip/err.h>
|
||||
#include <lwip/sys.h>
|
||||
#include <mdns.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <sdkconfig.h>
|
||||
#include <string.h>
|
||||
@@ -185,50 +187,33 @@ static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_
|
||||
|
||||
void wifi_manager_init()
|
||||
{
|
||||
#if CONFIG_WIFI_ENABLED
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
s_current_network_index = 0;
|
||||
s_retry_num = 0;
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
// Access Point erstellen
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
wifi_config_t ap_config = {.ap = {.ssid = "system-control",
|
||||
.ssid_len = strlen("system-control"),
|
||||
.password = "",
|
||||
.max_connection = 4,
|
||||
.authmode = WIFI_AUTH_OPEN}};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_DIAG_EVENT(TAG, "WiFi manager initialized with %d network(s), waiting for connection...", s_wifi_network_count);
|
||||
ESP_LOGI(TAG, "Access Point 'system-control' gestartet");
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or
|
||||
connection failed for all networks (WIFI_FAIL_BIT). The bits are set by event_handler() */
|
||||
EventBits_t bits =
|
||||
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
// DNS Hijack Server starten (alle DNS-Anfragen auf AP-IP umleiten)
|
||||
dns_server_start("192.168.4.1"); // ggf. dynamisch ermitteln
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT)
|
||||
{
|
||||
ESP_DIAG_EVENT(TAG, "Connected to AP SSID:%s", s_wifi_networks[s_current_network_index].ssid);
|
||||
|
||||
api_server_config_t s_config = API_SERVER_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(api_server_start(&s_config));
|
||||
}
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to connect to any configured WiFi network");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Unexpected event");
|
||||
}
|
||||
#endif
|
||||
// API-Server starten
|
||||
api_server_config_t s_config = API_SERVER_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(api_server_start(&s_config));
|
||||
}
|
||||
|
||||
@@ -13,19 +13,19 @@
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="header-controls captive-header">
|
||||
<button class="lang-toggle" onclick="toggleLanguage()" aria-label="Sprache wechseln">
|
||||
<span class="lang-flag" id="lang-flag">🇩🇪</span>
|
||||
<span id="lang-label">DE</span>
|
||||
</button>
|
||||
<button class="theme-toggle" onclick="toggleTheme()" aria-label="Theme wechseln">
|
||||
<span class="theme-toggle-icon" id="theme-icon">🌙</span>
|
||||
<span id="theme-label">Dark</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<div class="header-controls">
|
||||
<button class="lang-toggle" onclick="toggleLanguage()" aria-label="Sprache wechseln">
|
||||
<span class="lang-flag" id="lang-flag">🇩🇪</span>
|
||||
<span class="lang-label" id="lang-label">DE</span>
|
||||
</button>
|
||||
<button class="theme-toggle" onclick="toggleTheme()" aria-label="Theme wechseln">
|
||||
<span class="theme-toggle-icon" id="theme-icon">🌙</span>
|
||||
<span class="theme-toggle-label" id="theme-label">Dark</span>
|
||||
</button>
|
||||
</div>
|
||||
<h1>🚂 System Control</h1>
|
||||
<p data-i18n="captive.subtitle">WLAN-Einrichtung</p>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user