Beginning bumper's new journey #4

Merged
bmartin5692 merged 37 commits from dev into master 2019-02-22 14:34:25 +01:00
3 changed files with 94 additions and 5 deletions
Showing only changes of commit 2e874307ed - Show all commits

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ __pycache__
Pipfile.lock Pipfile.lock
/bumper.sublime-project /bumper.sublime-project
/bumper.sublime-workspace /bumper.sublime-workspace
.vscode/

View file

@ -1,13 +1,9 @@
[[source]] [[source]]
url = "https://pypi.python.org/simple" url = "https://pypi.python.org/simple"
verify_ssl = true verify_ssl = true
name = "pypi" name = "pypi"
[packages] [packages]
hbmqtt = "*"
[dev-packages] [dev-packages]

92
bumper/mqttserver.py Normal file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env python3
import logging
import asyncio
import os
from hbmqtt.broker import Broker
import hbmqtt
default_config = {
'listeners': {
'default': {
'type': 'tcp',
'bind': '0.0.0.0:1883',
},
'tls1': {
'bind': '0.0.0.0:8883',
'ssl': 'on',
#'cafile': '/some/cafile',
#'capath': '/some/folder',
#'capath': 'certificate data',
'certfile': './certs/cert.pem',
'keyfile': './certs/key.pem',
},
},
'sys_interval': 10,
'auth': {
'allow-anonymous': True,
'password-file': os.path.join(os.path.dirname(os.path.realpath(__file__)), "passwd"),
'plugins': [
'VacBotAuth'
]
},
'topic-check': {
'enabled': False
}
}
class BaseAuthPlugin:
def __init__(self, context):
self.context = context
try:
self.auth_config = self.context.config['auth']
except KeyError:
self.context.logger.warning("'auth' section not found in context configuration")
def authenticate(self, *args, **kwargs):
if not self.auth_config:
# auth config section not found
self.context.logger.warning("'auth' section not found in context configuration")
return False
return True
class VacBotAuth(BaseAuthPlugin):
def __init__(self, context):
super().__init__(context)
@asyncio.coroutine
def authenticate(self, *args, **kwargs):
authenticated = super().authenticate(*args, **kwargs)
if authenticated:
allow_anonymous = self.auth_config.get('allow-anonymous', True) # allow anonymous by default
if allow_anonymous:
authenticated = True
self.context.logger.debug("Authentication success: config allows anonymous")
else:
try:
session = kwargs.get('session', None)
authenticated = True if session.username else False
if self.context.logger.isEnabledFor(logging.DEBUG):
if authenticated:
self.context.logger.debug("Authentication success: session has a non empty username")
else:
self.context.logger.debug("Authentication failure: session has an empty username")
except KeyError:
self.context.logger.warning("Session informations not available")
authenticated = False
return authenticated
@asyncio.coroutine
def broker_coro():
broker = hbmqtt.broker.Broker(config=default_config, plugin_namespace=".")
print(broker.plugins_manager)
yield from broker.start()
if __name__ == '__main__':
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
logging.basicConfig(level=logging.DEBUG, format=formatter)
eloop = asyncio.get_event_loop().run_until_complete(broker_coro())
asyncio.get_event_loop().run_forever()