WIP: Moving along
Getting closer to handling
This commit is contained in:
parent
2e874307ed
commit
134d698eeb
4 changed files with 172 additions and 104 deletions
|
|
@ -6,87 +6,52 @@ 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)
|
||||
class MQTTServer():
|
||||
clients = []
|
||||
exit_flag = False
|
||||
default_config = {}
|
||||
|
||||
@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=".")
|
||||
def broker_coro(self):
|
||||
broker = hbmqtt.broker.Broker(config=self.default_config, plugin_namespace=".")
|
||||
|
||||
print(broker.plugins_manager)
|
||||
yield from broker.start()
|
||||
yield from broker.start()
|
||||
|
||||
def __init__(self, address, run_async=False):
|
||||
try:
|
||||
# Initialize bot server
|
||||
self.default_config = {
|
||||
'listeners': {
|
||||
'default': {
|
||||
'type': 'tcp',
|
||||
},
|
||||
'tls1': {
|
||||
'bind': "{}:{}".format(address[0], address[1]),
|
||||
'ssl': 'on',
|
||||
'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
|
||||
}
|
||||
}
|
||||
if run_async:
|
||||
self.run()
|
||||
finally:
|
||||
print("Done")
|
||||
|
||||
def run(self):
|
||||
formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s"
|
||||
logging.basicConfig(level=logging.DEBUG, format=formatter)
|
||||
eloop = asyncio.get_event_loop().run_until_complete(self.broker_coro())
|
||||
asyncio.get_event_loop().run_forever()
|
||||
|
||||
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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue