Skip to content

Commit

Permalink
update codebase, async logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Jan 21, 2025
1 parent 7f71305 commit c8b5a82
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 43 deletions.
83 changes: 61 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,81 @@
# CCIP-Read-DNS-Gateway: Your Gasless DNSSEC Support
# CCIP-Read-DNS-Gateway

Welcome aboard the CCIP-Read-DNS-Gateway! This isn't just any gateway, it's a special server that provides DNS resolution services for ENS domains, with a focus on gasless DNSSEC support.
## Overview

## Inside the Gateway
CCIP-Read-DNS-Gateway is a specialized server providing DNS resolution services for ENS domains with a focus on gasless DNSSEC support. It leverages the CCIP-Read protocol to offer efficient, off-chain DNS resolution and DNSSEC validation.

Let's take a quick look at what makes this gateway tick:
## Core Components

- `index.ts`: This is the starting point for our application. It sets up the DNS prover with a specified DoH gateway URL and gets the server up and running.
- `index.ts`: Express server for handling incoming requests. Mostly suitable for self-hosting or cloud development.
- `worker.ts`: Cloudflare worker script for handling incoming requests in a (Cloudflare Worker) serverless environment.

- `app.ts`: This is where the server gets its instructions. It sets up the server with the necessary routes and handlers, and uses the `@ensdomains/dnsprovejs` library to perform DNS queries and proofs.
- `app.ts`: Configures server routes and handlers. Utilizes the `@ensdomains/dnsprovejs` library for DNS queries and proof generation.

- `worker.ts`: This is our diligent Cloudflare worker, ready to handle incoming requests and direct them to the appropriate handlers. Useful for [getting started fast](#gateway-server).
## Installation

## Ready to Get Started?
```bash
git clone https://github.com/your-repo/ccip-read-dns-gateway.git
cd ccip-read-dns-gateway
npm install
```

### I will run in local
## Configuration

To run this server locally, you'll need to provide a DoH (DNS over HTTPS) gateway URL. Set the DOH_GATEWAY_URL environment variable and you're all set! The server will use this gateway to perform DNS queries.
Set the `DOH_GATEWAY_URL` environment variable to specify your DNS-over-HTTPS gateway:

Once you're ready, just run the index.ts file. You'll be up and running on port 8080 (default).
```bash
export DOH_GATEWAY_URL=https://your-doh-gateway.com/dns-query
```

### I will run as Cloudflare Worker
## Usage

If you want to run this as a Cloudflare Worker, the process is slightly different, but still super easy.
### Local Development

First, you'll need to install wrangler on your machine. You can do this by following the instructions [here](https://developers.cloudflare.com/workers/wrangler/install-and-update/#install-wrangler-globally).
To run the server locally:

Once wrangler is installed, use wrangler login to configure your account.
```bash
npm start
```

Next, navigate to the wrangler.toml file and update the DOH_GATEWAY_URL environment variable with the DoH server you want to use.
The server will start on `http://localhost:8080` by default.

Finally, run `wrangler publish` and voila! Your Cloudflare worker is up and running. Easy peasy!
### Cloudflare Worker Deployment

## Our Companions
1. Install Wrangler CLI:
```bash
npm install -g @cloudflare/wrangler
```

We've got some great companions on this journey:
2. Authenticate with your Cloudflare account:
```bash
wrangler login
```

- [`@ensdomains/dnsprovejs`](https://github.com/ensdomains/dnsprovejs): Our reliable ally for DNS proofs.
- [`@chainlink/ccip-read-server`](https://github.com/smartcontractkit/ccip-read): Sets the stage for a CCIP read server.
3. Update `wrangler.toml` with your DoH gateway URL:
```toml
[vars]
DOH_GATEWAY_URL = "https://your-doh-gateway.com/dns-query"
```

Remember, this is just a quick overview. For a deeper understanding, feel free to explore the source code. Happy coding!
4. Deploy the worker:
```bash
wrangler publish
```

## API Endpoints

- `GET /`: Health check endpoint
- `POST /`: Main endpoint for DNS resolution and proof generation

## Dependencies

- [`@ensdomains/dnsprovejs`](https://github.com/ensdomains/dnsprovejs): Library for DNS proof generation and validation
- [`@chainlink/ccip-read-server`](https://github.com/smartcontractkit/ccip-read): Framework for implementing CCIP-Read servers

## License

MIT

## Support

For support, please open an issue in the GitHub repository or contact our support team at [email protected].
1 change: 1 addition & 0 deletions ens-contracts
Submodule ens-contracts added at 0b6f79
32 changes: 18 additions & 14 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ export function makeApp(
}

if (trackEvent) {
trackEvent(
'resolve',
{
props: { name: decodedName, qtype: qTypes.toString(qtype) },
},
true
);
setTimeout(() => {
trackEvent(
'resolve',
{
props: { name: decodedName, qtype: qTypes.toString(qtype) },
},
true
).catch(console.error);
}, 0);
}

try {
Expand All @@ -68,13 +70,15 @@ export function makeApp(
return [ret];
} catch (error) {
if (trackEvent) {
trackEvent(
'error',
{
props: { name: decodedName, message: serializeError(error) },
},
true
);
setTimeout(() => {
trackEvent(
'error',
{
props: { name: decodedName, message: serializeError(error) },
},
true
).catch(console.error);
}, 0);
}

return emptyRRSet;
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export function serializeError(error: any) {
}
}

export const logAsync = (fn: Function, ...args: any[]) => {
setTimeout(() => {
Promise.resolve(fn(...args)).catch(err => {
console.error('Logging error:', err);
});
}, 0);
};

type DNSRecord = {
rrset: string;
sig: string;
Expand Down
18 changes: 12 additions & 6 deletions src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PropsDecoder, Tracker } from '@ensdomains/server-analytics';
import { dohQuery } from '@ensdomains/dnsprovejs';
import { ethers } from 'ethers';
import { makeApp } from './app';
import { extractENSRecord } from './utils';
import { extractENSRecord, logAsync } from './utils';

interface ENV {
DOH_GATEWAY_URL: string;
Expand Down Expand Up @@ -60,7 +60,7 @@ const propsDecoder: PropsDecoder<CFWRequest> = (
return { result: extractENSRecord(structuredData) };
};

module.exports = {
export default {
fetch: async function(
request: CFWRequest,
env: ENV,
Expand All @@ -69,11 +69,17 @@ module.exports = {
if (env.PLAUSIBLE_BASE_URL) {
tracker.apiEndpoint = env.PLAUSIBLE_BASE_URL;
}
await tracker.trackEvent(request, 'request', {}, true);
await tracker.trackPageview(request, {}, true);
const router = routeHandler(env, tracker.trackEvent.bind(tracker, request));
// analytics non-blocking
logAsync(tracker.trackEvent, request, 'request', {}, true);
logAsync(tracker.trackPageview, request, {}, true);
const router = routeHandler(env, (...args: any) =>

Check failure on line 75 in src/worker.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Delete `·`
logAsync(tracker.trackEvent.bind(tracker, request), ...args)
);
return router

Check failure on line 78 in src/worker.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Replace `⏎······.handle(request)⏎······` with `.handle(request)`
.handle(request)
.then(tracker.logResult.bind(this, propsDecoder, request));
.then((result: any) => {
logAsync(tracker.logResult, propsDecoder, request, result);

Check failure on line 81 in src/worker.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Delete `··`
return result;

Check failure on line 82 in src/worker.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Delete `··`
});

Check failure on line 83 in src/worker.ts

View workflow job for this annotation

GitHub Actions / Lint on Node 18.x

Delete `··`
},
};
2 changes: 1 addition & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ccip-read-dns-worker"
main = "src/worker.ts"
compatibility_date = "2023-02-14"
account_id = "de97cd2a1370a6ed2ad759f894179bff"
account_id = "15dcc9085cb794bb4f29d3e8177ac880"
node_compat = true

[dev]
Expand Down

0 comments on commit c8b5a82

Please sign in to comment.