Skip to content

Commit e1e3ea7

Browse files
authored
fix: update myservers config references to connect config references (#1810)
`myservers.cfg` no longer gets written to or read (except for migration purposes), so it'd be better to read from the new values instead of continuing to use the old ones @elibosley @Squidly271 . unless i'm missing something! see #1805 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Switches to a centralized remote-access configuration with a legacy fallback and richer client-side handling. * Optional GraphQL submission path for applying remote settings when available. * **Bug Fixes** * Normalized boolean and port handling to prevent incorrect values reaching the UI. * Improved error handling and UI state restoration during save/apply flows. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 8b155d1 commit e1e3ea7

File tree

1 file changed

+104
-18
lines changed
  • plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers

1 file changed

+104
-18
lines changed

plugin/source/dynamix.unraid.net/usr/local/emhttp/plugins/dynamix.my.servers/Connect.page

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,29 @@ Tag="globe"
1515
*/
1616
require_once "$docroot/plugins/dynamix.my.servers/include/state.php";
1717
require_once "$docroot/plugins/dynamix.my.servers/include/api-config.php";
18+
require_once "$docroot/plugins/dynamix.my.servers/include/connect-config.php";
1819
require_once "$docroot/webGui/include/Wrappers.php";
1920
$serverState = new ServerState();
2021

2122
$keyfile = $serverState->keyfileBase64;
2223

23-
$myServersFlashCfg = $serverState->myServersFlashCfg;
24+
$connectConfig = ConnectConfig::getConfig();
25+
$legacyRemoteCfg = $serverState->myServersFlashCfg['remote'] ?? [];
26+
27+
$remoteDynamicRemoteAccessType = $connectConfig['dynamicRemoteAccessType'] ?? ($legacyRemoteCfg['dynamicRemoteAccessType'] ?? null);
28+
$remoteWanAccessRaw = $connectConfig['wanaccess'] ?? ($legacyRemoteCfg['wanaccess'] ?? null);
29+
$remoteUpnpEnabledRaw = $connectConfig['upnpEnabled'] ?? ($legacyRemoteCfg['upnpEnabled'] ?? null);
30+
$remoteWanPortRaw = $connectConfig['wanport'] ?? ($legacyRemoteCfg['wanport'] ?? null);
31+
32+
$wanaccessEnabled = filter_var($remoteWanAccessRaw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
33+
if ($wanaccessEnabled === null) {
34+
$wanaccessEnabled = false;
35+
}
36+
$upnpEnabled = filter_var($remoteUpnpEnabledRaw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
37+
if ($upnpEnabled === null) {
38+
$upnpEnabled = false;
39+
}
40+
$remoteWanPort = is_numeric($remoteWanPortRaw) ? (int)$remoteWanPortRaw : 0;
2441

2542
$showT2Fa = (file_exists('/boot/config/plugins/dynamix.my.servers/showT2Fa'));
2643

@@ -37,9 +54,7 @@ $passwd_result = exec('/usr/bin/passwd --status root');
3754
$boolWebUIAuth = $isRegistered && (($passwd_result !== false) && (substr($passwd_result, 0, 6) == 'root P'));
3855

3956
// Helper to determine the current value for the remote access input
40-
$dynamicRemoteAccessType = $myServersFlashCfg['remote']['dynamicRemoteAccessType'] ?? null;
41-
$upnpEnabled = ($myServersFlashCfg['remote']['upnpEnabled'] ?? null) === 'yes';
42-
$wanaccessEnabled = ($myServersFlashCfg['remote']['wanaccess'] ?? null) === 'yes';
57+
$dynamicRemoteAccessType = $remoteDynamicRemoteAccessType ?? null;
4358

4459
$currentRemoteAccessValue = 'OFF';
4560
if ($dynamicRemoteAccessType === 'STATIC') {
@@ -59,6 +74,12 @@ if ($dynamicRemoteAccessType === 'STATIC') {
5974
$enableRemoteT2fa = $showT2Fa && $currentRemoteAccessValue !== 'OFF' && $hasMyUnraidNetCert;
6075
$enableLocalT2fa = $showT2Fa && $var['USE_SSL'] === 'auto' && $hasMyUnraidNetCert;
6176
$shade="shade-".($display['theme']??'unk');
77+
$wanAccessOriginal = $remoteWanAccessRaw;
78+
if (is_bool($wanAccessOriginal)) {
79+
$wanAccessOriginal = $wanAccessOriginal ? 'yes' : 'no';
80+
} elseif (!is_string($wanAccessOriginal)) {
81+
$wanAccessOriginal = '';
82+
}
6283
?>
6384
<style>
6485
div.shade-white{background-color:#ededed;margin-top:10px;padding:8px 0 3px 0}
@@ -68,13 +89,18 @@ div.shade-gray{background-color:#121510;margin-top:10px;padding:8px 0 3px 0}
6889
</style>
6990
<script>
7091
const hasMyUnraidNetCert = <?=($hasMyUnraidNetCert ? 'true' : 'false')?>;
71-
const wanAccessOrg = "<?=$myServersFlashCfg['remote']['wanaccess'] ?? null?>";
92+
const wanAccessOrg = "<?=$wanAccessOriginal?>";
7293

7394
function registerServer(button) {
7495

7596
const $remoteAccessInput = $('#remoteAccess');
7697
const $remoteAccessManualPort = $('#wanport');
7798

99+
const parsePort = (val) => {
100+
const parsed = parseInt(val, 10);
101+
return isNaN(parsed) ? null : parsed;
102+
};
103+
78104
let computedRemoteAccessConfig = null;
79105
switch ($remoteAccessInput.val()) {
80106
case 'ALWAYS_MANUAL':
@@ -119,19 +145,64 @@ function registerServer(button) {
119145
break;
120146
}
121147

122-
const enableLocalT2fa = <?=($enableLocalT2fa ? 'true' : 'false')?>;
123-
const enableRemoteT2fa = $remoteAccessInput.val() !== 'OFF' && hasMyUnraidNetCert;
148+
const enableLocalT2fa = <?=($enableLocalT2fa ? 'true' : 'false')?>;
149+
const enableRemoteT2fa = $remoteAccessInput.val() !== 'OFF' && hasMyUnraidNetCert;
124150

125-
var postobj = {
126-
"#cfg": "/boot/config/plugins/dynamix.my.servers/myservers.cfg",
127-
...(computedRemoteAccessConfig ? computedRemoteAccessConfig : {}),
128-
// only allow 'yes' value when fields are enabled
129-
"local_2Fa": enableLocalT2fa ? $('#local2fa').val() : 'no',
130-
"remote_2Fa": enableRemoteT2fa ? $('#remote2fa').val() : 'no',
131-
};
151+
const postobj = {
152+
"#cfg": "/boot/config/plugins/dynamix.my.servers/myservers.cfg",
153+
...(computedRemoteAccessConfig ? computedRemoteAccessConfig : {}),
154+
// only allow 'yes' value when fields are enabled
155+
"local_2Fa": enableLocalT2fa ? $('#local2fa').val() : 'no',
156+
"remote_2Fa": enableRemoteT2fa ? $('#remote2fa').val() : 'no',
157+
};
158+
159+
const buildConnectSettingsInput = () => {
160+
const selection = $remoteAccessInput.val();
161+
switch (selection) {
162+
case 'ALWAYS_MANUAL':
163+
return { accessType: 'ALWAYS', forwardType: 'STATIC', port: parsePort($remoteAccessManualPort.val()) };
164+
case 'ALWAYS_UPNP':
165+
return { accessType: 'ALWAYS', forwardType: 'UPNP', port: null };
166+
case 'DYNAMIC_UPNP':
167+
return { accessType: 'DYNAMIC', forwardType: 'UPNP', port: null };
168+
case 'DYNAMIC_MANUAL':
169+
return { accessType: 'DYNAMIC', forwardType: 'STATIC', port: parsePort($remoteAccessManualPort.val()) };
170+
default:
171+
return { accessType: 'DISABLED', forwardType: 'STATIC', port: null };
172+
}
173+
};
174+
175+
const $button = $(button);
176+
const originalLabel = $button.html();
177+
$button.prop("disabled", true).html("_(Applying)_ <i class=\"fa fa-spinner fa-spin\" aria-hidden=\"true\"></i>");
178+
const saveLegacyConfig = new Promise((resolve, reject) => {
179+
$.post('/webGui/include/Dispatcher.php', postobj).done(resolve).fail(reject);
180+
});
132181

133-
$(button).prop("disabled", true).html("_(Applying)_ <i class=\"fa fa-spinner fa-spin\" aria-hidden=\"true\"></i>");
134-
$.post('/webGui/include/Dispatcher.php', postobj, function(data2) {
182+
const apolloClient = window.apolloClient;
183+
const gql = window.gql || window.graphqlParse;
184+
185+
const mutations = [saveLegacyConfig];
186+
if (apolloClient && gql) {
187+
const updateConnectSettingsMutation = gql(`
188+
mutation UpdateConnectSettings($input: ConnectSettingsInput!) {
189+
updateApiSettings(input: $input) {
190+
accessType
191+
forwardType
192+
port
193+
}
194+
}
195+
`);
196+
197+
mutations.push(
198+
apolloClient.mutate({
199+
mutation: updateConnectSettingsMutation,
200+
variables: { input: buildConnectSettingsInput() },
201+
})
202+
);
203+
}
204+
205+
Promise.all(mutations).then(function() {
135206
<?if(!$isRegistered):?>
136207
swal({
137208
title: "",
@@ -150,7 +221,22 @@ function registerServer(button) {
150221
button.form.submit();
151222
}, delay);
152223
<?endif?>
153-
});
224+
}).catch(function(error) {
225+
let message = "_(Sorry, an error occurred)_";
226+
if (error && error.responseJSON && error.responseJSON.error) {
227+
message = error.responseJSON.error;
228+
} else if (error && error.message) {
229+
message = error.message;
230+
}
231+
$button.prop("disabled", false).html(originalLabel);
232+
swal({
233+
title: "Oops",
234+
text: message,
235+
type: "error",
236+
html: true,
237+
confirmButtonText: "_(Ok)_"
238+
});
239+
});
154240

155241
}
156242

@@ -196,7 +282,7 @@ function dnsCheckServer(button) {
196282
} else {
197283
swal({
198284
title: "Oops",
199-
text: "<?=sprintf(_("The Unraid server is unreachable from outside your network. Be sure you have configured your router to forward port") . " <strong style='font-weight: bold'>%u/TCP</strong> " . _("to the Unraid server at") . " <strong style='font-weight: bold'>%s</strong> " . _("port") . " <strong style='font-weight: bold'>%u</strong>", $myServersFlashCfg['remote']['wanport']??"", htmlspecialchars($eth0['IPADDR:0']??''), $var['PORTSSL']??443)?>",
285+
text: "<?=sprintf(_("The Unraid server is unreachable from outside your network. Be sure you have configured your router to forward port") . " <strong style='font-weight: bold'>%u/TCP</strong> " . _("to the Unraid server at") . " <strong style='font-weight: bold'>%s</strong> " . _("port") . " <strong style='font-weight: bold'>%u</strong>", $remoteWanPort, htmlspecialchars($eth0['IPADDR:0']??''), $var['PORTSSL']??443)?>",
200286
type: "error",
201287
html: true,
202288
confirmButtonText: "_(Ok)_"

0 commit comments

Comments
 (0)