-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreadonlydevice.py
More file actions
executable file
·220 lines (167 loc) · 7.2 KB
/
readonlydevice.py
File metadata and controls
executable file
·220 lines (167 loc) · 7.2 KB
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
#!/usr/bin/env python
import asyncio
from aiotools import current_taskgroup, TaskGroup
import sys
from collections import Counter
import time
import packet
from identity import Identity, AdvertData, IdentityStore, FileIdentityStore, SelfIdentity
from binascii import unhexlify, hexlify
import groupchannel
from exceptions import *
from ed25519_wrapper import ED25519_Wrapper
import interfaces.interface as interface
import configuration
from dispatch import default_dispatch
import logging
# Config file
if len(sys.argv)>1:
configfile = sys.argv[1]
config = configuration.get_config(configfile)
else:
config = configuration.get_config("readonly.toml")
# Default logging configuration
if sys.version_info >= (3,12):
# 3.12 adds 'taskName' to the LogRecord attributes
logformat = '%(asctime)s %(levelname)-8s %(taskName)s: %(name)s: %(message)s'
else:
logformat = '%(asctime)s %(levelname)-8s: %(name)s: %(message)s'
logging_default_config = configuration.get_config(
{'format': logformat,
'dateformat': '%Y-%m-%d %H:%M:%S',
'level': 'debug',
'file': 'readonly.log' })
loggingconfig = config.get('log', view=True)
loggingconfig.set_default(logging_default_config)
loglevel = getattr(logging, loggingconfig.get('level','DEBUG').upper(), logging.DEBUG)
logger = logging.getLogger(__name__)
logging.basicConfig(
format=loggingconfig.get('format'),
datefmt=loggingconfig.get('dateformat'),
filename=loggingconfig.get('file'),
level=loglevel)
logger.debug("Logging configured")
# Configure interfaces
packetinterfaces = interface.configure_interfaces(config)
# Create dispatcher
dispatcher = default_dispatch()
# Identity, if one is provided in config use that, else a random one
k = config.get('privatekey')
if k is not None:
k = unhexlify(k.encode())
private_key = ED25519_Wrapper(k)
if k is None:
print("Generated random identity key:", hexlify(private_key.private_key).decode('utf-8'))
logger.info(f"Generated random identity key {hexlify(private_key.private_key).decode('utf-8')}")
print("Identity public key:", hexlify(private_key.public_key).decode('utf-8'))
me = SelfIdentity(private_key=private_key, name='Readonly device')
ids_file = config.get('contacts')
if ids_file is None:
logger.debug("No identity file configured, using memory store")
ids = IdentityStore()
else:
logger.debug(f"Using identity file {ids_file}")
ids = FileIdentityStore(ids_file, private_key)
# Channels
# Create a list consisting of only the public channel
channels = groupchannel.channels(None, 1, True)
# Any additional channels
additional_channels = config.get('channels', [])
for c in additional_channels:
if c.startswith('#'):
channel = groupchannel.Channel(name=c)
else:
k = config.get('channel.'+c)
if k is not None:
channel = groupchannel.Channel(name=c, key=unhexlify(k.encode()))
else:
logger.error(f"No key configured for channel {c}")
continue
channels.append(channel)
logger.info(f"Configured additional channel {c}")
# Stats
stats_duplicate=0
stats_flood=Counter()
stats_direct=Counter()
stats_packettype=Counter()
async def periodicstats(file=None):
if file is None:
return
with open(file, "a") as statsfile:
logger.info(f"Writing periodic stats to {file}")
while True:
await asyncio.sleep(300)
t = int(time.time())
s = f"Packet receive stats at {time.ctime(t)} ({t})"
print(s, file=statsfile)
print("-" * len(s), file=statsfile)
rx = sum(stats_flood.values()) + sum(stats_direct.values())
print("RX packets:", rx, file=statsfile)
print("Duplicates:", stats_duplicate, file=statsfile)
print("Direct:", sum(stats_direct.values()), file=statsfile)
for hop in sorted(stats_direct.keys()):
print(f" {hop} hop(s): {stats_direct[hop]}", file=statsfile)
print("Flood:", sum(stats_flood.values()), file=statsfile)
for hop in sorted(stats_flood.keys()):
print(f" {hop} hop(s): {stats_flood[hop]}", file=statsfile)
print("Received packet types:", file=statsfile)
for type in sorted(stats_packettype.keys()):
print(f" {type}:{' '*(10-len(type))}{stats_packettype[type]}", file=statsfile)
print('', file=statsfile)
async def processpackets():
global stats_duplicate
rx_queue = dispatcher.add_queue('Read only device')
while True:
try:
m = await rx_queue.get()
if isinstance(m, bytes):
# Contains just a packet
receivedpacket = packet.MC_Incoming.convert_packet(bytearray(m), me, ids, channels)
else:
# Packet, RSSI, SNR
(p, rssi, snr) = m
receivedpacket = packet.MC_Incoming.convert_packet(bytearray(p), me, ids, channels, rssi, snr)
logger.debug("New packet: %s", hexlify(receivedpacket.packet).decode('utf-8'))
if receivedpacket.is_direct():
stats_direct[receivedpacket.pathlen] += 1
elif receivedpacket.is_flood():
stats_flood[receivedpacket.pathlen] += 1
stats_packettype[receivedpacket.typename] += 1
if dispatcher.hasSeen(receivedpacket):
logger.info(f"Duplicate! Already seen this packet")
stats_duplicate += 1
continue
logger.debug("Class: %s", receivedpacket.__class__.__name__)
if isinstance(receivedpacket, packet.MC_Advert):
if receivedpacket.advert.validate():
id = Identity(receivedpacket.advert)
id.create_shared_secret(private_key)
ids.add_identity(id)
output = None
if isinstance(receivedpacket, packet.MC_Advert) and receivedpacket.advert.validate():
output = f"[Advert] {receivedpacket.advert.name}"
if receivedpacket.advert.latlon is not None:
output += f" {receivedpacket.advert.latlon}"
elif isinstance(receivedpacket, packet.MC_GroupText) and receivedpacket.channel is not None:
output = f"[{receivedpacket.channel.strname}] {receivedpacket.message.message.decode('utf8')}"
elif isinstance(receivedpacket, packet.MC_Text) and receivedpacket.decrypted:
output = f"[@ {receivedpacket.source.name}] {receivedpacket.text}"
if output:
print(time.strftime('%Y-%m-%d %H:%M:%S'), output)
logger.info(str(receivedpacket))
except InvalidMeshcorePacket as e:
logger.warning(f"Error: {repr(e)}")
async def main():
async with TaskGroup() as tg:
for packetinterface in packetinterfaces:
await packetinterface.start()
dispatcher.add_interface(packetinterface)
await dispatcher.start()
tg.create_task(processpackets(), name="Packet processor")
statsfile = config.get('statsfile')
tg.create_task(periodicstats(statsfile), name="Periodic stats")
try:
asyncio.run(main())
except KeyboardInterrupt:
logger.info("Exiting on keyboard interrupt")
print("\nBye")