mirror of
https://github.com/unraid/api.git
synced 2025-12-31 05:29:48 -06:00
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 -->
This commit is contained in:
@@ -15,12 +15,29 @@ Tag="globe"
|
||||
*/
|
||||
require_once "$docroot/plugins/dynamix.my.servers/include/state.php";
|
||||
require_once "$docroot/plugins/dynamix.my.servers/include/api-config.php";
|
||||
require_once "$docroot/plugins/dynamix.my.servers/include/connect-config.php";
|
||||
require_once "$docroot/webGui/include/Wrappers.php";
|
||||
$serverState = new ServerState();
|
||||
|
||||
$keyfile = $serverState->keyfileBase64;
|
||||
|
||||
$myServersFlashCfg = $serverState->myServersFlashCfg;
|
||||
$connectConfig = ConnectConfig::getConfig();
|
||||
$legacyRemoteCfg = $serverState->myServersFlashCfg['remote'] ?? [];
|
||||
|
||||
$remoteDynamicRemoteAccessType = $connectConfig['dynamicRemoteAccessType'] ?? ($legacyRemoteCfg['dynamicRemoteAccessType'] ?? null);
|
||||
$remoteWanAccessRaw = $connectConfig['wanaccess'] ?? ($legacyRemoteCfg['wanaccess'] ?? null);
|
||||
$remoteUpnpEnabledRaw = $connectConfig['upnpEnabled'] ?? ($legacyRemoteCfg['upnpEnabled'] ?? null);
|
||||
$remoteWanPortRaw = $connectConfig['wanport'] ?? ($legacyRemoteCfg['wanport'] ?? null);
|
||||
|
||||
$wanaccessEnabled = filter_var($remoteWanAccessRaw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
if ($wanaccessEnabled === null) {
|
||||
$wanaccessEnabled = false;
|
||||
}
|
||||
$upnpEnabled = filter_var($remoteUpnpEnabledRaw, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
||||
if ($upnpEnabled === null) {
|
||||
$upnpEnabled = false;
|
||||
}
|
||||
$remoteWanPort = is_numeric($remoteWanPortRaw) ? (int)$remoteWanPortRaw : 0;
|
||||
|
||||
$showT2Fa = (file_exists('/boot/config/plugins/dynamix.my.servers/showT2Fa'));
|
||||
|
||||
@@ -37,9 +54,7 @@ $passwd_result = exec('/usr/bin/passwd --status root');
|
||||
$boolWebUIAuth = $isRegistered && (($passwd_result !== false) && (substr($passwd_result, 0, 6) == 'root P'));
|
||||
|
||||
// Helper to determine the current value for the remote access input
|
||||
$dynamicRemoteAccessType = $myServersFlashCfg['remote']['dynamicRemoteAccessType'] ?? null;
|
||||
$upnpEnabled = ($myServersFlashCfg['remote']['upnpEnabled'] ?? null) === 'yes';
|
||||
$wanaccessEnabled = ($myServersFlashCfg['remote']['wanaccess'] ?? null) === 'yes';
|
||||
$dynamicRemoteAccessType = $remoteDynamicRemoteAccessType ?? null;
|
||||
|
||||
$currentRemoteAccessValue = 'OFF';
|
||||
if ($dynamicRemoteAccessType === 'STATIC') {
|
||||
@@ -59,6 +74,12 @@ if ($dynamicRemoteAccessType === 'STATIC') {
|
||||
$enableRemoteT2fa = $showT2Fa && $currentRemoteAccessValue !== 'OFF' && $hasMyUnraidNetCert;
|
||||
$enableLocalT2fa = $showT2Fa && $var['USE_SSL'] === 'auto' && $hasMyUnraidNetCert;
|
||||
$shade="shade-".($display['theme']??'unk');
|
||||
$wanAccessOriginal = $remoteWanAccessRaw;
|
||||
if (is_bool($wanAccessOriginal)) {
|
||||
$wanAccessOriginal = $wanAccessOriginal ? 'yes' : 'no';
|
||||
} elseif (!is_string($wanAccessOriginal)) {
|
||||
$wanAccessOriginal = '';
|
||||
}
|
||||
?>
|
||||
<style>
|
||||
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}
|
||||
</style>
|
||||
<script>
|
||||
const hasMyUnraidNetCert = <?=($hasMyUnraidNetCert ? 'true' : 'false')?>;
|
||||
const wanAccessOrg = "<?=$myServersFlashCfg['remote']['wanaccess'] ?? null?>";
|
||||
const wanAccessOrg = "<?=$wanAccessOriginal?>";
|
||||
|
||||
function registerServer(button) {
|
||||
|
||||
const $remoteAccessInput = $('#remoteAccess');
|
||||
const $remoteAccessManualPort = $('#wanport');
|
||||
|
||||
const parsePort = (val) => {
|
||||
const parsed = parseInt(val, 10);
|
||||
return isNaN(parsed) ? null : parsed;
|
||||
};
|
||||
|
||||
let computedRemoteAccessConfig = null;
|
||||
switch ($remoteAccessInput.val()) {
|
||||
case 'ALWAYS_MANUAL':
|
||||
@@ -119,19 +145,64 @@ function registerServer(button) {
|
||||
break;
|
||||
}
|
||||
|
||||
const enableLocalT2fa = <?=($enableLocalT2fa ? 'true' : 'false')?>;
|
||||
const enableRemoteT2fa = $remoteAccessInput.val() !== 'OFF' && hasMyUnraidNetCert;
|
||||
const enableLocalT2fa = <?=($enableLocalT2fa ? 'true' : 'false')?>;
|
||||
const enableRemoteT2fa = $remoteAccessInput.val() !== 'OFF' && hasMyUnraidNetCert;
|
||||
|
||||
var postobj = {
|
||||
"#cfg": "/boot/config/plugins/dynamix.my.servers/myservers.cfg",
|
||||
...(computedRemoteAccessConfig ? computedRemoteAccessConfig : {}),
|
||||
// only allow 'yes' value when fields are enabled
|
||||
"local_2Fa": enableLocalT2fa ? $('#local2fa').val() : 'no',
|
||||
"remote_2Fa": enableRemoteT2fa ? $('#remote2fa').val() : 'no',
|
||||
};
|
||||
const postobj = {
|
||||
"#cfg": "/boot/config/plugins/dynamix.my.servers/myservers.cfg",
|
||||
...(computedRemoteAccessConfig ? computedRemoteAccessConfig : {}),
|
||||
// only allow 'yes' value when fields are enabled
|
||||
"local_2Fa": enableLocalT2fa ? $('#local2fa').val() : 'no',
|
||||
"remote_2Fa": enableRemoteT2fa ? $('#remote2fa').val() : 'no',
|
||||
};
|
||||
|
||||
$(button).prop("disabled", true).html("_(Applying)_ <i class=\"fa fa-spinner fa-spin\" aria-hidden=\"true\"></i>");
|
||||
$.post('/webGui/include/Dispatcher.php', postobj, function(data2) {
|
||||
const buildConnectSettingsInput = () => {
|
||||
const selection = $remoteAccessInput.val();
|
||||
switch (selection) {
|
||||
case 'ALWAYS_MANUAL':
|
||||
return { accessType: 'ALWAYS', forwardType: 'STATIC', port: parsePort($remoteAccessManualPort.val()) };
|
||||
case 'ALWAYS_UPNP':
|
||||
return { accessType: 'ALWAYS', forwardType: 'UPNP', port: null };
|
||||
case 'DYNAMIC_UPNP':
|
||||
return { accessType: 'DYNAMIC', forwardType: 'UPNP', port: null };
|
||||
case 'DYNAMIC_MANUAL':
|
||||
return { accessType: 'DYNAMIC', forwardType: 'STATIC', port: parsePort($remoteAccessManualPort.val()) };
|
||||
default:
|
||||
return { accessType: 'DISABLED', forwardType: 'STATIC', port: null };
|
||||
}
|
||||
};
|
||||
|
||||
const $button = $(button);
|
||||
const originalLabel = $button.html();
|
||||
$button.prop("disabled", true).html("_(Applying)_ <i class=\"fa fa-spinner fa-spin\" aria-hidden=\"true\"></i>");
|
||||
const saveLegacyConfig = new Promise((resolve, reject) => {
|
||||
$.post('/webGui/include/Dispatcher.php', postobj).done(resolve).fail(reject);
|
||||
});
|
||||
|
||||
const apolloClient = window.apolloClient;
|
||||
const gql = window.gql || window.graphqlParse;
|
||||
|
||||
const mutations = [saveLegacyConfig];
|
||||
if (apolloClient && gql) {
|
||||
const updateConnectSettingsMutation = gql(`
|
||||
mutation UpdateConnectSettings($input: ConnectSettingsInput!) {
|
||||
updateApiSettings(input: $input) {
|
||||
accessType
|
||||
forwardType
|
||||
port
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
mutations.push(
|
||||
apolloClient.mutate({
|
||||
mutation: updateConnectSettingsMutation,
|
||||
variables: { input: buildConnectSettingsInput() },
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
Promise.all(mutations).then(function() {
|
||||
<?if(!$isRegistered):?>
|
||||
swal({
|
||||
title: "",
|
||||
@@ -150,7 +221,22 @@ function registerServer(button) {
|
||||
button.form.submit();
|
||||
}, delay);
|
||||
<?endif?>
|
||||
});
|
||||
}).catch(function(error) {
|
||||
let message = "_(Sorry, an error occurred)_";
|
||||
if (error && error.responseJSON && error.responseJSON.error) {
|
||||
message = error.responseJSON.error;
|
||||
} else if (error && error.message) {
|
||||
message = error.message;
|
||||
}
|
||||
$button.prop("disabled", false).html(originalLabel);
|
||||
swal({
|
||||
title: "Oops",
|
||||
text: message,
|
||||
type: "error",
|
||||
html: true,
|
||||
confirmButtonText: "_(Ok)_"
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -196,7 +282,7 @@ function dnsCheckServer(button) {
|
||||
} else {
|
||||
swal({
|
||||
title: "Oops",
|
||||
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)?>",
|
||||
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)?>",
|
||||
type: "error",
|
||||
html: true,
|
||||
confirmButtonText: "_(Ok)_"
|
||||
|
||||
Reference in New Issue
Block a user