Use announce address to check the TCP port

This commit is contained in:
Arnaud 2024-11-21 09:27:08 +01:00
parent 37592fcdd5
commit 602516436a
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
2 changed files with 20 additions and 4 deletions

View File

@ -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<number> {
if (info.addrs.length === 0) {

View File

@ -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<PortForwardingResponse> => {
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
);
}
},