-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Current dialplan was not correct and complicated. This rework simplify it greatly and provide an easy way to do your own routing I think it will be a nice place to put helpers for authentifications. No idea if that will provide any advantages over a flat mapping of methods / middlewares issue #78
- Loading branch information
Showing
11 changed files
with
285 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,9 @@ | ||
import logging | ||
|
||
from collections import MutableMapping | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class Dialplan: | ||
def __init__(self, default=None): | ||
self._users = {} | ||
self.default = default | ||
|
||
async def resolve(self, username, protocol, local_addr, remote_addr): | ||
LOG.debug('Resolving dialplan for %s connecting on %s from %s via %s', | ||
username, local_addr, remote_addr, protocol) | ||
router = self._users.get(username) | ||
if not router: | ||
router = self._users.get('*', self.default) | ||
return router | ||
|
||
def add_user(self, username, router): | ||
self._users[username] = router | ||
|
||
|
||
class Router(MutableMapping): | ||
def __init__(self, default=None): | ||
self._routes = {} | ||
if default: | ||
self._routes['*'] = default | ||
|
||
# MutableMapping API | ||
def __eq__(self, other): | ||
return self is other | ||
|
||
def __getitem__(self, key): | ||
try: | ||
return self._routes[key.lower()] | ||
except KeyError: | ||
return self._routes['*'] | ||
|
||
def __setitem__(self, key, value): | ||
self._routes[key.lower()] = value | ||
|
||
def __delitem__(self, key): | ||
del self._routes[key.lower()] | ||
|
||
def __len__(self): | ||
return len(self._routes) | ||
|
||
def __iter__(self): | ||
return iter(self._routes) | ||
class BaseDialplan: | ||
async def resolve(self, method, username, protocol, local_addr, remote_addr): | ||
LOG.debug('Resolving dialplan for %s %s connecting on %s from %s via %s', | ||
method, username, local_addr, remote_addr, protocol) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import argparse | ||
import asyncio | ||
import contextlib | ||
import logging | ||
import random | ||
|
||
import aiosip | ||
|
||
sip_config = { | ||
'srv_host': 'qd.allocloud.com', | ||
'srv_port': 10065, | ||
'realm': 'XXXXXX', | ||
'user': 'subscriber', | ||
'pwd': 'hunter2', | ||
'local_host': '0.0.0.0', | ||
'local_port': random.randint(6001, 6100) | ||
} | ||
|
||
|
||
async def run_subscription(peer, duration): | ||
subscription = await peer.subscribe( | ||
from_details=aiosip.Contact.from_header('sip:{}@{}:{}'.format( | ||
sip_config['user'], sip_config['local_host'], | ||
sip_config['local_port'])), | ||
to_details=aiosip.Contact.from_header('sip:666@{}:{}'.format( | ||
sip_config['srv_host'], sip_config['srv_port'])), | ||
password=sip_config['pwd']) | ||
|
||
async def reader(): | ||
async for request in subscription: | ||
print('NOTIFY:', request.payload) | ||
await subscription.reply(request, status_code=200) | ||
|
||
with contextlib.suppress(asyncio.TimeoutError): | ||
await asyncio.wait_for(reader(), timeout=duration) | ||
|
||
if subscription.status_code == 200: | ||
await subscription.close() | ||
|
||
|
||
async def start(app, protocol, duration): | ||
if protocol is aiosip.WS: | ||
peer = await app.connect( | ||
'ws://{}:{}'.format(sip_config['srv_host'], sip_config['srv_port']), | ||
protocol=protocol, | ||
local_addr=(sip_config['local_host'], sip_config['local_port'])) | ||
else: | ||
peer = await app.connect( | ||
(sip_config['srv_host'], sip_config['srv_port']), | ||
protocol=protocol, | ||
local_addr=(sip_config['local_host'], sip_config['local_port'])) | ||
|
||
await run_subscription(peer, duration) | ||
await app.close() | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('-p', '--protocol', default='udp') | ||
parser.add_argument('-d', '--duration', type=int, default=5) | ||
args = parser.parse_args() | ||
|
||
loop = asyncio.get_event_loop() | ||
app = aiosip.Application(loop=loop) | ||
|
||
if args.protocol == 'udp': | ||
loop.run_until_complete(start(app, aiosip.UDP, args.duration)) | ||
elif args.protocol == 'tcp': | ||
loop.run_until_complete(start(app, aiosip.TCP, args.duration)) | ||
elif args.protocol == 'ws': | ||
loop.run_until_complete(start(app, aiosip.WS, args.duration)) | ||
else: | ||
raise RuntimeError("Unsupported protocol: {}".format(args.protocol)) | ||
|
||
loop.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
logging.basicConfig(level=logging.DEBUG) | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.