forked from mbachry/mosquitto_pyauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestmosquitto.py
229 lines (201 loc) · 8.43 KB
/
testmosquitto.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import mosquitto_pyplugin
import json
import sys
import time
import asyncio
from distutils.util import strtobool
class IntervalTimer:
def __init__(self, interval, callback):
self._interval = interval
self._callback = callback
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = new_event_loop()
asyncio.set_event_loop(loop)
self._task = loop.create_task(self._job())
async def _job(self):
while True:
offset = int(time.time()) % self._interval
await asyncio.sleep(self._interval - offset)
await self._callback()
def cancel(self):
self._task.cancel()
class Plugin:
def __init__(self, options):
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.__init__ (options: {})'.format(options)
)
self._add_value = options.get('add_value', 4711)
self._time_of_day = int(options.get('time_of_day', 0))
self._anonymous_allowed = bool(strtobool(options.get(
'allow_anonymous',
'False'
)))
if self._time_of_day > 0:
self.timer = IntervalTimer(self._time_of_day, self.publish_time)
async def publish_time(self):
mosquitto_pyplugin.broker_publish(
None,
'time-of-day',
time.asctime(time.gmtime())
)
def plugin_cleanup(self, options):
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.plugin_cleanup (options: {})'.format(options)
)
def basic_auth(self, client, username, password):
client_id = mosquitto_pyplugin.client_id(client)
client_address = mosquitto_pyplugin.client_address(client)
client_protocol = mosquitto_pyplugin.client_protocol(client)
client_protocol_version = mosquitto_pyplugin.client_protocol_version(
client
)
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.basic_auth (client_id: {} username: {} password: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {})'.format(
client_id, username, password, client_address, client_protocol,
client_protocol_version
)
)
client_certificate = mosquitto_pyplugin.client_certificate(client)
if client_certificate:
print(
'Plugin.basic_auth certificate (client_id: {}):\n{}'.format(
client_id, client_certificate
),
file=sys.stderr,
)
if ((not username or not password)
and not self._anonymous_allowed):
return mosquitto_pyplugin.MOSQ_ERR_AUTH
return mosquitto_pyplugin.MOSQ_ERR_SUCCESS
def acl_check(self, client, topic, access, payload):
client_id = mosquitto_pyplugin.client_id(client)
client_username = mosquitto_pyplugin.client_username(client)
client_address = mosquitto_pyplugin.client_address(client)
client_protocol = mosquitto_pyplugin.client_protocol(client)
client_protocol_version = mosquitto_pyplugin.client_protocol_version(
client
)
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.acl_check {}'.format(
mosquitto_pyplugin.topic_matches_sub('/#', topic)
)
)
access_text = None
if access == mosquitto_pyplugin.MOSQ_ACL_READ:
access_text = 'READ'
elif access == mosquitto_pyplugin.MOSQ_ACL_SUBSCRIBE:
access_text = 'SUBSCRIBE'
elif access == mosquitto_pyplugin.MOSQ_ACL_WRITE:
access_text = 'WRITE'
if access_text:
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.acl_check {} (client_id: {} client_username: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {} topic: {} access: {} '
'payload: {!r})'
.format(
access_text, client_id, client_username, client_address,
client_protocol, client_protocol_version, topic, access,
payload
)
)
return mosquitto_pyplugin.MOSQ_ERR_SUCCESS
def psk_key(self, client, hint, identity):
client_id = mosquitto_pyplugin.client_id(client)
client_username = mosquitto_pyplugin.client_username(client)
client_address = mosquitto_pyplugin.client_address(client)
client_protocol = mosquitto_pyplugin.client_protocol(client)
client_protocol_version = mosquitto_pyplugin.client_protocol_version(
client
)
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.psk_key (client_id: {} client_username: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {} hint: {} identity: {})'.format(
client_id, client_username, client_address, client_protocol,
client_protocol_version, hint, identity
)
)
return '0123456789'
def disconnect(self, client, reason):
client_id = mosquitto_pyplugin.client_id(client)
client_username = mosquitto_pyplugin.client_username(client)
client_address = mosquitto_pyplugin.client_address(client)
client_protocol = mosquitto_pyplugin.client_protocol(client)
client_protocol_version = mosquitto_pyplugin.client_protocol_version(
client
)
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'Plugin.disconnect (client_id: {} client_username: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {} reason: {})'.format(
client_id, client_username, client_address, client_protocol,
client_protocol_version, reason
)
)
def message(self, client, message_event):
client_id = mosquitto_pyplugin.client_id(client)
client_username = mosquitto_pyplugin.client_username(client)
client_address = mosquitto_pyplugin.client_address(client)
client_protocol = mosquitto_pyplugin.client_protocol(client)
client_protocol_version = mosquitto_pyplugin.client_protocol_version(
client
)
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'<<<< Plugin.message (client_id: {} client_username: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {} message: {})'.format(
client_id, client_username, client_address, client_protocol,
client_protocol_version, message_event
)
)
time_of_arrival = time.asctime(time.gmtime())
message_event.retain = False
if message_event.payload:
try:
data = json.loads(message_event.payload)
data['added_value'] = self._add_value
data['time-of-arrival'] = time_of_arrival
if 'Wifi' in data:
if 'SSId' in data['Wifi']:
data['Wifi']['SSId'] = 'xxxxxxxxx'
if 'BSSId' in data['Wifi']:
del data['Wifi']['BSSId']
message_event.payload = json.dumps(data)
except json.decoder.JSONDecodeError:
pass
message_event.properties.extend(
(
('user-property', ('time-of-arrival', time_of_arrival)),
('user-property', ('other-property', 'other-value')),
)
)
message_event.topic = f'changed/{message_event.topic}'
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'>>>> Plugin.message (client_id: {} client_username: {} '
'client_address: {} client_protocol: {} '
'client_protocol_version: {} message: {})'.format(
client_id, client_username, client_address, client_protocol,
client_protocol_version, message_event
)
)
return mosquitto_pyplugin.MOSQ_ERR_SUCCESS
def plugin_init(options):
mosquitto_pyplugin.log(
mosquitto_pyplugin.MOSQ_LOG_INFO,
'plugin_init (options: {})'.format(options)
)
return Plugin(options)