fix: admin/internet: better error messages for "verify"
Massimo Melina committed
Nov 9, 2024 at 14:34 UTC
795a0b8f129e0ee0a8cb51e6c3987cde05d6ecab
2 files changed
+17
-16
admin/src/InternetPage.ts
+16
-15
@@ -3,8 +3,10 @@ import { Alert, Box, Button, Card, CardContent, CircularProgress, Divider, Linea
3
import { CardMembership, Check, Dns, HomeWorkTwoTone, Lock, Public, PublicTwoTone, RouterTwoTone, Send, Storage,
4
Error as ErrorIcon, SvgIconComponent } from '@mui/icons-material'
5
import { apiCall, useApiEvents, useApiEx } from './api'
6
-import { closeDialog, DAY, formatTimestamp, wait, wantArray, with_, PORT_DISABLED, isIP, CFG, md,
7
- useRequestRender, replace, restartAnimation, prefix } from './misc'
6
+import {
7
+ closeDialog, DAY, formatTimestamp, wait, wantArray, with_, PORT_DISABLED, isIP, CFG, md,
8
+ useRequestRender, replace, restartAnimation, prefix, isIpLan
9
+} from './misc'
10
import { Flex, LinkBtn, Btn, Country, wikiLink } from './mui'
11
import { alertDialog, confirmDialog, formDialog, promptDialog, toast, waitDialog } from './dialog'
12
import { BoolField, Form, MultiSelectField, NumberField, SelectField } from '@hfs/mui-grid-form'
@@ -32,6 +34,7 @@ export default function InternetPage({ setTitleSide }: PageProps) {
34
const [mapping, setMapping] = useState(false)
35
const status = useApiEx('get_status')
36
const config = useApiEx('get_config', { only: ['base_url'] })
37
+ const base_url = config.data?.base_url
38
const localColor = with_([status.data?.http?.error, status.data?.https?.error], ([h, s]) =>
39
h && s ? 'error' : h || s ? 'warning' : 'success')
40
type GetNat = Awaited<ReturnType<typeof getNatInfo>>
@@ -243,11 +246,10 @@ export default function InternetPage({ setTitleSide }: PageProps) {
246
}
247
248
function baseUrlBox() {
246
- const url = config.data?.base_url
249
return config.element || h(TitleCard, { icon: Public, title: "Address" },
250
h(Flex, { flexWrap: 'wrap' },
251
"Main address: ",
250
- url ? h('tt', {}, url) : "automatic, not configured",
252
+ base_url ? h('tt', {}, base_url) : "automatic, not configured",
253
h(Btn, {
254
size: 'small',
255
variant: 'outlined',
@@ -309,7 +311,7 @@ export default function InternetPage({ setTitleSide }: PageProps) {
311
doubleNat && h(LinkBtn, { display: 'block', onClick: () => alertDialog(MSG_ISP, 'warning') }, "Double NAT"),
312
checkResult ? "Working!" : checkResult === false ? "Failed!" : '',
313
' ',
312
- publicIps.length > 0 && data.internalPort && h(LinkBtn, { onClick: () => verify() }, "Verify")
314
+ (base_url > '' || publicIps.length > 0) && data.internalPort && h(LinkBtn, { onClick: () => verify() }, "Verify")
315
)
316
}),
317
)
@@ -328,25 +330,24 @@ export default function InternetPage({ setTitleSide }: PageProps) {
330
if (!again && !await confirmDialog("This test will check if your server is working properly on the Internet")) return
331
setChecking(true)
332
try {
331
- const url = config.data?.base_url
332
- {
333
- const hostname = url && new URL(url).hostname
334
- const domain = !isIP(hostname) && hostname
335
- if (await stopOnCheckDomain(domain)) return
336
- }
337
- const urlResult = url && await apiCall('self_check', { url }).catch(() =>
338
- alertDialog(md(`Sorry, we couldn't verify your configured address ${url} 😰\nstill, we are going to test your IP address 🤞`), 'warning'))
333
+ const hostname = base_url && new URL(base_url).hostname
334
+ const checkUrl = !isIpLan(hostname) && base_url
335
+ if (!isIP(hostname) && await stopOnCheckDomain(hostname)) return
336
+ const urlResult = checkUrl && await apiCall('self_check', { url: checkUrl }).catch(e =>
337
+ alertDialog(!e.code ? e : "Sorry, this function is not available at the moment. Retry later.", 'error'))
338
+ if (checkUrl && !urlResult)
339
+ return
340
if (urlResult?.success) {
341
setCheckResult(true)
342
return alertDialog(h(Box, {}, "Your server is responding correctly over the Internet:",
343
h('ul', {}, h('li', {}, urlResult.url))), 'success')
344
}
345
if (urlResult?.success === false)
345
- await alertDialog(md(`Your configured address ${url} doesn't seem to work 😰\nstill, we are going to test your IP address 🤞`), 'warning')
346
+ await alertDialog(md(`Your configured address ${checkUrl} doesn't seem to work 😰\nstill, we are going to test your IP address 🤞`), 'warning')
347
const res = await apiCall('self_check', {})
348
if (res.some((x: any) => x.success)) {
349
setCheckResult(true)
349
- const mild = urlResult.success === false && md(`Your server is responding over the Internet 👍\nbut not with configured address ${url} 👎\njust on your IP:`)
350
+ const mild = urlResult.success === false && md(`Your server is responding over the Internet 👍\nbut not with configured address ${checkUrl} 👎\njust on your IP:`)
351
return alertDialog(h(Box, {}, mild || "Your server is responding correctly over the Internet:",
352
h('ul', {}, ...res.map((x: any) => h('li', {}, x.url)))), mild ? 'warning' : 'success')
353
}
shared/api.ts
+1
-1
@@ -60,7 +60,7 @@ export function apiCall<T=any>(cmd: string, params?: Dict, options: ApiCallOptio
60
stop?.()
61
if (err?.message?.includes('fetch')) {
62
console.error(err.message)
63
- throw Error("Network error")
63
+ throw Error("Server unreachable")
64
}
65
throw aborted || err
66
}).finally(() => clearTimeout(timeout)), {