fix lint errors

This commit is contained in:
kimbo 2020-03-04 22:06:27 -07:00
parent 81113a0dcc
commit a70ab62797

View File

@ -8,6 +8,7 @@ import json
import re import re
import os import os
import urllib.request import urllib.request
from typing import List
import dns.query import dns.query
import dns.rdatatype import dns.rdatatype
@ -22,15 +23,16 @@ from mitmproxy import ctx
blocklist_filename = 'blocklist.json' blocklist_filename = 'blocklist.json'
# additional hostnames to block # additional hostnames to block
additional_doh_names = [ additional_doh_names: List[str] = [
'dns.google.com' 'dns.google.com'
] ]
# additional IPs to block # additional IPs to block
additional_doh_ips = [ additional_doh_ips: List[str] = [
] ]
def get_doh_providers(): def get_doh_providers():
""" """
Scrape a list of DoH providers from curl's wiki page. Scrape a list of DoH providers from curl's wiki page.
@ -78,7 +80,10 @@ def get_doh_providers():
yield { yield {
'name': provider_name, 'name': provider_name,
'website': website, '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], 'hostname': doh_url[0],
'port': doh_url[1] if len(doh_url[1]) != 0 else '443', 'port': doh_url[1] if len(doh_url[1]) != 0 else '443',
'path': doh_url[2], 'path': doh_url[2],
@ -87,6 +92,7 @@ def get_doh_providers():
break break
return return
def get_ips(hostname): def get_ips(hostname):
""" """
Lookup all A and AAAA records for given hostname Lookup all A and AAAA records for given hostname
@ -107,6 +113,7 @@ def get_ips(hostname):
ips.append(str(i.address)) ips.append(str(i.address))
return ips return ips
def load_blocklist(): def load_blocklist():
""" """
Load a tuple containing two lists, in the form of (hostnames, ips). Load a tuple containing two lists, in the form of (hostnames, ips).
@ -135,6 +142,7 @@ def load_blocklist():
json.dump(obj, fp=fp) json.dump(obj, fp=fp)
return doh_hostnames, doh_ips return doh_hostnames, doh_ips
# load DoH hostnames and IP addresses to block # load DoH hostnames and IP addresses to block
doh_hostnames, doh_ips = load_blocklist() doh_hostnames, doh_ips = load_blocklist()
ctx.log.info('DoH blocklist loaded') ctx.log.info('DoH blocklist loaded')
@ -157,6 +165,7 @@ def _has_dns_message_content_type(flow):
return True return True
return False return False
def _request_has_dns_query_string(flow): def _request_has_dns_query_string(flow):
""" """
Check if the query string of a request contains the parameter 'dns' Check if the query string of a request contains the parameter 'dns'
@ -166,6 +175,7 @@ def _request_has_dns_query_string(flow):
""" """
return 'dns' in flow.request.query return 'dns' in flow.request.query
def _request_is_dns_json(flow): def _request_is_dns_json(flow):
""" """
Check if the request looks like DoH with JSON. Check if the request looks like DoH with JSON.
@ -190,6 +200,7 @@ def _request_is_dns_json(flow):
return True return True
return False return False
def _request_has_doh_looking_path(flow): def _request_has_doh_looking_path(flow):
""" """
Check if the path looks like it's DoH. Check if the path looks like it's DoH.
@ -204,6 +215,7 @@ def _request_has_doh_looking_path(flow):
path = flow.request.path.split('?')[0] path = flow.request.path.split('?')[0]
return path in doh_paths return path in doh_paths
def _requested_hostname_is_in_doh_blacklist(flow): def _requested_hostname_is_in_doh_blacklist(flow):
""" """
Check if server hostname is in our DoH provider blacklist. Check if server hostname is in our DoH provider blacklist.
@ -217,6 +229,7 @@ def _requested_hostname_is_in_doh_blacklist(flow):
ip = flow.server_conn.address ip = flow.server_conn.address
return hostname in doh_hostnames or hostname in doh_ips or ip in doh_ips return hostname in doh_hostnames or hostname in doh_ips or ip in doh_ips
doh_request_detection_checks = [ doh_request_detection_checks = [
_has_dns_message_content_type, _has_dns_message_content_type,
_request_has_dns_query_string, _request_has_dns_query_string,
@ -225,6 +238,7 @@ doh_request_detection_checks = [
_request_has_doh_looking_path _request_has_doh_looking_path
] ]
def request(flow): def request(flow):
for check in doh_request_detection_checks: for check in doh_request_detection_checks:
is_doh = check(flow) is_doh = check(flow)