Improve discovery: scan 5555 and 8008/eureka_info across both subnets

This commit is contained in:
Clarth 2026-12-03 20:51:49 -05:00
parent 363a6a9b3c
commit 717277ae87
3 changed files with 88 additions and 44 deletions

120
api.php
View file

@ -26,7 +26,7 @@ function read_input(): array
return $_POST; return $_POST;
} }
function scan_subnets(): array function scan_subnet_targets(): array
{ {
$subnets = ['192.168.1.', '192.168.2.']; $subnets = ['192.168.1.', '192.168.2.'];
$targets = []; $targets = [];
@ -35,7 +35,11 @@ function scan_subnets(): array
$targets[] = $prefix . $i; $targets[] = $prefix . $i;
} }
} }
return $targets;
}
function scan_port_5555(array $targets): array
{
$pending = []; // ip => [socket, deadline] $pending = []; // ip => [socket, deadline]
$found = []; $found = [];
$timeout = 1.2; $timeout = 1.2;
@ -60,22 +64,18 @@ function scan_subnets(): array
while (!empty($pending)) { while (!empty($pending)) {
$write = []; $write = [];
$except = []; $except = [];
foreach ($pending as $ip => $info) { foreach ($pending as $info) {
$write[] = $info['sock']; $write[] = $info['sock'];
$except[] = $info['sock']; $except[] = $info['sock'];
} }
$read = null; $read = null;
$ready = @stream_select($read, $write, $except, 0, 100000); // 100ms @stream_select($read, $write, $except, 0, 100000);
$now = microtime(true); $now = microtime(true);
$writable = []; $writable = [];
foreach ($write as $s) { foreach ($write as $s) { $writable[(int) $s] = $s; }
$writable[(int) $s] = $s;
}
$errored = []; $errored = [];
foreach ($except as $s) { foreach ($except as $s) { $errored[(int) $s] = $s; }
$errored[(int) $s] = $s;
}
foreach ($pending as $ip => $info) { foreach ($pending as $ip => $info) {
$id = (int) $info['sock']; $id = (int) $info['sock'];
@ -86,7 +86,6 @@ function scan_subnets(): array
if ($isWritable || $isErrored) { if ($isWritable || $isErrored) {
$open = false; $open = false;
if ($isWritable && !$isErrored) { if ($isWritable && !$isErrored) {
// For async connects, writable usually means connected; verify SO_ERROR when possible.
$err = null; $err = null;
if (function_exists('socket_import_stream') && function_exists('socket_get_option')) { if (function_exists('socket_import_stream') && function_exists('socket_get_option')) {
$sockRes = @socket_import_stream($info['sock']); $sockRes = @socket_import_stream($info['sock']);
@ -96,9 +95,7 @@ function scan_subnets(): array
} }
$open = ($err === 0 || $err === null); $open = ($err === 0 || $err === null);
} }
if ($open) { if ($open) $found[] = $ip;
$found[] = $ip;
}
fclose($info['sock']); fclose($info['sock']);
unset($pending[$ip]); unset($pending[$ip]);
continue; continue;
@ -109,16 +106,59 @@ function scan_subnets(): array
unset($pending[$ip]); unset($pending[$ip]);
} }
} }
if ($ready === 0) {
// loop will handle expirations
}
} }
sort($found, SORT_NATURAL); sort($found, SORT_NATURAL);
return $found; 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'] ?? ''); $action = $_GET['action'] ?? ($_POST['action'] ?? '');
$input = read_input(); $input = read_input();
if ($action === '' && isset($input['action'])) { if ($action === '' && isset($input['action'])) {
@ -140,55 +180,57 @@ try {
} }
case 'scan': { 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(); $known = load_devices();
$knownByIp = []; $knownByIp = [];
foreach ($known as $d) { foreach ($known as $d) {
if (isset($d['ip'])) { if (isset($d['ip'])) $knownByIp[$d['ip']] = $d;
$knownByIp[$d['ip']] = $d;
}
} }
$adb = adb_devices(); $adb = adb_devices();
$adbByIp = []; $adbByIp = [];
foreach ($adb as $d) { foreach ($adb as $d) {
if (!empty($d['ip'])) { if (!empty($d['ip'])) $adbByIp[$d['ip']] = $d;
$adbByIp[$d['ip']] = $d;
}
} }
$results = []; $results = [];
foreach ($foundIps as $ip) { foreach ($discoveredIps as $ip) {
$k = $knownByIp[$ip] ?? null; $k = $knownByIp[$ip] ?? null;
$a = $adbByIp[$ip] ?? null; $a = $adbByIp[$ip] ?? null;
$name = trim((string) ($k['name'] ?? '')); $name = trim((string) ($k['name'] ?? ''));
$eurekaName = trim((string) ($eureka[$ip] ?? ''));
if ($eurekaName !== '') {
$name = $eurekaName;
} elseif ($name === '') {
$lookup = lookup_device_name($ip); $lookup = lookup_device_name($ip);
if ($lookup !== null && $lookup !== '') { if ($lookup !== null && $lookup !== '') $name = $lookup;
$name = $lookup;
} }
$portOpen = in_array($ip, $port5555, true);
$results[] = [ $results[] = [
'ip' => $ip, 'ip' => $ip,
'port_open' => true, 'port_open' => $portOpen,
'name' => $name, 'name' => $name,
'last_seen' => gmdate('c'), 'last_seen' => gmdate('c'),
'last_connected' => $k['last_connected'] ?? null, 'last_connected' => $k['last_connected'] ?? null,
'adb_state' => $a['state'] ?? null, 'adb_state' => $a['state'] ?? null,
]; ];
$save = ['last_seen' => gmdate('c')]; $save = ['last_seen' => gmdate('c')];
if ($name !== '') { if ($name !== '') $save['name'] = $name;
$save['name'] = $name;
}
upsert_device($ip, $save); 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) { foreach ($known as $k) {
$ip = $k['ip'] ?? ''; $ip = $k['ip'] ?? '';
if (!is_allowed_ip($ip)) { if (!is_allowed_ip($ip)) continue;
continue; if (in_array($ip, $discoveredIps, true)) continue;
}
if (in_array($ip, $foundIps, true)) {
continue;
}
$a = $adbByIp[$ip] ?? null; $a = $adbByIp[$ip] ?? null;
$results[] = [ $results[] = [
'ip' => $ip, 'ip' => $ip,
@ -201,6 +243,8 @@ try {
} }
usort($results, function ($a, $b) { 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)) { if (($a['port_open'] ?? false) !== ($b['port_open'] ?? false)) {
return ($a['port_open'] ? 0 : 1) <=> ($b['port_open'] ? 0 : 1); return ($a['port_open'] ? 0 : 1) <=> ($b['port_open'] ? 0 : 1);
} }

View file

@ -189,7 +189,7 @@
els.deviceCount.textContent = String(devices.length); els.deviceCount.textContent = String(devices.length);
if (!devices.length) { if (!devices.length) {
els.devicesList.innerHTML = '<p class="empty">No devices found. Run a scan to discover boxes with port 5555 open.</p>'; els.devicesList.innerHTML = '<p class="empty">No devices found. Run a scan to discover Android TV devices on your network.</p>';
return; return;
} }
@ -322,10 +322,10 @@
updateAdbStatus(); updateAdbStatus();
renderDevices(); renderDevices();
renderControl(); renderControl();
const found = state.devices.filter(d => d.port_open).length; 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} open port(s) found.`; els.scanMeta.textContent = `Scanned at ${fmtTime(data.scanned_at)} — ${found} device(s) found.`;
if (found === 0) { 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) { } catch (e) {
setAlert(els.scanAlert, 'Scan error: ' + e.message, 'urgent'); setAlert(els.scanAlert, 'Scan error: ' + e.message, 'urgent');

View file

@ -44,7 +44,7 @@ declare(strict_types=1);
</div> </div>
<div id="scanAlert" class="alert" hidden></div> <div id="scanAlert" class="alert" hidden></div>
<div id="devicesList" class="device-grid device-grid-compact" aria-live="polite"> <div id="devicesList" class="device-grid device-grid-compact" aria-live="polite">
<p class="empty">No scan yet. Click <strong>Scan Network</strong> to look for devices with port 5555 open.</p> <p class="empty">No scan yet. Click <strong>Scan Network</strong> to discover Android TV devices on your network.</p>
</div> </div>
</section> </section>