From ff12d2eac5cbd99118c1663c7952f4bea3776de9 Mon Sep 17 00:00:00 2001 From: 0xKurt Date: Tue, 10 Sep 2024 15:05:58 +0200 Subject: [PATCH 1/4] fetch external ip address of indexer --- src/index.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/index.ts b/src/index.ts index e7f52d22..03f2063c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -121,6 +121,8 @@ async function main(): Promise { return decodeJsonWithBigInts(val); }); + await getExternalIP(baseLogger); + if (config.cacheDir) { if (config.removeCache) { await fs.rm(config.cacheDir, { recursive: true }); @@ -690,3 +692,28 @@ function monitorAndLogResources(config: { resourceMonitor.start(); } + +const getExternalIP = async (logger: Logger): Promise => { + const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"]; + for (const url of urls) { + try { + logger.debug(`Attempting to fetch IP address from: ${url}`); + const response = await fetch(url); + if (response.ok) { + const { ip } = await response.json(); + logger.info(`Successfully fetched IP address from: ${url}`); + return ip; + } + throw new Error(`Request failed with status: ${response.status}`); + } catch (error) { + if (error instanceof Error) { + logger.error(`Failed to fetch from ${url}: ${error.message}`); + } else { + logger.error(`Failed to fetch from ${url}: ${error}`); + } + } + } + throw new Error( + "Unable to fetch external IP address from both primary and fallback URLs." + ); +}; From a0159a0b69b456eed11a2c88afc1fe7345155a4c Mon Sep 17 00:00:00 2001 From: 0xKurt Date: Tue, 10 Sep 2024 15:09:27 +0200 Subject: [PATCH 2/4] fix log --- src/index.ts | 29 ++--------------------------- src/utils/index.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index 03f2063c..57a44be0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,7 +39,7 @@ import type { EventHandlerContext } from "./indexer/indexer.js"; import { handleEvent as handleAlloV1Event } from "./indexer/allo/v1/handleEvent.js"; import { handleEvent as handleAlloV2Event } from "./indexer/allo/v2/handleEvent.js"; import { Database } from "./database/index.js"; -import { decodeJsonWithBigInts } from "./utils/index.js"; +import { decodeJsonWithBigInts, getExternalIP } from "./utils/index.js"; import { Block } from "chainsauce/dist/cache.js"; import { createPublicClient, http } from "viem"; import { IndexerEvents } from "chainsauce/dist/indexer.js"; @@ -691,29 +691,4 @@ function monitorAndLogResources(config: { }); resourceMonitor.start(); -} - -const getExternalIP = async (logger: Logger): Promise => { - const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"]; - for (const url of urls) { - try { - logger.debug(`Attempting to fetch IP address from: ${url}`); - const response = await fetch(url); - if (response.ok) { - const { ip } = await response.json(); - logger.info(`Successfully fetched IP address from: ${url}`); - return ip; - } - throw new Error(`Request failed with status: ${response.status}`); - } catch (error) { - if (error instanceof Error) { - logger.error(`Failed to fetch from ${url}: ${error.message}`); - } else { - logger.error(`Failed to fetch from ${url}: ${error}`); - } - } - } - throw new Error( - "Unable to fetch external IP address from both primary and fallback URLs." - ); -}; +} \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index efeebb26..6cbf0a18 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,8 @@ // TODO: why is eslint not recognizing type narrowing? /* eslint-disable @typescript-eslint/no-unsafe-argument */ + +import { Logger } from "pino"; + /* eslint-disable @typescript-eslint/no-unsafe-member-access */ export function encodeJsonWithBigInts(value: unknown): string { return JSON.stringify(value, (_key, value) => { @@ -31,3 +34,28 @@ export const UINT64_MAX = 18446744073709551615n; export const getDateFromTimestamp = (timestamp: bigint): Date | null => { return timestamp < UINT64_MAX ? new Date(Number(timestamp) * 1000) : null; }; + +export const getExternalIP = async (logger: Logger): Promise => { + const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"]; + for (const url of urls) { + try { + logger.debug(`Attempting to fetch IP address from: ${url}`); + const response = await fetch(url); + if (response.ok) { + const { ip } = await response.json(); + logger.info(`Successfully fetched IP address: ${ip}`); + return ip; + } + throw new Error(`Request failed with status: ${response.status}`); + } catch (error) { + if (error instanceof Error) { + logger.error(`Failed to fetch from ${url}: ${error.message}`); + } else { + logger.error(`Failed to fetch from ${url}: ${error}`); + } + } + } + throw new Error( + "Unable to fetch external IP address from both primary and fallback URLs." + ); +}; From e1b47bca0aa1fcdddb2d0c6881e6b550620837c1 Mon Sep 17 00:00:00 2001 From: 0xKurt Date: Tue, 10 Sep 2024 15:41:38 +0200 Subject: [PATCH 3/4] lint --- src/utils/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 6cbf0a18..c13ebb28 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -42,7 +42,7 @@ export const getExternalIP = async (logger: Logger): Promise => { logger.debug(`Attempting to fetch IP address from: ${url}`); const response = await fetch(url); if (response.ok) { - const { ip } = await response.json(); + const { ip } = (await response.json()) as { ip: string }; logger.info(`Successfully fetched IP address: ${ip}`); return ip; } @@ -51,7 +51,7 @@ export const getExternalIP = async (logger: Logger): Promise => { if (error instanceof Error) { logger.error(`Failed to fetch from ${url}: ${error.message}`); } else { - logger.error(`Failed to fetch from ${url}: ${error}`); + logger.error(`Failed to fetch from ${url}`); } } } From b79efd41c878784963d7a0df32401b53bb02d225 Mon Sep 17 00:00:00 2001 From: 0xKurt Date: Tue, 10 Sep 2024 15:48:48 +0200 Subject: [PATCH 4/4] lint --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 57a44be0..5062a1aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -691,4 +691,4 @@ function monitorAndLogResources(config: { }); resourceMonitor.start(); -} \ No newline at end of file +}