From 602516436a6ec2bf3872e3465a0f8381c324fbef Mon Sep 17 00:00:00 2001 From: Arnaud Date: Thu, 21 Nov 2024 09:27:08 +0100 Subject: [PATCH] Use announce address to check the TCP port --- src/hooks/port-forwarding.util.ts | 10 ++++++++-- src/hooks/usePortForwarding.tsx | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/hooks/port-forwarding.util.ts b/src/hooks/port-forwarding.util.ts index 6833ca9..a5951a7 100644 --- a/src/hooks/port-forwarding.util.ts +++ b/src/hooks/port-forwarding.util.ts @@ -1,8 +1,14 @@ import { CodexDebugInfo, SafeValue, CodexError } from "@codex-storage/sdk-js" export const PortForwardingUtil = { - check: (port: number) => fetch(import.meta.env.VITE_GEO_IP_URL + "/port/" + port) - .then((res) => res.json()), + check: ([ip, port]: [string, number]) => { + const headers = { + "X-Real-IP-Custom": ip + } + + return fetch(import.meta.env.VITE_GEO_IP_URL + "/port/" + port, { headers }) + .then((res) => res.json()) + }, getTcpPort(info: CodexDebugInfo): SafeValue { if (info.addrs.length === 0) { diff --git a/src/hooks/usePortForwarding.tsx b/src/hooks/usePortForwarding.tsx index f77beb1..5159f7b 100644 --- a/src/hooks/usePortForwarding.tsx +++ b/src/hooks/usePortForwarding.tsx @@ -2,6 +2,7 @@ import { useQuery } from "@tanstack/react-query"; import { Errors } from "../utils/errors"; import { CodexDebugInfo } from "@codex-storage/sdk-js"; import { PortForwardingUtil } from "./port-forwarding.util"; +import { HealthCheckUtils } from "../components/HealthChecks/health-check.utils"; type PortForwardingResponse = { reachable: boolean }; @@ -9,12 +10,21 @@ export function usePortForwarding(info: CodexDebugInfo | undefined) { const { data, isFetching, refetch } = useQuery({ queryFn: (): Promise => { const port = PortForwardingUtil.getTcpPort(info!); + if (port.error) { Errors.report(port); return Promise.resolve({ reachable: false }); } else { - return PortForwardingUtil.check(port.data).catch((e) => - Errors.report(e) + const ip = HealthCheckUtils.extractAnnounceAddresses( + info!.announceAddresses + ); + if (ip.error) { + Errors.report(ip); + return Promise.resolve({ reachable: false }); + } + + return PortForwardingUtil.check([ip.data, port.data]).catch( + Errors.report ); } },