Skip to content

Commit 5cc1492

Browse files
committed
check for ap request
1 parent b3b6745 commit 5cc1492

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

main/http_server/axe-os/src/app/guards/ap-mode.guard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export const ApModeGuard: CanActivateFn = (): Observable<boolean> => {
99

1010
return systemService.getInfo().pipe(
1111
map(info => {
12-
// Backend detects which network the request came from (works with captive portals!)
1312
const requestFromAp = (info as any).requestFromAp || false;
1413
const isWifiConnected = info.wifiStatus && info.wifiStatus.includes('Connected');
15-
14+
1615
if (info.apEnabled && !isWifiConnected) {
1716
// AP mode only (no WiFi configured) - show setup page
1817
router.navigate(['/ap']);
1918
return false;
20-
} else if (info.apEnabled && isWifiConnected && requestFromAp) {
19+
} else if (requestFromAp && info.apEnabled && isWifiConnected) {
20+
// Only redirect to ipinfo if backend confirms request is from AP subnet
2121
window.location.href = '/ipinfo';
2222
return false;
2323
}

main/http_server/http_server.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ static esp_err_t GET_wifi_scan(httpd_req_t *req)
8282

8383
static GlobalState * GLOBAL_STATE;
8484
static httpd_handle_t server = NULL;
85+
86+
// Check if an HTTP request arrived on the AP network interface
87+
// by comparing the socket's local address against the AP interface IP
88+
static bool is_request_from_ap(httpd_req_t *req)
89+
{
90+
esp_netif_t *ap_netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
91+
if (ap_netif == NULL) {
92+
return false;
93+
}
94+
95+
esp_netif_ip_info_t ap_ip_info;
96+
if (esp_netif_get_ip_info(ap_netif, &ap_ip_info) != ESP_OK) {
97+
return false;
98+
}
99+
100+
int sockfd = httpd_req_to_sockfd(req);
101+
struct sockaddr_in6 local_addr;
102+
socklen_t addr_size = sizeof(local_addr);
103+
104+
// Use getsockname to get the LOCAL address (which interface the request arrived on)
105+
if (getsockname(sockfd, (struct sockaddr *)&local_addr, &addr_size) != 0) {
106+
return false;
107+
}
108+
109+
uint32_t local_ip = local_addr.sin6_addr.un.u32_addr[3];
110+
// If the socket's local IP matches the AP interface IP, the request came via the AP
111+
return local_ip == ap_ip_info.ip.addr;
112+
}
113+
85114
QueueHandle_t log_queue = NULL;
86115

87116
static int fd = -1;
@@ -314,13 +343,21 @@ static esp_err_t GET_ap_info(httpd_req_t *req)
314343
return ESP_OK;
315344
}
316345

317-
/* Handler for AP status page */
346+
/* Handler for AP status page - only accessible from AP network */
318347
static esp_err_t rest_ap_page_handler(httpd_req_t *req)
319348
{
349+
if (!GLOBAL_STATE->SYSTEM_MODULE.ap_enabled || !is_request_from_ap(req)) {
350+
// Not from AP network - redirect to main web interface
351+
httpd_resp_set_status(req, "302 Temporary Redirect");
352+
httpd_resp_set_hdr(req, "Location", "/");
353+
httpd_resp_send(req, "Redirecting", HTTPD_RESP_USE_STRLEN);
354+
return ESP_OK;
355+
}
356+
320357
extern const unsigned char ap_page_start[] asm("_binary_ap_page_html_start");
321358
extern const unsigned char ap_page_end[] asm("_binary_ap_page_html_end");
322359
const size_t ap_page_size = (ap_page_end - ap_page_start);
323-
360+
324361
httpd_resp_set_type(req, "text/html");
325362
httpd_resp_send(req, (const char*)ap_page_start, ap_page_size);
326363
return ESP_OK;
@@ -590,20 +627,8 @@ static esp_err_t GET_system_info(httpd_req_t * req)
590627
return ESP_OK;
591628
}
592629

593-
// Detect if request is coming from AP network (192.168.4.x)
594-
int sockfd = httpd_req_to_sockfd(req);
595-
struct sockaddr_in6 addr;
596-
socklen_t addr_size = sizeof(addr);
597-
bool request_from_ap = false;
598-
599-
if (getpeername(sockfd, (struct sockaddr *)&addr, &addr_size) == 0) {
600-
uint32_t client_ip = addr.sin6_addr.un.u32_addr[3];
601-
uint32_t ip_host_order = ntohl(client_ip);
602-
// Check if IP is in 192.168.4.0/24 range (AP network)
603-
if ((ip_host_order & 0xFFFFFF00) == 0xC0A80400) { // 192.168.4.x
604-
request_from_ap = true;
605-
}
606-
}
630+
// Detect if request is coming from AP network using actual AP netif subnet
631+
bool request_from_ap = is_request_from_ap(req);
607632

608633

609634
char * ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, CONFIG_ESP_WIFI_SSID);

0 commit comments

Comments
 (0)