diff --git a/frontend/src/pages/index/BackupModal.tsx b/frontend/src/pages/index/BackupModal.tsx index 473d30592..fbc09ed04 100644 --- a/frontend/src/pages/index/BackupModal.tsx +++ b/frontend/src/pages/index/BackupModal.tsx @@ -61,7 +61,7 @@ export default function BackupModal({ // importDB schedules the panel restart server-side; wait it out, then reload. onBusy({ busy: true, tip: `${t('pages.settings.restartPanel')}…` }); - await PromiseUtil.sleep(5000); + await PromiseUtil.waitForPanel(window.location.href); window.location.reload(); }); fileInput.click(); diff --git a/frontend/src/pages/settings/SettingsPage.tsx b/frontend/src/pages/settings/SettingsPage.tsx index 7f42169c0..c666bafd1 100644 --- a/frontend/src/pages/settings/SettingsPage.tsx +++ b/frontend/src/pages/settings/SettingsPage.tsx @@ -150,8 +150,9 @@ export default function SettingsPage() { try { const msg = (await HttpUtil.post('/panel/api/setting/restartPanel')) as ApiMsg; if (!msg?.success) return; - await PromiseUtil.sleep(5000); - window.location.replace(rebuildUrlAfterRestart()); + const target = rebuildUrlAfterRestart(); + await PromiseUtil.waitForPanel(target); + window.location.replace(target); } finally { setSpinning(false); } diff --git a/frontend/src/test/wait-for-panel.test.ts b/frontend/src/test/wait-for-panel.test.ts new file mode 100644 index 000000000..c16101226 --- /dev/null +++ b/frontend/src/test/wait-for-panel.test.ts @@ -0,0 +1,78 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { PromiseUtil } from '@/utils'; + +const fast = { delay: 0, interval: 1, timeout: 200 }; + +function answer(status: number, type: ResponseType = 'basic'): Response { + return { status, type } as Response; +} + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +describe('PromiseUtil.waitForPanel (#7190)', () => { + it('keeps polling through refused connections and proxy 502s until the panel answers', async () => { + vi.stubGlobal('window', { + location: { href: 'https://panel.test/panel/settings', origin: 'https://panel.test' }, + }); + const fetchMock = vi + .fn() + .mockRejectedValueOnce(new TypeError('Failed to fetch')) + .mockResolvedValueOnce(answer(502)) + .mockResolvedValueOnce(answer(200)); + vi.stubGlobal('fetch', fetchMock); + + await expect(PromiseUtil.waitForPanel('https://panel.test/panel/settings', fast)).resolves.toBe( + true, + ); + expect(fetchMock).toHaveBeenCalledTimes(3); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'same-origin', cache: 'no-store' }); + }); + + it('gives up after the timeout so the caller still navigates', async () => { + vi.stubGlobal('window', { + location: { href: 'https://panel.test/', origin: 'https://panel.test' }, + }); + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(answer(502))); + + await expect( + PromiseUtil.waitForPanel('https://panel.test/', { ...fast, timeout: 20 }), + ).resolves.toBe(false); + }); + + it('probes a new port or protocol with no-cors and accepts the opaque answer', async () => { + vi.stubGlobal('window', { + location: { + href: 'http://203.0.113.5:2053/panel/settings', + origin: 'http://203.0.113.5:2053', + }, + }); + const fetchMock = vi.fn().mockResolvedValue(answer(0, 'opaque')); + vi.stubGlobal('fetch', fetchMock); + + await expect( + PromiseUtil.waitForPanel('https://203.0.113.5:8443/panel/settings', fast), + ).resolves.toBe(true); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'no-cors' }); + }); + + it('probes the current origin when an https page would open an http target (mixed content)', async () => { + vi.stubGlobal('window', { + location: { + href: 'https://panel.test/panel/settings', + origin: 'https://panel.test', + protocol: 'https:', + }, + }); + const fetchMock = vi.fn().mockResolvedValue(answer(200)); + vi.stubGlobal('fetch', fetchMock); + + await expect(PromiseUtil.waitForPanel('http://panel.test/panel/settings', fast)).resolves.toBe( + true, + ); + expect(String(fetchMock.mock.calls[0][0])).toBe('https://panel.test/panel/settings'); + expect(fetchMock.mock.calls[0][1]).toMatchObject({ mode: 'same-origin' }); + }); +}); diff --git a/frontend/src/utils/index.ts b/frontend/src/utils/index.ts index e3f781620..84320e704 100644 --- a/frontend/src/utils/index.ts +++ b/frontend/src/utils/index.ts @@ -180,6 +180,35 @@ export class PromiseUtil { setTimeout(resolve, timeout); }); } + + // After the restart delay, polls the target until it answers below 500; false on timeout. + // https page -> http target is blocked as mixed content: probe this origin, the proxy's. + static async waitForPanel( + url: string, + { delay = 5000, interval = 2000, timeout = 90_000 } = {}, + ): Promise { + await PromiseUtil.sleep(delay); + const target = new URL(url, window.location.href); + const mixed = window.location.protocol === 'https:' && target.protocol === 'http:'; + const probe = mixed ? new URL(window.location.href) : target; + const sameOrigin = probe.origin === window.location.origin; + const deadline = Date.now() + timeout; + while (Date.now() < deadline) { + try { + const res = await fetch(probe, { + cache: 'no-store', + redirect: 'manual', + mode: sameOrigin ? 'same-origin' : 'no-cors', + signal: AbortSignal.timeout(Math.max(interval, 1000)), + }); + if (res.type === 'opaque' || res.type === 'opaqueredirect' || res.status < 500) return true; + } catch { + /* not listening yet */ + } + await PromiseUtil.sleep(interval); + } + return false; + } } export interface RandomSeqOptions {