Skip to content

Commit

Permalink
fix: Fetch ipv4 first for public IP (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 authored Sep 8, 2023
1 parent cc8994b commit 7daaae4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-teachers-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

fix: Simplify IP addr fetching, prefering ipv4
36 changes: 10 additions & 26 deletions apps/hubble/src/utils/p2p.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GossipAddressInfo, HubAsyncResult, HubError, HubResult } from "@farcaster/hub-nodejs";
import { Multiaddr, NodeAddress, multiaddr } from "@multiformats/multiaddr";
import { get } from "http";
import { AddressInfo, isIP } from "net";
import { Result, err, ok } from "neverthrow";
import { logger } from "./logger.js";
import axios from "axios";

/** Parses an address to verify it is actually a valid MultiAddr */
export const parseAddress = (multiaddrStr: string): HubResult<Multiaddr> => {
Expand Down Expand Up @@ -144,33 +144,17 @@ export const addressInfoToString = (addressInfo: AddressInfo): string => {
}
};

let lastIpFetch = { timestamp: new Date().getTime(), ip: "" };

/** Returns a publicly visible IPv4 or IPv6 address of the running process */
export const getPublicIp = async (): HubAsyncResult<string> => {
// TODO: refactor to make this more readable, the nested promise/return/try-catch is hard to
// reason about
return new Promise((resolve, reject) => {
const now = new Date().getTime();
const since = now - lastIpFetch.timestamp;
if (since <= 10 * 60 * 1000 && lastIpFetch.ip !== "") {
logger.debug({ component: "utils/p2p", ip: lastIpFetch.ip }, "Cached public IP");
resolve(ok(lastIpFetch.ip));
return;
}
try {
get({ host: "api64.ipify.org", port: 80, path: "/" }, (resp) => {
resp.on("data", (ip: Buffer) => {
logger.info({ component: "utils/p2p", ip: ip.toString() }, "Fetched public IP");
lastIpFetch = { timestamp: now, ip: ip.toString() };
resolve(ok(ip.toString()));
});
});
// biome-ignore lint/suspicious/noExplicitAny: error catching
} catch (err: any) {
reject(new HubError("unavailable.network_failure", err));
}
});
try {
const response = await axios.get("http://api.ipify.org?format=text");
const ip = response.data;
logger.info({ component: "utils/p2p", ip }, "Fetched public IP");
return ok(ip);
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
} catch (error: any) {
return err(new HubError("unavailable.network_failure", error.message));
}
};

/* -------------------------------------------------------------------------- */
Expand Down

0 comments on commit 7daaae4

Please sign in to comment.