Skip to content

Commit a907574

Browse files
committed
client/server, python3
1 parent 983bda2 commit a907574

File tree

14 files changed

+227
-65
lines changed

14 files changed

+227
-65
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,5 @@ coverage.xml
5151
# Sphinx documentation
5252
docs/_build/
5353

54+
turnc-udpc.json
55+
turnc-udpd.json

client.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"realm": "trisoft.com.pl",
3+
"username": "passuser",
4+
"userpass": "password",
5+
"turnhost": "192.168.2.106",
6+
"turnport": 3478
7+
}

logging.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ formatter=form01
1616
args=(sys.stdout,)
1717

1818
[loggers]
19-
keys=root,jostedal
19+
keys=root,sturn
2020

2121
[logger_root]
2222
level=NOTSET
2323
handlers=hand01
2424

25-
[logger_jostedal]
25+
[logger_sturn]
2626
level=NOTSET
2727
handlers=hand01
2828
propagate=0
29-
qualname=jostedal
29+
qualname=sturn

example.config renamed to server.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"turnhost": "192.168.2.106",
3+
"turnport": 3478,
24
"software": "Sturn",
35
"realm": "trisoft.com.pl",
46

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
requires=['Twisted'],
1010

1111
author='Grzegorz Makarewicz',
12-
url='http://www.pexip.com',
12+
url='https://github.com/m32/sturn',
1313

1414
license='MIT',
1515

stunc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env vpython3
2+
import sys
23
import socket
4+
import json
35
from sturn import stun
46
from sturn.stun import agent, attributes ,authentication
57

@@ -19,18 +21,20 @@ def close(self):
1921
self.conn.close()
2022

2123
def Request(self, data):
22-
print('req', data.format())
2324
self.conn.sendto(data, (self.host, self.port))
2425
resp = self.conn.recvfrom(maxMessageSize)
2526
#import pdb; pdb.set_trace()
2627
resp = agent.Message.decode(resp[0])
27-
print('resp', resp.format())
2828
return resp
2929

3030
def main():
31-
#cls = Demo('192.168.2.104', 3478, '192.168.2.104', 54320)
32-
cls = Demo('drugi.trisoft.com.pl', 3478, '192.168.2.104', 54320)
31+
with open(sys.argv[1]) as fp:
32+
config = json.load(fp)
33+
cls = Demo(config['turnhost'], config['turnport'])
3334
req = agent.Message.encode(stun.METHOD_BINDING, stun.CLASS_REQUEST)
3435
resp = cls.Request(req)
3536

37+
address = resp.get_attr(stun.ATTR_XOR_MAPPED_ADDRESS, stun.ATTR_MAPPED_ADDRESS)
38+
print('public address: family:{} port:{} address:{}'.format(address.family, address.port, address.address))
39+
3640
main()

sturn/stun/agent.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def decode(cls, data):
5454
assert data[0] >> 6 == stun.MSG_STUN, \
5555
"Stun message MUST start with 0b00"
5656
msg_type, msg_length, magic_cookie, transaction_id = cls._struct.unpack_from(data)
57-
# assert magic_cookie == MAGIC_COOKIE, \
57+
# assert magic_cookie == stun.MAGIC_COOKIE, \
5858
# "Incorrect magic cookie ({:#x})".format(magic_cookie)
5959
assert msg_length % 4 == 0, \
6060
"Message not aliged to 4 byte boundary"
@@ -66,10 +66,18 @@ def decode(cls, data):
6666
while offset < cls._struct.size + msg_length:
6767
attr_type, attr_length = Attribute.struct.unpack_from(data, offset)
6868
offset += Attribute.struct.size
69-
attr = cls.get_attr_cls(attr_type).decode(data, offset, attr_length)
69+
clst = cls.get_attr_cls(attr_type)
70+
attr = clst.decode(data, offset, attr_length)
7071
msg._attributes.append(attr)
71-
offset += len(attr)
72-
offset += attr.padding
72+
#print(clst, type(attr), 'type: %04X/%d'%(attr_type, attr_type), 'offset:', offset, attr)
73+
try:
74+
offset += len(attr)
75+
except:
76+
offset += clst._struct.size
77+
try:
78+
offset += attr.padding
79+
except:
80+
pass
7381
return msg
7482

7583
@classmethod
@@ -121,7 +129,7 @@ def __repr__(self):
121129
"magic_cookie={:#010x}, transaction_id={}, attributes={})".format(
122130
type(self).__name__, self.msg_method, self.msg_class,
123131
len(self) - self._struct.size,
124-
self.magic_cookie, self.transaction_id.encode('hex'),
132+
self.magic_cookie, self.transaction_id.hex(),
125133
self._attributes))
126134

127135
def format(self):
@@ -142,6 +150,7 @@ class Attribute(bytes):
142150
"""STUN message attribute structure
143151
:see: http://tools.ietf.org/html/rfc5389#section-15
144152
"""
153+
_struct = struct.Struct('>2H')
145154
struct = struct.Struct('>2H')
146155

147156
def __new__(cls, data, *args, **kwargs):

sturn/stun/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def encode(cls, msg, username):
2727
return cls(username.encode('utf8'))
2828

2929
def __repr__(self, *args, **kwargs):
30-
return "USERNAME({!r})".format(str(self))
30+
return "USERNAME({})".format(str(self))
3131

3232

3333
@attribute

sturn/stun/protocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _stun_received(self, msg, addr):
6060
handler = self._handlers.get((msg.msg_method, msg.msg_class))
6161
if handler:
6262
logger.info("%s Received STUN", self)
63-
logger.debug(msg.format())
63+
#logger.debug(msg.format())
6464
handler(msg, addr)
6565
else:
6666
logger.info("%s Received unrecognized STUN", self)

sturn/stun/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def respond(self, response, addr):
1919
response.add_attr(attributes.Fingerprint)
2020
self.transport.write(response, addr)
2121
logger.info("%s Sending response", self)
22-
logger.debug(response.format())
22+
#logger.debug(response.format())
2323

2424
def _stun_binding_request(self, msg, addr):
2525
if msg.msg_class == stun.CLASS_REQUEST:
@@ -40,7 +40,7 @@ def _stun_binding_request(self, msg, addr):
4040
response.add_attr(attributes.Software, self.software)
4141
self.transport.write(response, addr)
4242
logger.info("%s Sending response", self)
43-
logger.debug(response.format())
43+
#logger.debug(response.format())
4444

4545
def _stun_binding_indication(self, msg, addr):
4646
pass

0 commit comments

Comments
 (0)