admin/config: automatic redirect on https when switching off http

Massimo Melina committed Mar 1, 2023 at 11:19 UTC 4337e4819cc49f95f9304d0af0c09739ef87d0cd
1 file changed +23 -7
admin/src/ConfigPage.ts
+23 -7
@@ -146,17 +146,33 @@ export default function ConfigPage() {
146 if (_.isEmpty(changes))
147 return toast("Nothing to save")
148 const loc = window.location
149 - const newPort = loc.protocol === 'http:' ? changes.port : changes.https_port
150 - if (newPort <= 0 && !await confirmDialog("You are switching off the server port and you will be disconnected"))
151 - return
152 - else if (newPort > 0 && !await confirmDialog("You are changing the port and you may be disconnected"))
149 + const keys = ['port','https_port']
150 + if (keys.every(k => changes[k] !== undefined))
151 + return alertDialog("You cannot change both http and https port at once. Please, do one, save, and then do the other.", 'warning')
152 + const working = [status?.http?.listening, status?.https?.listening]
153 + const onHttps = location.protocol === 'https:'
154 + if (onHttps) {
155 + keys.reverse()
156 + working.reverse()
157 + }
158 + const newPort = changes[keys[0]]
159 + const otherPort = values[keys[1]]
160 + const otherIsReliable = otherPort > 0 && working[1]
161 + const otherProtocol = onHttps ? 'http' : 'https'
162 + if (newPort < 0 && !otherIsReliable)
163 + return alertDialog("You cannot switch off this port unless you have a working fixed port for " + otherProtocol, 'warning')
164 + if (newPort === 0 && !otherIsReliable)
165 + return alertDialog("You cannot randomize this port unless you have a working fixed port for " + otherProtocol, 'warning')
166 + if (newPort > 0 && !await confirmDialog("You are changing the port and you may be disconnected"))
167 return
154 - if (loc.protocol === 'https:' && ('cert' in changes || 'private_key' in changes) && !await confirmDialog("You may disrupt https service, kicking you out"))
168 + if (onHttps && ('cert' in changes || 'private_key' in changes) && !await confirmDialog("You may disrupt https service, kicking you out"))
169 return
170 await apiCall('set_config', { values: changes })
157 - if (newPort > 0) {
171 + if (newPort !== undefined) {
172 await alertDialog("You are being redirected but in some cases this may fail. Hold on tight!", 'warning')
159 - return window.location.href = loc.protocol + '//' + loc.hostname + ':' + newPort + loc.pathname
173 + // we have to jump protocol also in case of random port, because we want people to know their port while using GUI
174 + return window.location.href = newPort <= 0 ? (onHttps ? 'http:' : 'https:') + '//' + loc.hostname + ':' + otherPort + loc.pathname
175 + : loc.protocol + '//' + loc.hostname + ':' + newPort + loc.pathname
176 }
177 setTimeout(reloadStatus, 'port' in changes || 'https_port' in changes ? 1000 : 0) // give some time to consider new ports
178 Object.assign(loaded!, changes) // since changes are recalculated subscribing state.config, but it depends on 'loaded' to (which cannot be subscribed), be sure to update loaded first