@@ -82,6 +82,35 @@ static esp_err_t GET_wifi_scan(httpd_req_t *req)
8282
8383static GlobalState * GLOBAL_STATE ;
8484static 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+
85114QueueHandle_t log_queue = NULL ;
86115
87116static 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 */
318347static 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