Skip to content
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

Fix PEP errors & issue #21 #31

Open
wants to merge 4 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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ language: python

python:
- "2.7"
- "3.3"
- "3.4"
- "3.6"
- "3.7"

install:
- pip install -r requirements.txt
Expand Down
25 changes: 13 additions & 12 deletions stun/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@


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]})
for attr, value in dictAttrToVal.items():
dictValToAttr[value] = attr
for msg_type, value in dictMsgTypeToVal.items():
dictValToMsgType[value] = msg_type


def gen_tran_id():
Expand Down Expand Up @@ -132,17 +130,17 @@ 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 = binascii.b2a_hex(buf[0:2]).decode()
bind_resp_msg = dictValToMsgType[msgtype] == "BindResponseMsg"
tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).upper()
tranid_match = tranid.upper() == binascii.b2a_hex(buf[4:20]).decode().upper()
if bind_resp_msg and tranid_match:
recvCorr = True
retVal['Resp'] = True
len_message = int(binascii.b2a_hex(buf[2:4]), 16)
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)
Expand Down Expand Up @@ -188,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:
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)
log.debug('Trying all STUN hosts')
for stun_server in stun_servers_list:
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
Expand Down
1 change: 1 addition & 0 deletions stun/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ def main():
except KeyboardInterrupt:
sys.exit()


if __name__ == '__main__':
main()