2022-02-02 23:12:03 +01:00
|
|
|
import asyncnet, asyncdispatch, nativesockets
|
|
|
|
import strutils, options, tables
|
|
|
|
import lib/dns
|
2022-01-23 18:29:13 +01:00
|
|
|
|
2022-02-02 23:12:03 +01:00
|
|
|
const records = {
|
|
|
|
DnsType.A: {"m5w.de": @["\127\0\0\1"]}.toTable,
|
|
|
|
DnsType.TXT: {"m5w.de": @["hello world", "abc"]}.toTable
|
|
|
|
}.toTable
|
2022-01-23 18:29:13 +01:00
|
|
|
|
2022-02-02 23:12:03 +01:00
|
|
|
proc handleDnsRequest(data: string): Option[string] =
|
|
|
|
let msg = parseMessage(data)
|
2022-01-23 18:29:13 +01:00
|
|
|
|
2022-02-02 23:12:03 +01:00
|
|
|
if len(msg.questions) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
let question = msg.questions[0]
|
|
|
|
# todo: handle missing record
|
|
|
|
let answer = records[question.qtype][question.qname]
|
|
|
|
let response = mkResponse(msg.header.id, question, answer)
|
|
|
|
|
|
|
|
return some(packMessage(response))
|
2022-01-23 18:29:13 +01:00
|
|
|
|
|
|
|
proc serve() {.async.} =
|
|
|
|
let server = newAsyncSocket(sockType=SockType.SOCK_DGRAM, protocol=Protocol.IPPROTO_UDP, buffered = false)
|
|
|
|
server.setSockOpt(OptReuseAddr, true)
|
|
|
|
server.bindAddr(Port(12345))
|
|
|
|
|
|
|
|
while true:
|
2022-02-02 23:12:03 +01:00
|
|
|
try:
|
|
|
|
let request = await server.recvFrom(size=512)
|
|
|
|
let response = handleDnsRequest(request.data)
|
|
|
|
|
|
|
|
if (response.isSome):
|
|
|
|
await server.sendTo(request.address, request.port, response.unsafeGet)
|
|
|
|
except:
|
|
|
|
continue
|
2022-01-23 18:29:13 +01:00
|
|
|
|
|
|
|
proc main() =
|
|
|
|
asyncCheck serve()
|
|
|
|
runForever()
|
|
|
|
|
|
|
|
main()
|