Skip to content

Commit 9c92f5b

Browse files
authored
Merge pull request #29 from netbirdio/fix/disabled-settings-page-delay
fix: avoid status and settings page delays when NetBird is disabled
2 parents f7fe83b + cd516f1 commit 9c92f5b

3 files changed

Lines changed: 71 additions & 16 deletions

File tree

src/usr/local/emhttp/plugins/netbird/Netbird-1-Settings.page

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ $enableSsh = $cfg['ENABLE_SSH'] ?? '0';
1818
$manageDns = $cfg['MANAGE_DNS'] ?? '1';
1919
$logLevel = $cfg['LOG_LEVEL'] ?? 'info';
2020

21-
$profiles = Netbird\listProfiles();
22-
$activeProf = Netbird\activeProfile();
21+
$enableCfg = strtolower(trim((string) $enabled));
22+
$serviceEnabled = !in_array($enableCfg, ['0', 'false'], true);
23+
$daemonReady = $serviceEnabled && Netbird\daemonRunning() && file_exists('/var/run/netbird.sock');
24+
$profiles = $daemonReady ? Netbird\listProfiles() : [];
25+
if (!$profiles) {
26+
$profiles = Netbird\savedProfiles();
27+
}
28+
$activeProf = '';
29+
foreach ($profiles as $profile) {
30+
if ($profile['active']) {
31+
$activeProf = $profile['name'];
32+
break;
33+
}
34+
}
2335

2436
// Which profile's credentials is the form editing? ?nbprofile= wins, then the
2537
// active profile, then "default" (NetBird's built-in profile).
@@ -28,7 +40,19 @@ if (!Netbird\validProfileName($editProf)) {
2840
$editProf = '';
2941
}
3042
if ($editProf === '') {
31-
$editProf = $activeProf !== '' ? $activeProf : 'default';
43+
$editProf = $activeProf;
44+
if ($editProf === '' && $profiles) {
45+
$editProf = $profiles[0]['name'];
46+
foreach ($profiles as $profile) {
47+
if ($profile['name'] === 'default') {
48+
$editProf = 'default';
49+
break;
50+
}
51+
}
52+
}
53+
if ($editProf === '') {
54+
$editProf = 'default';
55+
}
3256
}
3357

3458
// Credentials are stored per profile (profiles/<name>.cfg), not globally.
@@ -61,14 +85,20 @@ $preSharedKey = $creds['PRESHARED_KEY'];
6185
</option>
6286
<?php endforeach; ?>
6387
</select>
64-
<input type="button" value="_(Switch)_" onclick="nbProfile('select', $('#nb-profile-select').val())">
65-
<input type="button" value="_(Delete)_" onclick="nbProfileDelete()">
88+
<?php if ($daemonReady): ?>
89+
<input type="button" value="_(Switch)_" onclick="nbProfile('select', $('#nb-profile-select').val())">
90+
<input type="button" value="_(Delete)_" onclick="nbProfileDelete()">
91+
<?php endif; ?>
92+
<?php else: ?>
93+
<span class="nb-profile-empty">No saved profiles yet — editing <code><?=htmlspecialchars($editProf)?></code>.</span>
94+
<?php endif; ?>
95+
<?php if ($daemonReady): ?>
96+
&nbsp;&nbsp;
97+
<input type="text" id="nb-profile-new" placeholder="new-profile-name" maxlength="32">
98+
<input type="button" value="_(+ Add)_" onclick="nbProfileAdd()">
6699
<?php else: ?>
67-
<span class="nb-profile-empty">No profiles found yet — editing <code><?=htmlspecialchars($editProf)?></code>. Start the daemon and add one below.</span>
100+
<span class="nb-profile-empty">Profile controls are available while NetBird is running.</span>
68101
<?php endif; ?>
69-
&nbsp;&nbsp;
70-
<input type="text" id="nb-profile-new" placeholder="new-profile-name" maxlength="32">
71-
<input type="button" value="_(+ Add)_" onclick="nbProfileAdd()">
72102
</div>
73103

74104
<blockquote class="inline_help">

src/usr/local/emhttp/plugins/netbird/include/common.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function nb(array $args, int $timeoutSec = 0): array
5151
*/
5252
function statusJson(): ?array
5353
{
54-
[$rc, $out] = nb(['status', '--json']);
54+
[$rc, $out] = nb(['status', '--json'], 3);
5555
if ($rc !== 0 || $out === '') {
5656
return null;
5757
}
@@ -130,7 +130,7 @@ function readApplyResult(): ?array
130130
*/
131131
function listProfiles(): array
132132
{
133-
[$rc, $out] = nb(['profile', 'list']);
133+
[$rc, $out] = nb(['profile', 'list'], 3);
134134
if ($rc !== 0) {
135135
return [];
136136
}
@@ -152,6 +152,27 @@ function listProfiles(): array
152152
return $profiles;
153153
}
154154

155+
/**
156+
* List profiles with credentials saved by the plugin without contacting the
157+
* daemon. This keeps the Settings page usable while NetBird is disabled.
158+
*
159+
* @return array<int, array{name:string, active:bool}>
160+
*/
161+
function savedProfiles(): array
162+
{
163+
$profiles = [];
164+
foreach (glob(PROFILE_DIR . '/*.cfg') ?: [] as $path) {
165+
$name = pathinfo($path, PATHINFO_FILENAME);
166+
if (validProfileName($name)) {
167+
$profiles[] = ['name' => $name, 'active' => false];
168+
}
169+
}
170+
usort($profiles, static function (array $a, array $b): int {
171+
return strcasecmp($a['name'], $b['name']);
172+
});
173+
return $profiles;
174+
}
175+
155176
/**
156177
* Convenience: name of the currently-active profile, or '' if none.
157178
*/

src/usr/local/emhttp/plugins/netbird/include/status_view.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
$docroot = $docroot ?? $_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp';
44
require_once "{$docroot}/plugins/netbird/include/common.php";
55

6-
$running = Netbird\daemonRunning();
7-
$status = Netbird\statusJson();
6+
$cfg = Netbird\readCfg();
7+
$enableCfg = strtolower(trim((string) ($cfg['ENABLE_NETBIRD'] ?? '0')));
8+
$enabled = !in_array($enableCfg, ['0', 'false'], true);
9+
$running = Netbird\daemonRunning();
10+
$daemonReady = $enabled && $running && file_exists('/var/run/netbird.sock');
11+
$status = $daemonReady ? Netbird\statusJson() : null;
812

913
// Whether the plugin lets NetBird manage host DNS (MANAGE_DNS, default on).
10-
$manageDns = (Netbird\readCfg()['MANAGE_DNS'] ?? '1') === '1';
14+
$manageDns = ($cfg['MANAGE_DNS'] ?? '1') === '1';
1115

1216
// The host's live resolver, so issue #2 ("NetBird took over my DNS") is visible
1317
// at a glance: when management is on, NetBird rewrites this to point at its own
@@ -19,8 +23,8 @@
1923
}
2024

2125
$rawStatus = '';
22-
if (!$status) {
23-
[$rc, $out] = Netbird\nb(['status']);
26+
if ($daemonReady && !$status) {
27+
[$rc, $out] = Netbird\nb(['status'], 3);
2428
$rawStatus = $out;
2529
}
2630
?>

0 commit comments

Comments
 (0)