Commit 04a267cf authored by Romain Courteaud's avatar Romain Courteaud

Report dns query status

parent c702d4fc
import dns
from urlchecker_network import logNetwork
from peewee import fn
URL_TO_CHECK = "example.org"
TIMEOUT = 2
def reportDnsQuery(db, resolver_ip=None, domain=None, rdtype=None):
# http://charlesleifer.com/blog/techniques-for-querying-lists-of-objects-and-determining-the-top-related-item/
DnsChangeAlias = db.DnsChange.alias()
subquery = (
DnsChangeAlias.select(
DnsChangeAlias.resolver_ip,
DnsChangeAlias.domain,
DnsChangeAlias.rdtype,
fn.MAX(DnsChangeAlias.status_id).alias("max_status_id"),
)
.group_by(
DnsChangeAlias.resolver_ip,
DnsChangeAlias.domain,
DnsChangeAlias.rdtype,
)
.alias("dns_change_max_subquery")
)
StatusAlias = db.Status.alias()
query = (
db.DnsChange.select(db.DnsChange, StatusAlias)
.join(StatusAlias)
.switch(db.DnsChange)
.join(
subquery,
on=(
(db.DnsChange.status_id == subquery.c.max_status_id)
& (db.DnsChange.resolver_ip == subquery.c.resolver_ip)
& (db.DnsChange.domain == subquery.c.domain)
& (db.DnsChange.rdtype == subquery.c.rdtype)
),
)
.order_by(
db.DnsChange.domain.asc(),
db.DnsChange.rdtype.asc(),
db.DnsChange.resolver_ip.asc(),
)
)
if resolver_ip is not None:
if type(resolver_ip) == list:
query = query.where(db.DnsChange.resolver_ip << resolver_ip)
else:
query = query.where(db.DnsChange.resolver_ip == resolver_ip)
if domain is not None:
if type(domain) == list:
query = query.where(db.DnsChange.domain << domain)
else:
query = query.where(db.DnsChange.domain == domain)
if rdtype is not None:
if type(rdtype) == list:
query = query.where(db.DnsChange.rdtype << rdtype)
else:
query = query.where(db.DnsChange.rdtype == rdtype)
return query
def logDnsQuery(db, status_id, resolver_ip, domain_text, rdtype, answer_list):
answer_list.sort()
response = ", ".join(answer_list)
......@@ -12,16 +72,9 @@ def logDnsQuery(db, status_id, resolver_ip, domain_text, rdtype, answer_list):
with db._db.atomic():
try:
# Check previous parameter value
previous_entry = (
db.DnsChange.select()
.where(
db.DnsChange.resolver_ip == resolver_ip,
db.DnsChange.domain == domain_text,
db.DnsChange.rdtype == rdtype,
)
.order_by(db.DnsChange.status.desc())
.get()
)
previous_entry = reportDnsQuery(
db, resolver_ip=resolver_ip, domain=domain_text, rdtype=rdtype
).get()
except db.DnsChange.DoesNotExist:
previous_entry = None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment