Skip to content

Commit

Permalink
add txt record extract util, update logger for latest server-analytic…
Browse files Browse the repository at this point in the history
…s changes
  • Loading branch information
mdtanrikulu committed Apr 15, 2024
1 parent c0f0aaa commit 8b5583e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
39 changes: 39 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Check failure on line 52 in src/utils.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Delete `⏎`
46 changes: 25 additions & 21 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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 = {
Expand All @@ -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));
},
};

0 comments on commit 8b5583e

Please sign in to comment.