From 9d26f8bcac64c05f9923fa1cd0eaea1ed5f32572 Mon Sep 17 00:00:00 2001 From: Ovv Date: Wed, 2 May 2018 13:02:59 +0200 Subject: [PATCH] remove proxy --- Pipfile | 1 + Pipfile.lock | 33 ++++- aiosip/application.py | 22 ++-- aiosip/dialog.py | 92 ++++++------- aiosip/peers.py | 6 +- tests/test_sip_proxy.py | 278 ++++++++++++++++++++-------------------- 6 files changed, 232 insertions(+), 200 deletions(-) diff --git a/Pipfile b/Pipfile index 9913f06..9658245 100644 --- a/Pipfile +++ b/Pipfile @@ -13,6 +13,7 @@ async-timeout = "*" [dev-packages] twine = "*" pytest-asyncio = "*" +"flake8" = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index a0c825e..4f5be24 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "1cbcfbb303bbb27a34d4232aded8f8227e0dbeb813392f831dcc61cedd8d1f1d" + "sha256": "f9180e3e4762bce444c395c2572cc4bdba8c0be6e37c40504b017790c4a116ce" }, "pipfile-spec": 6, "requires": { @@ -179,6 +179,14 @@ ], "version": "==3.0.4" }, + "flake8": { + "hashes": [ + "sha256:7253265f7abd8b313e3892944044a365e3f4ac3fcdcfb4298f55ee9ddf188ba0", + "sha256:c7841163e2b576d435799169b78703ad6ac1bbb0f199994fc05f700b2a90ea37" + ], + "index": "pypi", + "version": "==3.5.0" + }, "idna": { "hashes": [ "sha256:2c6a5de3089009e3da7c5dde64a141dbc8551d5b7f6cf4ed7c2568d0cc520a8f", @@ -186,6 +194,13 @@ ], "version": "==2.6" }, + "mccabe": { + "hashes": [ + "sha256:ab8a6258860da4b6677da4bd2fe5dc2c659cff31b3ee4f7f5d64e79735b80d42", + "sha256:dd8d182285a0fe56bace7f45b5e7d1a6ebcbf524e8f3bd87eb0f125271b8831f" + ], + "version": "==0.6.1" + }, "more-itertools": { "hashes": [ "sha256:0dd8f72eeab0d2c3bd489025bb2f6a1b8342f9b198f6fc37b52d15cfa4531fea", @@ -216,6 +231,22 @@ ], "version": "==1.5.3" }, + "pycodestyle": { + "hashes": [ + "sha256:1ec08a51c901dfe44921576ed6e4c1f5b7ecbad403f871397feedb5eb8e4fa14", + "sha256:5ff2fbcbab997895ba9ead77e1b38b3ebc2e5c3b8a6194ef918666e4c790a00e", + "sha256:682256a5b318149ca0d2a9185d365d8864a768a28db66a84a2ea946bcc426766", + "sha256:6c4245ade1edfad79c3446fadfc96b0de2759662dc29d07d80a6f27ad1ca6ba9" + ], + "version": "==2.3.1" + }, + "pyflakes": { + "hashes": [ + "sha256:08bd6a50edf8cffa9fa09a463063c425ecaaf10d1eb0335a7e8b1401aef89e6f", + "sha256:8d616a382f243dbf19b54743f280b80198be0bca3a5396f1d2e1fca6223e8805" + ], + "version": "==1.6.0" + }, "pytest": { "hashes": [ "sha256:54713b26c97538db6ff0703a12b19aeaeb60b5e599de542e7fca0ec83b9038e8", diff --git a/aiosip/application.py b/aiosip/application.py index 4ce4a7d..9068ef1 100644 --- a/aiosip/application.py +++ b/aiosip/application.py @@ -13,7 +13,7 @@ from collections import MutableMapping from . import __version__ -from .dialog import Dialog, ProxyDialog +from .dialog import Dialog from .dialplan import BaseDialplan from .protocol import UDP, TCP, WS from .peers import UDPConnector, TCPConnector, WSConnector @@ -128,16 +128,16 @@ async def prepare(self, status_code, *args, **kwargs): return dialog - async def proxy(self, message, proxy_peer=None, dialog_factory=ProxyDialog): - if not proxy_peer: - proxy_peer = await self.app.connect( - remote_addr=(message.to_details.host, message.to_details.port), - protocol=peer.protocol - ) - - dialog = self._create_dialog(dialog_factory=dialog_factory, proxy_peer=proxy_peer) - dialog.proxy(message) - return dialog + # async def proxy(self, message, proxy_peer=None, dialog_factory=ProxyDialog): + # if not proxy_peer: + # proxy_peer = await self.app.connect( + # remote_addr=(message.to_details.host, message.to_details.port), + # protocol=peer.protocol + # ) + # + # dialog = self._create_dialog(dialog_factory=dialog_factory, proxy_peer=proxy_peer) + # dialog.proxy(message) + # return dialog request = Request() await route(request, msg) diff --git a/aiosip/dialog.py b/aiosip/dialog.py index f9b7f33..e9019b1 100644 --- a/aiosip/dialog.py +++ b/aiosip/dialog.py @@ -449,49 +449,49 @@ async def close(self, timeout=None): self._close() -class ProxyDialog(DialogBase): - def __init__(self, *args, proxy_peer, **kwargs): - super().__init__(*args, **kwargs) - self.proxy_peer = proxy_peer - self._incoming = asyncio.Queue() - - @property - def dialog_id(self): - return frozenset((self.to_details['params']['tag'], - self.from_details['params'].get('tag'), - self.call_id)) - - async def receive_message(self, msg): - if 'tag' not in self.from_details['params'] and 'tag' in msg.to_details['params']: - del self.app._dialogs[self.dialog_id] - self.from_details['params']['tag'] = msg.to_details['params']['tag'] - self.app._dialogs[self.dialog_id] = self - - await self._incoming.put(msg) - - async def recv(self): - return await self._incoming.get() - - def proxy(self, message): - # TODO: should be cleaner - if not isinstance(message.headers['Via'], list): - message.headers['Via'] = [message.headers['Via'], ] - - if f'{self.peer.peer_addr[0]}:{self.peer.peer_addr[1]}' in message.headers['Via'][0]: - message.headers['Via'].insert(0, self.proxy_peer.generate_via_headers()) - self.proxy_peer.send_message(message) - elif f'{self.peer.local_addr[0]}:{self.peer.local_addr[1]}' in message.headers['Via'][0]: - message.headers['Via'].pop(0) - self.proxy_peer.send_message(message) - elif f'{self.proxy_peer.peer_addr[0]}:{self.proxy_peer.peer_addr[1]}' in message.headers['Via'][0]: - message.headers['Via'].insert(0, self.peer.generate_via_headers()) - self.peer.send_message(message) - elif f'{self.proxy_peer.local_addr[0]}:{self.proxy_peer.local_addr[1]}' in message.headers['Via'][0]: - message.headers['Via'].pop(0) - self.peer.send_message(message) - else: - message.headers['Via'].insert(0, self.proxy_peer.generate_via_headers()) - self.proxy_peer.send_message(message) - - async def close(self, *args, **kwargs): - self._close() +# class ProxyDialog(DialogBase): +# def __init__(self, *args, proxy_peer, **kwargs): +# super().__init__(*args, **kwargs) +# self.proxy_peer = proxy_peer +# self._incoming = asyncio.Queue() +# +# @property +# def dialog_id(self): +# return frozenset((self.to_details['params']['tag'], +# self.from_details['params'].get('tag'), +# self.call_id)) +# +# async def receive_message(self, msg): +# if 'tag' not in self.from_details['params'] and 'tag' in msg.to_details['params']: +# del self.app._dialogs[self.dialog_id] +# self.from_details['params']['tag'] = msg.to_details['params']['tag'] +# self.app._dialogs[self.dialog_id] = self +# +# await self._incoming.put(msg) +# +# async def recv(self): +# return await self._incoming.get() +# +# def proxy(self, message): +# # TODO: should be cleaner +# if not isinstance(message.headers['Via'], list): +# message.headers['Via'] = [message.headers['Via'], ] +# +# if f'{self.peer.peer_addr[0]}:{self.peer.peer_addr[1]}' in message.headers['Via'][0]: +# message.headers['Via'].insert(0, self.proxy_peer.generate_via_headers()) +# self.proxy_peer.send_message(message) +# elif f'{self.peer.local_addr[0]}:{self.peer.local_addr[1]}' in message.headers['Via'][0]: +# message.headers['Via'].pop(0) +# self.proxy_peer.send_message(message) +# elif f'{self.proxy_peer.peer_addr[0]}:{self.proxy_peer.peer_addr[1]}' in message.headers['Via'][0]: +# message.headers['Via'].insert(0, self.peer.generate_via_headers()) +# self.peer.send_message(message) +# elif f'{self.proxy_peer.local_addr[0]}:{self.proxy_peer.local_addr[1]}' in message.headers['Via'][0]: +# message.headers['Via'].pop(0) +# self.peer.send_message(message) +# else: +# message.headers['Via'].insert(0, self.proxy_peer.generate_via_headers()) +# self.proxy_peer.send_message(message) +# +# async def close(self, *args, **kwargs): +# self._close() diff --git a/aiosip/peers.py b/aiosip/peers.py index 340395c..8b4e9ec 100644 --- a/aiosip/peers.py +++ b/aiosip/peers.py @@ -9,7 +9,7 @@ from . import utils from .contact import Contact from .protocol import UDP, TCP, WS -from .dialog import Dialog, InviteDialog, ProxyDialog +from .dialog import Dialog, InviteDialog LOG = logging.getLogger(__name__) @@ -35,8 +35,8 @@ def send_message(self, msg): def _create_dialog(self, method, from_details, to_details, contact_details=None, password=None, call_id=None, headers=None, payload=None, cseq=0, inbound=False, dialog_factory=Dialog, **kwargs): - if not issubclass(dialog_factory, ProxyDialog): - from_details.add_tag() + # if not issubclass(dialog_factory, ProxyDialog): + from_details.add_tag() if not call_id: call_id = str(uuid.uuid4()) diff --git a/tests/test_sip_proxy.py b/tests/test_sip_proxy.py index dbbcbc5..8845018 100644 --- a/tests/test_sip_proxy.py +++ b/tests/test_sip_proxy.py @@ -4,142 +4,142 @@ import itertools -@pytest.mark.parametrize('close_order', itertools.permutations(('client', 'server', 'proxy'))) # noQa C901: too complex -async def test_proxy_subscribe(test_server, test_proxy, protocol, loop, from_details, to_details, close_order): - callback_complete = loop.create_future() - callback_complete_proxy = loop.create_future() - - class ServerDialplan(aiosip.BaseDialplan): - - async def resolve(self, *args, **kwargs): - await super().resolve(*args, **kwargs) - return self.subscribe - - async def subscribe(self, request, message): - await request.prepare(status_code=200) - callback_complete.set_result(message) - - class ProxyDialplan(aiosip.BaseDialplan): - async def resolve(self, *args, **kwargs): - await super().resolve(*args, **kwargs) - - return self.proxy_subscribe - - async def proxy_subscribe(self, request, message): - dialog = await request.proxy(message) - callback_complete_proxy.set_result(message) - async for message in dialog: - dialog.proxy(message) - - app = aiosip.Application(loop=loop, debug=True) - - server_app = aiosip.Application(loop=loop, debug=True, dialplan=ServerDialplan()) - await test_server(server_app) - - proxy_app = aiosip.Application(loop=loop, dialplan=ProxyDialplan()) - proxy = await test_proxy(proxy_app) - - peer = await app.connect( - protocol=protocol, - remote_addr=(proxy.sip_config['server_host'], proxy.sip_config['server_port']) - ) - - await peer.subscribe( - expires=1800, - from_details=aiosip.Contact.from_header(from_details), - to_details=aiosip.Contact.from_header(to_details), - ) - - received_request_server = await asyncio.wait_for(callback_complete, timeout=2) - received_request_proxy = await asyncio.wait_for(callback_complete_proxy, timeout=2) - - assert received_request_server.method == 'SUBSCRIBE' - assert received_request_server.payload == received_request_proxy.payload - assert received_request_server.headers == received_request_proxy.headers - - for item in close_order: - if item == 'client': - await app.close() - elif item == 'server': - await server_app.close() - elif item == 'proxy': - await proxy_app.close() - else: - raise ValueError('Invalid close_order') - - -@pytest.mark.parametrize('close_order', itertools.permutations(('client', 'server', 'proxy'))) # noQa C901: too complex -async def test_proxy_notify(test_server, test_proxy, protocol, loop, from_details, to_details, close_order): - - callback_complete = loop.create_future() - callback_complete_proxy = loop.create_future() - - class ServerDialpan(aiosip.BaseDialplan): - - async def resolve(self, *args, **kwargs): - await super().resolve(*args, **kwargs) - - return self.subscribe - - async def subscribe(self, request, message): - dialog = await request.prepare(status_code=200) - await asyncio.sleep(0.2) - await dialog.notify(payload='1') - - class ProxyDialplan(aiosip.BaseDialplan): - async def resolve(self, *args, **kwargs): - await super().resolve(*args, **kwargs) - - return self.proxy_subscribe - - async def proxy_subscribe(self, request, message): - dialog = await request.proxy(message) - - async for message in dialog: - dialog.proxy(message) - - if message.method == 'NOTIFY': - callback_complete_proxy.set_result(message) - - app = aiosip.Application(loop=loop, debug=True) - - server_app = aiosip.Application(loop=loop, debug=True, dialplan=ServerDialpan()) - await test_server(server_app) - - proxy_app = aiosip.Application(loop=loop, debug=True, dialplan=ProxyDialplan()) - proxy = await test_proxy(proxy_app) - - peer = await app.connect( - protocol=protocol, - remote_addr=(proxy.sip_config['server_host'], proxy.sip_config['server_port']) - ) - - subscription = await peer.subscribe( - expires=1800, - from_details=aiosip.Contact.from_header(from_details), - to_details=aiosip.Contact.from_header(to_details) - ) - - async for msg in subscription: - await subscription.reply(msg, 200) - callback_complete.set_result(msg) - break # We only expect a single message - - received_notify_server = await asyncio.wait_for(callback_complete, timeout=2) - received_notify_proxy = await asyncio.wait_for(callback_complete_proxy, timeout=2) - - assert received_notify_server.method == 'NOTIFY' - assert received_notify_server.payload == '1' - - assert received_notify_server.payload == received_notify_proxy.payload - assert received_notify_server.headers == received_notify_proxy.headers - - for item in close_order: - if item == 'client': - await app.close() - elif item == 'server': - await server_app.close() - elif item == 'proxy': - await proxy_app.close() - else: - raise ValueError('Invalid close_order') +# @pytest.mark.parametrize('close_order', itertools.permutations(('client', 'server', 'proxy'))) # noQa C901: too complex +# async def test_proxy_subscribe(test_server, test_proxy, protocol, loop, from_details, to_details, close_order): +# callback_complete = loop.create_future() +# callback_complete_proxy = loop.create_future() +# +# class ServerDialplan(aiosip.BaseDialplan): +# +# async def resolve(self, *args, **kwargs): +# await super().resolve(*args, **kwargs) +# return self.subscribe +# +# async def subscribe(self, request, message): +# await request.prepare(status_code=200) +# callback_complete.set_result(message) +# +# class ProxyDialplan(aiosip.BaseDialplan): +# async def resolve(self, *args, **kwargs): +# await super().resolve(*args, **kwargs) +# +# return self.proxy_subscribe +# +# async def proxy_subscribe(self, request, message): +# dialog = await request.proxy(message) +# callback_complete_proxy.set_result(message) +# async for message in dialog: +# dialog.proxy(message) +# +# app = aiosip.Application(loop=loop, debug=True) +# +# server_app = aiosip.Application(loop=loop, debug=True, dialplan=ServerDialplan()) +# await test_server(server_app) +# +# proxy_app = aiosip.Application(loop=loop, dialplan=ProxyDialplan()) +# proxy = await test_proxy(proxy_app) +# +# peer = await app.connect( +# protocol=protocol, +# remote_addr=(proxy.sip_config['server_host'], proxy.sip_config['server_port']) +# ) +# +# await peer.subscribe( +# expires=1800, +# from_details=aiosip.Contact.from_header(from_details), +# to_details=aiosip.Contact.from_header(to_details), +# ) +# +# received_request_server = await asyncio.wait_for(callback_complete, timeout=2) +# received_request_proxy = await asyncio.wait_for(callback_complete_proxy, timeout=2) +# +# assert received_request_server.method == 'SUBSCRIBE' +# assert received_request_server.payload == received_request_proxy.payload +# assert received_request_server.headers == received_request_proxy.headers +# +# for item in close_order: +# if item == 'client': +# await app.close() +# elif item == 'server': +# await server_app.close() +# elif item == 'proxy': +# await proxy_app.close() +# else: +# raise ValueError('Invalid close_order') +# +# +# @pytest.mark.parametrize('close_order', itertools.permutations(('client', 'server', 'proxy'))) # noQa C901: too complex +# async def test_proxy_notify(test_server, test_proxy, protocol, loop, from_details, to_details, close_order): +# +# callback_complete = loop.create_future() +# callback_complete_proxy = loop.create_future() +# +# class ServerDialpan(aiosip.BaseDialplan): +# +# async def resolve(self, *args, **kwargs): +# await super().resolve(*args, **kwargs) +# +# return self.subscribe +# +# async def subscribe(self, request, message): +# dialog = await request.prepare(status_code=200) +# await asyncio.sleep(0.2) +# await dialog.notify(payload='1') +# +# class ProxyDialplan(aiosip.BaseDialplan): +# async def resolve(self, *args, **kwargs): +# await super().resolve(*args, **kwargs) +# +# return self.proxy_subscribe +# +# async def proxy_subscribe(self, request, message): +# dialog = await request.proxy(message) +# +# async for message in dialog: +# dialog.proxy(message) +# +# if message.method == 'NOTIFY': +# callback_complete_proxy.set_result(message) +# +# app = aiosip.Application(loop=loop, debug=True) +# +# server_app = aiosip.Application(loop=loop, debug=True, dialplan=ServerDialpan()) +# await test_server(server_app) +# +# proxy_app = aiosip.Application(loop=loop, debug=True, dialplan=ProxyDialplan()) +# proxy = await test_proxy(proxy_app) +# +# peer = await app.connect( +# protocol=protocol, +# remote_addr=(proxy.sip_config['server_host'], proxy.sip_config['server_port']) +# ) +# +# subscription = await peer.subscribe( +# expires=1800, +# from_details=aiosip.Contact.from_header(from_details), +# to_details=aiosip.Contact.from_header(to_details) +# ) +# +# async for msg in subscription: +# await subscription.reply(msg, 200) +# callback_complete.set_result(msg) +# break # We only expect a single message +# +# received_notify_server = await asyncio.wait_for(callback_complete, timeout=2) +# received_notify_proxy = await asyncio.wait_for(callback_complete_proxy, timeout=2) +# +# assert received_notify_server.method == 'NOTIFY' +# assert received_notify_server.payload == '1' +# +# assert received_notify_server.payload == received_notify_proxy.payload +# assert received_notify_server.headers == received_notify_proxy.headers +# +# for item in close_order: +# if item == 'client': +# await app.close() +# elif item == 'server': +# await server_app.close() +# elif item == 'proxy': +# await proxy_app.close() +# else: +# raise ValueError('Invalid close_order')