From b723b019a5069fba472d8f0b8f0b43109bc6c79c Mon Sep 17 00:00:00 2001 From: a7md0 <10885603+a7md0@users.noreply.github.com> Date: Tue, 4 Jun 2019 06:41:43 +0300 Subject: [PATCH 1/4] Update __init__.py Fix for python 3 --- stun/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stun/__init__.py b/stun/__init__.py index d449cc6..42bf0a5 100644 --- a/stun/__init__.py +++ b/stun/__init__.py @@ -88,10 +88,10 @@ def _initialize(): - items = dictAttrToVal.items() + items = list(dictAttrToVal.items()) for i in range(len(items)): dictValToAttr.update({items[i][1]: items[i][0]}) - items = dictMsgTypeToVal.items() + items = list(dictMsgTypeToVal.items()) for i in range(len(items)): dictValToMsgType.update({items[i][1]: items[i][0]}) @@ -133,7 +133,7 @@ def stun_test(sock, host, port, source_ip, source_port, send_data=""): retVal['Resp'] = False return retVal msgtype = binascii.b2a_hex(buf[0:2]) - bind_resp_msg = dictValToMsgType[msgtype] == "BindResponseMsg" + bind_resp_msg = dictValToMsgType[int(msgtype)] == "BindResponseMsg" tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).upper() if bind_resp_msg and tranid_match: recvCorr = True From 5af0c12ffb3f3b1f3422162880150e5a1d564a2c Mon Sep 17 00:00:00 2001 From: Aaron Sinclair Date: Fri, 4 Oct 2019 16:17:41 -0700 Subject: [PATCH 2/4] Fix PEP errors & fixed issue #21 properly --- stun/__init__.py | 16 +++++++--------- stun/cli.py | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/stun/__init__.py b/stun/__init__.py index 42bf0a5..7d5031c 100644 --- a/stun/__init__.py +++ b/stun/__init__.py @@ -88,12 +88,10 @@ def _initialize(): - items = list(dictAttrToVal.items()) - for i in range(len(items)): - dictValToAttr.update({items[i][1]: items[i][0]}) - items = list(dictMsgTypeToVal.items()) - for i in range(len(items)): - dictValToMsgType.update({items[i][1]: items[i][0]}) + for attr, value in dictAttrToVal.items(): + dictValToAttr[value] = attr + for msg_type, value in dictMsgTypeToVal.items(): + dictValToMsgType[value] = msg_type def gen_tran_id(): @@ -191,9 +189,9 @@ def get_nat_type(s, source_ip, source_port, stun_host=None, stun_port=3478): ret = stun_test(s, stun_host, port, source_ip, source_port) resp = ret['Resp'] else: - for stun_host in stun_servers_list: - log.debug('Trying STUN host: %s', stun_host) - ret = stun_test(s, stun_host, port, source_ip, source_port) + for stun_server in stun_servers_list: + log.debug('Trying STUN host: %s', stun_server) + ret = stun_test(s, stun_server, port, source_ip, source_port) resp = ret['Resp'] if resp: break diff --git a/stun/cli.py b/stun/cli.py index 543a92d..ab61d2d 100644 --- a/stun/cli.py +++ b/stun/cli.py @@ -60,5 +60,6 @@ def main(): except KeyboardInterrupt: sys.exit() + if __name__ == '__main__': main() From 986d8a396816f109fe757787fb67b992f77f5967 Mon Sep 17 00:00:00 2001 From: Aaron Sinclair Date: Fri, 4 Oct 2019 16:23:09 -0700 Subject: [PATCH 3/4] Removed old Python version & added new ones --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2d6c5f5..c89b3fe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,9 @@ language: python python: - "2.7" - - "3.3" - "3.4" + - "3.6" + - "3.7" install: - pip install -r requirements.txt From 2d724e7b8432ed120add1a22c9975cd511eb78ee Mon Sep 17 00:00:00 2001 From: Aaron Sinclair Date: Thu, 10 Oct 2019 12:31:34 -0700 Subject: [PATCH 4/4] 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