Skip to content

Commit ad75757

Browse files
authored
Merge pull request #9 from WantClue/ap-removal-code
check for ap request
2 parents a48275f + 5cc1492 commit ad75757

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
@@ -85,6 +85,35 @@ static esp_err_t GET_wifi_scan(httpd_req_t *req)
8585

8686
static GlobalState * GLOBAL_STATE;
8787
static 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+
88117
QueueHandle_t log_queue = NULL;
89118

90119
static 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 */
324353
static 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

Comments
 (0)