From 2d724e7b8432ed120add1a22c9975cd511eb78ee Mon Sep 17 00:00:00 2001 From: Aaron Sinclair Date: Thu, 10 Oct 2019 12:31:34 -0700 Subject: [PATCH] Got things working in Python 3 properly! --- stun/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/stun/__init__.py b/stun/__init__.py index 7d5031c..4182188 100644 --- a/stun/__init__.py +++ b/stun/__init__.py @@ -130,9 +130,9 @@ def stun_test(sock, host, port, source_ip, source_port, send_data=""): else: retVal['Resp'] = False return retVal - msgtype = binascii.b2a_hex(buf[0:2]) - bind_resp_msg = dictValToMsgType[int(msgtype)] == "BindResponseMsg" - tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).upper() + msgtype = binascii.b2a_hex(buf[0:2]).decode() + bind_resp_msg = dictValToMsgType[msgtype] == "BindResponseMsg" + tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).decode().upper() if bind_resp_msg and tranid_match: recvCorr = True retVal['Resp'] = True @@ -140,7 +140,7 @@ def stun_test(sock, host, port, source_ip, source_port, send_data=""): len_remain = len_message base = 20 while len_remain: - attr_type = binascii.b2a_hex(buf[base:(base + 2)]) + attr_type = binascii.b2a_hex(buf[base:(base + 2)]).decode() attr_len = int(binascii.b2a_hex(buf[(base + 2):(base + 4)]), 16) if attr_type == MappedAddress: port = int(binascii.b2a_hex(buf[base + 6:base + 8]), 16) @@ -186,14 +186,17 @@ def get_nat_type(s, source_ip, source_port, stun_host=None, stun_port=3478): log.debug("Do Test1") resp = False if stun_host: + log.debug('Trying STUN host: %s', stun_host) ret = stun_test(s, stun_host, port, source_ip, source_port) resp = ret['Resp'] else: + log.debug('Trying all STUN hosts') for stun_server in stun_servers_list: - log.debug('Trying STUN host: %s', stun_server) + log.debug(' - %s', stun_server) ret = stun_test(s, stun_server, port, source_ip, source_port) resp = ret['Resp'] if resp: + stun_host = stun_server break if not resp: return Blocked, ret