Replaced pydns with dnslib

master
Ruakij 3 years ago
parent 95b56955b0
commit d39024d369

@ -3,11 +3,7 @@
import os, re, _thread, subprocess, socket, datetime, getopt, sys, re, atexit, yaml, logging as log import os, re, _thread, subprocess, socket, datetime, getopt, sys, re, atexit, yaml, logging as log
from pathlib import Path from pathlib import Path
import dns.flags import dnslib as dns
import dns.message
import dns.rdataclass
import dns.rdatatype
import dns.name
from typing import cast from typing import cast
@ -17,7 +13,7 @@ def main(args):
log.debug("Logging started") log.debug("Logging started")
global config global config
config = readConfig("/data/config.yml") config = readConfig("data/config.yml")
s = setupSocket(config['socket']['address'], config['socket']['port']) s = setupSocket(config['socket']['address'], config['socket']['port'])
@ -42,22 +38,22 @@ def startListen(s):
def receiveFromWire(s): def receiveFromWire(s):
(wire, address) = s.recvfrom(512) (wire, address) = s.recvfrom(512)
dmsg = dns.message.from_wire(wire) dmsg = dns.DNSRecord.parse(wire)
return (address, dmsg) return (address, dmsg)
def handleQuery(s, address, dmsg): def handleQuery(s, address, dmsg):
log.info(f'{address[0]} |\tGot query') log.info(f'{address[0]} |\tGot query {dmsg.header.id}')
opcode = dmsg.opcode() opcode = dmsg.header.opcode
if opcode != dns.opcode.NOTIFY: if opcode != dns.OPCODE.NOTIFY:
log.error(f"{address[0]} |\tExpected opcode=NOTIFY, but was {dns.opcode.to_text(opcode)}") log.error(f"{address[0]} |\tExpected opcode=NOTIFY, but was {dns.OPCODE[opcode]}")
makeResponseWithRCode(s, address, dmsg, dns.rcode.REFUSED) makeResponseWithRCode(s, address, dmsg, dns.RCODE.REFUSED)
return False return False
rcode = dmsg.rcode() rcode = dmsg.header.rcode
if rcode != dns.rcode.NOERROR: if rcode != dns.RCODE.NOERROR:
log.error(f"{address[0]} |\tExpected rcode=NOERROR, but was {dns.rcode.to_text(rcode)}") log.error(f"{address[0]} |\tExpected rcode=NOERROR, but was {dns.RCODE[rcode]}")
makeResponseWithRCode(s, address, dmsg, dns.rcode.FORMERR) makeResponseWithRCode(s, address, dmsg, dns.RCODE.REFUSED)
return False return False
#flags = dmsg.flags #flags = dmsg.flags
@ -65,39 +61,40 @@ def handleQuery(s, address, dmsg):
# print('Expected flags=AA, but was', dns.flags.to_text(flags)) # print('Expected flags=AA, but was', dns.flags.to_text(flags))
# continue # continue
if len(dmsg.question) != 1: if len(dmsg.questions) != 1:
log.error(f'{address[0]} |\tExpected question-len=1, but was {len(dmsg.question)}') log.error(f'{address[0]} |\tExpected question-len=1, but was {len(dmsg.question)}')
makeResponseWithRCode(s, address, dmsg, dns.rcode.FORMERR) makeResponseWithRCode(s, address, dmsg, dns.RCODE.FORMERR)
return False return False
# Check record in question # Check record in question
record = dmsg.question[0] record = dmsg.questions[0]
r_datatype = record.rdtype r_qtype = record.qtype
if r_datatype != dns.rdatatype.SOA: if r_qtype != dns.QTYPE.SOA:
log.error(f'{address[0]} |\tExpected record to be SOA, but was {r_datatype}') log.error(f'{address[0]} |\tExpected record to be SOA, but was {dns.QTYPE[r_qtype]}')
makeResponseWithRCode(s, address, dmsg, dns.rcode.FORMERR) makeResponseWithRCode(s, address, dmsg, dns.RCODE.FORMERR)
return False return False
log.info(f'{address[0]} |\tNOTIFY for {record.name}') name = str(record.qname)
_thread.start_new_thread(updateNsData, (record.name,)) log.info(f'{address[0]} |\tNOTIFY for {name}')
response = dns.message.make_response(dmsg) # type: dns.message.Message _thread.start_new_thread(updateNsData, (name,))
response.flags |= dns.flags.AA
response = dmsg.reply() # type: dns.message.Message
response.header.aa = 1
sendResponse(s, address, response) sendResponse(s, address, response)
log.debug(f'{address[0]} |\tSent response') log.debug(f'{address[0]} |\tSent response')
return True return True
def makeResponseWithRCode(socket, address, dmsg, rcode): def makeResponseWithRCode(socket, address, dmsg, rcode):
response = dns.message.make_response(dmsg) # type: dns.message.Message response = dmsg.reply() # type: dns.message.Message
response.set_rcode(rcode) response.header.rcode = rcode
sendResponse(socket, address, response) sendResponse(socket, address, response)
def sendResponse(socket, address, response): def sendResponse(socket, address, response):
wire = response.to_wire(cast(dns.name.Name, response)) socket.sendto(response.pack(), address)
socket.sendto(wire, address)
def setupLogging(verbose: bool): def setupLogging(verbose: bool):
level = log.INFO level = log.INFO
@ -123,7 +120,7 @@ def readYamlFile(file: str):
def updateNsData(zone): def updateNsData(zone):
hasToDelete = False hasToDelete = False
try: try:
zone = str(zone)[:-1] zone = zone[:-1]
adaptedZone = adaptZoneName(zone) adaptedZone = adaptZoneName(zone)
log.info(f'{adaptedZone} |\tUpdating NS-Data') log.info(f'{adaptedZone} |\tUpdating NS-Data')

Loading…
Cancel
Save