Adding version checking

This commit is contained in:
2026-03-11 11:08:24 -04:00
parent 0aabf321ef
commit 73a39f4f3f
4 changed files with 74 additions and 3 deletions

View File

@@ -180,6 +180,64 @@ function run_fetch(): array {
return $results;
}
// ── Upstream version check ────────────────────────────────────────────────────
define('UPSTREAM_VERSION_URL', 'https://git.ny.daprogs.com/api/v1/repos/DAProgs/portspoof_concentrator/raw/version.php?ref=main');
define('UPSTREAM_VERSION_CACHE', __DIR__ . '/../upstream_version.cache');
define('UPSTREAM_VERSION_TTL', 3600); // seconds
/**
* Fetch the upstream version string from the git repo, with a 1-hour file cache.
* Returns null silently on any failure so the page always loads.
*/
function fetch_upstream_version(): ?string {
// Return cached value if still fresh
if (file_exists(UPSTREAM_VERSION_CACHE)) {
$cached = json_decode(file_get_contents(UPSTREAM_VERSION_CACHE), true);
if (isset($cached['version'], $cached['checked_at'])
&& (time() - $cached['checked_at']) < UPSTREAM_VERSION_TTL) {
return $cached['version'];
}
}
$ch = curl_init(UPSTREAM_VERSION_URL);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 5,
CURLOPT_FOLLOWLOCATION => true,
]);
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($body === false || $code !== 200) {
return null;
}
if (!preg_match("/define\s*\(\s*'APP_VERSION'\s*,\s*'([^']+)'\s*\)/", $body, $m)) {
return null;
}
$version = $m[1];
file_put_contents(
UPSTREAM_VERSION_CACHE,
json_encode(['version' => $version, 'checked_at' => time()]),
LOCK_EX
);
return $version;
}
/**
* Returns true if $upstream is a newer version than $local.
* Format: YYMM.N e.g. 2603.4
*/
function is_newer_version(string $upstream, string $local): bool {
$parse = fn($v) => array_map('intval', explode('.', $v, 2));
[$um, $un] = $parse($upstream);
[$lm, $ln] = $parse($local);
return $um > $lm || ($um === $lm && $un > $ln);
}
// ── API queries ───────────────────────────────────────────────────────────────
/**

View File

@@ -72,8 +72,10 @@ button[type=submit]:hover { opacity: .85; }
.link-btn.danger { color: var(--red); }
.alert { padding: .65rem 1rem; border-radius: 6px; font-size: .875rem; }
.alert.ok { background: rgba(63,185,80,.15); color: var(--green); border: 1px solid rgba(63,185,80,.4); }
.alert.err { background: rgba(248,81,73,.15); color: var(--red); border: 1px solid rgba(248,81,73,.4); }
.alert.ok { background: rgba(63,185,80,.15); color: var(--green); border: 1px solid rgba(63,185,80,.4); }
.alert.err { background: rgba(248,81,73,.15); color: var(--red); border: 1px solid rgba(248,81,73,.4); }
.alert.warn { background: rgba(210,153,34,.15); color: var(--yellow); border: 1px solid rgba(210,153,34,.4); }
.alert.warn a { color: var(--yellow); }
.badge { display: inline-block; font-size: .7rem; padding: .1rem .45rem; border-radius: 3px; background: var(--border); color: var(--muted); }
.badge.ok { background: rgba(63,185,80,.2); color: var(--green); }

View File

@@ -20,6 +20,9 @@ if ($filter_node) {
$max_ip_cnt = $t_ips ? max(array_column($t_ips, 'cnt')) : 1;
$max_port_cnt = $t_ports ? max(array_column($t_ports, 'cnt')) : 1;
$upstream_version = fetch_upstream_version();
$update_available = $upstream_version && is_newer_version($upstream_version, APP_VERSION);
?>
<!DOCTYPE html>
<html lang="en">
@@ -46,6 +49,14 @@ $max_port_cnt = $t_ports ? max(array_column($t_ports, 'cnt')) : 1;
</header>
<main>
<?php if ($update_available): ?>
<div class="alert warn">
Update available: <strong>v<?= h($upstream_version) ?></strong> —
you are running <strong>v<?= h(APP_VERSION) ?></strong>.
<a href="https://git.ny.daprogs.com/DAProgs/portspoof_concentrator" target="_blank">View release ↗</a>
</div>
<?php endif; ?>
<!-- Stat cards -->
<div class="stats-row">
<div class="stat-card">

View File

@@ -1,2 +1,2 @@
<?php
define('APP_VERSION', '2603.3');
define('APP_VERSION', '2603.4');