Skip to content

Commit

Permalink
chore: small code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vandot committed Mar 24, 2023
1 parent c5186e4 commit 22648fa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lodns.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.5"
version = "0.1.6"
author = "vandot"
description = "Simple DNS server for local development"
license = "MIT"
Expand Down
23 changes: 7 additions & 16 deletions src/lodns.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ proc writeHelp() =
quit()

proc main() =
var install, start, uninstall = false
var
ip :string
port :int
Expand All @@ -33,35 +32,27 @@ proc main() =
of cmdArgument:
case key
of "install":
install = true
(ip, port) = systemProbe()
install(ip, port, tld)
of "start":
start = true
(ip, port) = systemProbe()
serve(ip, port, tld)
of "uninstall":
uninstall = true
uninstall(tld)
else:
echo "unknown argument: ", key
writeHelp()
of cmdLongOption, cmdShortOption:
case key
of "v", "version":
writeVersion()
quit()
of "h", "help":
writeHelp()
else:
echo "unknown option: ", key
writeHelp()
of cmdEnd:
discard

if install or start:
(ip, port) = systemProbe()
if install:
install(ip, port, tld)
if uninstall:
uninstall(tld)
if start:
serve(ip, port, tld)
else:
writeHelp()

when isMainModule:
main()
8 changes: 5 additions & 3 deletions src/lodnspkg/actions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ proc systemProbe*(): (string, int) =
active.stripLineEnd
if systemd != "systemd" or active != "active":
echo "linux initialization is supported only for systemd using systemd-resolved"
quit()
quit(1)
var version = execProcess("systemd --version | head -1 | awk '{ print $2}'")
version.stripLineEnd
# IPv4 dummy address (RFC7600)
Expand All @@ -40,6 +40,7 @@ proc install*(ip: string, port: int, tld: string) =
var exitCode = execCmd("systemctl restart systemd-networkd.service")
if exitCode != 0:
echo "systemctl restart systemd-networkd.service failed with code " & $exitCode
quit(1)

when defined macosx:
if not dirExists("/etc/resolver"):
Expand All @@ -52,7 +53,7 @@ proc install*(ip: string, port: int, tld: string) =
var exitCode = execCmd(addCommand)
if exitCode != 0:
echo addCommand & " failed with code " & $exitCode
quit()
quit(1)

proc uninstall*(tld: string) =
when defined linux:
Expand All @@ -64,6 +65,7 @@ proc uninstall*(tld: string) =
exitCode = execCmd("systemctl restart systemd-networkd.service")
if exitCode != 0:
echo "systemctl restart systemd-networkd.service failed with code " & $exitCode
quit(1)

when defined macosx:
removeFile("/etc/resolver/" & tld)
Expand All @@ -73,4 +75,4 @@ proc uninstall*(tld: string) =
var exitCode = execCmd(removeCommand)
if exitCode != 0:
echo removeCommand & " failed with code " & $exitCode
quit()
quit(1)
22 changes: 12 additions & 10 deletions src/lodnspkg/server.nim
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import std/[net, logging, asyncnet, strutils, asyncdispatch]
import std/[net, asyncnet, strutils, asyncdispatch, times]
import pkg/[dnsprotocol]

template log(lvl: string, msg: varargs[string, `$`]) =
stdout.writeLine("[" & getDateStr() & "T" & getClockStr() & "] " & lvl.toUpper() & " " & @msg)

proc serve*(ip: string, port: int, tld: string) =
var logger = newConsoleLogger(fmtStr="[$datetime] $levelname ")
const withSize = 500
var rr :ResourceRecord
var socket = newAsyncSocket(sockType = SOCK_DGRAM, protocol = IPPROTO_UDP)
let socket = newAsyncSocket(sockType = SOCK_DGRAM, protocol = IPPROTO_UDP)

socket.bindAddr(address = ip, port = Port port)
logger.log(lvlInfo, "status=listening ip=", ip, " port=", port)
log("info", "status=listening ip=", ip, " port=", port)
while true:
let request = waitfor socket.recvFrom(withSize)
let request = waitFor socket.recvFrom(withSize)
let message = parseMessage(request.data)
let domain = message.questions[0].qname
case message.questions[0].qtype
of QType.A:
logger.log(lvlInfo, "type=A domain=", domain)
log("info", "type=A domain=", domain)
rr = initResourceRecord(domain, Type.A, Class.IN, 299'i32, 4'u16,
RDataA(address: [127'u8, 0, 0, 1]))
of QType.AAAA:
logger.log(lvlInfo, "type=AAAA domain=", domain)
log("info", "type=AAAA domain=", domain)
rr = initResourceRecord(domain, Type.AAAA, Class.IN, 299'i32, 16'u16,
RDataAAAA(address: [0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]))
else:
logger.log(lvlError, "type=", message.questions[0].qtype, " domain=", domain, " error=unsupported-type")
log("error", "type=", message.questions[0].qtype, " domain=", domain, " error=unsupported-type")
rr = initResourceRecord(domain, Type.NULL, Class.IN, 0'i32, 0'u16, RDataNULL())
if rsplit(domain, ".", maxsplit=2)[1] != tld:
logger.log(lvlError, "type=", message.questions[0].qtype, " domain=", domain, " error=unsupported-domain")
log("error", "type=", message.questions[0].qtype, " domain=", domain, " error=unsupported-domain")
rr = initResourceRecord(domain, Type.NULL, Class.IN, 0'i32, 0'u16, RDataNULL())
let header = initHeader(message.header.id, QR.Response)
let reply = initMessage(header, message.questions, @[rr])
let bmsg = toBinMsg(reply)
waitfor socket.sendTo(request.address, request.port, bmsg)
waitFor socket.sendTo(request.address, request.port, bmsg)

0 comments on commit 22648fa

Please sign in to comment.