Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kimbo committed Mar 5, 2020
1 parent 81113a0 commit a70ab62
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions examples/complex/block_dns_over_https.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import re
import os
import urllib.request
from typing import List

import dns.query
import dns.rdatatype
Expand All @@ -22,15 +23,16 @@
blocklist_filename = 'blocklist.json'

# additional hostnames to block
additional_doh_names = [
additional_doh_names: List[str] = [
'dns.google.com'
]

# additional IPs to block
additional_doh_ips = [
additional_doh_ips: List[str] = [

]


def get_doh_providers():
"""
Scrape a list of DoH providers from curl's wiki page.
Expand Down Expand Up @@ -78,7 +80,10 @@ def get_doh_providers():
yield {
'name': provider_name,
'website': website,
'url': 'https://{}{}{}'.format(doh_url[0], ':{}'.format(doh_url[1]) if len(doh_url[1]) != 0 else '', doh_url[2]),
'url': 'https://{}{}{}'.format(doh_url[0],
':{}'.format(doh_url[1])
if len(doh_url[1]) != 0
else '', doh_url[2]),
'hostname': doh_url[0],
'port': doh_url[1] if len(doh_url[1]) != 0 else '443',
'path': doh_url[2],
Expand All @@ -87,6 +92,7 @@ def get_doh_providers():
break
return


def get_ips(hostname):
"""
Lookup all A and AAAA records for given hostname
Expand All @@ -107,6 +113,7 @@ def get_ips(hostname):
ips.append(str(i.address))
return ips


def load_blocklist():
"""
Load a tuple containing two lists, in the form of (hostnames, ips).
Expand Down Expand Up @@ -135,6 +142,7 @@ def load_blocklist():
json.dump(obj, fp=fp)
return doh_hostnames, doh_ips


# load DoH hostnames and IP addresses to block
doh_hostnames, doh_ips = load_blocklist()
ctx.log.info('DoH blocklist loaded')
Expand All @@ -157,6 +165,7 @@ def _has_dns_message_content_type(flow):
return True
return False


def _request_has_dns_query_string(flow):
"""
Check if the query string of a request contains the parameter 'dns'
Expand All @@ -166,6 +175,7 @@ def _request_has_dns_query_string(flow):
"""
return 'dns' in flow.request.query


def _request_is_dns_json(flow):
"""
Check if the request looks like DoH with JSON.
Expand All @@ -190,6 +200,7 @@ def _request_is_dns_json(flow):
return True
return False


def _request_has_doh_looking_path(flow):
"""
Check if the path looks like it's DoH.
Expand All @@ -204,6 +215,7 @@ def _request_has_doh_looking_path(flow):
path = flow.request.path.split('?')[0]
return path in doh_paths


def _requested_hostname_is_in_doh_blacklist(flow):
"""
Check if server hostname is in our DoH provider blacklist.
Expand All @@ -217,6 +229,7 @@ def _requested_hostname_is_in_doh_blacklist(flow):
ip = flow.server_conn.address
return hostname in doh_hostnames or hostname in doh_ips or ip in doh_ips


doh_request_detection_checks = [
_has_dns_message_content_type,
_request_has_dns_query_string,
Expand All @@ -225,6 +238,7 @@ def _requested_hostname_is_in_doh_blacklist(flow):
_request_has_doh_looking_path
]


def request(flow):
for check in doh_request_detection_checks:
is_doh = check(flow)
Expand Down

0 comments on commit a70ab62

Please sign in to comment.