-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathchannel.uc
More file actions
executable file
·273 lines (246 loc) · 6.65 KB
/
Copy pathchannel.uc
File metadata and controls
executable file
·273 lines (246 loc) · 6.65 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import * as struct from "struct";
import * as crypto from "crypto.crypto";
const meshtasticChannelPresets = [
"ShortTurbo AQ==",
"ShortSlow AQ==",
"ShortFast AQ==",
"MediumSlow AQ==",
"MediumFast AQ==",
"LongSlow AQ==",
"LongFast AQ==",
"LongMod AQ==",
"LongTurbo AQ=="
];
const meshcorePublicNamekey = "MeshCore izOH6cXN6mrJ5e26oRXNcg==";
const arednChannelPostfix = " og==";
const arednPublicChannel = `AREDN${arednChannelPostfix}`;
global.channelByNameKey = {};
global.channelsByMeshtasticHash = {};
global.channelsByMeshcoreHash = {};
global.localChannelByNameKey = {};
let meshtasticChannel;
function expandSymmetricKey(key)
{
key = b64dec(key);
if (length(key) === 1) {
return [ 0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, 0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, ord(key, 0) ];
}
else {
const crypto = [];
for (let i = 0; i < length(key); i++) {
crypto[i] = ord(key, i);
}
return crypto;
}
}
function getMeshtasticHash(name, crypto)
{
let hash = 0;
for (let i = 0; i < length(name); i++) {
hash ^= ord(name, i);
}
for (let i = 0; i < length(crypto); i++) {
hash ^= crypto[i];
}
return hash;
}
function getMeshcoreHash(key)
{
return crypto.sha256hash(struct.pack(`${length(key)}B`, ...key))[0];
}
export function isAREDNPreset(namekey)
{
return namekey === arednPublicChannel;
};
export function isAREDNOnly(namekey)
{
return rindex(namekey, arednChannelPostfix) !== -1;
};
export function isMeshtasticPreset(namekey)
{
return !namekey || index(meshtasticChannelPresets, namekey) !== -1;
};
export function isMeshcorePreset(namekey)
{
return namekey === meshcorePublicNamekey;
};
function addMessageNameKey(namekey)
{
if (channelByNameKey[namekey]) {
return channelByNameKey[namekey];
}
const nk = split(namekey, " ");
const skey = expandSymmetricKey(nk[1]);
const meshtastichash = getMeshtasticHash(nk[0], skey);
const meshcorehash = getMeshcoreHash(skey);
const chan = { namekey: namekey, symmetrickey: skey, meshtastichash: meshtastichash, meshcorehash: meshcorehash, telemetry: false };
channelByNameKey[namekey] = chan;
// The channelsBy... lists are used to speed packet decoding at the edge, so no point including channels in these
// lists if they can never decode the specific meshtasticore type.
if (!isAREDNOnly(namekey)) {
if (!isMeshcorePreset(namekey)) {
push(channelsByMeshtasticHash[meshtastichash] ?? (channelsByMeshtasticHash[meshtastichash] = []), chan);
}
if (!isMeshtasticPreset(namekey)) {
push(channelsByMeshcoreHash[meshcorehash] ?? (channelsByMeshcoreHash[meshcorehash] = []), chan);
}
}
return chan;
}
function removeMessageNameKey(namekey)
{
const chan = channelByNameKey[namekey];
if (chan) {
let idx = index(channelsByMeshtasticHash[chan.meshtastichash], chan);
if (idx >= 0) {
splice(channelsByMeshtasticHash[chan.meshtastichash], idx, 1);
}
idx = index(channelsByMeshcoreHash[chan.meshcorehash], chan);
if (idx >= 0) {
splice(channelsByMeshcoreHash[chan.meshcorehash], idx, 1);
}
delete channelByNameKey[namekey];
}
}
function setLocalChannel(config)
{
const namekey = config.namekey;
// Validate
const nk = split(namekey, " ");
// Two parts, separated by space
if (length(nk) !== 2) {
return false;
}
// Name cannot be more than 13 characters
if (length(nk[0]) > 13) {
return false;
}
try {
// Valid key sizes: 1, 16, 32
const lkey = length(b64dec(nk[1]));
if (!(lkey === 1 || lkey === 16 || lkey === 32)) {
return false;
}
}
catch (_) {
return false;
}
const chan = addMessageNameKey(namekey);
if (isMeshtasticPreset(namekey)) {
chan.telemetry = true;
meshtasticChannel = chan;
}
if (isAREDNPreset(namekey)) {
chan.telemetry = true;
}
if (config.telemetry !== null) {
chan.telemetry = config.telemetry;
}
localChannelByNameKey[namekey] = chan;
return true;
};
export function getChannelsByMeshtasticHash(hash)
{
if (!hash && meshtasticChannel) {
return [ meshtasticChannel ];
}
return channelsByMeshtasticHash[hash];
};
export function getChannelsByMeshcoreHash(hash)
{
return channelsByMeshcoreHash[hash];
};
export function getLocalChannelByNameKey(namekey)
{
if (!namekey) {
return meshtasticChannel;
}
return localChannelByNameKey[namekey];
};
export function getChannelByNameKey(namekey)
{
if (!namekey) {
return meshtasticChannel;
}
return channelByNameKey[namekey];
};
export function getAllChannelNamekeys()
{
return keys(channelByNameKey);
};
export function getAllLocalChannels()
{
return values(localChannelByNameKey);
};
export function getTelemetryChannels()
{
const telemetry = [];
for (let namekey in channelByNameKey) {
const chan = channelByNameKey[namekey];
if (chan.telemetry) {
push(telemetry, chan);
}
}
return telemetry;
};
export function updateLocalChannels(channels)
{
const oldLocalChannelByNameKey = localChannelByNameKey;
localChannelByNameKey = {};
for (let i = 0; i < length(channels); i++) {
setLocalChannel(channels[i]);
}
const newchannels = [];
for (let namekey in localChannelByNameKey) {
if (!oldLocalChannelByNameKey[namekey]) {
push(newchannels, namekey);
}
else {
delete oldLocalChannelByNameKey[namekey];
}
}
return { newchannels: newchannels, oldchannels: keys(oldLocalChannelByNameKey) };
};
export function updateRemoteNameKeys(namekeys)
{
const remotekeys = {};
for (let nk in channelByNameKey) {
if (!localChannelByNameKey[nk]) {
remotekeys[nk] = true;
}
}
for (let i = 0; i < length(namekeys); i++) {
const namekey = namekeys[i];
if (!remotekeys[namekey]) {
addMessageNameKey(namekey);
}
else {
delete remotekeys[namekey];
}
}
for (let nk in remotekeys) {
removeMessageNameKey(nk);
}
};
export function isDirect(namekey)
{
return index(namekey, "DirectMessages ") === 0;
};
export function setup(config)
{
const channels = config.channels;
if (channels) {
for (let i = 0; i < length(channels); i++) {
setLocalChannel(channels[i]);
}
}
};
export function tick()
{
};
export function process(msg)
{
};
export function cmd(msg, reply)
{
};