Skip to content
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

feat: always answer with the NS record in the authority section #87

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function apiBuilder (cfg: APIConfig): Promise<FastifyTypebox> {
const store = new Store(cfg, db, protocols)

server.log.info('Initializing DNS server')
const dns = await initDnsServer(cfg.dnsport, store.sites, server.log)
const dns = await initDnsServer(cfg.dnsport, store.sites, server.log, cfg.host)

server.log.info('Done')
await registerAuth(cfg, server, store)
Expand Down
12 changes: 11 additions & 1 deletion dns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import { SiteConfigStore } from '../config/sites.js'

const TTL = 604800 // 7 days ttl

export async function initDnsServer (port: number, store: SiteConfigStore, logger?: FastifyBaseLogger): Promise<ReturnType<typeof dns2.createServer>> {
export async function initDnsServer (port: number, store: SiteConfigStore, logger?: FastifyBaseLogger, host?: string = 'api.distributed.press'): Promise<ReturnType<typeof dns2.createServer>> {
const server = dns2.createServer({
udp: true,
handle: (request, send, rinfo) => {
const response = dns2.Packet.createResponseFromRequest(request)
const [{ name }] = request.questions
logger?.info(`[dns] ${rinfo.address}:${rinfo.port} asked for ${name}`)

// TODO: Pass server from config
response.authorities.push({
name,
type: dns2.Packet.TYPE.NS,
class: dns2.Packet.CLASS.IN,
ttl: TTL,
ns: `${host}.`
})

const cleanedName = name.toLowerCase().replace('_dnslink.', '')
store.get(cleanedName)
.then(({ links }) => {
Expand All @@ -33,6 +42,7 @@ export async function initDnsServer (port: number, store: SiteConfigStore, logge
data: `dnslink=${links.hyper.dnslink}`
})
}

send(response)
})
.catch((error) => {
Expand Down
Loading