From 717277ae87c73819e486ee466cfca21916d7ec9d Mon Sep 17 00:00:00 2001 From: Clarth Date: Thu, 3 Dec 2026 20:51:49 -0500 Subject: [PATCH] Improve discovery: scan 5555 and 8008/eureka_info across both subnets --- api.php | 122 ++++++++++++++++++++++++++++++++++---------------- assets/app.js | 8 ++-- index.php | 2 +- 3 files changed, 88 insertions(+), 44 deletions(-) diff --git a/api.php b/api.php index 4014e5d..ef03466 100644 --- a/api.php +++ b/api.php @@ -26,7 +26,7 @@ function read_input(): array return $_POST; } -function scan_subnets(): array +function scan_subnet_targets(): array { $subnets = ['192.168.1.', '192.168.2.']; $targets = []; @@ -35,7 +35,11 @@ function scan_subnets(): array $targets[] = $prefix . $i; } } + return $targets; +} +function scan_port_5555(array $targets): array +{ $pending = []; // ip => [socket, deadline] $found = []; $timeout = 1.2; @@ -60,22 +64,18 @@ function scan_subnets(): array while (!empty($pending)) { $write = []; $except = []; - foreach ($pending as $ip => $info) { + foreach ($pending as $info) { $write[] = $info['sock']; $except[] = $info['sock']; } $read = null; - $ready = @stream_select($read, $write, $except, 0, 100000); // 100ms + @stream_select($read, $write, $except, 0, 100000); $now = microtime(true); $writable = []; - foreach ($write as $s) { - $writable[(int) $s] = $s; - } + foreach ($write as $s) { $writable[(int) $s] = $s; } $errored = []; - foreach ($except as $s) { - $errored[(int) $s] = $s; - } + foreach ($except as $s) { $errored[(int) $s] = $s; } foreach ($pending as $ip => $info) { $id = (int) $info['sock']; @@ -86,7 +86,6 @@ function scan_subnets(): array if ($isWritable || $isErrored) { $open = false; if ($isWritable && !$isErrored) { - // For async connects, writable usually means connected; verify SO_ERROR when possible. $err = null; if (function_exists('socket_import_stream') && function_exists('socket_get_option')) { $sockRes = @socket_import_stream($info['sock']); @@ -96,9 +95,7 @@ function scan_subnets(): array } $open = ($err === 0 || $err === null); } - if ($open) { - $found[] = $ip; - } + if ($open) $found[] = $ip; fclose($info['sock']); unset($pending[$ip]); continue; @@ -109,16 +106,59 @@ function scan_subnets(): array unset($pending[$ip]); } } - - if ($ready === 0) { - // loop will handle expirations - } } sort($found, SORT_NATURAL); return $found; } +function scan_eureka_info(array $targets): array +{ + // Returns ip => name for devices responding on 8008/setup/eureka_info + $found = []; + if (!function_exists('curl_multi_init')) { + return $found; + } + + foreach (array_chunk($targets, 128) as $chunk) { + $mh = curl_multi_init(); + $handles = []; + foreach ($chunk as $ip) { + $url = 'http://' . $ip . ':8008/setup/eureka_info'; + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT_MS => 700, + CURLOPT_TIMEOUT_MS => 2000, + CURLOPT_HTTPHEADER => ['Accept: application/json'], + ]); + curl_multi_add_handle($mh, $ch); + $handles[$ip] = $ch; + } + do { + $status = curl_multi_exec($mh, $running); + if ($running) curl_multi_select($mh, 0.2); + } while ($running && $status === CURLM_OK); + + foreach ($handles as $ip => $ch) { + $code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + $body = (string) curl_multi_getcontent($ch); + if ($code === 200 && $body !== '') { + $data = json_decode($body, true); + if (is_array($data)) { + $name = trim((string) ($data['name'] ?? '')); + if ($name !== '') $found[$ip] = $name; + } + } + curl_multi_remove_handle($mh, $ch); + curl_close($ch); + } + curl_multi_close($mh); + } + + return $found; +} + $action = $_GET['action'] ?? ($_POST['action'] ?? ''); $input = read_input(); if ($action === '' && isset($input['action'])) { @@ -140,55 +180,57 @@ try { } case 'scan': { - $foundIps = scan_subnets(); + $targets = scan_subnet_targets(); + $port5555 = scan_port_5555($targets); + $eureka = scan_eureka_info($targets); // ip => name + + $discoveredIps = array_values(array_unique(array_merge($port5555, array_keys($eureka)))); + sort($discoveredIps, SORT_NATURAL); + $known = load_devices(); $knownByIp = []; foreach ($known as $d) { - if (isset($d['ip'])) { - $knownByIp[$d['ip']] = $d; - } + if (isset($d['ip'])) $knownByIp[$d['ip']] = $d; } $adb = adb_devices(); $adbByIp = []; foreach ($adb as $d) { - if (!empty($d['ip'])) { - $adbByIp[$d['ip']] = $d; - } + if (!empty($d['ip'])) $adbByIp[$d['ip']] = $d; } $results = []; - foreach ($foundIps as $ip) { + foreach ($discoveredIps as $ip) { $k = $knownByIp[$ip] ?? null; $a = $adbByIp[$ip] ?? null; $name = trim((string) ($k['name'] ?? '')); - $lookup = lookup_device_name($ip); - if ($lookup !== null && $lookup !== '') { - $name = $lookup; + $eurekaName = trim((string) ($eureka[$ip] ?? '')); + if ($eurekaName !== '') { + $name = $eurekaName; + } elseif ($name === '') { + $lookup = lookup_device_name($ip); + if ($lookup !== null && $lookup !== '') $name = $lookup; } + + $portOpen = in_array($ip, $port5555, true); $results[] = [ 'ip' => $ip, - 'port_open' => true, + 'port_open' => $portOpen, 'name' => $name, 'last_seen' => gmdate('c'), 'last_connected' => $k['last_connected'] ?? null, 'adb_state' => $a['state'] ?? null, ]; + $save = ['last_seen' => gmdate('c')]; - if ($name !== '') { - $save['name'] = $name; - } + if ($name !== '') $save['name'] = $name; upsert_device($ip, $save); } - // Also include known devices that were not found in this scan + // Include known devices not discovered in this scan foreach ($known as $k) { $ip = $k['ip'] ?? ''; - if (!is_allowed_ip($ip)) { - continue; - } - if (in_array($ip, $foundIps, true)) { - continue; - } + if (!is_allowed_ip($ip)) continue; + if (in_array($ip, $discoveredIps, true)) continue; $a = $adbByIp[$ip] ?? null; $results[] = [ 'ip' => $ip, @@ -201,6 +243,8 @@ try { } usort($results, function ($a, $b) { + $aDisc = !empty($a['last_seen']) && ($a['port_open'] || ($a['adb_state'] ?? null)); + $bDisc = !empty($b['last_seen']) && ($b['port_open'] || ($b['adb_state'] ?? null)); if (($a['port_open'] ?? false) !== ($b['port_open'] ?? false)) { return ($a['port_open'] ? 0 : 1) <=> ($b['port_open'] ? 0 : 1); } diff --git a/assets/app.js b/assets/app.js index fee48c9..450ed8b 100644 --- a/assets/app.js +++ b/assets/app.js @@ -189,7 +189,7 @@ els.deviceCount.textContent = String(devices.length); if (!devices.length) { - els.devicesList.innerHTML = '

No devices found. Run a scan to discover boxes with port 5555 open.

'; + els.devicesList.innerHTML = '

No devices found. Run a scan to discover Android TV devices on your network.

'; return; } @@ -322,10 +322,10 @@ updateAdbStatus(); renderDevices(); renderControl(); - const found = state.devices.filter(d => d.port_open).length; - els.scanMeta.textContent = `Scanned at ${fmtTime(data.scanned_at)} — ${found} open port(s) found.`; + const found = state.devices.filter(d => d.port_open || d.adb_state || d.last_seen === data.scanned_at).length; + els.scanMeta.textContent = `Scanned at ${fmtTime(data.scanned_at)} — ${found} device(s) found.`; if (found === 0) { - setAlert(els.scanAlert, 'No devices with port 5555 open were found. Known devices (if any) are shown below.', 'warning'); + setAlert(els.scanAlert, 'No devices were discovered on 192.168.1.* or 192.168.2.*. Known devices (if any) are shown below.', 'warning'); } } catch (e) { setAlert(els.scanAlert, 'Scan error: ' + e.message, 'urgent'); diff --git a/index.php b/index.php index 6b2793e..4c84b63 100644 --- a/index.php +++ b/index.php @@ -44,7 +44,7 @@ declare(strict_types=1);
-

No scan yet. Click Scan Network to look for devices with port 5555 open.

+

No scan yet. Click Scan Network to discover Android TV devices on your network.