-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_broker.py
executable file
·197 lines (144 loc) · 5.73 KB
/
start_broker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
from tornado.options import parse_command_line, options, define
from tornado.ioloop import IOLoop
import signal
from logging import getLogger
from broker.access_control import SinglePasswordAuthentication, NoAuthentication, FileAuthentication, WebAuthentication
from broker.persistence import InMemoryPersistence, RedisPersistence
from broker.server import MQTTServer
from paho.mqtt.paho_partner_pair import Paho_Partner_Pair
define('rhost', 'localhost', str, "Redis host address")
define('rport', 6379, int, "Redis host port")
define('rpassword', None, str, "Redis password")
define('redis', False, bool, "Use redis as queue backend")
define('ssl', False, bool, "Use SSL/TLS on socket")
define('sslkey', None, str, "SSL/TLS Key file path")
define('sslcert', None, str, "SSL/TLS Certificate file path")
define('authfile', None, str, "Authentication and authorization config file path")
define('webauth', None, str, "Authentication and authorization web API address")
define('password', None, str, "Password for client authentication")
class OsSignalHandler():
def __init__(self, log):
self.log = log
self.servers = list()
# handle interrupts
signal.signal(signal.SIGINT, self.handle_signal)
# Handle unix kill command
signal.signal(signal.SIGTERM, self.handle_signal)
def add(self, server):
self.servers.append(server)
def handle_keyboard_interrupt(self):
self._beautifully_exit()
def handle_signal(self, *args):
self._beautifully_exit()
def _beautifully_exit(self):
""" Handles client disconnection before stopping the broker. """
print("Disconnecting clients")
self.log.info('disconnecting clients')
for server in self.servers:
server.disconnect_all_clients()
IOLoop.instance().stop()
def create_redis_persistence(options, log):
import redis
redis_client = redis.StrictRedis(
host=options.rhost,
port=options.rport,
password=options.rpassword
)
print("[option] Using redis at (%s, %d)" %
(options.rhost, options.rport))
log.info("[option] Using redis at (%s, %d)" %
(options.rhost, options.rport))
return RedisPersistence(redis_client)
def create_ssl_options(options):
if options.sslkey and options.sslcert:
import ssl
ssl_options = {
"ssl_version": ssl.PROTOCOL_TLSv1,
"cert_reqs": ssl.CERT_NONE,
"keyfile": options.sslkey,
"certfile": options.sslcert,
}
print("[option] Using SSL/TLS")
else:
print("[option] To use SSL/TLS both sslkey and sslcert must be defined")
ssl_options = None
return ssl_options
def start_mqtt_server(persistence, clients,
authentication_agent, log):
EXTERNAL_ADDRESS = "test.mosquitto.org"
server = MQTTServer(authentication=authentication_agent,
persistence=persistence,
clients=clients,
ssl_options=None)
ppp = Paho_Partner_Pair()
server.listen(1883)
ppp.connect(EXTERNAL_ADDRESS)
print("listening port 1883")
log.info("listening port 1883")
return server
def start_secure_mqtt_server(persistence, clients,
authentication_agent, log):
ssl_options = create_ssl_options(options)
server = MQTTServer(authentication=authentication_agent,
persistence=persistence,
clients=clients,
ssl_options=ssl_options)
server.listen(8883)
print("listening port 8883")
log.info("listening port 8883")
return server
def get_persistence(options, log):
if options.redis:
log.info('persistence: redis')
return create_redis_persistence(options, log)
else:
log.info('persistence: memory')
return InMemoryPersistence()
def get_authentication_agent(options, log):
if options.authfile is not None:
print("auth: file")
log.info("authentication agent: file authentication")
return FileAuthentication(options.authfile)
elif options.webauth is not None:
print("auth: web")
log.info("authentication agent: web authentication")
return WebAuthentication(options.webauth)
elif options.password is not None:
print("auth: single pw authentication")
log.info("authentication agent: single password authentication")
return SinglePasswordAuthentication(options.password)
else:
print("auth: no authentication")
log.info("authentication agent: no authentication")
return NoAuthentication()
def main():
log = getLogger('activity.broker')
log.info('starting broker')
persistence = get_persistence(options, log)
authentication_agent = get_authentication_agent(options, log)
signal_handler = OsSignalHandler(log)
clients = dict()
log.info('starting server')
server = start_mqtt_server(persistence, clients,
authentication_agent, log)
signal_handler.add(server)
if options.ssl:
print("starting secure server")
log.info('starting secure server')
sserver = start_secure_mqtt_server(persistence, clients,
authentication_agent,
log)
signal_handler.add(sserver)
else:
sserver = None
try:
print("MQTT-Broker Started")
log.info('broker started')
IOLoop.instance().start()
except KeyboardInterrupt:
signal_handler.handle_keyboard_interrupt()
print("MQTT-Broker Stopped")
log.info('broker stopped')
parse_command_line()
main()