From 8b5583e08ece79e819b6f7ec43efcd96864a7bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammed=20Tanr=C4=B1kulu?= Date: Mon, 15 Apr 2024 18:26:36 +0200 Subject: [PATCH] add txt record extract util, update logger for latest server-analytics changes --- src/utils.ts | 39 +++++++++++++++++++++++++++++++++++++++ src/worker.ts | 46 +++++++++++++++++++++++++--------------------- 2 files changed, 64 insertions(+), 21 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 6231de4..cf90da7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,3 +11,42 @@ export function serializeError(error: any) { return JSON.stringify(error); } } + +type DNSRecord = { + rrset: string; + sig: string; +}; + +function hexToAscii(hex: string): string { + let result = ''; + for (let i = 0; i < hex.length; i += 2) { + const part = parseInt(hex.substring(i, i + 2), 16); + if (part) result += String.fromCharCode(part); + } + return result; +} + +export function extractENSRecord(dnsRecords: DNSRecord[]): string[] { + const txtPrefix = '0x0010'; // 16 + const txtRecords: string[] = []; + + for (const record of dnsRecords) { + if (record.rrset.startsWith(txtPrefix)) { + const contentStart = txtPrefix.length; + const rawContent = record.rrset.slice(contentStart); + let asciiContent = hexToAscii(rawContent); + + asciiContent = asciiContent.split('\t').join(); + + const parts = asciiContent.split(','); + for (const part of parts) { + if (part.includes('ENS1')) { + txtRecords.push(part.slice(2)); + } + } + } + } + + return txtRecords; +} + diff --git a/src/worker.ts b/src/worker.ts index 7f7ebd7..349ea80 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -5,13 +5,25 @@ import { import { Server } from '@ensdomains/ccip-read-cf-worker'; import { Tracker } from '@ensdomains/server-analytics'; import { dohQuery } from '@ensdomains/dnsprovejs'; +import { ethers } from 'ethers'; import { makeApp } from './app'; +import { extractENSRecord } from './utils'; interface ENV { DOH_GATEWAY_URL: string; PLAUSIBLE_BASE_URL: string; } +const abi_RRSetWithSignature = [ + ethers.utils.ParamType.from({ + components: [ + { type: 'bytes', name: 'rrset' }, + { type: 'bytes', name: 'sig' }, + ], + type: 'tuple[]', + }), +]; + const tracker = new Tracker('ccip-read-dns-worker.ens-cf.workers.dev', { enableLogging: true, }); @@ -28,26 +40,16 @@ const routeHandler = (env: ENV, trackEvent?: Function) => { return app; }; -const logResult = async (request: CFWRequest, result: Response) => { - if (!result.body) { - return result; - } - const [streamForLog, streamForResult] = result.body.tee(); - try { - const resultForLog: { data: string } = await new Response( - streamForLog - ).json(); - - await tracker.trackEvent( - request, - 'result', - { props: { result: resultForLog.data.substring(0, 200) } }, - true - ); - } catch (error) { - console.log('error logging result:', error); - } - return new Response(streamForResult, result); +const dataDecoder = async (data: string) => { + const decodedData = ethers.utils.defaultAbiCoder.decode( + abi_RRSetWithSignature, + data + )[0]; + const structuredData = decodedData.map((item: string[]) => ({ + rrset: item[0], + sig: item[1], + })); + return extractENSRecord(structuredData); }; module.exports = { @@ -62,6 +64,8 @@ module.exports = { await tracker.trackEvent(request, 'request', {}, true); await tracker.trackPageview(request, {}, true); const router = routeHandler(env, tracker.trackEvent.bind(tracker, request)); - return router.handle(request).then(logResult.bind(this, request)); + return router + .handle(request) + .then(tracker.logResult.bind(this, request, dataDecoder)); }, };