Skip to content

DNS server #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged

DNS server #10

merged 1 commit into from
Jan 30, 2025

Conversation

gregnr
Copy link
Member

@gregnr gregnr commented Jan 29, 2025

Adds basic DNS server. No built-in recursive querying or zone record handling - rather you implement a request() callback that can be used to resolve DNS queries as you wish.

import { createStack } from 'tcpip';
import { createDnsServer, ptrNameToIP } from '@tcpip/dns';

const stack = await createStack();

const tapInterface = await stack.createTapInterface({
  ip: '10.0.0.1/24',
  mac: '02:00:00:00:00:01',
});

const dnsServer = await createDnsServer({
  stack,
  async request(query) {
    const { type, name } = query;

    // Handle A record for example.com
    if (type === 'A' && name === 'example.com') {
      return {
        type,
        ip: '10.0.0.1',
        ttl: 300,
      };
    }

    // Handle reverse lookup for 10.0.0.1
    if (type === 'PTR') {
      const { type: ipType, ip } = ptrNameToIP(name);

      if (ipType === 'ipv4' && ip === '10.0.0.1') {
        return {
          type,
          ptr: 'example.com',
          ttl: 300,
        };
      }
    }
  },
});

@gregnr gregnr changed the base branch from feat/c2w to main January 30, 2025 00:23
@gregnr gregnr marked this pull request as ready for review January 30, 2025 00:25
@gregnr gregnr merged commit d0a3d2c into main Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant