@@ -85,6 +85,35 @@ static esp_err_t GET_wifi_scan(httpd_req_t *req)
8585
8686static GlobalState * GLOBAL_STATE ;
8787static httpd_handle_t server = NULL ;
88+
89+ // Check if an HTTP request arrived on the AP network interface
90+ // by comparing the socket's local address against the AP interface IP
91+ static bool is_request_from_ap (httpd_req_t * req )
92+ {
93+ esp_netif_t * ap_netif = esp_netif_get_handle_from_ifkey ("WIFI_AP_DEF" );
94+ if (ap_netif == NULL ) {
95+ return false;
96+ }
97+
98+ esp_netif_ip_info_t ap_ip_info ;
99+ if (esp_netif_get_ip_info (ap_netif , & ap_ip_info ) != ESP_OK ) {
100+ return false;
101+ }
102+
103+ int sockfd = httpd_req_to_sockfd (req );
104+ struct sockaddr_in6 local_addr ;
105+ socklen_t addr_size = sizeof (local_addr );
106+
107+ // Use getsockname to get the LOCAL address (which interface the request arrived on)
108+ if (getsockname (sockfd , (struct sockaddr * )& local_addr , & addr_size ) != 0 ) {
109+ return false;
110+ }
111+
112+ uint32_t local_ip = local_addr .sin6_addr .un .u32_addr [3 ];
113+ // If the socket's local IP matches the AP interface IP, the request came via the AP
114+ return local_ip == ap_ip_info .ip .addr ;
115+ }
116+
88117QueueHandle_t log_queue = NULL ;
89118
90119static int fd = -1 ;
@@ -320,13 +349,21 @@ static esp_err_t GET_ap_info(httpd_req_t *req)
320349 return ESP_OK ;
321350}
322351
323- /* Handler for AP status page */
352+ /* Handler for AP status page - only accessible from AP network */
324353static esp_err_t rest_ap_page_handler (httpd_req_t * req )
325354{
355+ if (!GLOBAL_STATE -> SYSTEM_MODULE .ap_enabled || !is_request_from_ap (req )) {
356+ // Not from AP network - redirect to main web interface
357+ httpd_resp_set_status (req , "302 Temporary Redirect" );
358+ httpd_resp_set_hdr (req , "Location" , "/" );
359+ httpd_resp_send (req , "Redirecting" , HTTPD_RESP_USE_STRLEN );
360+ return ESP_OK ;
361+ }
362+
326363 extern const unsigned char ap_page_start [] asm("_binary_ap_page_html_start" );
327364 extern const unsigned char ap_page_end [] asm("_binary_ap_page_html_end" );
328365 const size_t ap_page_size = (ap_page_end - ap_page_start );
329-
366+
330367 httpd_resp_set_type (req , "text/html" );
331368 httpd_resp_send (req , (const char * )ap_page_start , ap_page_size );
332369 return ESP_OK ;
@@ -603,20 +640,8 @@ static esp_err_t GET_system_info(httpd_req_t * req)
603640 return ESP_OK ;
604641 }
605642
606- // Detect if request is coming from AP network (192.168.4.x)
607- int sockfd = httpd_req_to_sockfd (req );
608- struct sockaddr_in6 addr ;
609- socklen_t addr_size = sizeof (addr );
610- bool request_from_ap = false;
611-
612- if (getpeername (sockfd , (struct sockaddr * )& addr , & addr_size ) == 0 ) {
613- uint32_t client_ip = addr .sin6_addr .un .u32_addr [3 ];
614- uint32_t ip_host_order = ntohl (client_ip );
615- // Check if IP is in 192.168.4.0/24 range (AP network)
616- if ((ip_host_order & 0xFFFFFF00 ) == 0xC0A80400 ) { // 192.168.4.x
617- request_from_ap = true;
618- }
619- }
643+ // Detect if request is coming from AP network using actual AP netif subnet
644+ bool request_from_ap = is_request_from_ap (req );
620645
621646
622647 char * ssid = nvs_config_get_string (NVS_CONFIG_WIFI_SSID , CONFIG_ESP_WIFI_SSID );
0 commit comments