Skip to content

Add surpport for python 3.5. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions stun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,19 @@
SymmetricNAT = "Symmetric NAT"
ChangedAddressError = "Meet an error, when do Test1 on Changed IP and Port"

def b2a_hexstr(abytes):
return binascii.b2a_hex(abytes).decode("ascii")

def _initialize():
items = dictAttrToVal.items()
for i in range(len(items)):
dictValToAttr.update({items[i][1]: items[i][0]})
items = dictMsgTypeToVal.items()
for i in range(len(items)):
dictValToMsgType.update({items[i][1]: items[i][0]})

global dictValToAttr, dictValToMsgType
dictValToAttr= {v: k for k, v in dictAttrToVal.items()}
dictValToMsgType = {v: k for k, v in dictMsgTypeToVal.items()}

def gen_tran_id():
a = ''.join(random.choice('0123456789ABCDEF') for i in range(32))
# return binascii.a2b_hex(a)
return a


def stun_test(sock, host, port, source_ip, source_port, send_data=""):
retVal = {'Resp': False, 'ExternalIP': None, 'ExternalPort': None,
'SourceIP': None, 'SourcePort': None, 'ChangedIP': None,
Expand Down Expand Up @@ -132,45 +129,45 @@ 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])
msgtype = b2a_hexstr(buf[0:2])
bind_resp_msg = dictValToMsgType[msgtype] == "BindResponseMsg"
tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).upper()
tranid_match = tranid.upper() == b2a_hexstr(buf[4:20]).upper()
if bind_resp_msg and tranid_match:
recvCorr = True
retVal['Resp'] = True
len_message = int(binascii.b2a_hex(buf[2:4]), 16)
len_message = int(b2a_hexstr(buf[2:4]), 16)
len_remain = len_message
base = 20
while len_remain:
attr_type = binascii.b2a_hex(buf[base:(base + 2)])
attr_len = int(binascii.b2a_hex(buf[(base + 2):(base + 4)]), 16)
attr_type = b2a_hexstr(buf[base:(base + 2)])
attr_len = int(b2a_hexstr(buf[(base + 2):(base + 4)]), 16)
if attr_type == MappedAddress:
port = int(binascii.b2a_hex(buf[base + 6:base + 8]), 16)
port = int(b2a_hexstr(buf[base + 6:base + 8]), 16)
ip = ".".join([
str(int(binascii.b2a_hex(buf[base + 8:base + 9]), 16)),
str(int(binascii.b2a_hex(buf[base + 9:base + 10]), 16)),
str(int(binascii.b2a_hex(buf[base + 10:base + 11]), 16)),
str(int(binascii.b2a_hex(buf[base + 11:base + 12]), 16))
str(int(b2a_hexstr(buf[base + 8:base + 9]), 16)),
str(int(b2a_hexstr(buf[base + 9:base + 10]), 16)),
str(int(b2a_hexstr(buf[base + 10:base + 11]), 16)),
str(int(b2a_hexstr(buf[base + 11:base + 12]), 16))
])
retVal['ExternalIP'] = ip
retVal['ExternalPort'] = port
if attr_type == SourceAddress:
port = int(binascii.b2a_hex(buf[base + 6:base + 8]), 16)
port = int(b2a_hexstr(buf[base + 6:base + 8]), 16)
ip = ".".join([
str(int(binascii.b2a_hex(buf[base + 8:base + 9]), 16)),
str(int(binascii.b2a_hex(buf[base + 9:base + 10]), 16)),
str(int(binascii.b2a_hex(buf[base + 10:base + 11]), 16)),
str(int(binascii.b2a_hex(buf[base + 11:base + 12]), 16))
str(int(b2a_hexstr(buf[base + 8:base + 9]), 16)),
str(int(b2a_hexstr(buf[base + 9:base + 10]), 16)),
str(int(b2a_hexstr(buf[base + 10:base + 11]), 16)),
str(int(b2a_hexstr(buf[base + 11:base + 12]), 16))
])
retVal['SourceIP'] = ip
retVal['SourcePort'] = port
if attr_type == ChangedAddress:
port = int(binascii.b2a_hex(buf[base + 6:base + 8]), 16)
port = int(b2a_hexstr(buf[base + 6:base + 8]), 16)
ip = ".".join([
str(int(binascii.b2a_hex(buf[base + 8:base + 9]), 16)),
str(int(binascii.b2a_hex(buf[base + 9:base + 10]), 16)),
str(int(binascii.b2a_hex(buf[base + 10:base + 11]), 16)),
str(int(binascii.b2a_hex(buf[base + 11:base + 12]), 16))
str(int(b2a_hexstr(buf[base + 8:base + 9]), 16)),
str(int(b2a_hexstr(buf[base + 9:base + 10]), 16)),
str(int(b2a_hexstr(buf[base + 10:base + 11]), 16)),
str(int(b2a_hexstr(buf[base + 11:base + 12]), 16))
])
retVal['ChangedIP'] = ip
retVal['ChangedPort'] = port
Expand Down
1 change: 1 addition & 0 deletions stun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def main():
print('NAT Type:', nat_type)
print('External IP:', external_ip)
print('External Port:', external_port)
input('Press any key to continue')
except KeyboardInterrupt:
sys.exit()

Expand Down