Skip to content

Commit

Permalink
Fix Flake8 Non Compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Putt authored and Robert Putt committed Aug 3, 2024
1 parent 694f67f commit 5ba978d
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions https_dns_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
myconfig.read_file(open(config_path))

if len(sys.argv) == 2:
ENVIRONMENT=str(sys.argv[1])
ENVIRONMENT = str(sys.argv[1])
else:
ENVIRONMENT='DNS1'
ENVIRONMENT = 'DNS1'

GOOGLE_DNS_URL = myconfig.get(ENVIRONMENT, 'GOOGLE_DNS_URL')
PINNED_CERT = myconfig.get(ENVIRONMENT, 'PINNED_CERT').encode('utf-8')
Expand All @@ -39,27 +39,33 @@

HTTPResponse = requests.packages.urllib3.response.HTTPResponse
orig_HTTPResponse__init__ = HTTPResponse.__init__


def new_HTTPResponse__init__(self, *args, **kwargs):
orig_HTTPResponse__init__(self, *args, **kwargs)
try:
self.peercert = base64.b64encode(self._connection.sock.getpeercert(True))
self.peercert = base64.b64encode(
self._connection.sock.getpeercert(True)
)
except AttributeError:
pass
HTTPResponse.__init__ = new_HTTPResponse__init__


HTTPResponse.__init__ = new_HTTPResponse__init__
HTTPAdapter = requests.adapters.HTTPAdapter
orig_HTTPAdapter_build_response = HTTPAdapter.build_response


def new_HTTPAdapter_build_response(self, request, resp):
response = orig_HTTPAdapter_build_response(self, request, resp)
try:
response.peercert = resp.peercert
except AttributeError:
pass
return response
HTTPAdapter.build_response = new_HTTPAdapter_build_response


HTTPAdapter.build_response = new_HTTPAdapter_build_response
CACHE = {}


Expand All @@ -71,14 +77,15 @@ def resolve(self, request, handler):
headers = {"Host": "dns.google"}

try:
if CACHE[hostname]['dt'] > datetime.datetime.now() - datetime.timedelta(minutes=5):
ttl = datetime.datetime.now() - datetime.timedelta(minutes=5)
if CACHE[hostname]['dt'] > ttl:
logging.info("Cache Hit: %s" % hostname)
answer = CACHE[hostname][ltype]
else:
logging.info("Cache Expired: %s" % hostname)
del CACHE[hostname]
raise Exception("Cache Expired")
except:
except Exception:
lookup_resp = requests.get(
'%sname=%s&type=%s' % (
GOOGLE_DNS_URL,
Expand All @@ -93,21 +100,28 @@ def resolve(self, request, handler):
if PINNED_CERT != lookup_resp.peercert:
logging.info(lookup_resp.peercert)
if EXIT_ON_MITM:
logging.error("REMOTE SSL CERT DID NOT MATCH EXPECTED (PINNED) "
"SSL CERT, EXITING IN CASE OF MAN IN THE MIDDLE ATTACK")
logging.error(
"REMOTE SSL CERT DID NOT MATCH EXPECTED (PINNED) "
"SSL CERT, EXITING IN CASE OF MAN IN THE MIDDLE ATTACK"
)
my_pid = os.getpid()
os.kill(my_pid, signal.SIGINT)
else:
logging.warning("REMOTE SSL CERT DID NOT MATCH EXPECTED (PINNED) "
"SSL CERT. NOT EXITING, BECAUSE YOU SAID SO IN YOUR CONFIG")

logging.warning(
"REMOTE SSL CERT DID NOT MATCH EXPECTED (PINNED) "
"SSL CERT. NOT EXITING, BECAUSE YOU SAID SO IN YOUR "
"CONFIG"
)

if lookup_resp.status_code == 200:
try:
logging.info("Cache Miss: %s" % hostname)
answer = json.loads(lookup_resp.text)['Answer']
CACHE[hostname] = {ltype: answer, "dt": datetime.datetime.now()}
except:
CACHE[hostname] = {
ltype: answer,
"dt": datetime.datetime.now()
}
except Exception:
answer = []
else:
answer = []
Expand Down

0 comments on commit 5ba978d

Please sign in to comment.