mirror of
https://github.com/markqvist/Reticulum.git
synced 2026-11-19 22:46:32 -05:00
Compare commits
1478 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cff4d03fdb | ||
|
|
528986836b | ||
|
|
01ffb98ee5 | ||
|
|
eb00622fef | ||
|
|
e8b3b14b73 | ||
|
|
ef3e80868e | ||
|
|
48ba76df9c | ||
|
|
41260ea163 | ||
|
|
1f7cd14513 | ||
|
|
b9903c53a8 | ||
|
|
1a71073cc0 | ||
|
|
f7b54ccc11 | ||
|
|
3f55e9ebeb | ||
|
|
c90a5d5887 | ||
|
|
9568b70458 | ||
|
|
8e7dc8b776 | ||
|
|
4a058544a5 | ||
|
|
8c73e203b1 | ||
|
|
70eb417d90 | ||
|
|
5bd93ad979 | ||
|
|
d72cfdd7f0 | ||
|
|
833f6cf2aa | ||
|
|
05ac4bba8e | ||
|
|
0123133e71 | ||
|
|
f876ed8dd9 | ||
|
|
48c7706188 | ||
|
|
4916017aee | ||
|
|
12b2b8c013 | ||
|
|
cc5d6dd065 | ||
|
|
25cce0a847 | ||
|
|
2b1a984c91 | ||
|
|
338df09607 | ||
|
|
aecc1547da | ||
|
|
da368e05e7 | ||
|
|
884f109b2b | ||
|
|
fd36a97220 | ||
|
|
51fa841327 | ||
|
|
b7a93b9b6b | ||
|
|
13b98a6bc2 | ||
|
|
e35bd08f18 | ||
|
|
715707b1bf | ||
|
|
d077d1ab00 | ||
|
|
bb63dd4406 | ||
|
|
e96c81c3f2 | ||
|
|
29ce7ca568 | ||
|
|
ad6c709e80 | ||
|
|
f361a354ba | ||
|
|
096e6726b4 | ||
|
|
a450be5d7e | ||
|
|
85d3697938 | ||
|
|
29dc449af8 | ||
|
|
c850cdac6c | ||
|
|
350b7e2614 | ||
|
|
56b23c4810 | ||
|
|
464e75986e | ||
|
|
3281e28315 | ||
|
|
daec67a116 | ||
|
|
819076c378 | ||
|
|
d183791956 | ||
|
|
01e7ed8443 | ||
|
|
d5f1e014db | ||
|
|
6852971d3c | ||
|
|
04585ff2a8 | ||
|
|
54159d1d93 | ||
|
|
134dc6777c | ||
|
|
20e25a9f0c | ||
|
|
9b6b724b08 | ||
|
|
f96cef7ed9 | ||
|
|
db67adea0a | ||
|
|
3c4b271f41 | ||
|
|
5f2dbc169d | ||
|
|
9ef6c90ea6 | ||
|
|
0bc5c37c39 | ||
|
|
1855f9927f | ||
|
|
70b9d58f15 | ||
|
|
7852f53a4c | ||
|
|
3ac3236b2f | ||
|
|
023849ed29 | ||
|
|
50b6733a1c | ||
|
|
16621a78e3 | ||
|
|
132ec70f75 | ||
|
|
aaf08e6107 | ||
|
|
2c7cf8272b | ||
|
|
c5d205ffea | ||
|
|
e0da9c381e | ||
|
|
d4af18fd6f | ||
|
|
cb1260891d | ||
|
|
2960e6a41d | ||
|
|
baccc6d8a2 | ||
|
|
1fee7af7f2 | ||
|
|
dad1302b1e | ||
|
|
9d67be9acb | ||
|
|
50e939ab0e | ||
|
|
29e5a997ac | ||
|
|
01aef5bc2a | ||
|
|
65a7208a9d | ||
|
|
acb38a8a1c | ||
|
|
c97e2158f5 | ||
|
|
426673e974 | ||
|
|
f15b34870a | ||
|
|
eb826cde90 | ||
|
|
e9d82e698d | ||
|
|
3305fc1cbd | ||
|
|
53dc223a03 | ||
|
|
9b6678b317 | ||
|
|
ceac4a28bd | ||
|
|
8a09c9b550 | ||
|
|
e49fafecce | ||
|
|
ecd677ba7f | ||
|
|
cda4d72bb8 | ||
|
|
d5891fde14 | ||
|
|
fa683e6c21 | ||
|
|
d3874ddedb | ||
|
|
825c2e1993 | ||
|
|
ade030ada9 | ||
|
|
0cc6f5aef3 | ||
|
|
263dc6bae5 | ||
|
|
6927c3c589 | ||
|
|
22bf35d59d | ||
|
|
e64ce99565 | ||
|
|
af4504d675 | ||
|
|
2117087710 | ||
|
|
c4ed59be2c | ||
|
|
1d9067c3bd | ||
|
|
f1fc51ba09 | ||
|
|
fe2fe94dd8 | ||
|
|
d45a5860fd | ||
|
|
eab2983b95 | ||
|
|
1ee89ad8d6 | ||
|
|
2ef855c892 | ||
|
|
81b6ba364c | ||
|
|
9152bc1319 | ||
|
|
c147201470 | ||
|
|
5b2b8afbdf | ||
|
|
1a6e26ac2b | ||
|
|
9b46438233 | ||
|
|
2e6963c388 | ||
|
|
da027e9c0f | ||
|
|
f57fa2655c | ||
|
|
796ef2d44b | ||
|
|
75465efad8 | ||
|
|
34bb4ac88c | ||
|
|
2c142973bf | ||
|
|
a8cc6cb239 | ||
|
|
5f46fc6b48 | ||
|
|
78fb90ea6f | ||
|
|
c0fe08c08d | ||
|
|
d7749b0b06 | ||
|
|
5d38ce3072 | ||
|
|
025f6d609f | ||
|
|
ba3c42459b | ||
|
|
134db17c81 | ||
|
|
4867e889e2 | ||
|
|
ca6451a02a | ||
|
|
ba8c2d76db | ||
|
|
c37b5f7f44 | ||
|
|
9955e1bf67 | ||
|
|
30c6428e0b | ||
|
|
c3af19893f | ||
|
|
8d5871915a | ||
|
|
7c1b648607 | ||
|
|
c68066edff | ||
|
|
51be88fbd8 | ||
|
|
7ec975fc88 | ||
|
|
156c7323a5 | ||
|
|
d3ec00cd51 | ||
|
|
d2dd730801 | ||
|
|
d1da5c3d67 | ||
|
|
9a01a20ce6 | ||
|
|
4ebd4dcc5b | ||
|
|
3e83fcc18b | ||
|
|
156290733e | ||
|
|
044d4c9adc | ||
|
|
af8d05819d | ||
|
|
e24d207312 | ||
|
|
f3ebec9cb1 | ||
|
|
ee82fa75a7 | ||
|
|
ae67448e88 | ||
|
|
a29bfb51c4 | ||
|
|
318b78b416 | ||
|
|
2578750bf7 | ||
|
|
cadee01f9e | ||
|
|
f0875ac44e | ||
|
|
49ae71e06c | ||
|
|
1e6ebd3fa0 | ||
|
|
2bae9ff0dc | ||
|
|
d1a7e0c8a0 | ||
|
|
fca509ff04 | ||
|
|
e40191b3d1 | ||
|
|
7f2b3b9b52 | ||
|
|
0a25e1a9c8 | ||
|
|
ddeb44b1de | ||
|
|
39a888c1e2 | ||
|
|
1f74990752 | ||
|
|
a714c20043 | ||
|
|
942434b288 | ||
|
|
47318034dd | ||
|
|
bb1320913a | ||
|
|
fc95caf519 | ||
|
|
6d1409be1c | ||
|
|
0a0e661beb | ||
|
|
26aec0040e | ||
|
|
135e941fa2 | ||
|
|
1d9ebe8c38 | ||
|
|
4f79110995 | ||
|
|
5cdff28703 | ||
|
|
06dc4ad550 | ||
|
|
6c9d717cd6 | ||
|
|
f25b31702b | ||
|
|
73c60e6a49 | ||
|
|
cce96d38c6 | ||
|
|
71583c5c2d | ||
|
|
d5962d14eb | ||
|
|
d3153bd778 | ||
|
|
69c425d9ae | ||
|
|
a93c6ba5be | ||
|
|
1cf176f6e5 | ||
|
|
b899389956 | ||
|
|
3ad70c63ea | ||
|
|
3b7429149e | ||
|
|
e2ba876ebf | ||
|
|
6ecda49394 | ||
|
|
d23261c8d9 | ||
|
|
84709ccf09 | ||
|
|
7283cb417a | ||
|
|
2c30a88e85 | ||
|
|
24b1ac521e | ||
|
|
e68f4ff118 | ||
|
|
c95fd8e6ca | ||
|
|
3f95b47282 | ||
|
|
aeccf69fc7 | ||
|
|
8a7ad40d64 | ||
|
|
c4be063650 | ||
|
|
87a75c2553 | ||
|
|
c963581a6f | ||
|
|
1f7dbe6dde | ||
|
|
0af42a6885 | ||
|
|
ff4e04099c | ||
|
|
780b7e6bfc | ||
|
|
192898864c | ||
|
|
c7be675739 | ||
|
|
21a2365045 | ||
|
|
a22de54b4a | ||
|
|
99de23c040 | ||
|
|
395241b190 | ||
|
|
15e6dd4a88 | ||
|
|
34b36362ab | ||
|
|
ddd55d50cc | ||
|
|
cb66b3d10f | ||
|
|
8743ddcbe5 | ||
|
|
23101e45f6 | ||
|
|
2289a8effa | ||
|
|
3c479ddb65 | ||
|
|
9d85931c9d | ||
|
|
58881af307 | ||
|
|
7373f81349 | ||
|
|
29f6bc8652 | ||
|
|
abf355b4d7 | ||
|
|
484cf2d1a5 | ||
|
|
e699bb23f1 | ||
|
|
badd850088 | ||
|
|
ffad91431a | ||
|
|
d0231f6ee3 | ||
|
|
b4c6deee78 | ||
|
|
900b4c607c | ||
|
|
9397114f56 | ||
|
|
16ba292bd0 | ||
|
|
032e14384f | ||
|
|
b79f6e0b1c | ||
|
|
3b0425c8a8 | ||
|
|
b043414add | ||
|
|
1e7432af39 | ||
|
|
b67872d946 | ||
|
|
99bfde84bf | ||
|
|
0bc132d7df | ||
|
|
28eba69900 | ||
|
|
1565126ffd | ||
|
|
0dbc9e90a3 | ||
|
|
9199cc9e4b | ||
|
|
7785fd277a | ||
|
|
851c18bc27 | ||
|
|
996d822393 | ||
|
|
0c97853554 | ||
|
|
fae64abf05 | ||
|
|
7330c20c6d | ||
|
|
e7a7c48c0a | ||
|
|
f231fdcb4d | ||
|
|
87823c091f | ||
|
|
36a62e7b1a | ||
|
|
859dc81805 | ||
|
|
409380a458 | ||
|
|
b0933d5c6f | ||
|
|
0bb41bf948 | ||
|
|
27910f25a1 | ||
|
|
602d52f178 | ||
|
|
7396e3994e | ||
|
|
8a82a50fc9 | ||
|
|
5c1141d1ce | ||
|
|
6b6dd73bc8 | ||
|
|
1a7e9e31a1 | ||
|
|
2f29b56e96 | ||
|
|
3bc149e3d5 | ||
|
|
eb8414bee9 | ||
|
|
ea98db4f53 | ||
|
|
ad3c06636b | ||
|
|
9ce4502961 | ||
|
|
a3cd84111f | ||
|
|
d5b2fc5609 | ||
|
|
83a30b187a | ||
|
|
943771a3f9 | ||
|
|
5b4117dadc | ||
|
|
6bc0481cdf | ||
|
|
4eebf0b685 | ||
|
|
149e415109 | ||
|
|
53ed9c1ed3 | ||
|
|
ccc5468bcc | ||
|
|
c1d7c12b52 | ||
|
|
9878b18377 | ||
|
|
6e8c976421 | ||
|
|
b28f5ebf7a | ||
|
|
77c8256a1a | ||
|
|
dc9cf0c2f4 | ||
|
|
fa07c8c87c | ||
|
|
b7fe01237d | ||
|
|
3543dc9802 | ||
|
|
5b04a88026 | ||
|
|
5fdd661081 | ||
|
|
ae0191578f | ||
|
|
e714c559cc | ||
|
|
75cbc73f64 | ||
|
|
d32ba8c1a1 | ||
|
|
ff3a72209e | ||
|
|
803b548951 | ||
|
|
67a45ac5e9 | ||
|
|
e71c01959f | ||
|
|
0a1b9453b2 | ||
|
|
79cad39e78 | ||
|
|
de0dac695b | ||
|
|
281c47f3e0 | ||
|
|
99c428a9f5 | ||
|
|
cb9071b478 | ||
|
|
10848b7cff | ||
|
|
89e73db94a | ||
|
|
0ec84c7491 | ||
|
|
ac9130f01f | ||
|
|
54a919f045 | ||
|
|
bafc09d3a6 | ||
|
|
419956bada | ||
|
|
1a16fdb727 | ||
|
|
3b7cfc0666 | ||
|
|
de76b8939f | ||
|
|
d80245b62c | ||
|
|
dcbc7638d0 | ||
|
|
0536b972d7 | ||
|
|
47add6381d | ||
|
|
be2ba7c2f3 | ||
|
|
dd204e11ce | ||
|
|
be4ee32908 | ||
|
|
9f66b5a6a3 | ||
|
|
d38a8de571 | ||
|
|
dea0124c57 | ||
|
|
aba8d606dd | ||
|
|
8221f82dc0 | ||
|
|
38e9d1cdd4 | ||
|
|
17e980ff79 | ||
|
|
f111709902 | ||
|
|
2d2167140d | ||
|
|
d044db2931 | ||
|
|
516cb106c1 | ||
|
|
7e197542e5 | ||
|
|
77f7632584 | ||
|
|
5e013464da | ||
|
|
b9278ce352 | ||
|
|
d81421dad3 | ||
|
|
629e4fde2d | ||
|
|
7311dc8544 | ||
|
|
0e070aacf6 | ||
|
|
39899651ee | ||
|
|
1034d78828 | ||
|
|
9c2f424aaa | ||
|
|
dca5b9639e | ||
|
|
40281f91da | ||
|
|
cf5d6a796e | ||
|
|
9302415f9e | ||
|
|
9da6664976 | ||
|
|
1694a17a75 | ||
|
|
3fdfe93ec7 | ||
|
|
a9538e9fd1 | ||
|
|
956d688e10 | ||
|
|
1c83e732ad | ||
|
|
5da0870e2a | ||
|
|
171868c6cb | ||
|
|
cfddb9abe6 | ||
|
|
929aba0282 | ||
|
|
7347034f9a | ||
|
|
aef9e5b416 | ||
|
|
e806ae5837 | ||
|
|
602085a1cd | ||
|
|
84597f3186 | ||
|
|
88c629e3ad | ||
|
|
2058596dd1 | ||
|
|
b123a756b0 | ||
|
|
bfab2964b6 | ||
|
|
1bad7f5807 | ||
|
|
26e3ca4f2b | ||
|
|
d25ea38c84 | ||
|
|
e32d4df754 | ||
|
|
561e2f23e1 | ||
|
|
b397870c97 | ||
|
|
6f6751d6b6 | ||
|
|
2aed542e61 | ||
|
|
091e021d0e | ||
|
|
fc69f323a8 | ||
|
|
6b2c5bcf0b | ||
|
|
3885d23293 | ||
|
|
b1aa527861 | ||
|
|
4c69b376b2 | ||
|
|
2b1b9589f1 | ||
|
|
378840a6bf | ||
|
|
7b8923b61f | ||
|
|
b3ef214e72 | ||
|
|
cab513fa31 | ||
|
|
60eb0509f2 | ||
|
|
42f6d64b9c | ||
|
|
6ba7cc8cb4 | ||
|
|
880db0a7b7 | ||
|
|
5f2f4438d0 | ||
|
|
7488336985 | ||
|
|
614e7bd834 | ||
|
|
386ef1f370 | ||
|
|
4ab0755d0a | ||
|
|
d478e380c9 | ||
|
|
05e6717d21 | ||
|
|
9ae3db169e | ||
|
|
68cda4a855 | ||
|
|
4b914fb9a4 | ||
|
|
bde5611a0d | ||
|
|
49073fcca5 | ||
|
|
6738db5437 | ||
|
|
49918c7e1d | ||
|
|
954567c581 | ||
|
|
77a1bb9b19 | ||
|
|
df80181006 | ||
|
|
0c0034f1ae | ||
|
|
2e9443ffe2 | ||
|
|
65222e0de8 | ||
|
|
db7daa4d94 | ||
|
|
c95bb551ab | ||
|
|
acecc1f490 | ||
|
|
39e3854dac | ||
|
|
6cd5be18ec | ||
|
|
0e2041c837 | ||
|
|
338173802d | ||
|
|
40a862acbd | ||
|
|
d825e39379 | ||
|
|
731a63b01b | ||
|
|
7c912d0936 | ||
|
|
503bd6c87b | ||
|
|
1a70bd3cd1 | ||
|
|
636012bbe5 | ||
|
|
e1b7bc316c | ||
|
|
1c488947bd | ||
|
|
1809461a07 | ||
|
|
01a78bee2a | ||
|
|
8c08b9ce09 | ||
|
|
efb8c8500d | ||
|
|
f366dd9cf2 | ||
|
|
0d15cfc134 | ||
|
|
e81532f541 | ||
|
|
d41afd2d81 | ||
|
|
7a1291d530 | ||
|
|
fc0f84f23e | ||
|
|
c2eac12ff5 | ||
|
|
e46496032c | ||
|
|
2d7f858a54 | ||
|
|
9ebcb55cb5 | ||
|
|
bbc1a0d06b | ||
|
|
31298e5eda | ||
|
|
e31c570d2b | ||
|
|
9b4947ef46 | ||
|
|
0d9bf5b862 | ||
|
|
c6f9ef1047 | ||
|
|
90ac62620d | ||
|
|
0c41027765 | ||
|
|
691211bf0d | ||
|
|
0ec51cce67 | ||
|
|
55755c6b3c | ||
|
|
72ba27d63c | ||
|
|
6c1182c2c2 | ||
|
|
ce81b339dc | ||
|
|
3db15dae44 | ||
|
|
38a73f9508 | ||
|
|
ebd9b862e9 | ||
|
|
4fc8e03d65 | ||
|
|
b48b96e616 | ||
|
|
529a9fd460 | ||
|
|
64fee86ebc | ||
|
|
e3f1a5e7cd | ||
|
|
0416c41956 | ||
|
|
4760103aa6 | ||
|
|
b2188ce9a7 | ||
|
|
4631d78bea | ||
|
|
0d16e2305e | ||
|
|
e5d37355b8 | ||
|
|
224124aac7 | ||
|
|
e29b839429 | ||
|
|
12c21d4e25 | ||
|
|
6a761a768c | ||
|
|
1ecf845cea | ||
|
|
a29a08716f | ||
|
|
e46b012e95 | ||
|
|
0f33d71966 | ||
|
|
00d57a22ee | ||
|
|
c25b56db9a | ||
|
|
a4b6129875 | ||
|
|
bce5f859da | ||
|
|
93526c17f5 | ||
|
|
6d4523c27e | ||
|
|
c4297d318f | ||
|
|
90e1dbb912 | ||
|
|
cbf50460c2 | ||
|
|
889c5ff55f | ||
|
|
566aa68fc0 | ||
|
|
3ca71527d9 | ||
|
|
5577e781fb | ||
|
|
7611fca6bf | ||
|
|
c10c465c02 | ||
|
|
9bc73819f6 | ||
|
|
bebf211b01 | ||
|
|
1af173e8a5 | ||
|
|
b051e76d00 | ||
|
|
4838875691 | ||
|
|
122f17fad6 | ||
|
|
be36abd857 | ||
|
|
fa4d4c6716 | ||
|
|
a5728be421 | ||
|
|
032b1aa3b2 | ||
|
|
fb7479a6f6 | ||
|
|
f81b267509 | ||
|
|
6527423526 | ||
|
|
d8bc20d4cb | ||
|
|
6c6238ce29 | ||
|
|
cbba3502b7 | ||
|
|
2b79db03c0 | ||
|
|
2c9edc43f1 | ||
|
|
ef9244f79f | ||
|
|
2d811dc398 | ||
|
|
e64d815033 | ||
|
|
bfade97051 | ||
|
|
3c4ef622cc | ||
|
|
3898d63689 | ||
|
|
ec8d43a548 | ||
|
|
5081255317 | ||
|
|
13a538168f | ||
|
|
a5ed0a4321 | ||
|
|
d93f9798ae | ||
|
|
88833f170b | ||
|
|
cd6911ed53 | ||
|
|
b95c51b96e | ||
|
|
3a36c367fe | ||
|
|
cf6010da59 | ||
|
|
bb2897445f | ||
|
|
406b141370 | ||
|
|
00b2f81a82 | ||
|
|
9adf045a31 | ||
|
|
de0f399a16 | ||
|
|
dca2a92829 | ||
|
|
b1b3ff7143 | ||
|
|
a0f0f31807 | ||
|
|
72db6e0ef2 | ||
|
|
b70688883a | ||
|
|
dd3ddb9d8a | ||
|
|
4ba1750c01 | ||
|
|
eacff56fe2 | ||
|
|
8768e163ae | ||
|
|
c0bc19d998 | ||
|
|
c99ec922aa | ||
|
|
0a6f76e41b | ||
|
|
156ce9cd34 | ||
|
|
176092ebf9 | ||
|
|
98817e3f9d | ||
|
|
276b7bb92f | ||
|
|
d1813e1ff0 | ||
|
|
5355a9bb20 | ||
|
|
48d17a8616 | ||
|
|
7e603f9606 | ||
|
|
ac6f0199f5 | ||
|
|
71f6f721fd | ||
|
|
51b7dc7cd4 | ||
|
|
03ddb08248 | ||
|
|
422dc05549 | ||
|
|
50c0a354c9 | ||
|
|
e98487c1cf | ||
|
|
50e03a24e8 | ||
|
|
0cc42568c7 | ||
|
|
41790ca707 | ||
|
|
20b1bfd01e | ||
|
|
1be5e10abe | ||
|
|
13a9ebed83 | ||
|
|
aee8c3be0c | ||
|
|
ef8ccf67ed | ||
|
|
91d531b053 | ||
|
|
0de169836e | ||
|
|
a2ef978208 | ||
|
|
5b3bb050e7 | ||
|
|
9b9efe5fac | ||
|
|
482d5ac4cb | ||
|
|
804a7ddbe1 | ||
|
|
739fdd387e | ||
|
|
f3517d2e4b | ||
|
|
c59f1e3dd6 | ||
|
|
2cf07099e3 | ||
|
|
0292465666 | ||
|
|
b4e15503c1 | ||
|
|
a5ca1ee41e | ||
|
|
f9c786fa28 | ||
|
|
bcf35030bc | ||
|
|
237eada209 | ||
|
|
afb984d3d4 | ||
|
|
675a25c90e | ||
|
|
f6d6314842 | ||
|
|
22ab5c29bd | ||
|
|
4931c6a54c | ||
|
|
d3fcc2a38c | ||
|
|
e9609b7f25 | ||
|
|
70cd51f0fa | ||
|
|
fa7699a37d | ||
|
|
2c9b794182 | ||
|
|
eae84ed8ba | ||
|
|
31539c5a0e | ||
|
|
abca32bca4 | ||
|
|
1d7cfe7c20 | ||
|
|
a516960e6f | ||
|
|
870bee16b1 | ||
|
|
3814102936 | ||
|
|
aaadff547d | ||
|
|
f0a3fadcd1 | ||
|
|
7cbce84cbd | ||
|
|
fe334c0d7c | ||
|
|
33d5a8e2a8 | ||
|
|
e80bf471ec | ||
|
|
7dfdea2395 | ||
|
|
74b61aebd2 | ||
|
|
ce9071e2d3 | ||
|
|
d6cf59dcc8 | ||
|
|
de61652d37 | ||
|
|
6181f62d93 | ||
|
|
ed66b4873e | ||
|
|
26869941a4 | ||
|
|
ee7b4e7ae5 | ||
|
|
7866484453 | ||
|
|
817b3b1a12 | ||
|
|
17f6968467 | ||
|
|
a96a1d6692 | ||
|
|
c1081fa9a4 | ||
|
|
dd3104094b | ||
|
|
dc68eea313 | ||
|
|
794e437f6d | ||
|
|
ebf544d335 | ||
|
|
939f30fef2 | ||
|
|
4549bbfdb9 | ||
|
|
6c989eb38e | ||
|
|
137d73ad0d | ||
|
|
58d4162f6d | ||
|
|
67625395fe | ||
|
|
f62512381a | ||
|
|
888e3102de | ||
|
|
f83435c697 | ||
|
|
5243d646f0 | ||
|
|
bdb284ce5d | ||
|
|
1b34820601 | ||
|
|
01010dd599 | ||
|
|
da32709f7c | ||
|
|
cdc6159a15 | ||
|
|
eb2f7ae455 | ||
|
|
a74f1bd89f | ||
|
|
603f709139 | ||
|
|
0dd063a32e | ||
|
|
9885a70a88 | ||
|
|
e4a85de089 | ||
|
|
ca0d2dffbe | ||
|
|
511d169c77 | ||
|
|
19bc8ef85c | ||
|
|
1e33d3eebb | ||
|
|
d18f434583 | ||
|
|
64749b4d18 | ||
|
|
875d8ef7eb | ||
|
|
20283f1536 | ||
|
|
c4af328802 | ||
|
|
a2193b9ffd | ||
|
|
d2542fd49b | ||
|
|
0333884877 | ||
|
|
63947ed69a | ||
|
|
d6d18ce29c | ||
|
|
2ef58d8b59 | ||
|
|
15f2d1635e | ||
|
|
c83b71f49a | ||
|
|
f0824fd71e | ||
|
|
8dde60658f | ||
|
|
9437648ae5 | ||
|
|
71b19aca2c | ||
|
|
7d320f8cd5 | ||
|
|
340d0883a7 | ||
|
|
66096acc29 | ||
|
|
e35100d865 | ||
|
|
128455ef01 | ||
|
|
10156cc90e | ||
|
|
4f5482f2ae | ||
|
|
2b9fdae74b | ||
|
|
7506caa0da | ||
|
|
b1f522277c | ||
|
|
af6e0c9ecf | ||
|
|
176567e3f1 | ||
|
|
15cd4268ac | ||
|
|
9307db16c4 | ||
|
|
0f29ab629a | ||
|
|
b2a4ceb853 | ||
|
|
6c7f1d068b | ||
|
|
b76beb602d | ||
|
|
0c68f6491a | ||
|
|
038981474a | ||
|
|
df0b4a5165 | ||
|
|
db7359f56d | ||
|
|
12e45b6483 | ||
|
|
ba8fca6f87 | ||
|
|
9b99b72f61 | ||
|
|
03cfbc2eb6 | ||
|
|
c92872a81b | ||
|
|
f3f4d9bca3 | ||
|
|
e7a317f0a0 | ||
|
|
d5b64a4af3 | ||
|
|
5667a0bbac | ||
|
|
7e46422c16 | ||
|
|
869a803149 | ||
|
|
f744e4d9a3 | ||
|
|
1a7607cba3 | ||
|
|
d881c111f6 | ||
|
|
bdac57ec0b | ||
|
|
c15f566cfa | ||
|
|
bdc79b9097 | ||
|
|
102eccb77d | ||
|
|
e8b236c7d8 | ||
|
|
d69491eb80 | ||
|
|
256a4d0b92 | ||
|
|
c5add012c1 | ||
|
|
6ecc8933b4 | ||
|
|
42b5661979 | ||
|
|
6333fb39bf | ||
|
|
ea27a8b8a7 | ||
|
|
358f9c3b0c | ||
|
|
cb3ef69072 | ||
|
|
eee9354657 | ||
|
|
ff86a1d7e6 | ||
|
|
e49f31322c | ||
|
|
95502e2c21 | ||
|
|
3dd4145e62 | ||
|
|
1d7ddc3f8a | ||
|
|
d731b4396c | ||
|
|
c186a1f6b0 | ||
|
|
a049ec8b7b | ||
|
|
4c93f6c7f4 | ||
|
|
35c7a89b19 | ||
|
|
c86b9c9703 | ||
|
|
64ebdd0ee3 | ||
|
|
9179b914d5 | ||
|
|
eb5d46b20b | ||
|
|
54c36f515b | ||
|
|
5c5668a4fc | ||
|
|
eeefb60c89 | ||
|
|
018df10a26 | ||
|
|
93ead77435 | ||
|
|
bd0e1ad0ca | ||
|
|
d0ceeacb37 | ||
|
|
7d5fb6a13f | ||
|
|
855ef7bfd1 | ||
|
|
323890021a | ||
|
|
e004e7592b | ||
|
|
0ebec014e5 | ||
|
|
1b624cc0e2 | ||
|
|
e8d161c0d5 | ||
|
|
e5c7dd7ec7 | ||
|
|
7d6ed59e6e | ||
|
|
11e4e7953a | ||
|
|
a5b292ee81 | ||
|
|
d619bafb8d | ||
|
|
0119a589dc | ||
|
|
b7346bed4d | ||
|
|
fcea57cb8e | ||
|
|
8d8af5e60a | ||
|
|
1a732ac1c1 | ||
|
|
f827d945be | ||
|
|
e03c4ee455 | ||
|
|
35e7ccb773 | ||
|
|
a932a10492 | ||
|
|
c5108c3a19 | ||
|
|
767782e425 | ||
|
|
60c440a3b6 | ||
|
|
6551a25877 | ||
|
|
70db2c5369 | ||
|
|
8ed31d0dc8 | ||
|
|
ef1ecb35e1 | ||
|
|
6768f10631 | ||
|
|
fee6a53473 | ||
|
|
bbfa3b0aa0 | ||
|
|
325ae654ef | ||
|
|
8655a4fb37 | ||
|
|
b30d272ee6 | ||
|
|
cc90ac2853 | ||
|
|
55473f39cb | ||
|
|
6d73881b07 | ||
|
|
d107cd4b42 | ||
|
|
33247e21b2 | ||
|
|
6bdc769af3 | ||
|
|
e923ccbf1b | ||
|
|
d402ee33a2 | ||
|
|
d8d420745f | ||
|
|
524f2068cd | ||
|
|
5db089ff19 | ||
|
|
08d6780c73 | ||
|
|
ca3f0bba6d | ||
|
|
830327e4a2 | ||
|
|
f96409dfa9 | ||
|
|
18e2da7d2b | ||
|
|
dfd046afb6 | ||
|
|
63d7f1e295 | ||
|
|
9d076d6a19 | ||
|
|
c6fa33a8aa | ||
|
|
37fa4392a5 | ||
|
|
90c88ade00 | ||
|
|
bb08f63a9f | ||
|
|
bdfad57d3f | ||
|
|
7ceb2d2078 | ||
|
|
304acdd0c1 | ||
|
|
8b6609c588 | ||
|
|
8a1d3aedd4 | ||
|
|
d49f100edd | ||
|
|
83d9ee1c5f | ||
|
|
b527c59735 | ||
|
|
23498a7a0a | ||
|
|
ac2cf79451 | ||
|
|
42b7426ed8 | ||
|
|
928c02099b | ||
|
|
c0ae63e27a | ||
|
|
62532e1c54 | ||
|
|
3136b53277 | ||
|
|
9352cff870 | ||
|
|
9e5fd0f079 | ||
|
|
1d37ba4780 | ||
|
|
134c1fb6ac | ||
|
|
24df04f304 | ||
|
|
26595bb25a | ||
|
|
5ee7dcf5a3 | ||
|
|
8b2ba9907f | ||
|
|
d1c59ef3b6 | ||
|
|
2dd23b15a8 | ||
|
|
93ad11f193 | ||
|
|
ec27d8bfde | ||
|
|
4d6e164d62 | ||
|
|
d82ffce504 | ||
|
|
7ecd435911 | ||
|
|
49f56e7d0d | ||
|
|
b8d6a14599 | ||
|
|
9c166936ad | ||
|
|
69db87cc24 | ||
|
|
5d86232fbe | ||
|
|
607e80bc82 | ||
|
|
f9625b2b88 | ||
|
|
3d8079c02b | ||
|
|
5c05a7fa58 | ||
|
|
2fa959a560 | ||
|
|
c39494d9fa | ||
|
|
a3cd1ea83d | ||
|
|
d4ddf6bb13 | ||
|
|
8661a3886b | ||
|
|
2ddbef70fe | ||
|
|
bb051e5a11 | ||
|
|
080085e813 | ||
|
|
85454b1f25 | ||
|
|
3f5653f650 | ||
|
|
b1357eb146 | ||
|
|
7731e799f4 | ||
|
|
15320e4d2c | ||
|
|
78596b687a | ||
|
|
729dc8dc11 | ||
|
|
3c08eb8122 | ||
|
|
9d12c86ac8 | ||
|
|
3bd573688c | ||
|
|
07ff87974e | ||
|
|
e8fa92950d | ||
|
|
ab6532742e | ||
|
|
4e583770e5 | ||
|
|
f9b6dc2ab8 | ||
|
|
1c2bc0c7b8 | ||
|
|
05760f914c | ||
|
|
3f6e8605af | ||
|
|
b6bfd1655c | ||
|
|
8cbd0e22ff | ||
|
|
15ec64e974 | ||
|
|
3de16e085e | ||
|
|
4cbd4ed60c | ||
|
|
b8fbd616e5 | ||
|
|
f8a79d2f51 | ||
|
|
0218ff4e26 | ||
|
|
1f3ce7e78f | ||
|
|
9009e1d232 | ||
|
|
cc73b2c2b9 | ||
|
|
dbf19ed054 | ||
|
|
a1cff4e8ab | ||
|
|
c9822968c8 | ||
|
|
8acabd95b5 | ||
|
|
49f6a6924d | ||
|
|
8d73265cf4 | ||
|
|
fceb7d18d7 | ||
|
|
337007cf70 | ||
|
|
4733d6d75a | ||
|
|
c8235544e8 | ||
|
|
3d1111ff02 | ||
|
|
83c9f2b10a | ||
|
|
734eb53aa7 | ||
|
|
6d39cb8e7c | ||
|
|
3c3f38b239 | ||
|
|
86d52d3884 | ||
|
|
6782672cb8 | ||
|
|
7fada7e5ab | ||
|
|
4380026a4e | ||
|
|
5143ea3d02 | ||
|
|
4802bcd829 | ||
|
|
6038096b95 | ||
|
|
2acfc31350 | ||
|
|
2742e5253f | ||
|
|
46f2e994b9 | ||
|
|
2c97a20c12 | ||
|
|
9be10ebd47 | ||
|
|
93cbfe7f7e | ||
|
|
4589de2115 | ||
|
|
662054ae25 | ||
|
|
3cf186f3cb | ||
|
|
7a91c82e4b | ||
|
|
72aace40d3 | ||
|
|
0c9a65b5f1 | ||
|
|
ea749499c3 | ||
|
|
828cbe7f20 | ||
|
|
1d8d547872 | ||
|
|
16c53221e3 | ||
|
|
74936010c4 | ||
|
|
f3245e1d65 | ||
|
|
1f74570ed9 | ||
|
|
88d1b7d2d1 | ||
|
|
fb5dcf0631 | ||
|
|
a23086d3fc | ||
|
|
a4cbcbca97 | ||
|
|
9dd008d42b | ||
|
|
76fa07cb90 | ||
|
|
35d72f27ed | ||
|
|
852891c779 | ||
|
|
f4aa7dc389 | ||
|
|
d7c3859f61 | ||
|
|
85d77c10a1 | ||
|
|
95222c7793 | ||
|
|
0a18b47e8c | ||
|
|
70f5126499 | ||
|
|
b60eab0fcf | ||
|
|
17310fc294 | ||
|
|
9c892dc1a4 | ||
|
|
c596dab806 | ||
|
|
fcb590e661 | ||
|
|
328017cca0 | ||
|
|
63dba562ae | ||
|
|
cf20f26098 | ||
|
|
e1e6063d17 | ||
|
|
ccbbe6f2f8 | ||
|
|
55c95bf59a | ||
|
|
043a5dc4e7 | ||
|
|
32a1cdf494 | ||
|
|
f924086198 | ||
|
|
6abb31e469 | ||
|
|
3eee369704 | ||
|
|
695d4d8684 | ||
|
|
015692d51e | ||
|
|
86004a89e5 | ||
|
|
86031ef3f8 | ||
|
|
034239daf3 | ||
|
|
a7b0f9924e | ||
|
|
a1d35b34b9 | ||
|
|
8d7e337dff | ||
|
|
de7e0996ce | ||
|
|
7377b69144 | ||
|
|
c933cfdaa3 | ||
|
|
726185cee2 | ||
|
|
de1000bfda | ||
|
|
555e8c0376 | ||
|
|
d836de3fe7 | ||
|
|
6ade1269ea | ||
|
|
a8b519e06e | ||
|
|
7d502306ea | ||
|
|
e9fa57c660 | ||
|
|
7d4ab17f0d | ||
|
|
d532902320 | ||
|
|
e592244443 | ||
|
|
c1def5da19 | ||
|
|
6a7f081f12 | ||
|
|
11555198eb | ||
|
|
6c77e27a50 | ||
|
|
17e8159fd8 | ||
|
|
c71f5d8c5e | ||
|
|
31cc9fc7d1 | ||
|
|
1d2421b0af | ||
|
|
a5df765951 | ||
|
|
622019ee06 | ||
|
|
45e12cc668 | ||
|
|
a21024a57e | ||
|
|
c175491bb0 | ||
|
|
09b0469faf | ||
|
|
3d63bbf4bf | ||
|
|
56d5d01497 | ||
|
|
a70bd44426 | ||
|
|
8c082b2fcc | ||
|
|
1732cac806 | ||
|
|
e1340e87eb | ||
|
|
e9bfef2131 | ||
|
|
b408699e65 | ||
|
|
3d1c508868 | ||
|
|
84e0746c9c | ||
|
|
b5658c4865 | ||
|
|
d413a4bc53 | ||
|
|
ce5ab902b6 | ||
|
|
294408b0bb | ||
|
|
53372fbe4c | ||
|
|
7fdac2118b | ||
|
|
1dbf78ed71 | ||
|
|
c9101a0c21 | ||
|
|
2e6264c04b | ||
|
|
e0aa46ba22 | ||
|
|
8093c3cd2c | ||
|
|
c6778e4e29 | ||
|
|
c77548d299 | ||
|
|
26d435ea64 | ||
|
|
c3f0d98e41 | ||
|
|
3c50f4aee9 | ||
|
|
4a930ba82a | ||
|
|
866e63f0fe | ||
|
|
d461cfa8ce | ||
|
|
18708636fb | ||
|
|
1901cca2f3 | ||
|
|
344019f108 | ||
|
|
e22a8021d3 | ||
|
|
111c9c0ed0 | ||
|
|
2445d18149 | ||
|
|
739523d559 | ||
|
|
23c0a493b1 | ||
|
|
fa353fb0b3 | ||
|
|
9f817bd918 | ||
|
|
2e5480a6bd | ||
|
|
1b50b7f446 | ||
|
|
ecc413ee01 | ||
|
|
0b1bf13b84 | ||
|
|
1fc6e68f3f | ||
|
|
1bee46ed81 | ||
|
|
a7772ffcd9 | ||
|
|
1263444b2b | ||
|
|
286a78ef8c | ||
|
|
0accff3e18 | ||
|
|
5f62481e62 | ||
|
|
82b8e1f79a | ||
|
|
85e2ca96bc | ||
|
|
fdbf287fee | ||
|
|
fa4b69181f | ||
|
|
a32641d9f4 | ||
|
|
44d8db043e | ||
|
|
be89b12c96 | ||
|
|
fd954589b5 | ||
|
|
a2f44668b6 | ||
|
|
ab2ab37844 | ||
|
|
b280a734a2 | ||
|
|
5c1bd15639 | ||
|
|
24fc67f242 | ||
|
|
642e0fc87e | ||
|
|
1528c09049 | ||
|
|
0f4617e9c4 | ||
|
|
a496e22ad1 | ||
|
|
a420565ded | ||
|
|
b3f0a479c2 | ||
|
|
9e18a6d1a8 | ||
|
|
34fd72dc97 | ||
|
|
ed9df7b211 | ||
|
|
965dbca514 | ||
|
|
f08272c853 | ||
|
|
843891cdd3 | ||
|
|
a6d59b1fa7 | ||
|
|
51d1d9fbfd | ||
|
|
de1358be8b | ||
|
|
4eb5dbc633 | ||
|
|
a1e6ce2357 | ||
|
|
16e833ddb7 | ||
|
|
4af35bd7ea | ||
|
|
7d305527e9 | ||
|
|
1d84dc94a0 | ||
|
|
f825ba38a0 | ||
|
|
f076c2d143 | ||
|
|
58a20fffb5 | ||
|
|
15a123875f | ||
|
|
7cadb3af8b | ||
|
|
01984a33eb | ||
|
|
7329817d95 | ||
|
|
ad4af7dd50 | ||
|
|
f2a778ffa4 | ||
|
|
1a77b5752c | ||
|
|
2b3d6a0989 | ||
|
|
0b508a04b8 | ||
|
|
13aebeecf9 | ||
|
|
47d3c640d6 | ||
|
|
19f27598d9 | ||
|
|
f2ef22e1a0 | ||
|
|
251e1b8a35 | ||
|
|
5de4e24a9f | ||
|
|
5e4d32c4c0 | ||
|
|
e1327842b1 | ||
|
|
c13412369a | ||
|
|
18e4e66db8 | ||
|
|
5392d635dd | ||
|
|
e56e80aade | ||
|
|
994c4fd699 | ||
|
|
ef64fefa96 | ||
|
|
344ff21c1e | ||
|
|
d34e06cb8c | ||
|
|
8f65a0320b | ||
|
|
b42e1c93da | ||
|
|
e0ca14eb21 | ||
|
|
48fe97291b | ||
|
|
f400fd7b60 | ||
|
|
fd1d464f06 | ||
|
|
28afdb36fe | ||
|
|
6c7db096fc | ||
|
|
5a7fcb0ec3 | ||
|
|
d647da7a4a | ||
|
|
d7df390bb4 | ||
|
|
9d36ff48dd | ||
|
|
8743388263 | ||
|
|
58486654d5 | ||
|
|
326d719a49 | ||
|
|
c9b6dc007a | ||
|
|
1bcac5e234 | ||
|
|
dad58e14e2 | ||
|
|
db85939322 | ||
|
|
4f4eb1fce5 | ||
|
|
e55000ee1a | ||
|
|
9c2bf9fba8 | ||
|
|
563784573b | ||
|
|
e2903f18da | ||
|
|
2f47456668 | ||
|
|
79b3101fe0 | ||
|
|
9788675934 | ||
|
|
10c63fcaa2 | ||
|
|
707c012318 | ||
|
|
3f30e17eb4 | ||
|
|
9eff138c3c | ||
|
|
b0fb5d1898 | ||
|
|
d542da38b2 | ||
|
|
c8b446ecaf | ||
|
|
6ed6af5b98 | ||
|
|
12d39916b9 | ||
|
|
12d4de0619 | ||
|
|
7ab87f688a | ||
|
|
9024a277ac | ||
|
|
fc00d9a5aa | ||
|
|
106a773f22 | ||
|
|
93d9cb3b69 | ||
|
|
99504b7f7d | ||
|
|
72c1995551 | ||
|
|
3d8c6c3839 | ||
|
|
0a06ffd074 | ||
|
|
12abb544bf | ||
|
|
78fe132cc2 | ||
|
|
b516d7f092 | ||
|
|
0961df316f | ||
|
|
8ad2986877 | ||
|
|
6214487fb3 | ||
|
|
2219a5454c | ||
|
|
712a5d1b06 | ||
|
|
cbc3b800fb | ||
|
|
e7348d0812 | ||
|
|
59e638402c | ||
|
|
bcd6de015d | ||
|
|
b798c84160 | ||
|
|
708f666787 | ||
|
|
4f03302ae2 | ||
|
|
d8f6ab206b | ||
|
|
472e69fe9a | ||
|
|
aeed5279f8 | ||
|
|
f3b8965fa6 | ||
|
|
1bbaab1db9 | ||
|
|
bf2fcbba37 | ||
|
|
a63dd67a07 | ||
|
|
b27f9836ae | ||
|
|
9504c5b863 | ||
|
|
9767b3453e | ||
|
|
643fbbbc84 | ||
|
|
2a5bcd5f52 | ||
|
|
237c3160eb | ||
|
|
fcdcf1a2a8 | ||
|
|
7c99aca1d0 | ||
|
|
309f1999e7 | ||
|
|
fa6de7ff79 | ||
|
|
47dfcab170 | ||
|
|
8abd19800f | ||
|
|
b2d6ed733d | ||
|
|
1179757893 | ||
|
|
d328ef5ce0 | ||
|
|
f577d3018f | ||
|
|
e6db629915 | ||
|
|
acaab30b91 | ||
|
|
76cedeed07 | ||
|
|
5beea74eb3 | ||
|
|
1f91a8f6f2 | ||
|
|
080216bd55 | ||
|
|
aa37172293 | ||
|
|
5836d7f8ba | ||
|
|
a699d7c110 | ||
|
|
8eedbb9d91 | ||
|
|
2afa85db60 | ||
|
|
a5672e7afe | ||
|
|
77d40215c8 | ||
|
|
0896df05b6 | ||
|
|
cf0b1c6237 | ||
|
|
08b129c8e0 | ||
|
|
e2ea397715 | ||
|
|
25a73e6ef9 | ||
|
|
f1c4bba3f2 | ||
|
|
704019ded8 | ||
|
|
79f5b92bae | ||
|
|
5aaa743ef8 | ||
|
|
9721c0bf85 | ||
|
|
56848cdb63 | ||
|
|
41ad089ff7 | ||
|
|
2df355d7b4 | ||
|
|
39a63b0643 | ||
|
|
ddf14e5636 | ||
|
|
7138749307 | ||
|
|
af7697f223 | ||
|
|
0bcb4b8573 | ||
|
|
6d47b59b1e | ||
|
|
3d8eaffe9a | ||
|
|
d8039aca17 | ||
|
|
4e4d379486 | ||
|
|
87faffa785 | ||
|
|
adef3f80f0 | ||
|
|
319c798f78 | ||
|
|
8579a7f2a5 | ||
|
|
ffbbba7395 | ||
|
|
e66745c9ef | ||
|
|
45fc9338a7 | ||
|
|
f2969bd1b0 | ||
|
|
e0f1f3f947 | ||
|
|
e3827f2e25 | ||
|
|
fad1d4972c | ||
|
|
2c33ce6c98 | ||
|
|
c0d7f42f17 | ||
|
|
d5a8e4b056 | ||
|
|
76dd50a060 | ||
|
|
6f9a9a7ad9 | ||
|
|
eaec9a493b | ||
|
|
d3c8555b39 | ||
|
|
446f5c0989 | ||
|
|
f3b72a8a3c | ||
|
|
d2c5a1f34b | ||
|
|
182b49cc04 | ||
|
|
cc8bd34cd4 | ||
|
|
957ece7394 | ||
|
|
762343adf9 | ||
|
|
8d32b378d9 | ||
|
|
41e816d299 | ||
|
|
4226a62f23 | ||
|
|
5dda28559b | ||
|
|
d055ca50d6 | ||
|
|
799bcfc7aa | ||
|
|
045cb662ef | ||
|
|
51e3983bf8 | ||
|
|
95fdc41845 | ||
|
|
d795fbeaf3 | ||
|
|
13037d68ed | ||
|
|
6da5df9f21 | ||
|
|
8128f573ef | ||
|
|
accf104553 | ||
|
|
5387264dcb | ||
|
|
308a6906db | ||
|
|
96ce7e3f47 | ||
|
|
f186b6266b | ||
|
|
756029e5af | ||
|
|
c1673f39b6 | ||
|
|
30a08c4192 | ||
|
|
d680f4d411 | ||
|
|
29a52e19cf | ||
|
|
11511168dc | ||
|
|
d4ea698236 | ||
|
|
11e06b477e | ||
|
|
4e4c68071f | ||
|
|
5f502746a4 | ||
|
|
17bbb9c0b4 | ||
|
|
8b13d6e08b | ||
|
|
efa512be32 | ||
|
|
594f5fba1e | ||
|
|
2912fb2184 | ||
|
|
02496f39f7 | ||
|
|
4e31f113c6 | ||
|
|
9aded3e1da | ||
|
|
3337d18e9a | ||
|
|
2cb6d019f9 | ||
|
|
3dc260a300 | ||
|
|
4d7f5b8ca6 | ||
|
|
48be5f65d8 | ||
|
|
b5d854a55c | ||
|
|
552663c625 | ||
|
|
e6f0b92464 | ||
|
|
08a6820aa0 | ||
|
|
cc1faa55be | ||
|
|
840966f3e6 | ||
|
|
763078a1ae | ||
|
|
5fb6abd019 | ||
|
|
7065856229 | ||
|
|
668ef9253a | ||
|
|
6f333b8234 | ||
|
|
32c839f497 | ||
|
|
cbdef1d538 | ||
|
|
c398b34dd8 | ||
|
|
9a1884cfec | ||
|
|
378dc1e931 | ||
|
|
be821b6927 | ||
|
|
af46e98865 | ||
|
|
65b1667ae7 | ||
|
|
5bc1fc2bde | ||
|
|
4ae0f28aa0 | ||
|
|
62ecc0549d | ||
|
|
cbf4c71a73 | ||
|
|
1d27fae370 | ||
|
|
05b9a80a07 | ||
|
|
38241452d3 | ||
|
|
40e040807a | ||
|
|
437da99d63 | ||
|
|
3cbcbec942 | ||
|
|
bc7a8cd09f | ||
|
|
ab0ac46d5a | ||
|
|
d7791c60e2 | ||
|
|
5dc8cdc6dc | ||
|
|
cdc33a25c5 | ||
|
|
2b6766f68a | ||
|
|
e871bbdc07 | ||
|
|
6a98158ba6 | ||
|
|
ef8d44c257 | ||
|
|
6a48a4d1c0 | ||
|
|
4d2ba28934 | ||
|
|
98d4f1c69e | ||
|
|
a0f0d73204 | ||
|
|
1dbb1a6a35 | ||
|
|
cc50ca82b8 | ||
|
|
373790c890 | ||
|
|
ef30d21b58 | ||
|
|
c4cafed6aa | ||
|
|
828eec5e0d | ||
|
|
a8c50fe7d4 | ||
|
|
ab9fc7b370 | ||
|
|
0dc972f7c9 | ||
|
|
796cffe29d | ||
|
|
a0f6c99fb5 | ||
|
|
eff0c91ed0 | ||
|
|
dba6cd8393 | ||
|
|
e7daceec82 | ||
|
|
a65473f6ab | ||
|
|
1851fda9e0 | ||
|
|
80eec131f8 | ||
|
|
bfe5b876de | ||
|
|
da8a0ee5e9 | ||
|
|
3269384439 | ||
|
|
9a766eac8c | ||
|
|
9d2456500a | ||
|
|
df85beac3e | ||
|
|
3dd020cb86 | ||
|
|
67da6be040 | ||
|
|
d2efd6c3e4 | ||
|
|
ea4a525db6 | ||
|
|
c83043b087 | ||
|
|
c07e968218 | ||
|
|
a6eeac14d2 | ||
|
|
a65bc3bc7b | ||
|
|
8e4b0b3b16 | ||
|
|
d34cefe31d | ||
|
|
3a68a3fc02 | ||
|
|
a4b6a64611 | ||
|
|
4f189f5319 | ||
|
|
cb69085280 | ||
|
|
f4d13986af | ||
|
|
6125c835f7 | ||
|
|
3049049d5b | ||
|
|
628c4984a3 | ||
|
|
b58cb3c0ed | ||
|
|
b267687c7f | ||
|
|
581b16f87c | ||
|
|
f9d42082a2 | ||
|
|
f8925eaed1 | ||
|
|
f4c1ece10a | ||
|
|
d13b034cab | ||
|
|
008afd88d1 | ||
|
|
68ca903db4 | ||
|
|
8f4b4fa82d | ||
|
|
768f562437 | ||
|
|
9f0a4bfe69 | ||
|
|
13b4291840 | ||
|
|
6dc33126a5 | ||
|
|
fa31dced22 | ||
|
|
194f6aef1d | ||
|
|
a12b630a4e | ||
|
|
c3ff73591a | ||
|
|
1967811d68 | ||
|
|
0e24a0d8bb | ||
|
|
5913f61e7d | ||
|
|
9a7e517c73 | ||
|
|
99af71de75 | ||
|
|
06848b6731 | ||
|
|
4ece3a6140 | ||
|
|
ae92432878 | ||
|
|
a4468da9b1 | ||
|
|
187931a0ea | ||
|
|
d3533e17e8 | ||
|
|
b0944429db | ||
|
|
7170573da7 | ||
|
|
4cd94c776a | ||
|
|
3483de1fc2 | ||
|
|
df3c2cffb3 | ||
|
|
f0e3bc0c14 | ||
|
|
b4d1d54ccb | ||
|
|
de3438248f | ||
|
|
456eea9c13 | ||
|
|
3cdebb6e8a | ||
|
|
e0a9dad114 | ||
|
|
b1aa355d5b | ||
|
|
129591392f | ||
|
|
e51f0f14d9 | ||
|
|
2c520bb936 | ||
|
|
d3bccb2b4e | ||
|
|
e28f44cfe5 | ||
|
|
45e5c85868 | ||
|
|
c5bc92e4ea | ||
|
|
ebb8a35129 | ||
|
|
f2046b2453 | ||
|
|
f7351a3eb5 | ||
|
|
28d55279d8 | ||
|
|
8104db4fcc | ||
|
|
b8658cd47c | ||
|
|
ecaa8d53e0 | ||
|
|
ca1ec1acef | ||
|
|
13283cb8e2 | ||
|
|
5a42adb05b | ||
|
|
98afe98870 | ||
|
|
f5420d3be3 | ||
|
|
50b5ab80c4 | ||
|
|
e6371d74b5 | ||
|
|
0ab38faeac | ||
|
|
b0444104cc | ||
|
|
4757d6ee87 | ||
|
|
1780965ef8 | ||
|
|
aaa88e9b7d | ||
|
|
17ce91a4a2 | ||
|
|
08751a762a | ||
|
|
77c0beecf2 | ||
|
|
28bcf6a8ac | ||
|
|
61004b4dfb | ||
|
|
e5c22b8a3f | ||
|
|
001d0f30aa | ||
|
|
fbe4bb03d1 | ||
|
|
3469b6beb8 | ||
|
|
c696efe0bc | ||
|
|
d0ca61f373 | ||
|
|
350687eda9 | ||
|
|
d898641e6a | ||
|
|
db576d73bb | ||
|
|
5fcdd17665 | ||
|
|
e8f2bd9b0c | ||
|
|
3002023a70 | ||
|
|
95cea24527 |
312 changed files with 82680 additions and 27781 deletions
1
.github/ISSUE_TEMPLATE/🐛-bug-report.md
vendored
1
.github/ISSUE_TEMPLATE/🐛-bug-report.md
vendored
|
|
@ -12,6 +12,7 @@ Before creating a bug report on this issue tracker, you **must** read the [Contr
|
||||||
|
|
||||||
- The issue tracker is used by developers of this project. **Do not use it to ask general questions, or for support requests**.
|
- The issue tracker is used by developers of this project. **Do not use it to ask general questions, or for support requests**.
|
||||||
- Ideas and feature requests can be made on the [Discussions](https://github.com/markqvist/Reticulum/discussions). **Only** feature requests accepted by maintainers and developers are tracked and included on the issue tracker. **Do not post feature requests here**.
|
- Ideas and feature requests can be made on the [Discussions](https://github.com/markqvist/Reticulum/discussions). **Only** feature requests accepted by maintainers and developers are tracked and included on the issue tracker. **Do not post feature requests here**.
|
||||||
|
- Do not submit code written using large language models (LLMs) or other generative 'AI' programs (see the [Generative AI Policy](/Contributing.md#generative-ai-policy) for details).
|
||||||
- After reading the [Contribution Guidelines](https://github.com/markqvist/Reticulum/blob/master/Contributing.md), **delete this section only** (*"Read the Contribution Guidelines"*) from your bug report, **and fill in all the other sections**.
|
- After reading the [Contribution Guidelines](https://github.com/markqvist/Reticulum/blob/master/Contributing.md), **delete this section only** (*"Read the Contribution Guidelines"*) from your bug report, **and fill in all the other sections**.
|
||||||
|
|
||||||
**Describe the Bug**
|
**Describe the Bug**
|
||||||
|
|
|
||||||
10
.github/workflows/build.yml
vendored
10
.github/workflows/build.yml
vendored
|
|
@ -28,8 +28,10 @@ jobs:
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.11
|
||||||
- run: make test
|
- run: |
|
||||||
|
python -m pip install -q cryptography
|
||||||
|
make test
|
||||||
|
|
||||||
package:
|
package:
|
||||||
needs: test
|
needs: test
|
||||||
|
|
@ -41,7 +43,7 @@ jobs:
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
python-version: 3.x
|
python-version: 3.11
|
||||||
- run: |
|
- run: |
|
||||||
python -m pip install -q build wheel setuptools
|
python -m pip install -q build wheel setuptools
|
||||||
make remove_symlinks
|
make remove_symlinks
|
||||||
|
|
@ -91,6 +93,6 @@ jobs:
|
||||||
# .artifacts/documentation/latex/reticulumnetworkstack.pdf
|
# .artifacts/documentation/latex/reticulumnetworkstack.pdf
|
||||||
# .artifacts/documentation/epub/ReticulumNetworkStack.epub
|
# .artifacts/documentation/epub/ReticulumNetworkStack.epub
|
||||||
draft: true
|
draft: true
|
||||||
generate_release_notes: true
|
generate_release_notes: false
|
||||||
prerelease: ${{ contains(github.ref, '-') }}
|
prerelease: ${{ contains(github.ref, '-') }}
|
||||||
fail_on_unmatched_files: true
|
fail_on_unmatched_files: true
|
||||||
|
|
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -13,3 +13,5 @@ tests/rnsconfig/storage
|
||||||
tests/rnsconfig/logfile*
|
tests/rnsconfig/logfile*
|
||||||
*.data
|
*.data
|
||||||
*.result
|
*.result
|
||||||
|
.buildinfo.bak
|
||||||
|
docs/Reticulum Manual.*
|
||||||
|
|
|
||||||
292
CRNS/__init__.py
Normal file
292
CRNS/__init__.py
Normal file
|
|
@ -0,0 +1,292 @@
|
||||||
|
compiling = False
|
||||||
|
noticed = False
|
||||||
|
notice_delay = 0.3
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
from importlib.util import find_spec
|
||||||
|
if find_spec("pyximport") and find_spec("cython"):
|
||||||
|
import json
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
from importlib.abc import MetaPathFinder
|
||||||
|
from importlib.machinery import ExtensionFileLoader, SourceFileLoader
|
||||||
|
from importlib.machinery import EXTENSION_SUFFIXES
|
||||||
|
from importlib.util import spec_from_file_location
|
||||||
|
from distutils.extension import Extension
|
||||||
|
from pyximport import pyxbuild
|
||||||
|
|
||||||
|
COMPILE_DIRECTIVES = { "annotation_typing": False }
|
||||||
|
|
||||||
|
_WORKER_SCRIPT = r"""
|
||||||
|
import sys, os, json
|
||||||
|
from distutils.extension import Extension
|
||||||
|
from pyximport import pyxbuild
|
||||||
|
def main():
|
||||||
|
tasks_path = sys.argv[1]
|
||||||
|
with open(tasks_path, "r") as fh: tasks = json.load(fh)
|
||||||
|
results = []
|
||||||
|
for task in tasks:
|
||||||
|
try:
|
||||||
|
os.makedirs(task["scratch"], exist_ok=True)
|
||||||
|
extension = Extension(name=task["basename"], sources=[task["source"]])
|
||||||
|
extension.cython_directives = task["directives"]
|
||||||
|
so_path = pyxbuild.pyx_to_dll(task["source"], ext=extension, build_in_temp=True, pyxbuild_dir=task["scratch"])
|
||||||
|
os.makedirs(os.path.dirname(task["target"]), exist_ok=True)
|
||||||
|
os.replace(so_path, task["target"])
|
||||||
|
results.append({"fullname": task["fullname"], "ok": True, "error": None})
|
||||||
|
except Exception as e:
|
||||||
|
results.append({"fullname": task["fullname"], "ok": False,
|
||||||
|
"error": "{}: {}".format(type(e).__name__, e)})
|
||||||
|
with open(tasks_path + ".results", "w") as fh: json.dump(results, fh)
|
||||||
|
if __name__ == "__main__": main()
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _compile_jobs():
|
||||||
|
jobs = os.environ.get("CRNS_JOBS")
|
||||||
|
if jobs: return max(1, int(jobs))
|
||||||
|
return max(1, min(os.cpu_count() or 1, 16))
|
||||||
|
|
||||||
|
class _ImportFinder(MetaPathFinder):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.build_dir = os.path.join(os.path.expanduser("~"), ".pyxbld")
|
||||||
|
self.mirror_root = os.path.join(self.build_dir, "rnslib")
|
||||||
|
self.source_root = self._find_source_root()
|
||||||
|
self.abi_suffix = EXTENSION_SUFFIXES[0]
|
||||||
|
self.unbuildable = {}
|
||||||
|
self.unbuildable_path = os.path.join(self.build_dir, "unbuildable.json")
|
||||||
|
self._verify_mirror_directives()
|
||||||
|
try:
|
||||||
|
with open(self.unbuildable_path, "r") as fh: self.unbuildable = json.load(fh)
|
||||||
|
except Exception: self.unbuildable = {}
|
||||||
|
|
||||||
|
def _verify_mirror_directives(self):
|
||||||
|
# If the compiler directives have changed since the mirror was
|
||||||
|
# built, the entire mirror is invalidated and rebuilt.
|
||||||
|
stamp_path = os.path.join(self.build_dir, "rnslib.stamp")
|
||||||
|
directives = json.dumps(COMPILE_DIRECTIVES, sort_keys=True)
|
||||||
|
stamp = directives + "\n" + self.abi_suffix
|
||||||
|
previous = None
|
||||||
|
try:
|
||||||
|
with open(stamp_path, "r") as fh: previous = fh.read()
|
||||||
|
except Exception: pass
|
||||||
|
if previous != stamp:
|
||||||
|
shutil.rmtree(self.mirror_root, ignore_errors=True)
|
||||||
|
os.makedirs(self.mirror_root, exist_ok=True)
|
||||||
|
try:
|
||||||
|
os.makedirs(self.build_dir, exist_ok=True)
|
||||||
|
with open(stamp_path, "w") as fh: fh.write(stamp)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def _save_unbuildable(self):
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.dirname(self.unbuildable_path), exist_ok=True)
|
||||||
|
with open(self.unbuildable_path, "w") as fh: json.dump(self.unbuildable, fh, indent=1)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def _mark_unbuildable(self, fullname, source):
|
||||||
|
try:
|
||||||
|
self.unbuildable[fullname] = int(os.path.getmtime(source))
|
||||||
|
self._save_unbuildable()
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def _unmark_buildable(self, fullname):
|
||||||
|
if fullname in self.unbuildable:
|
||||||
|
del self.unbuildable[fullname]
|
||||||
|
self._save_unbuildable()
|
||||||
|
|
||||||
|
def _is_unbuildable(self, fullname, source):
|
||||||
|
if fullname not in self.unbuildable: return False
|
||||||
|
try:
|
||||||
|
if int(os.path.getmtime(source)) == self.unbuildable[fullname]: return True
|
||||||
|
except Exception: pass
|
||||||
|
self.unbuildable.pop(fullname, None)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _find_source_root(self):
|
||||||
|
here = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
candidate = os.path.abspath(os.path.join(here, "..", "RNS", "__init__.py"))
|
||||||
|
if os.path.exists(candidate): return os.path.dirname(os.path.dirname(candidate))
|
||||||
|
for entry in [os.getcwd()] + list(sys.path):
|
||||||
|
if not entry: continue
|
||||||
|
candidate = os.path.join(entry, "RNS", "__init__.py")
|
||||||
|
if os.path.exists(candidate): return os.path.dirname(candidate)
|
||||||
|
raise RuntimeError("CRNS could not locate the RNS source tree")
|
||||||
|
|
||||||
|
def _all_source_modules(self):
|
||||||
|
rns_root = os.path.join(self.source_root, "RNS")
|
||||||
|
for root, dirs, files in os.walk(rns_root):
|
||||||
|
dirs[:] = [d for d in dirs if d not in ("__pycache__", "i2plib")]
|
||||||
|
rel = os.path.relpath(root, self.source_root).replace(os.sep, ".")
|
||||||
|
for fn in sorted(files):
|
||||||
|
if not fn.endswith(".py"): continue
|
||||||
|
if fn == "__init__.py": fullname = rel
|
||||||
|
else: fullname = rel + "." + fn[:-3]
|
||||||
|
yield fullname
|
||||||
|
|
||||||
|
def _mirror_target(self, fullname, is_package):
|
||||||
|
rel = fullname.split(".")
|
||||||
|
basename = rel[-1]
|
||||||
|
if is_package: return os.path.join(self.mirror_root, *rel, "__init__" + self.abi_suffix)
|
||||||
|
return os.path.join(self.mirror_root, *rel[:-1], basename + self.abi_suffix)
|
||||||
|
|
||||||
|
def _source(self, fullname):
|
||||||
|
rel = fullname.split(".")
|
||||||
|
package_source = os.path.join(self.source_root, *rel, "__init__.py")
|
||||||
|
if os.path.exists(package_source): return package_source, True
|
||||||
|
module_source = os.path.join(self.source_root, *rel) + ".py"
|
||||||
|
if os.path.exists(module_source): return module_source, False
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
def _compile(self, fullname, source):
|
||||||
|
basename = fullname.rsplit(".", 1)[-1]
|
||||||
|
extension = Extension(name=basename, sources=[source])
|
||||||
|
extension.cython_directives = COMPILE_DIRECTIVES
|
||||||
|
so_path = pyxbuild.pyx_to_dll(source, ext=extension, build_in_temp=True, pyxbuild_dir=self.build_dir)
|
||||||
|
abi_suffix = os.path.basename(so_path)[len(basename):]
|
||||||
|
return so_path, abi_suffix
|
||||||
|
|
||||||
|
def _is_current(self, fullname, source, is_package):
|
||||||
|
target = self._mirror_target(fullname, is_package)
|
||||||
|
if not os.path.exists(target): return False
|
||||||
|
try: return os.path.getmtime(target) >= os.path.getmtime(source)
|
||||||
|
except Exception: return False
|
||||||
|
|
||||||
|
def _ensure_compiled(self, fullname, source, is_package):
|
||||||
|
target = self._mirror_target(fullname, is_package)
|
||||||
|
if os.path.exists(target) and os.path.getmtime(target) >= os.path.getmtime(source): return target
|
||||||
|
so_path, abi_suffix = self._compile(fullname, source)
|
||||||
|
os.makedirs(os.path.dirname(target), exist_ok=True)
|
||||||
|
os.replace(so_path, target)
|
||||||
|
return target
|
||||||
|
|
||||||
|
def precompile(self):
|
||||||
|
if not os.path.isdir(self.mirror_root):
|
||||||
|
os.makedirs(self.mirror_root, exist_ok=True)
|
||||||
|
|
||||||
|
pending = []
|
||||||
|
for fullname in self._all_source_modules():
|
||||||
|
source, is_package = self._source(fullname)
|
||||||
|
if source is None:
|
||||||
|
continue
|
||||||
|
if self._is_current(fullname, source, is_package):
|
||||||
|
self._unmark_buildable(fullname)
|
||||||
|
continue
|
||||||
|
if self._is_unbuildable(fullname, source):
|
||||||
|
continue
|
||||||
|
|
||||||
|
pending.append((fullname, source, is_package))
|
||||||
|
|
||||||
|
if not pending: return
|
||||||
|
|
||||||
|
jobs = min(_compile_jobs(), len(pending))
|
||||||
|
if jobs > 1:
|
||||||
|
# Round-robin assignment spreads the large modules evenly
|
||||||
|
# across workers.
|
||||||
|
chunks = [[] for _ in range(jobs)]
|
||||||
|
for i, item in enumerate(pending): chunks[i % jobs].append(item)
|
||||||
|
|
||||||
|
workers_dir = os.path.join(self.build_dir, "workers")
|
||||||
|
os.makedirs(workers_dir, exist_ok=True)
|
||||||
|
procs = []
|
||||||
|
for w in range(jobs):
|
||||||
|
tasks = []
|
||||||
|
for fullname, source, is_package in chunks[w]:
|
||||||
|
tasks.append({ "fullname": fullname,
|
||||||
|
"basename": fullname.rsplit(".", 1)[-1],
|
||||||
|
"source": source,
|
||||||
|
"target": self._mirror_target(fullname, is_package),
|
||||||
|
"scratch": os.path.join(workers_dir, "w{}".format(w)),
|
||||||
|
"directives": COMPILE_DIRECTIVES })
|
||||||
|
task_path = os.path.join(workers_dir, "tasks{}.json".format(w))
|
||||||
|
with open(task_path, "w") as fh: json.dump(tasks, fh)
|
||||||
|
procs.append((w, task_path, subprocess.Popen([sys.executable, "-c", _WORKER_SCRIPT, task_path],
|
||||||
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)))
|
||||||
|
|
||||||
|
for w, task_path, proc in procs:
|
||||||
|
stdout, stderr = proc.communicate()
|
||||||
|
if proc.returncode != 0:
|
||||||
|
for fullname, _, _ in chunks[w]:
|
||||||
|
source, _ = self._source(fullname)
|
||||||
|
self._mark_unbuildable(fullname, source)
|
||||||
|
print(f"CRNS: compile worker failed for {fullname}, using interpreted module", file=sys.stderr)
|
||||||
|
if stderr:
|
||||||
|
tail = stderr.decode(errors="replace").strip().splitlines()[-5:]
|
||||||
|
print("CRNS: worker output:\n" + "\n".join(tail), file=sys.stderr)
|
||||||
|
continue
|
||||||
|
|
||||||
|
results = {}
|
||||||
|
try:
|
||||||
|
with open(task_path + ".results", "r") as fh:
|
||||||
|
for entry in json.load(fh): results[entry["fullname"]] = entry
|
||||||
|
except Exception: results = {}
|
||||||
|
|
||||||
|
for fullname, _, _ in chunks[w]:
|
||||||
|
entry = results.get(fullname)
|
||||||
|
if entry is not None and entry.get("ok"): self._unmark_buildable(fullname)
|
||||||
|
else:
|
||||||
|
error = "" if entry is None else entry.get("error", "")
|
||||||
|
source, _ = self._source(fullname)
|
||||||
|
self._mark_unbuildable(fullname, source)
|
||||||
|
print(f"Could not compile {fullname}, using interpreted module: {error}", file=sys.stderr)
|
||||||
|
else:
|
||||||
|
for fullname, source, is_package in pending:
|
||||||
|
try:
|
||||||
|
self._ensure_compiled(fullname, source, is_package)
|
||||||
|
self._unmark_buildable(fullname)
|
||||||
|
except Exception as e:
|
||||||
|
self._mark_unbuildable(fullname, source)
|
||||||
|
print(f"Could not compile {fullname}, using interpreted module: {e}", file=sys.stderr)
|
||||||
|
|
||||||
|
def _spec(self, fullname, source, is_package, compile_attempted):
|
||||||
|
if fullname == "RNS.vendor.i2plib" or fullname.startswith("RNS.vendor.i2plib."):
|
||||||
|
rel = fullname.split(".")
|
||||||
|
locations = [os.path.join(self.source_root, *rel)] if is_package else None
|
||||||
|
return spec_from_file_location(fullname, source, loader=SourceFileLoader(fullname, source), submodule_search_locations=locations)
|
||||||
|
|
||||||
|
if compile_attempted:
|
||||||
|
try:
|
||||||
|
target = self._ensure_compiled(fullname, source, is_package)
|
||||||
|
self._unmark_buildable(fullname)
|
||||||
|
if is_package: locations = [os.path.dirname(target)]
|
||||||
|
else: locations = None
|
||||||
|
return spec_from_file_location(fullname, target, loader=ExtensionFileLoader(fullname, target),
|
||||||
|
submodule_search_locations=locations)
|
||||||
|
except Exception as e:
|
||||||
|
self._mark_unbuildable(fullname, source)
|
||||||
|
print(f"Could not compile {fullname}, using interpreted module: {e}", file=sys.stderr)
|
||||||
|
|
||||||
|
rel = fullname.split(".")
|
||||||
|
locations = [os.path.join(self.source_root, *rel)] if is_package else None
|
||||||
|
return spec_from_file_location(fullname, source,loader=SourceFileLoader(fullname, source),submodule_search_locations=locations)
|
||||||
|
|
||||||
|
def find_spec(self, fullname, path, target=None):
|
||||||
|
if not isinstance(fullname, str): return None
|
||||||
|
if not (fullname == "RNS" or fullname.startswith("RNS.")): return None
|
||||||
|
|
||||||
|
source, is_package = self._source(fullname)
|
||||||
|
if source is None: return None
|
||||||
|
|
||||||
|
return self._spec(fullname, source, is_package, compile_attempted=not self._is_unbuildable(fullname, source))
|
||||||
|
|
||||||
|
_importfinder = _ImportFinder()
|
||||||
|
sys.meta_path.insert(0, _importfinder)
|
||||||
|
|
||||||
|
def notice_job():
|
||||||
|
global noticed
|
||||||
|
started = time.time()
|
||||||
|
while compiling:
|
||||||
|
if time.time() > started+notice_delay and compiling:
|
||||||
|
noticed = True
|
||||||
|
print("Compiling RNS object code...")
|
||||||
|
sys.stdout.flush()
|
||||||
|
break
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
compiling = True
|
||||||
|
threading.Thread(target=notice_job, daemon=True).start()
|
||||||
|
_importfinder.precompile()
|
||||||
|
import RNS; compiling = False
|
||||||
|
if noticed: print("Compilation done"); sys.stdout.flush()
|
||||||
1092
Changelog.md
1092
Changelog.md
File diff suppressed because it is too large
Load diff
|
|
@ -2,42 +2,66 @@
|
||||||
|
|
||||||
Welcome, and thank you for your interest in contributing to Reticulum!
|
Welcome, and thank you for your interest in contributing to Reticulum!
|
||||||
|
|
||||||
Apart from writing code, there are many ways in which you can contribute. Before interacting with this community, read these short and simple guidelines.
|
Before interacting with this community, read these short and simple guidelines.
|
||||||
|
|
||||||
|
If you didn't shit your pants after reading this document, you may well find that we are all really quite cuddly and nice to be around. Mostly.
|
||||||
|
|
||||||
## Expected Conduct
|
## Expected Conduct
|
||||||
|
|
||||||
First and foremost, there is one simple requirement for taking part in this community: While we primarily interact virtually, your actions matter and have real consequences. Therefore: **Act like a responsible, civilized person** - also in the face of disputes and heated disagreements. Speak your mind here, discussions are welcome. Just do so in the spirit of being face-to-face with everyone else. Thank you.
|
First and foremost, there is one simple requirement for taking part in the development of this technology: While we primarily interact virtually, your actions matter and have real consequences. Therefore: **Act like a responsible, civilized person** - especially in the face of disputes and heated disagreements. Speak your mind here; discussions are welcome. Just do so in the spirit of being face-to-face with everyone else. Thank you.
|
||||||
|
|
||||||
## Asking Questions
|
In order to keep the discourse navigable and useful, the following types of posts and inquiries will be deleted and ignored:
|
||||||
|
|
||||||
If you want to ask a question, **do not open an issue**. The issue tracker is used by people *working on Reticulum* to track bugs, issues and improvements.
|
- Spam.
|
||||||
|
- Questions that have already been adequately answered elsewhere. Utilize search functions.
|
||||||
|
- Low-effort posts or comments that contain no actual information or useful substance. [This is not a tea-house](./This%20Is%20Not%20a%20Teahouse.md).
|
||||||
|
- Post or comments solely containing personal opinions or beliefs without adding anything to the discussion. Facebook and X exists.
|
||||||
|
- Content that simply waste the developer's / maintainer's time with completely obvious "ideas", "insights" or "recommendations". Yes, we have *at least* 8 neurons ourselves.
|
||||||
|
- Posts that fail to understand that developing a highly complex software project with a very small amount of resources and people takes time. Imagining perfection on our behalf is useless.
|
||||||
|
|
||||||
Instead, ask away on the [discussions](https://github.com/markqvist/Reticulum/discussions) or on the [Reticulum Matrix channel](https://matrix.to/#/#reticulum:matrix.org) at `#reticulum:matrix.org`
|
If you're new to the community and start out your engagement with any of the above transgressions, you will simply be banned permanently without notice or explanation, and any further communication from you will be ignored.
|
||||||
|
|
||||||
## Providing Feedback & Ideas
|
Except for the case of real, well-grounded, clearly documented, human-written security advisories, technical investigations or bug reports, if your inquiry or communication is not attached to a Reticulum Identity, expect it to be ignored.
|
||||||
|
|
||||||
Likewise, feedback, ideas and feature requests are a very welcome way to contribute, and should also be posted on the [discussions](https://github.com/markqvist/Reticulum/discussions), or on the [Reticulum Matrix channel](https://matrix.to/#/#reticulum:matrix.org) at `#reticulum:matrix.org`.
|
If you find this "harsh", "unfair" or "unwelcoming", go somewhere else. This is not social club, but a work environment for the people contributing to the project.
|
||||||
|
|
||||||
Please do not post feature requests or general ideas on the issue tracker, or in direct messages to the primary developers. You are much more likely to get a response and start a constructive discussion by posting your ideas in the public channels created for these purposes.
|
## Reporting Bugs
|
||||||
|
|
||||||
## Reporting Issues
|
If you have found a bug or issue in this project, please report it to the maintainer over LXMF, or if strictly not possible, over email. Detailed and serious bug reports can also be posted in the various on-network forums and RRC channels that the developers frequent. Due to the realities described in [This Is Not a Teahouse](./This%20Is%20Not%20a%20Teahouse.md), there is no way to report issues or request features over the internet.
|
||||||
|
|
||||||
If you have found a bug or issue in this project, please report it using the [issue tracker](https://github.com/markqvist/Reticulum/issues). If at all possible, be sure to include details on how to reproduce the bug.
|
|
||||||
|
|
||||||
Anything submitted to the issue tracker that does not follow these guidelines will be closed and removed without comments or explanation.
|
|
||||||
|
|
||||||
## Writing Code
|
## Writing Code
|
||||||
|
|
||||||
If you are interested in contributing code, fixing open issues or adding features, please coordinate the effort with the maintainer or one of the main developers **before** submitting a pull request. Before deciding to contribute, it is also a good idea to ensure your efforts are in alignment with the [Roadmap](./Roadmap.md) and current development focus.
|
Patches and code contributions are *generally* only welcomed from developers already known to the community, with a proven track-record of competence, skill, careful and considerate design and responsible conduct. If we do not know you yet, but you have created something that you believe lives up to such standards, we are eager to hear from you.
|
||||||
|
|
||||||
Pull requests have a high chance of being accepted if they are:
|
Current development priorities and work documents are not publicly available on the internet, but shared and coordinated directly between the developers over Reticulum itself, following the principles outlined in [Distributed Development](https://reticulum.network/manual/distributed.html).
|
||||||
|
|
||||||
- In alignment with the [Roadmap](./Roadmap.md) or solve an open issue or feature request
|
## Commit Signing
|
||||||
- Sufficiently tested to work with all API functions, and pass the standard test suite
|
|
||||||
- Functionally and conceptually complete and well-designed
|
|
||||||
- Not simply formatting or code style changes
|
|
||||||
- Well-documented
|
|
||||||
|
|
||||||
Even new ideas and proposals that have not been approved by a maintainer, or fall outside the established roadmap, are *occasionally* accepted - if they possess the remaining of the above qualities. If not, they will be closed and removed without comments or explanation.
|
All commits in Reticulum and related projects **must** be signed with a Reticulum identity. If you want your author credentials to be included directly in the commit log, ensure that you have [configured commit signing](https://reticulum.network/manual/git.html#commit-signing). Bare patches submitted manually or fetched remote commits without signatures are still very welcome, but we will review, squash merge and sign with the key of the maintainer that approved the changes. Attribution will be provided in the commit message instead (unless you wish to remain anonymous).
|
||||||
|
|
||||||
By contributing code to this project, you agree that copyright for the code is transferred to the Reticulum maintainers and that the code is irrevocably placed under the [MIT license](./LICENSE).
|
## Machine Assistance & LLMs
|
||||||
|
|
||||||
|
For a highly complex, critical piece of cryptographically based networking infrastructure, we should not need to state the following, but alas, experience has proven otherwise. Therefore:
|
||||||
|
|
||||||
|
**Including material generated using LLMs or other "AI" programs in Reticulum is, almost without exception, prohibited.**
|
||||||
|
|
||||||
|
Before even *thinking* about using a generative software system to "create" something for Reticulum, read [Brandolini's Reference](https://reticulum.network/manual/brandolinis.html), in full. If you do not have the capacity for that, you have failed the first requirement for contributing: Basic literacy.
|
||||||
|
|
||||||
|
*If* you are not already a known contributor with a proven record of being able to responsibly handle these tools, a **full** audit trail of all ML-utilizing workflows, including prompts, LLM reasoning traces, tool calls and outputs (that is, the *entire*, unaltered session log) will need to be provided with any proposed contribution. Without that, we will not even *consider* looking at it.
|
||||||
|
|
||||||
|
This applies to all Reticulum-related projects and documentation, as well as all submitted issues, reports and discussion, **except** of course in cases where language translation, speech recogntion, or other assistive technologies are required.
|
||||||
|
|
||||||
|
In **all** circumstances, clear disclosure is **mandatory**. Any piece of material, file, code snippet, method, function or similar that was generated with machine assistance must be prepended with a disclosure header or comment that states the framework, exact model used and reviewer, such as:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Generated with lc/deepseek_v4_flash_q8_k_xl
|
||||||
|
# Reviewed and corrected by Mark Qvist
|
||||||
|
```
|
||||||
|
|
||||||
|
Additionally, any explanatory comments or doc-strings generated by software must be manually re-written in clear, concise language by the reviewer stated in the disclosure header.
|
||||||
|
|
||||||
|
## Contributor License Agreement
|
||||||
|
|
||||||
|
By contributing code to this project, you agree that copyright for the code is transferred to the Reticulum maintainers and that the code is irrevocably placed under the [Reticulum License](./LICENSE).
|
||||||
|
|
||||||
|
For a more in-depth elaboration on the current CLA position, see [this post](https://rns.recipes/forum/general/brandolinis-reference-or-alternatively-agentsmd?page=1#post-3002).
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
# MIT License - Copyright (c) 2024 Mark Qvist / unsigned.io
|
|
||||||
|
|
||||||
# This example illustrates creating a custom interface
|
# This example illustrates creating a custom interface
|
||||||
# definition, that can be loaded and used by Reticulum at
|
# definition, that can be loaded and used by Reticulum at
|
||||||
# runtime. Any number of custom interfaces can be created
|
# runtime. Any number of custom interfaces can be created
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,9 @@ def server(configpath):
|
||||||
"linkexample"
|
"linkexample"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# We can optionally configure a link request stamp cost
|
||||||
|
server_destination.set_link_cost(14.2)
|
||||||
|
|
||||||
# We configure a function that will get called every time
|
# We configure a function that will get called every time
|
||||||
# a new client creates a link to this destination.
|
# a new client creates a link to this destination.
|
||||||
server_destination.set_link_established_callback(client_connected)
|
server_destination.set_link_established_callback(client_connected)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
##########################################################
|
##########################################################
|
||||||
# This RNS example demonstrates how to set perform #
|
# This RNS example demonstrates how to perform requests #
|
||||||
# requests and receive responses over a link. #
|
# and receive responses over a link. #
|
||||||
##########################################################
|
##########################################################
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
|
||||||
301
Examples/Resource.py
Normal file
301
Examples/Resource.py
Normal file
|
|
@ -0,0 +1,301 @@
|
||||||
|
##########################################################
|
||||||
|
# This RNS example demonstrates how to transfer a #
|
||||||
|
# resource over an established link #
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
import argparse
|
||||||
|
import RNS
|
||||||
|
|
||||||
|
# Let's define an app name. We'll use this for all
|
||||||
|
# destinations we create. Since this echo example
|
||||||
|
# is part of a range of example utilities, we'll put
|
||||||
|
# them all within the app namespace "example_utilities"
|
||||||
|
APP_NAME = "example_utilities"
|
||||||
|
|
||||||
|
##########################################################
|
||||||
|
#### Server Part #########################################
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
# A reference to the latest client link that connected
|
||||||
|
latest_client_link = None
|
||||||
|
|
||||||
|
# This initialisation is executed when the users chooses
|
||||||
|
# to run as a server
|
||||||
|
def server(configpath):
|
||||||
|
# We must first initialise Reticulum
|
||||||
|
reticulum = RNS.Reticulum(configpath)
|
||||||
|
|
||||||
|
# Randomly create a new identity for our link example
|
||||||
|
server_identity = RNS.Identity()
|
||||||
|
|
||||||
|
# We create a destination that clients can connect to. We
|
||||||
|
# want clients to create links to this destination, so we
|
||||||
|
# need to create a "single" destination type.
|
||||||
|
server_destination = RNS.Destination(
|
||||||
|
server_identity,
|
||||||
|
RNS.Destination.IN,
|
||||||
|
RNS.Destination.SINGLE,
|
||||||
|
APP_NAME,
|
||||||
|
"resourceexample"
|
||||||
|
)
|
||||||
|
|
||||||
|
# We configure a function that will get called every time
|
||||||
|
# a new client creates a link to this destination.
|
||||||
|
server_destination.set_link_established_callback(client_connected)
|
||||||
|
|
||||||
|
# Everything's ready!
|
||||||
|
# Let's Wait for client resources or user input
|
||||||
|
server_loop(server_destination)
|
||||||
|
|
||||||
|
def server_loop(destination):
|
||||||
|
# Let the user know that everything is ready
|
||||||
|
RNS.log(
|
||||||
|
"Resource example "+
|
||||||
|
RNS.prettyhexrep(destination.hash)+
|
||||||
|
" running, waiting for a connection."
|
||||||
|
)
|
||||||
|
|
||||||
|
RNS.log("Hit enter to manually send an announce (Ctrl-C to quit)")
|
||||||
|
|
||||||
|
# We enter a loop that runs until the users exits.
|
||||||
|
# If the user hits enter, we will announce our server
|
||||||
|
# destination on the network, which will let clients
|
||||||
|
# know how to create messages directed towards it.
|
||||||
|
while True:
|
||||||
|
entered = input()
|
||||||
|
destination.announce()
|
||||||
|
RNS.log("Sent announce from "+RNS.prettyhexrep(destination.hash))
|
||||||
|
|
||||||
|
# When a client establishes a link to our server
|
||||||
|
# destination, this function will be called with
|
||||||
|
# a reference to the link.
|
||||||
|
def client_connected(link):
|
||||||
|
global latest_client_link
|
||||||
|
RNS.log("Client connected")
|
||||||
|
|
||||||
|
# We configure the link to accept all resources
|
||||||
|
# and set a callback for completed resources
|
||||||
|
link.set_resource_strategy(RNS.Link.ACCEPT_ALL)
|
||||||
|
link.set_resource_concluded_callback(resource_concluded)
|
||||||
|
|
||||||
|
link.set_link_closed_callback(client_disconnected)
|
||||||
|
latest_client_link = link
|
||||||
|
|
||||||
|
def client_disconnected(link):
|
||||||
|
RNS.log("Client disconnected")
|
||||||
|
|
||||||
|
def resource_concluded(resource):
|
||||||
|
if resource.status == RNS.Resource.COMPLETE:
|
||||||
|
RNS.log(f"Resource {resource} received")
|
||||||
|
RNS.log(f"Metadata: {resource.metadata}")
|
||||||
|
RNS.log(f"Application flag is {'set' if resource.app_flag else 'not set'}")
|
||||||
|
RNS.log(f"Data length: {os.stat(resource.data.name).st_size}")
|
||||||
|
RNS.log(f"Data can be read directly from: {resource.data}")
|
||||||
|
RNS.log(f"Data can be moved or copied from: {resource.data.name}")
|
||||||
|
RNS.log(f"First 32 bytes of data: {RNS.hexrep(resource.data.read(32))}")
|
||||||
|
else:
|
||||||
|
RNS.log(f"Receiving resource {resource} failed")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
##########################################################
|
||||||
|
#### Client Part #########################################
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
# A reference to the server link
|
||||||
|
server_link = None
|
||||||
|
|
||||||
|
def random_text_generator():
|
||||||
|
texts = ["They looked up", "On each full moon", "Becky was upset", "I’ll stay away from it", "The pet shop stocks everything"]
|
||||||
|
return texts[random.randint(0, len(texts)-1)]
|
||||||
|
|
||||||
|
# This initialisation is executed when the users chooses
|
||||||
|
# to run as a client
|
||||||
|
def client(destination_hexhash, configpath):
|
||||||
|
# We need a binary representation of the destination
|
||||||
|
# hash that was entered on the command line
|
||||||
|
try:
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
|
if len(destination_hexhash) != dest_len:
|
||||||
|
raise ValueError(
|
||||||
|
"Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)
|
||||||
|
)
|
||||||
|
|
||||||
|
destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
|
except:
|
||||||
|
RNS.log("Invalid destination entered. Check your input!\n")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
# We must first initialise Reticulum
|
||||||
|
reticulum = RNS.Reticulum(configpath)
|
||||||
|
|
||||||
|
# Check if we know a path to the destination
|
||||||
|
if not RNS.Transport.has_path(destination_hash):
|
||||||
|
RNS.log("Destination is not yet known. Requesting path and waiting for announce to arrive...")
|
||||||
|
RNS.Transport.request_path(destination_hash)
|
||||||
|
while not RNS.Transport.has_path(destination_hash):
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
# Recall the server identity
|
||||||
|
server_identity = RNS.Identity.recall(destination_hash)
|
||||||
|
|
||||||
|
# Inform the user that we'll begin connecting
|
||||||
|
RNS.log("Establishing link with server...")
|
||||||
|
|
||||||
|
# When the server identity is known, we set
|
||||||
|
# up a destination
|
||||||
|
server_destination = RNS.Destination(
|
||||||
|
server_identity,
|
||||||
|
RNS.Destination.OUT,
|
||||||
|
RNS.Destination.SINGLE,
|
||||||
|
APP_NAME,
|
||||||
|
"resourceexample"
|
||||||
|
)
|
||||||
|
|
||||||
|
# And create a link
|
||||||
|
link = RNS.Link(server_destination)
|
||||||
|
|
||||||
|
# We'll set up functions to inform the
|
||||||
|
# user when the link is established or closed
|
||||||
|
link.set_link_established_callback(link_established)
|
||||||
|
link.set_link_closed_callback(link_closed)
|
||||||
|
|
||||||
|
# Everything is set up, so let's enter a loop
|
||||||
|
# for the user to interact with the example
|
||||||
|
client_loop()
|
||||||
|
|
||||||
|
def client_loop():
|
||||||
|
global server_link
|
||||||
|
|
||||||
|
# Wait for the link to become active
|
||||||
|
while not server_link:
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
should_quit = False
|
||||||
|
while not should_quit:
|
||||||
|
try:
|
||||||
|
print("> ", end=" ")
|
||||||
|
text = input()
|
||||||
|
|
||||||
|
# Check if we should quit the example
|
||||||
|
if text == "quit" or text == "q" or text == "exit":
|
||||||
|
should_quit = True
|
||||||
|
server_link.teardown()
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Generate 32 megabytes of random data
|
||||||
|
data = os.urandom(32*1024*1024)
|
||||||
|
RNS.log(f"Data length: {len(data)}")
|
||||||
|
RNS.log(f"First 32 bytes of data: {RNS.hexrep(data[:32])}")
|
||||||
|
|
||||||
|
# Generate some metadata
|
||||||
|
metadata = {"text": random_text_generator(),
|
||||||
|
"numbers": [1,2,3,4], "blob": os.urandom(16)}
|
||||||
|
|
||||||
|
# An optional application-specific flag can be set
|
||||||
|
flag_set = True
|
||||||
|
|
||||||
|
# Send the resource
|
||||||
|
resource = RNS.Resource(data, server_link, metadata=metadata, app_flag=flag_set,
|
||||||
|
callback=resource_concluded_sending, auto_compress=False)
|
||||||
|
|
||||||
|
# Alternatively, you can stream data
|
||||||
|
# directly from an open file descriptor
|
||||||
|
|
||||||
|
# with open("/path/to/file", "rb") as data_file:
|
||||||
|
# resource = RNS.Resource(data_file, server_link, metadata=metadata,
|
||||||
|
# callback=resource_concluded_sending, auto_compress=False)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log("Error while sending resource over the link: "+str(e))
|
||||||
|
should_quit = True
|
||||||
|
server_link.teardown()
|
||||||
|
|
||||||
|
def resource_concluded_sending(resource):
|
||||||
|
if resource.status == RNS.Resource.COMPLETE: RNS.log(f"The resource {resource} was sent successfully")
|
||||||
|
else: RNS.log(f"Sending the resource {resource} failed")
|
||||||
|
|
||||||
|
# This function is called when a link
|
||||||
|
# has been established with the server
|
||||||
|
def link_established(link):
|
||||||
|
# We store a reference to the link
|
||||||
|
# instance for later use
|
||||||
|
global server_link
|
||||||
|
server_link = link
|
||||||
|
|
||||||
|
# Inform the user that the server is
|
||||||
|
# connected
|
||||||
|
RNS.log("Link established with server, hit enter to send a resource, or type in \"quit\" to quit")
|
||||||
|
|
||||||
|
# When a link is closed, we'll inform the
|
||||||
|
# user, and exit the program
|
||||||
|
def link_closed(link):
|
||||||
|
if link.teardown_reason == RNS.Link.TIMEOUT:
|
||||||
|
RNS.log("The link timed out, exiting now")
|
||||||
|
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
|
||||||
|
RNS.log("The link was closed by the server, exiting now")
|
||||||
|
else:
|
||||||
|
RNS.log("Link closed, exiting now")
|
||||||
|
|
||||||
|
time.sleep(1.5)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
##########################################################
|
||||||
|
#### Program Startup #####################################
|
||||||
|
##########################################################
|
||||||
|
|
||||||
|
# This part of the program runs at startup,
|
||||||
|
# and parses input of from the user, and then
|
||||||
|
# starts up the desired program mode.
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
parser = argparse.ArgumentParser(description="Simple resource example")
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--server",
|
||||||
|
action="store_true",
|
||||||
|
help="wait for incoming resources from clients"
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--config",
|
||||||
|
action="store",
|
||||||
|
default=None,
|
||||||
|
help="path to alternative Reticulum config directory",
|
||||||
|
type=str
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"destination",
|
||||||
|
nargs="?",
|
||||||
|
default=None,
|
||||||
|
help="hexadecimal hash of the server destination",
|
||||||
|
type=str
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.config:
|
||||||
|
configarg = args.config
|
||||||
|
else:
|
||||||
|
configarg = None
|
||||||
|
|
||||||
|
if args.server:
|
||||||
|
server(configarg)
|
||||||
|
else:
|
||||||
|
if (args.destination == None):
|
||||||
|
print("")
|
||||||
|
parser.print_help()
|
||||||
|
print("")
|
||||||
|
else:
|
||||||
|
client(args.destination, configarg)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("")
|
||||||
|
sys.exit(0)
|
||||||
|
|
@ -239,7 +239,7 @@ def link_established(link):
|
||||||
RNS.log("Link established with server, sending...")
|
RNS.log("Link established with server, sending...")
|
||||||
rd = os.urandom(link.mdu)
|
rd = os.urandom(link.mdu)
|
||||||
started = time.time()
|
started = time.time()
|
||||||
while link.status == RNS.Link.ACTIVE and data_sent < data_cap*1.25:
|
while link.status != RNS.Link.CLOSED and data_sent < data_cap*1.25:
|
||||||
RNS.Packet(server_link, rd, create_receipt=False).send()
|
RNS.Packet(server_link, rd, create_receipt=False).send()
|
||||||
data_sent += len(rd)
|
data_sent += len(rd)
|
||||||
|
|
||||||
|
|
|
||||||
7
FUNDING.json
Normal file
7
FUNDING.json
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"drips": {
|
||||||
|
"ethereum": {
|
||||||
|
"ownedBy": "0xae89F3B94fC4AD6563F0864a55F9a697a90261ff"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
|
liberapay: Reticulum
|
||||||
ko_fi: markqvist
|
ko_fi: markqvist
|
||||||
custom: "https://unsigned.io/donate"
|
custom: "https://unsigned.io/donate"
|
||||||
|
|
|
||||||
16
LICENSE
16
LICENSE
|
|
@ -1,6 +1,6 @@
|
||||||
MIT License, unless otherwise noted
|
Reticulum License
|
||||||
|
|
||||||
Copyright (c) 2016-2024 Mark Qvist / unsigned.io
|
Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
furnished to do so, subject to the following conditions:
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
- The Software shall not be used in any kind of system which includes amongst
|
||||||
copies or substantial portions of the Software.
|
its functions the ability to purposefully do harm to human beings.
|
||||||
|
|
||||||
|
- The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
an artificial intelligence, machine learning or language model training
|
||||||
|
dataset, including but not limited to any use that contributes to the
|
||||||
|
training or development of such a model or algorithm.
|
||||||
|
|
||||||
|
- The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
|
||||||
33
MIRROR.md
Normal file
33
MIRROR.md
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
This repository is a public mirror. All potential future development is happening elsewhere.
|
||||||
|
|
||||||
|
I am stepping back from all public-facing interaction with this project. Reticulum has always been primarily my work, and continuing in the current public, internet-facing model is no longer sustainable.
|
||||||
|
|
||||||
|
The software remains available for use as-is. Occasional updates may appear at unpredictable intervals, but there will be no support, no responses to issues, no discussions, and no community management in this or any other public venue. If it doesn't work for you, it doesn't work. That is the entire extent of available troubleshooting assistance I can offer you.
|
||||||
|
|
||||||
|
If you've followed this project for a while, you already know what this means. You know who designed, wrote and tested this, and you know how many years of my life it took. You'll also know about both my particular challenges and strengths, and how I believe anything worth building needs to be built and maintained with our own hands.
|
||||||
|
|
||||||
|
Seven months ago, I said I needed to step back, that I was exhausted, and that I needed to recover. I believed a public resolve would be enough to effectuate that, but while striving to get just a few more useful features and protocols out, the unproductive requests and demands also ramped up, and I got pulled back into the same patterns and draining interactions that I'd explicitly said I couldn't sustain anymore.
|
||||||
|
|
||||||
|
So here's what you might have already guessed: I'm done playing the game by rules I can't win at.
|
||||||
|
|
||||||
|
Everything you need is right here, and by any sensible measure, it's done. Anyone who wants to invest the time, skill and persistence can build on it, or completely re-imagine it with different priorities. That was always the point.
|
||||||
|
|
||||||
|
The people who actually contributed - you know who you are, and you know I mean it when I say: Thank you. All of you who've used this to build something real - that was the goal, and you did it without needing me to hold your hand.
|
||||||
|
|
||||||
|
The rest of you: You have what you need. Use it or don't. I am not going to be the person who explains it to you anymore.
|
||||||
|
|
||||||
|
This is not a temporary break. It's not "see you after some rest", but a recognition that the current model is fundamentally incompatible with my life, my health, and my reality.
|
||||||
|
|
||||||
|
If you want to support continued work, you can do so at the donation links listed in this repository. But please understand, that this is not purchasing support or guaranteeing updates. It is support for work that happens on my timeline, according to my capacity, which at the moment is not what it was.
|
||||||
|
|
||||||
|
If you want Reticulum to continue evolving, you have the power to make that happen. The protocol is public domain. The code is open source. Everything you need is right here. I've provided the tools, but building what comes next is not my responsibility anymore. It's yours.
|
||||||
|
|
||||||
|
To the small group of people who has actually been here, and understood what this work was and what it cost - you already know where to find me if it actually matters.
|
||||||
|
|
||||||
|
To everyone else: This is where we part ways. No hard feelings. It's just time.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
असतो मा सद्गमय
|
||||||
|
तमसो मा ज्योतिर्गमय
|
||||||
|
मृत्योर्मा अमृतं गमय
|
||||||
63
Makefile
63
Makefile
|
|
@ -21,9 +21,29 @@ clean:
|
||||||
@-rm -rf ./tests/__pycache__
|
@-rm -rf ./tests/__pycache__
|
||||||
@-rm -rf ./tests/rnsconfig/storage
|
@-rm -rf ./tests/rnsconfig/storage
|
||||||
@-rm -rf ./*.egg-info
|
@-rm -rf ./*.egg-info
|
||||||
|
@-rm -rf ./RNS/*.c
|
||||||
|
@-rm -rf ./RNS/Cryptography/*.c
|
||||||
|
@-rm -rf ./RNS/Cryptography/aes/*.c
|
||||||
|
@-rm -rf ./RNS/Cryptography/pure25519/*.c
|
||||||
|
@-rm -rf ./RNS/Interfaces/*.c
|
||||||
|
@-rm -rf ./RNS/Interfaces/util/*.c
|
||||||
|
@-rm -rf ./RNS/Interfaces/Android/*.c
|
||||||
|
@-rm -rf ./RNS/Utilities/*.c
|
||||||
|
@-rm -rf ./RNS/Utilities/rngit/*.c
|
||||||
|
@-rm -rf ./RNS/Utilities/rnsh/*.c
|
||||||
|
@-rm -rf ./RNS/vendor/*.c
|
||||||
|
@-rm -rf ./RNS/vendor/i2plib/*.c
|
||||||
|
@-rm -rf
|
||||||
@make -C docs clean
|
@make -C docs clean
|
||||||
@echo Done
|
@echo Done
|
||||||
|
|
||||||
|
purge_docs:
|
||||||
|
@echo Purging documentation build...
|
||||||
|
@-rm -rf ./docs/manual
|
||||||
|
@-rm -rf ./docs/markdown
|
||||||
|
@-rm -rf ./docs/*.pdf
|
||||||
|
@-rm -rf ./docs/*.epub
|
||||||
|
|
||||||
remove_symlinks:
|
remove_symlinks:
|
||||||
@echo Removing symlinks for build...
|
@echo Removing symlinks for build...
|
||||||
-rm Examples/RNS
|
-rm Examples/RNS
|
||||||
|
|
@ -34,28 +54,57 @@ create_symlinks:
|
||||||
-ln -s ../RNS ./Examples/
|
-ln -s ../RNS ./Examples/
|
||||||
-ln -s ../../RNS ./RNS/Utilities/
|
-ln -s ../../RNS ./RNS/Utilities/
|
||||||
|
|
||||||
build_sdist_only:
|
build_sdist: purge_docs
|
||||||
python3 setup.py sdist
|
python3 setup.py sdist
|
||||||
|
|
||||||
build_wheel:
|
build_wheel:
|
||||||
python3 setup.py sdist bdist_wheel
|
python3 setup.py bdist_wheel
|
||||||
|
|
||||||
build_pure_wheel:
|
build_pure_wheel:
|
||||||
python3 setup.py sdist bdist_wheel --pure
|
python3 setup.py bdist_wheel --pure
|
||||||
|
|
||||||
|
build_native:
|
||||||
|
python3 setup.py bdist_wheel --native
|
||||||
|
|
||||||
documentation:
|
documentation:
|
||||||
make -C docs html
|
make -C docs html markdown
|
||||||
|
|
||||||
manual:
|
manual:
|
||||||
make -C docs latexpdf epub
|
make -C docs latexpdf epub
|
||||||
|
|
||||||
release: test remove_symlinks build_wheel build_pure_wheel documentation manual create_symlinks
|
distcollect:
|
||||||
|
mv docs/Reticulum\ Manual.* dist
|
||||||
|
|
||||||
|
build_spkg: remove_symlinks build_sdist create_symlinks
|
||||||
|
|
||||||
|
release: test remove_symlinks build_sdist build_wheel build_pure_wheel documentation manual distcollect create_symlinks
|
||||||
|
|
||||||
debug: remove_symlinks build_wheel build_pure_wheel create_symlinks
|
debug: remove_symlinks build_wheel build_pure_wheel create_symlinks
|
||||||
|
|
||||||
|
native: remove_symlinks build_native create_symlinks
|
||||||
|
|
||||||
|
local: release sign
|
||||||
|
|
||||||
|
sign:
|
||||||
|
rngit release rns://7649a50d84610232d1416b41d2896aff/reticulum/reticulum create $$(python setup.py --getversion):dist --name rns --local
|
||||||
|
|
||||||
upload:
|
upload:
|
||||||
@echo Ready to publish release, hit enter to continue
|
@echo Ready to publish release over Reticulum
|
||||||
|
@read VOID
|
||||||
|
rngit release rns://7649a50d84610232d1416b41d2896aff/reticulum/reticulum create $$(python setup.py --getversion):dist --name rns
|
||||||
|
|
||||||
|
upload-pip: upload-rns-pip upload-rnspure-pip
|
||||||
|
|
||||||
|
upload-rns-pip:
|
||||||
|
@echo Ready to publish rns release, hit enter to continue
|
||||||
@read VOID
|
@read VOID
|
||||||
@echo Uploading to PyPi...
|
@echo Uploading to PyPi...
|
||||||
twine upload dist/*
|
twine upload dist/rns-*.whl dist/rns-*.tar.gz
|
||||||
@echo Release published
|
@echo Release published
|
||||||
|
|
||||||
|
upload-rnspure-pip:
|
||||||
|
@echo Ready to publish rnspure release, hit enter to continue
|
||||||
|
@read VOID
|
||||||
|
@echo Uploading to PyPi...
|
||||||
|
twine upload dist/rnspure-*.whl
|
||||||
|
@echo Release published
|
||||||
363
README.md
363
README.md
|
|
@ -1,41 +1,37 @@
|
||||||
Reticulum Network Stack β <img align="right" src="https://static.pepy.tech/personalized-badge/rns?period=total&units=international_system&left_color=grey&right_color=blue&left_text=Installs" style="padding-left:10px"/><a href="https://github.com/markqvist/Reticulum/actions/workflows/build.yml"><img align="right" src="https://github.com/markqvist/Reticulum/actions/workflows/build.yml/badge.svg"/></a>
|
Reticulum Network Stack <img align="right" src="https://static.pepy.tech/personalized-badge/rns?period=month&units=international_system&left_color=grey&right_color=blue&left_text=Installs/month" style="padding-left:10px"/><a href="https://github.com/markqvist/Reticulum/actions/workflows/build.yml"><img align="right" src="https://github.com/markqvist/Reticulum/actions/workflows/build.yml/badge.svg"/></a>
|
||||||
==========
|
==========
|
||||||
|
|
||||||
|
> [!WARNING]
|
||||||
|
> A significant number of hastily launched, incorrect, dubious, directly harmful and, in most cases, entirely LLM-generated fakes of Reticulum implementations, "documentation", "guides" and user-facing applications are currently being marketed under polished, but ultimately misleading or outright deceptive claims.
|
||||||
|
>
|
||||||
|
> For more detailed information, see the relevant sections of the [Brandolini's Reference](https://reticulum.network/manual/brandolinis.html) chapter of the manual. Known good community implementations are listed in the [Community Implementations](#community-implementations) section of this readme. For software and projects recognized by the Reticulum community, see the [Programs Using Reticulum](https://reticulum.network/manual/software.html) chapter of the manual.
|
||||||
|
>
|
||||||
|
> Machine-generated content derived from the RNS source code or documentation (such as "implementations", "guides" for sale, et cetera), are in several, well-documented cases violating the open source licenses that granted rights of copying and distribution.
|
||||||
|
>
|
||||||
|
> Any claimed assertion of copyright or grant of license (Apache, GPL, et cetera) that such projects are making are [void grants](https://reticulum.network/manual/brandolinis.html#void-grants-legal-foundations-of-machine-generated-code). Both the violators themselves, and all downstream projects using the infringing derivatives, are **directly** and personally liable for the legal consequences.
|
||||||
|
>
|
||||||
|
> Do **not** use these "projects", they are a hazard to the entire ecosystem we have been carefully building over the last ten years.
|
||||||
|
|
||||||
<p align="center"><img width="200" src="https://raw.githubusercontent.com/markqvist/Reticulum/master/docs/source/graphics/rns_logo_512.png"></p>
|
<p align="center"><img width="200" src="https://raw.githubusercontent.com/markqvist/Reticulum/master/docs/source/graphics/rns_logo_512.png"></p>
|
||||||
|
|
||||||
Reticulum is the cryptography-based networking stack for building local and wide-area
|
*This repository is [a public mirror](./MIRROR.md). All development is happening elsewhere.*
|
||||||
networks with readily available hardware. It can operate even with very high latency
|
|
||||||
and extremely low bandwidth. Reticulum allows you to build wide-area networks
|
|
||||||
with off-the-shelf tools, and offers end-to-end encryption and connectivity,
|
|
||||||
initiator anonymity, autoconfiguring cryptographically backed multi-hop
|
|
||||||
transport, efficient addressing, unforgeable delivery acknowledgements and
|
|
||||||
more.
|
|
||||||
|
|
||||||
The vision of Reticulum is to allow anyone to be their own network operator,
|
To understand the foundational philosophy and goals of this system, read the [Zen of Reticulum](Zen%20of%20Reticulum.md).
|
||||||
and to make it cheap and easy to cover vast areas with a myriad of independent,
|
|
||||||
inter-connectable and autonomous networks. Reticulum **is not** *one* network.
|
|
||||||
It is **a tool** for building *thousands of networks*. Networks without
|
|
||||||
kill-switches, surveillance, censorship and control. Networks that can freely
|
|
||||||
interoperate, associate and disassociate with each other, and require no
|
|
||||||
central oversight. Networks for human beings. *Networks for the people*.
|
|
||||||
|
|
||||||
Reticulum is a complete networking stack, and does not rely on IP or higher
|
Reticulum is the cryptography-based networking stack for building local and wide-area networks with readily available hardware. It can operate even with very high latency and extremely low bandwidth. Reticulum allows you to build wide-area networks with off-the-shelf tools, and offers end-to-end encryption and connectivity, initiator anonymity, autoconfiguring cryptographically backed multi-hop transport, efficient addressing, unforgeable delivery acknowledgements and more.
|
||||||
layers, but it is possible to use IP as the underlying carrier for Reticulum.
|
|
||||||
It is therefore trivial to tunnel Reticulum over the Internet or private IP
|
|
||||||
networks.
|
|
||||||
|
|
||||||
Having no dependencies on traditional networking stacks frees up overhead that
|
The vision of Reticulum is to allow anyone to be their own network operator, and to make it cheap and easy to cover vast areas with a myriad of independent, inter-connectable and autonomous networks. Reticulum **is not** *one* network. It is **a tool** for building *thousands of networks*. Networks without kill-switches, surveillance, censorship and control. Networks that can freely interoperate, associate and disassociate with each other, and require no central oversight. Networks for human beings. *Networks for the people*.
|
||||||
has been used to implement a networking stack built directly on cryptographic
|
|
||||||
principles, allowing resilience and stable functionality, even in open and
|
|
||||||
trustless networks.
|
|
||||||
|
|
||||||
No kernel modules or drivers are required. Reticulum runs completely in
|
Reticulum is a complete networking stack, and does not rely on IP or higher layers, but it is possible to use IP as the underlying carrier for Reticulum. It is therefore trivial to tunnel Reticulum over the Internet or private IP networks.
|
||||||
userland, and can run on practically any system that runs Python 3.
|
|
||||||
|
Having no dependencies on traditional networking stacks frees up overhead that has been used to implement a networking stack built directly on cryptographic principles, allowing resilience and stable functionality, even in open and trustless networks.
|
||||||
|
|
||||||
|
No kernel modules or drivers are required. Reticulum runs completely in userland, and can run on practically any system that runs Python 3.
|
||||||
|
|
||||||
## Read The Manual
|
## Read The Manual
|
||||||
The full documentation for Reticulum is available at [markqvist.github.io/Reticulum/manual/](https://markqvist.github.io/Reticulum/manual/).
|
The full documentation for Reticulum is available at [markqvist.github.io/Reticulum/manual/](https://markqvist.github.io/Reticulum/manual/).
|
||||||
|
|
||||||
You can also download the [Reticulum manual as a PDF](https://github.com/markqvist/Reticulum/raw/master/docs/Reticulum%20Manual.pdf) or [as an e-book in EPUB format](https://github.com/markqvist/Reticulum/raw/master/docs/Reticulum%20Manual.epub).
|
You can also download the [Reticulum manual as a PDF](https://github.com/markqvist/Reticulum/releases/latest/download/Reticulum.Manual.pdf) or [as an e-book in EPUB format](https://github.com/markqvist/Reticulum/releases/latest/download/Reticulum.Manual.epub).
|
||||||
|
|
||||||
For more info, see [reticulum.network](https://reticulum.network/) and [the FAQ section of the wiki](https://github.com/markqvist/Reticulum/wiki/Frequently-Asked-Questions).
|
For more info, see [reticulum.network](https://reticulum.network/) and [the FAQ section of the wiki](https://github.com/markqvist/Reticulum/wiki/Frequently-Asked-Questions).
|
||||||
|
|
||||||
|
|
@ -52,7 +48,7 @@ For more info, see [reticulum.network](https://reticulum.network/) and [the FAQ
|
||||||
- Forward Secrecy is available for all communication types, both for single packets and over links
|
- Forward Secrecy is available for all communication types, both for single packets and over links
|
||||||
- Reticulum uses the following format for encrypted tokens:
|
- Reticulum uses the following format for encrypted tokens:
|
||||||
- Ephemeral per-packet and link keys and derived from an ECDH key exchange on Curve25519
|
- Ephemeral per-packet and link keys and derived from an ECDH key exchange on Curve25519
|
||||||
- AES-128 in CBC mode with PKCS7 padding
|
- AES-256 in CBC mode with PKCS7 padding
|
||||||
- HMAC using SHA256 for authentication
|
- HMAC using SHA256 for authentication
|
||||||
- IVs are generated through os.urandom()
|
- IVs are generated through os.urandom()
|
||||||
- Unforgeable packet delivery confirmations
|
- Unforgeable packet delivery confirmations
|
||||||
|
|
@ -62,7 +58,7 @@ For more info, see [reticulum.network](https://reticulum.network/) and [the FAQ
|
||||||
- Easily create your own custom interfaces for communicating over anything
|
- Easily create your own custom interfaces for communicating over anything
|
||||||
- Authentication and virtual network segmentation on all supported interface types
|
- Authentication and virtual network segmentation on all supported interface types
|
||||||
- An intuitive and easy-to-use API
|
- An intuitive and easy-to-use API
|
||||||
- Simpler and easier to use than sockets APIs and simpler, but more powerful
|
- Simpler and easier to use than sockets APIs, but more powerful
|
||||||
- Makes building distributed and decentralised applications much simpler
|
- Makes building distributed and decentralised applications much simpler
|
||||||
- Reliable and efficient transfer of arbitrary amounts of data
|
- Reliable and efficient transfer of arbitrary amounts of data
|
||||||
- Reticulum can handle a few bytes of data or files of many gigabytes
|
- Reticulum can handle a few bytes of data or files of many gigabytes
|
||||||
|
|
@ -74,65 +70,88 @@ For more info, see [reticulum.network](https://reticulum.network/) and [the FAQ
|
||||||
- Low cost of keeping links open at only 0.44 bits per second
|
- Low cost of keeping links open at only 0.44 bits per second
|
||||||
- Reliable sequential delivery with Channel and Buffer mechanisms
|
- Reliable sequential delivery with Channel and Buffer mechanisms
|
||||||
|
|
||||||
## Roadmap
|
## Community Resources
|
||||||
While Reticulum is already a fully featured and functional networking stack,
|
|
||||||
many improvements and additions are actively being worked on, and planned for the future.
|
|
||||||
|
|
||||||
To learn more about the direction and future of Reticulum, please see the [Development Roadmap](./Roadmap.md).
|
The references listed here are not run, managed or moderated by the Reticulum project or its maintainer. They are provided by dedicated, independent volunteers in the community, for the benefit of anyone interested in learning more about Reticulum.
|
||||||
|
|
||||||
|
- [Reticulum Community Wiki](https://reticulum.miraheze.org/wiki)
|
||||||
|
- [RNS Recipes](https://rns.recipes/)
|
||||||
|
- [RNS Moscow](https://rns.moscow/)
|
||||||
|
- [RMAP.world](https://rmap.world/)
|
||||||
|
|
||||||
|
If you find anything that is no longer accurate, incorrect or simply not quite up to date, do not demand or complain towards the maintainers of these resources. Instead, take the task upon yourself to help out and improve things by working on, and offering the contributions you would like to see, like the creators of the above did themselves.
|
||||||
|
|
||||||
|
## Reference Implementation
|
||||||
|
|
||||||
|
The Python code in this repository is the Reference Implementation of Reticulum. The Reticulum Protocol is defined entirely and authoritatively by this reference implementation, and its associated manual. It is maintained by Mark Qvist, identified by the Reticulum Identity `<bc7291552be7a58f361522990465165c>`.
|
||||||
|
|
||||||
|
No second-order protocol specification derivatives currently do, or will ever exist. You can't get a better specification than one that is executable, functional, testable, deployed and operating 24/7 across the planet.
|
||||||
|
|
||||||
|
There is not, and never will be a separate, derived "formal spec", "RFC" or anything similar for Reticulum:
|
||||||
|
|
||||||
|
**The reference implementation *is* the final, authoritative specification.**
|
||||||
|
|
||||||
|
Compatibility with the Reticulum Protocol is defined as having full interoperability, and sufficient functional parity with this reference implementation. Any specific protocol implementation that achieves this is Reticulum. Any that does not is not Reticulum.
|
||||||
|
|
||||||
|
The reference implementation is licensed under the Reticulum License.
|
||||||
|
|
||||||
|
The Reticulum Protocol was dedicated to the Public Domain in 2016.
|
||||||
|
|
||||||
|
## Community Implementations
|
||||||
|
|
||||||
|
The implementations listed here have demonstrated wire-compatibility, sufficient feature parity, sensible security and development practices, long-term commitment and care for implementation quality. Additionally, they have a track record of at least 18 months of active development.
|
||||||
|
|
||||||
|
Most importantly, their developers and maintainers are recognized by the community to actually know what they are doing, and as having a commitment to improving the overall Reticulum ecosystem.
|
||||||
|
|
||||||
|
- [Chad Atterman's](https://github.com/attermann) [microReticulum](https://github.com/attermann/microReticulum/) is a C++ implementation targeting 32-bit micro-controllers.
|
||||||
|
- [Quad4's](https://quad4.io/) [Reticulum-Go](https://reticulum-go.quad4.io/) is a Go implementation targeting both desktop, mobile and embedded systems.
|
||||||
|
|
||||||
|
*If you are the maintainer of an implementation not listed above, and believe that it deserves inclusion by following the same standards of rigor and commitment, your proposal will be welcomed.*
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
**Warning!** A significant number of hastily launched, incorrect, dubious, directly harmful and, in most cases, entirely LLM-generated fakes of Reticulum implementations (and user facing applications) are currently being marketed and pushed under polished, but ultimately misleading or outright deceptive claims. Most of these are either quick attention or money grabs, completely unreviewed, incompatible, without maintenance or human oversight, actively trying to fragment the community, or a combination thereof.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
For more extensive information on this topic, read the [Brandolini's Reference](https://reticulum.network/manual/brandolinis.html) chapter of the manual (for the legal foundations, see [Void Grants](https://reticulum.network/manual/brandolinis.html#void-grants-legal-foundations-of-machine-generated-code) specifically). The chapter is very long, very detailed, and is the polite, measured analysis, intended as an evolving, sectioned reference on these issues.
|
||||||
|
|
||||||
|
If you would rather just have the short, unfiltered and quite angry version, which consists of an enacted response to one particular manifestation of the phenomenon as a whole, used as an example, you can read [Yes, I am Angry](docs/history/2026_09_18_Yes_I_am_Angry.md). It gets the same point across, in a very different language, if you can handle that sort of communication.
|
||||||
|
|
||||||
|
The core Reticulum maintainers and developers **strongly advise against using any such "implementation"**. The [deceitful and self-serving behavior](https://reticulum.network/manual/brandolinis.html#a-persistent-but-fragile-triad) of a small number of either malintented, attention-seeking or incompetent individuals has caused significant [damage and waste of time and resources](https://reticulum.network/manual/brandolinis.html#intent-consequence) for both developers, node operators, users and the community and Reticulum project at large.
|
||||||
|
|
||||||
|
We kindly, but urgently ask all users to excercise caution, common sense and healthy skepticism when [evaluating](https://reticulum.network/manual/brandolinis.html#evaluating-a-reticulum-implementation) the potential use of any implementation not listed above; and to accept that **choosing to do so regardless translates the responsibility for potential damage from an irresponsible or malicious individual onto yourself**.
|
||||||
|
|
||||||
## Examples of Reticulum Applications
|
## Examples of Reticulum Applications
|
||||||
If you want to quickly get an idea of what Reticulum can do, take a look at the
|
If you want to quickly get an idea of what Reticulum can do, take a look at the [Programs Using Reticulum](https://reticulum.network/manual/software.html) section of the manual, or the following resources:
|
||||||
following resources.
|
|
||||||
|
|
||||||
- You can use the [rnsh](https://github.com/acehoss/rnsh) program to establish remote shell sessions over Reticulum.
|
|
||||||
- [LXMF](https://github.com/markqvist/lxmf) is a distributed, delay and disruption tolerant message transfer protocol built on Reticulum
|
- [LXMF](https://github.com/markqvist/lxmf) is a distributed, delay and disruption tolerant message transfer protocol built on Reticulum
|
||||||
|
- The [LXST](https://github.com/markqvist/lxst) protocol and framework provides real-time audio and signals transport over Reticulum. It includes primitives and utilities for building voice-based applications and hardware devices, such as the `rnphone` program, that can be used to build hardware telephones.
|
||||||
- For an off-grid, encrypted and resilient mesh communications platform, see [Nomad Network](https://github.com/markqvist/NomadNet)
|
- For an off-grid, encrypted and resilient mesh communications platform, see [Nomad Network](https://github.com/markqvist/NomadNet)
|
||||||
- The Android, Linux, macOS and Windows app [Sideband](https://github.com/markqvist/Sideband) has a graphical interface and focuses on ease of use.
|
- The Android, Linux, macOS and Windows app [Sideband](https://github.com/markqvist/Sideband) has a graphical interface and many advanced features, such as file transfers, image and voice messages, real-time voice calls, a distributed telemetry system, mapping capabilities and full plugin extensibility.
|
||||||
- [MeshChat](https://github.com/liamcottle/reticulum-meshchat) is a user-friendly LXMF client, that also supports voice calls.
|
- [MeshChatX](https://git.quad4.io/RNS-Things/MeshChatX) is a full-featured LXMF client with many built-in tools and functionalities, that also supports image and voice messages, file transfers and voice calls. It also includes a built-in page browser for browsing Nomad Network nodes.
|
||||||
|
- You can use the included [rnsh](https://reticulum.network/manual/using.html#the-rnsh-utility) program to establish remote shell sessions over Reticulum.
|
||||||
|
- The included [rngit](https://reticulum.network/manual/git.html) system provides a full solution for managing, hosting, collaborating on and interacting with Git repositories over Reticulum.
|
||||||
|
|
||||||
## Where can Reticulum be used?
|
## Where can Reticulum be used?
|
||||||
Over practically any medium that can support at least a half-duplex channel
|
Over practically any medium that can support at least a half-duplex channel with greater throughput than 5 bits per second, and an MTU of 500 bytes. Data radios, modems, LoRa radios, serial lines, AX.25 TNCs, amateur radio digital modes, WiFi and Ethernet devices, free-space optical links, and similar systems are all examples of the types of physical devices Reticulum can use.
|
||||||
with greater throughput than 5 bits per second, and an MTU of 500 bytes. Data radios,
|
|
||||||
modems, LoRa radios, serial lines, AX.25 TNCs, amateur radio digital modes,
|
|
||||||
WiFi and Ethernet devices, free-space optical links, and similar systems are
|
|
||||||
all examples of the types of physical devices Reticulum can use.
|
|
||||||
|
|
||||||
An open-source LoRa-based interface called
|
An open-source LoRa-based interface called [RNode](https://markqvist.github.io/Reticulum/manual/hardware.html#rnode) has been designed specifically for use with Reticulum. It is possible to build yourself, or it can be purchased as a complete transceiver that just needs a USB, WiFi or Bluetooth connection to the host.
|
||||||
[RNode](https://markqvist.github.io/Reticulum/manual/hardware.html#rnode) has
|
|
||||||
been designed specifically for use with Reticulum. It is possible to build
|
|
||||||
yourself, or it can be purchased as a complete transceiver that just needs a
|
|
||||||
USB connection to the host.
|
|
||||||
|
|
||||||
Reticulum can also be encapsulated over existing IP networks, so there's
|
Reticulum can also be encapsulated over existing IP networks, so there's nothing stopping you from using it over wired Ethernet, your local WiFi network or the Internet, where it'll work just as well. In fact, one of the strengths of Reticulum is how easily it allows you to connect different mediums into a self-configuring, resilient and encrypted mesh, using any available mixture of available infrastructure.
|
||||||
nothing stopping you from using it over wired Ethernet, your local WiFi network
|
|
||||||
or the Internet, where it'll work just as well. In fact, one of the strengths
|
|
||||||
of Reticulum is how easily it allows you to connect different mediums into a
|
|
||||||
self-configuring, resilient and encrypted mesh, using any available mixture of
|
|
||||||
available infrastructure.
|
|
||||||
|
|
||||||
As an example, it's possible to set up a Raspberry Pi connected to both a LoRa
|
As an example, it's possible to set up a Raspberry Pi connected to both a LoRa radio, a packet radio TNC and a WiFi network. Once the interfaces are configured, Reticulum will take care of the rest, and any device on the WiFi network can communicate with nodes on the LoRa and packet radio sides of the network, and vice versa.
|
||||||
radio, a packet radio TNC and a WiFi network. Once the interfaces are
|
|
||||||
configured, Reticulum will take care of the rest, and any device on the WiFi
|
|
||||||
network can communicate with nodes on the LoRa and packet radio sides of the
|
|
||||||
network, and vice versa.
|
|
||||||
|
|
||||||
## How do I get started?
|
## How do I get started?
|
||||||
The best way to get started with the Reticulum Network Stack depends on what
|
The best way to get started with the Reticulum Network Stack depends on what you want to do. For full details and examples, have a look at the [Getting Started Fast](https://markqvist.github.io/Reticulum/manual/gettingstartedfast.html) section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
||||||
you want to do. For full details and examples, have a look at the
|
|
||||||
[Getting Started Fast](https://markqvist.github.io/Reticulum/manual/gettingstartedfast.html)
|
|
||||||
section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
|
||||||
|
|
||||||
To simply install Reticulum and related utilities on your system, the easiest way is via `pip`.
|
To simply install Reticulum and related utilities on your system, the easiest way is via `pip`. You can then start any program that uses Reticulum, or start Reticulum as a system service with [the rnsd utility](https://markqvist.github.io/Reticulum/manual/using.html#the-rnsd-utility).
|
||||||
You can then start any program that uses Reticulum, or start Reticulum as a system service with
|
|
||||||
[the rnsd utility](https://markqvist.github.io/Reticulum/manual/using.html#the-rnsd-utility).
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install rns
|
pip install rns
|
||||||
```
|
```
|
||||||
|
|
||||||
If you are using an operating system that blocks normal user package installation via `pip`,
|
If you are using an operating system that blocks normal user package installation via `pip`, you can return `pip` to normal behaviour by editing the `~/.config/pip/pip.conf` file, and adding the following directive in the `[global]` section:
|
||||||
you can return `pip` to normal behaviour by editing the `~/.config/pip/pip.conf` file,
|
|
||||||
and adding the following directive in the `[global]` section:
|
|
||||||
|
|
||||||
```text
|
```text
|
||||||
[global]
|
[global]
|
||||||
|
|
@ -145,23 +164,14 @@ Alternatively, you can use the `pipx` tool to install Reticulum in an isolated e
|
||||||
pipx install rns
|
pipx install rns
|
||||||
```
|
```
|
||||||
|
|
||||||
When first started, Reticulum will create a default configuration file,
|
When first started, Reticulum will create a default configuration file, providing basic connectivity to other Reticulum peers that might be locally reachable. The default config file contains a few examples, and references for creating a more complex configuration.
|
||||||
providing basic connectivity to other Reticulum peers that might be locally
|
|
||||||
reachable. The default config file contains a few examples, and references for
|
|
||||||
creating a more complex configuration.
|
|
||||||
|
|
||||||
If you have an old version of `pip` on your system, you may need to upgrade it first with `pip install pip --upgrade`. If you no not already have `pip` installed, you can install it using the package manager of your system with `sudo apt install python3-pip` or similar.
|
If you have an old version of `pip` on your system, you may need to upgrade it first with `pip install pip --upgrade`. If you no not already have `pip` installed, you can install it using the package manager of your system with `sudo apt install python3-pip` or similar.
|
||||||
|
|
||||||
For more detailed examples on how to expand communication over many mediums such
|
For more detailed examples on how to expand communication over many mediums such as packet radio or LoRa, serial ports, or over fast IP links and the Internet using the UDP and TCP interfaces, take a look at the [Supported Interfaces](https://markqvist.github.io/Reticulum/manual/interfaces.html) section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
||||||
as packet radio or LoRa, serial ports, or over fast IP links and the Internet using
|
|
||||||
the UDP and TCP interfaces, take a look at the [Supported Interfaces](https://markqvist.github.io/Reticulum/manual/interfaces.html)
|
|
||||||
section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
|
||||||
|
|
||||||
## Included Utilities
|
## Included Utilities
|
||||||
Reticulum includes a range of useful utilities for managing your networks,
|
Reticulum includes a range of useful utilities for managing your networks, viewing status and information, and other tasks. You can read more about these programs in the [Included Utility Programs](https://markqvist.github.io/Reticulum/manual/using.html#included-utility-programs) section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
||||||
viewing status and information, and other tasks. You can read more about these
|
|
||||||
programs in the [Included Utility Programs](https://markqvist.github.io/Reticulum/manual/using.html#included-utility-programs)
|
|
||||||
section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/).
|
|
||||||
|
|
||||||
- The system daemon `rnsd` for running Reticulum as an always-available service
|
- The system daemon `rnsd` for running Reticulum as an always-available service
|
||||||
- An interface status utility called `rnstatus`, that displays information about interfaces
|
- An interface status utility called `rnstatus`, that displays information about interfaces
|
||||||
|
|
@ -169,22 +179,18 @@ section of the [Reticulum Manual](https://markqvist.github.io/Reticulum/manual/)
|
||||||
- A diagnostics tool called `rnprobe` for checking connectivity to destinations
|
- A diagnostics tool called `rnprobe` for checking connectivity to destinations
|
||||||
- A simple file transfer program called `rncp` making it easy to transfer files between systems
|
- A simple file transfer program called `rncp` making it easy to transfer files between systems
|
||||||
- The identity management and encryption utility `rnid` let's you manage Identities and encrypt/decrypt files
|
- The identity management and encryption utility `rnid` let's you manage Identities and encrypt/decrypt files
|
||||||
- The remote command execution program `rnx` let's you run commands and
|
- The `rnsh` program allows you to establish fully interactive shell session with remote systems
|
||||||
programs and retrieve output from remote systems
|
- The remote command execution program `rnx` let's you run simple commands and programs and retrieve output from remote systems
|
||||||
|
- The `rngit` program provides a full multi-repository Git node for serving repositories over Reticulum
|
||||||
|
- The included `git-remote-rns` helper allows you to interact with Git repositories over Reticulum
|
||||||
|
|
||||||
All tools, including `rnx` and `rncp`, work reliably and well even over very
|
All tools, including `rnx` and `rncp`, work reliably and well even over very low-bandwidth links like LoRa or Packet Radio. For full-featured remote shells over Reticulum, also have a look at the [rnsh](https://github.com/acehoss/rnsh) program.
|
||||||
low-bandwidth links like LoRa or Packet Radio. For full-featured remote shells
|
|
||||||
over Reticulum, also have a look at the [rnsh](https://github.com/acehoss/rnsh)
|
|
||||||
program.
|
|
||||||
|
|
||||||
## Supported interface types and devices
|
## Supported interface types and devices
|
||||||
|
|
||||||
Reticulum implements a range of generalised interface types that covers most of
|
Reticulum implements a range of generalised interface types that covers most of the communications hardware that Reticulum can run over. If your hardware is not supported, it's [simple to implement a custom interface module](https://markqvist.github.io/Reticulum/manual/interfaces.html#custom-interfaces).
|
||||||
the communications hardware that Reticulum can run over. If your hardware is
|
|
||||||
not supported, it's [simple to implement a custom interface module](https://markqvist.github.io/Reticulum/manual/interfaces.html#custom-interfaces).
|
|
||||||
|
|
||||||
Pull requests for custom interfaces are gratefully accepted, provided they are
|
Contributions of custom interfaces are gratefully accepted, provided they are generally useful and well-tested in real-world usage.
|
||||||
generally useful and well-tested in real-world usage.
|
|
||||||
|
|
||||||
Currently, the following built-in interfaces are supported:
|
Currently, the following built-in interfaces are supported:
|
||||||
|
|
||||||
|
|
@ -199,120 +205,71 @@ Currently, the following built-in interfaces are supported:
|
||||||
- Custom hardware via stdio or pipes
|
- Custom hardware via stdio or pipes
|
||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
Reticulum targets a *very* wide usable performance envelope, but prioritises
|
*All performance numbers here assume `rnsd` running in fully interpreted Python mode, limited to a single CPU core.*
|
||||||
functionality and performance on low-bandwidth mediums. The goal is to
|
|
||||||
provide a dynamic performance envelope from 250 bits per second, to 1 gigabit
|
|
||||||
per second on normal hardware.
|
|
||||||
|
|
||||||
Currently, the usable performance envelope is approximately 150 bits per second
|
Reticulum targets a *very* wide usable performance envelope, but prioritises functionality and performance on low-bandwidth mediums. The goal is to provide a dynamic performance envelope from 100 bits per second, to 10 gigabits per second on normal hardware.
|
||||||
to 40 megabits per second, with physical mediums faster than that not being
|
|
||||||
saturated. Performance beyond the current level is intended for future
|
Currently, the on-network, usable performance envelope is approximately 150 bits per second to 1 gigabit per second on modest hardware. Real-world performance will vary widely with the speed of your system, availability of cryptographic hardware acceleration, and other factors.
|
||||||
upgrades, but not highly prioritised at this point in time.
|
|
||||||
|
Raw routing throughput for a transport node hosted on a Raspberry Pi 5 is approximately 5 gigabits per second. Systems with modern crypto hardware acceleration can achieve transport throughput of 15 gigabits per second and higher.
|
||||||
|
|
||||||
|
While the reference implementation is perfectly capable of reaching 100+ Gbps, by utilizing multiple CPU cores and vectorized transport, performance beyond the current level is not higly prioritised, since RNS is already able to saturate available physical medium bandwidth in almost all real-world situations.
|
||||||
|
|
||||||
|
The primary objectives for RNS are *reliability*, *correctness*, *security*, *proper traffic management* and *actually practical performance*, not hollow on-paper "benchmarks" with little to no bearing on real-world usability.
|
||||||
|
|
||||||
|
All alternative-implementation maintainers are *highly* encouraged to adopt a similar approach, and especially to focus on getting their implementations working properly and reliably before publishing empirically dubious or factually wrong performance claims.
|
||||||
|
|
||||||
## Current Status
|
## Current Status
|
||||||
Reticulum should currently be considered beta software. All core protocol
|
All core protocol features are implemented and functioning, but additions will probably occur as real-world use is explored and understood. The API and wire-format can be considered stable.
|
||||||
features are implemented and functioning, but additions will probably occur as
|
|
||||||
real-world use is explored. There will be bugs. The API and wire-format can be
|
|
||||||
considered relatively stable at the moment, but could change if warranted.
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
The installation of the default `rns` package requires the dependencies listed
|
The installation of the default `rns` package requires only two external dependencies, listed below. Almost all systems and distributions have readily available packages for these dependencies, and when the `rns` package is installed with `pip`, they will be downloaded and installed as well.
|
||||||
below. Almost all systems and distributions have readily available packages for
|
|
||||||
these dependencies, and when the `rns` package is installed with `pip`, they
|
|
||||||
will be downloaded and installed as well.
|
|
||||||
|
|
||||||
- [PyCA/cryptography](https://github.com/pyca/cryptography)
|
- [PyCA/cryptography](https://github.com/pyca/cryptography)
|
||||||
- [pyserial](https://github.com/pyserial/pyserial)
|
- [pyserial](https://github.com/pyserial/pyserial)
|
||||||
|
|
||||||
On more unusual systems, and in some rare cases, it might not be possible to
|
On more unusual systems, and in some rare cases, it might not be possible to install or even compile one or more of the above modules. In such situations, you can use the `rnspure` package instead, which require no external dependencies for installation. Please note that the contents of the `rns` and `rnspure` packages are *identical*. The only difference is that the `rnspure` package lists no dependencies required for installation.
|
||||||
install or even compile one or more of the above modules. In such situations,
|
|
||||||
you can use the `rnspure` package instead, which require no external
|
|
||||||
dependencies for installation. Please note that the contents of the `rns` and
|
|
||||||
`rnspure` packages are *identical*. The only difference is that the `rnspure`
|
|
||||||
package lists no dependencies required for installation.
|
|
||||||
|
|
||||||
No matter how Reticulum is installed and started, it will load external
|
No matter how Reticulum is installed and started, it will load external dependencies only if they are *needed* and *available*. If for example you want to use Reticulum on a system that cannot support [pyserial](https://github.com/pyserial/pyserial), it is perfectly possible to do so using the `rnspure` package, but Reticulum will not be able to use serial-based interfaces. All other available modules will still be loaded when needed.
|
||||||
dependencies only if they are *needed* and *available*. If for example you want
|
|
||||||
to use Reticulum on a system that cannot support
|
|
||||||
[pyserial](https://github.com/pyserial/pyserial), it is perfectly possible to
|
|
||||||
do so using the `rnspure` package, but Reticulum will not be able to use
|
|
||||||
serial-based interfaces. All other available modules will still be loaded when
|
|
||||||
needed.
|
|
||||||
|
|
||||||
**Please Note!** If you use the `rnspure` package to run Reticulum on systems
|
**Please Note!** If you use the `rnspure` package to run Reticulum on systems that do not support [PyCA/cryptography](https://github.com/pyca/cryptography), it is important that you read and understand the [Cryptographic Primitives](#cryptographic-primitives) section of this document.
|
||||||
that do not support [PyCA/cryptography](https://github.com/pyca/cryptography),
|
|
||||||
it is important that you read and understand the [Cryptographic
|
## Bootstrapping Connectivity
|
||||||
Primitives](#cryptographic-primitives) section of this document.
|
|
||||||
|
Reticulum is not a service you subscribe to, nor is it a single global network you "join". Reticulum provides functionality for discovering available public interfaces over the network itself, and the broader community has provided various directories of publicly available entrypoints to bootstrap connectivity.
|
||||||
|
|
||||||
|
To learn how to establish initial connectivity over Reticulum, read the [Bootstrapping Connectivity](https://reticulum.network/manual/gettingstartedfast.html#bootstrapping-connectivity) section of the manual.
|
||||||
|
|
||||||
|
If you already have a general idea of how this works, you can use community-run sites such as [directory.rns.recipes](https://directory.rns.recipes/) and [rmap.world](https://rmap.world) to find interface definitions for initial connectivity to the global distributed Reticulum backbone.
|
||||||
|
|
||||||
## Public Testnet
|
## Public Testnet
|
||||||
If you just want to get started experimenting without building any physical
|
***Important!** Historically, a developer-targeted testnet was made available by the Reticulum project itself. As the amount of global Reticulum nodes and entrypoints have grown to a substantial quantity, this public testnet, including the Amsterdam Testnet entrypoint, has now been decommissioned. If your still have instances that relied on this entrypoint for connectivity, transition to using the distributed backbone instead. Reticulum now includes a full on-network interface discovery and connectivity bootstrapping system. Read the [Bootstrapping Connectivity](https://reticulum.network/manual/gettingstartedfast.html#bootstrapping-connectivity) section of the manual for pointers.*
|
||||||
networks, you are welcome to join the RNS Development Testnet.
|
|
||||||
|
|
||||||
The testnet is just that, an informal network for testing and experimenting.
|
|
||||||
It will be up most of the time, and anyone can join, but it also means that
|
|
||||||
there's no guarantees for service availability.
|
|
||||||
|
|
||||||
It probably goes without saying, but *don't use the testnet entry-points as
|
|
||||||
hardcoded or default interfaces in any applications you ship to users*. When
|
|
||||||
shipping applications, the best practice is to provide your own default
|
|
||||||
connectivity solutions, if needed and applicable, or in most cases, simply
|
|
||||||
leave it up to the user which networks to connect to, and how.
|
|
||||||
|
|
||||||
The testnet runs the very latest version of Reticulum (often even a short while
|
|
||||||
before it is publicly released). Sometimes experimental versions of Reticulum
|
|
||||||
might be deployed to nodes on the testnet, which means strange behaviour might
|
|
||||||
occur. If none of that scares you, you can join the testnet via either TCP or
|
|
||||||
I2P. Just add one of the following interfaces to your Reticulum configuration
|
|
||||||
file:
|
|
||||||
|
|
||||||
```
|
|
||||||
# TCP/IP interface to the RNS Amsterdam Hub
|
|
||||||
[[RNS Testnet Amsterdam]]
|
|
||||||
type = TCPClientInterface
|
|
||||||
enabled = yes
|
|
||||||
target_host = amsterdam.connect.reticulum.network
|
|
||||||
target_port = 4965
|
|
||||||
|
|
||||||
# TCP/IP interface to the BetweenTheBorders Hub (community-provided)
|
|
||||||
[[RNS Testnet BetweenTheBorders]]
|
|
||||||
type = TCPClientInterface
|
|
||||||
enabled = yes
|
|
||||||
target_host = reticulum.betweentheborders.com
|
|
||||||
target_port = 4242
|
|
||||||
|
|
||||||
# Interface to Testnet I2P Hub
|
|
||||||
[[RNS Testnet I2P Hub]]
|
|
||||||
type = I2PInterface
|
|
||||||
enabled = yes
|
|
||||||
peers = g3br23bvx3lq5uddcsjii74xgmn6y5q325ovrkq2zw2wbzbqgbuq.b32.i2p
|
|
||||||
```
|
|
||||||
|
|
||||||
The testnet also contains a number of [Nomad Network](https://github.com/markqvist/nomadnet) nodes, and LXMF propagation nodes.
|
|
||||||
|
|
||||||
## Support Reticulum
|
## Support Reticulum
|
||||||
You can help support the continued development of open, free and private communications systems by donating via one of the following channels:
|
For this to be possible, I need your help. Please support the continued development of open, free and private communications systems by donating via one of the following channels:
|
||||||
|
|
||||||
- Monero:
|
- Monero:
|
||||||
```
|
```
|
||||||
84FpY1QbxHcgdseePYNmhTHcrgMX4nFfBYtz2GKYToqHVVhJp8Eaw1Z1EedRnKD19b3B8NiLCGVxzKV17UMmmeEsCrPyA5w
|
84FpY1QbxHcgdseePYNmhTHcrgMX4nFfBYtz2GKYToqHVVhJp8Eaw1Z1EedRnKD19b3B8NiLCGVxzKV17UMmmeEsCrPyA5w
|
||||||
```
|
```
|
||||||
- Ethereum
|
|
||||||
```
|
|
||||||
0xFDabC71AC4c0C78C95aDDDe3B4FA19d6273c5E73
|
|
||||||
```
|
|
||||||
- Bitcoin
|
- Bitcoin
|
||||||
```
|
```
|
||||||
35G9uWVzrpJJibzUwpNUQGQNFzLirhrYAH
|
bc1pgqgu8h8xvj4jtafslq396v7ju7hkgymyrzyqft4llfslz5vp99psqfk3a6
|
||||||
```
|
```
|
||||||
|
- Ethereum
|
||||||
|
```
|
||||||
|
0x91C421DdfB8a30a49A71d63447ddb54cEBe3465E
|
||||||
|
```
|
||||||
|
- Liberapay: https://liberapay.com/Reticulum/
|
||||||
|
|
||||||
- Ko-Fi: https://ko-fi.com/markqvist
|
- Ko-Fi: https://ko-fi.com/markqvist
|
||||||
|
|
||||||
Are certain features in the development roadmap are important to you or your
|
|
||||||
organisation? Make them a reality quickly by sponsoring their implementation.
|
|
||||||
|
|
||||||
## Cryptographic Primitives
|
## Cryptographic Primitives
|
||||||
Reticulum uses a simple suite of efficient, strong and well-tested cryptographic
|
Reticulum uses a simple suite of efficient, strong and well-tested cryptographic primitives, with widely available implementations that can be used both on general-purpose CPUs and on microcontrollers.
|
||||||
primitives, with widely available implementations that can be used both on
|
|
||||||
general-purpose CPUs and on microcontrollers. The utilised primitives are:
|
One of the primary considerations for choosing this particular set of primitives is that they can be implemented *safely* with relatively few pitfalls, on practically all current computing platforms.
|
||||||
|
|
||||||
|
The primitives listed here **are authoritative**. Anything claiming to be Reticulum, but not using these exact primitives **is not** Reticulum, and possibly an intentionally compromised or weakened clone. The utilised primitives are:
|
||||||
|
|
||||||
- Reticulum Identity Keys are 512-bit Curve25519 keysets
|
- Reticulum Identity Keys are 512-bit Curve25519 keysets
|
||||||
- A 256-bit Ed25519 key for signatures
|
- A 256-bit Ed25519 key for signatures
|
||||||
|
|
@ -320,20 +277,14 @@ general-purpose CPUs and on microcontrollers. The utilised primitives are:
|
||||||
- HKDF for key derivation
|
- HKDF for key derivation
|
||||||
- Encrypted tokens are based on the [Fernet spec](https://github.com/fernet/spec/)
|
- Encrypted tokens are based on the [Fernet spec](https://github.com/fernet/spec/)
|
||||||
- Ephemeral keys derived from an ECDH key exchange on Curve25519
|
- Ephemeral keys derived from an ECDH key exchange on Curve25519
|
||||||
- AES-128 in CBC mode with PKCS7 padding
|
|
||||||
- HMAC using SHA256 for message authentication
|
- HMAC using SHA256 for message authentication
|
||||||
- IVs are generated through os.urandom()
|
- IVs must be generated through `os.urandom()` or better
|
||||||
|
- AES-256 in CBC mode with PKCS7 padding
|
||||||
- No Fernet version and timestamp metadata fields
|
- No Fernet version and timestamp metadata fields
|
||||||
- SHA-256
|
- SHA-256
|
||||||
- SHA-512
|
- SHA-512
|
||||||
|
|
||||||
In the default installation configuration, the `X25519`, `Ed25519` and
|
In the default installation configuration, the `X25519`, `Ed25519`, and `AES-256-CBC` primitives are provided by [OpenSSL](https://www.openssl.org/) (via the [PyCA/cryptography](https://github.com/pyca/cryptography) package). The hashing functions `SHA-256` and `SHA-512` are provided by the standard Python [hashlib](https://docs.python.org/3/library/hashlib.html). The `HKDF`, `HMAC`, `Token` primitives, and the `PKCS7` padding function are always provided by the following internal implementations:
|
||||||
`AES-128-CBC` primitives are provided by [OpenSSL](https://www.openssl.org/)
|
|
||||||
(via the [PyCA/cryptography](https://github.com/pyca/cryptography) package).
|
|
||||||
The hashing functions `SHA-256` and `SHA-512` are provided by the standard
|
|
||||||
Python [hashlib](https://docs.python.org/3/library/hashlib.html). The `HKDF`,
|
|
||||||
`HMAC`, `Token` primitives, and the `PKCS7` padding function are always
|
|
||||||
provided by the following internal implementations:
|
|
||||||
|
|
||||||
- [HKDF.py](RNS/Cryptography/HKDF.py)
|
- [HKDF.py](RNS/Cryptography/HKDF.py)
|
||||||
- [HMAC.py](RNS/Cryptography/HMAC.py)
|
- [HMAC.py](RNS/Cryptography/HMAC.py)
|
||||||
|
|
@ -341,43 +292,29 @@ provided by the following internal implementations:
|
||||||
- [PKCS7.py](RNS/Cryptography/PKCS7.py)
|
- [PKCS7.py](RNS/Cryptography/PKCS7.py)
|
||||||
|
|
||||||
|
|
||||||
Reticulum also includes a complete implementation of all necessary primitives
|
Reticulum also includes a complete implementation of all necessary primitives in pure Python. If OpenSSL and PyCA are not available on the system when Reticulum is started, Reticulum will instead use the internal pure-python primitives. A trivial consequence of this is performance, with the OpenSSL backend being *much* faster. The most important consequence however, is the potential loss of security by using primitives that has not seen the same amount of scrutiny, testing and review as those from OpenSSL.
|
||||||
in pure Python. If OpenSSL & PyCA are not available on the system when
|
|
||||||
Reticulum is started, Reticulum will instead use the internal pure-python
|
|
||||||
primitives. A trivial consequence of this is performance, with the OpenSSL
|
|
||||||
backend being *much* faster. The most important consequence however, is the
|
|
||||||
potential loss of security by using primitives that has not seen the same
|
|
||||||
amount of scrutiny, testing and review as those from OpenSSL.
|
|
||||||
|
|
||||||
If you want to use the internal pure-python primitives, it is **highly
|
Please note that by default, installing Reticulum will **require** OpenSSL and PyCA to also be automatically installed if not already available. It is only possible to use the pure-python primitives if this requirement is specifically overridden by the user, for example by installing the `rnspure` package instead of the normal `rns` package, or by running directly from local source-code.
|
||||||
advisable** that you have a good understanding of the risks that this pose, and
|
|
||||||
make an informed decision on whether those risks are acceptable to you.
|
|
||||||
|
|
||||||
Reticulum is relatively young software, and should be considered as such. While
|
If you want to use the internal pure-python primitives, it is **highly advisable** that you have a good understanding of the risks that this pose, and make an informed decision on whether those risks are acceptable to you.
|
||||||
it has been built with cryptography best-practices very foremost in mind, it
|
|
||||||
_has not_ been externally security audited, and there could very well be
|
Having been in development for ten, and wider deployment for around five years, Reticulum is relatively young software, and should be considered as such. While it has been carefully built with cryptography best-practices very foremost in mind, there could very well still be privacy or security breaking bugs.
|
||||||
privacy or security breaking bugs. If you want to help out, or help sponsor an
|
|
||||||
audit, please do get in touch.
|
|
||||||
|
|
||||||
## Acknowledgements & Credits
|
## Acknowledgements & Credits
|
||||||
Reticulum can only exist because of the mountain of Open Source work it was
|
Reticulum can only exist because of the mountain of Open Source work it was built on top of, the contributions of everyone involved, and everyone that has supported the project through the years. To everyone who has helped, thank you so much.
|
||||||
built on top of, the contributions of everyone involved, and everyone that has
|
|
||||||
supported the project through the years. To everyone who has helped, thank you
|
|
||||||
so much.
|
|
||||||
|
|
||||||
A number of other modules and projects are either part of, or used by
|
A number of other modules and projects are either part of, or used by Reticulum. Sincere thanks to the authors and contributors of the following projects:
|
||||||
Reticulum. Sincere thanks to the authors and contributors of the following
|
|
||||||
projects:
|
|
||||||
|
|
||||||
- [PyCA/cryptography](https://github.com/pyca/cryptography), *BSD License*
|
- [PyCA/cryptography](https://github.com/pyca/cryptography), *BSD License*
|
||||||
- [Pure-25519](https://github.com/warner/python-pure25519) by [Brian Warner](https://github.com/warner), *MIT License*
|
- [Pure-25519](https://github.com/warner/python-pure25519) by [Brian Warner](https://github.com/warner), *MIT License*
|
||||||
- [Pysha2](https://github.com/thomdixon/pysha2) by [Thom Dixon](https://github.com/thomdixon), *MIT License*
|
- [Pysha2](https://github.com/thomdixon/pysha2) by [Thom Dixon](https://github.com/thomdixon), *MIT License*
|
||||||
- [Python-AES](https://github.com/orgurar/python-aes) by [Or Gur Arie](https://github.com/orgurar), *MIT License*
|
- [Python AES-128](https://github.com/orgurar/python-aes) by [Or Gur Arie](https://github.com/orgurar), *MIT License*
|
||||||
|
- [Python AES-256](https://github.com/boppreh/aes) by [BoppreH](https://github.com/boppreh), *MIT License*
|
||||||
- [Curve25519.py](https://gist.github.com/nickovs/cc3c22d15f239a2640c185035c06f8a3#file-curve25519-py) by [Nicko van Someren](https://gist.github.com/nickovs), *Public Domain*
|
- [Curve25519.py](https://gist.github.com/nickovs/cc3c22d15f239a2640c185035c06f8a3#file-curve25519-py) by [Nicko van Someren](https://gist.github.com/nickovs), *Public Domain*
|
||||||
- [I2Plib](https://github.com/l-n-s/i2plib) by [Viktor Villainov](https://github.com/l-n-s)
|
- [I2Plib](https://github.com/l-n-s/i2plib) by [Viktor Villainov](https://github.com/l-n-s)
|
||||||
- [PySerial](https://github.com/pyserial/pyserial) by Chris Liechti, *BSD License*
|
- [PySerial](https://github.com/pyserial/pyserial) by Chris Liechti, *BSD License*
|
||||||
- [Configobj](https://github.com/DiffSK/configobj) by Michael Foord, Nicola Larosa, Rob Dennis & Eli Courtwright, *BSD License*
|
- [Configobj](https://github.com/DiffSK/configobj) by Michael Foord, Nicola Larosa, Rob Dennis & Eli Courtwright, *BSD License*
|
||||||
- [Six](https://github.com/benjaminp/six) by [Benjamin Peterson](https://github.com/benjaminp), *MIT License*
|
- [ifaddr](https://github.com/pydron/ifaddr) by Stefan C. Mueller, *MIT License*
|
||||||
- [ifaddr](https://github.com/pydron/ifaddr) by [Pydron](https://github.com/pydron), *MIT License*
|
|
||||||
- [Umsgpack.py](https://github.com/vsergeev/u-msgpack-python) by [Ivan A. Sergeev](https://github.com/vsergeev)
|
- [Umsgpack.py](https://github.com/vsergeev/u-msgpack-python) by [Ivan A. Sergeev](https://github.com/vsergeev)
|
||||||
|
- [rnsh](https://github.com/acehoss/rnsh) by [Aaron Heise](https://github.com/acehoss)
|
||||||
- [Python](https://www.python.org)
|
- [Python](https://www.python.org)
|
||||||
|
|
|
||||||
291
README.mu
Normal file
291
README.mu
Normal file
|
|
@ -0,0 +1,291 @@
|
||||||
|
>> Reticulum Network Stack
|
||||||
|
|
||||||
|
To understand the foundational philosophy and goals of this system, read the `_`!`[Zen of Reticulum`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=Zen+of+Reticulum.md]`!`_.
|
||||||
|
|
||||||
|
Reticulum is the cryptography-based networking stack for building local and wide-area networks with readily available hardware. It can operate even with very high latency and extremely low bandwidth. Reticulum allows you to build wide-area networks with off-the-shelf tools, and offers end-to-end encryption and connectivity, initiator anonymity, autoconfiguring cryptographically backed multi-hop transport, efficient addressing, unforgeable delivery acknowledgements and more.
|
||||||
|
|
||||||
|
`(RNS Logo`w=18`a=c`:/media/reticulum/reticulum/HEAD/docs/source/graphics/rns_logo_256.webp)
|
||||||
|
|
||||||
|
The vision of Reticulum is to allow anyone to be their own network operator, and to make it cheap and easy to cover vast areas with a myriad of independent, inter-connectable and autonomous networks. Reticulum `!is not`! `*one`* network. It is `!a tool`! for building `*thousands of networks`*. Networks without kill-switches, surveillance, censorship and control. Networks that can freely interoperate, associate and disassociate with each other, and require no central oversight. Networks for human beings. `*Networks for the people`*.
|
||||||
|
|
||||||
|
Reticulum is a complete networking stack, and does not rely on IP or higher layers, but it is possible to use IP as the underlying carrier for Reticulum. It is therefore trivial to tunnel Reticulum over the Internet or private IP networks.
|
||||||
|
|
||||||
|
Having no dependencies on traditional networking stacks frees up overhead that has been used to implement a networking stack built directly on cryptographic principles, allowing resilience and stable functionality, even in open and trustless networks.
|
||||||
|
|
||||||
|
No kernel modules or drivers are required. Reticulum runs completely in userland, and can run on practically any system that runs Python.
|
||||||
|
|
||||||
|
>> Read The Manual
|
||||||
|
|
||||||
|
The full documentation for Reticulum is available on `_`!`[this node`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/index.md]`!`_.
|
||||||
|
|
||||||
|
You can also download the `_`!`[Reticulum manual as a PDF`:/file/artifact`g=reticulum|r=reticulum|t=latest|a=Reticulum+Manual.pdf]`!`_ or `_`!`[as an e-book in EPUB format`:/file/artifact`g=reticulum|r=reticulum|t=latest|a=Reticulum+Manual.epub]`!`_.
|
||||||
|
|
||||||
|
>> Notable Features
|
||||||
|
|
||||||
|
• Coordination-less globally unique addressing and identification
|
||||||
|
• Fully self-configuring multi-hop routing over heterogeneous carriers
|
||||||
|
• Flexible scalability over heterogeneous topologies
|
||||||
|
• Reticulum can carry data over any mixture of physical mediums and topologies
|
||||||
|
• Low-bandwidth networks can co-exist and interoperate with large, high-bandwidth networks
|
||||||
|
• Initiator anonymity, communicate without revealing your identity
|
||||||
|
• Reticulum does not include source addresses on any packets
|
||||||
|
• Asymmetric X25519 encryption and Ed25519 signatures as a basis for all communication
|
||||||
|
• The foundational Reticulum Identity Keys are 512-bit Elliptic Curve keysets
|
||||||
|
• Forward Secrecy is available for all communication types, both for single packets and over links
|
||||||
|
• Reticulum uses the following format for encrypted tokens:
|
||||||
|
• Ephemeral per-packet and link keys and derived from an ECDH key exchange on Curve25519
|
||||||
|
• AES-256 in CBC mode with PKCS7 padding
|
||||||
|
• HMAC using SHA256 for authentication
|
||||||
|
• IVs are generated through os.urandom()
|
||||||
|
• Unforgeable packet delivery confirmations
|
||||||
|
• Flexible and extensible interface system
|
||||||
|
• Reticulum includes a large variety of built-in interface types
|
||||||
|
• Ability to load and utilise custom user- or community-supplied interface types
|
||||||
|
• Easily create your own custom interfaces for communicating over anything
|
||||||
|
• Authentication and virtual network segmentation on all supported interface types
|
||||||
|
• An intuitive and easy-to-use API
|
||||||
|
• Simpler and easier to use than sockets APIs, but more powerful
|
||||||
|
• Makes building distributed and decentralised applications much simpler
|
||||||
|
• Reliable and efficient transfer of arbitrary amounts of data
|
||||||
|
• Reticulum can handle a few bytes of data or files of many gigabytes
|
||||||
|
• Sequencing, compression, transfer coordination and checksumming are automatic
|
||||||
|
• The API is very easy to use, and provides transfer progress
|
||||||
|
• Lightweight, flexible and expandable Request/Response mechanism
|
||||||
|
• Efficient link establishment
|
||||||
|
• Total cost of setting up an encrypted and verified link is only 3 packets, totalling 297 bytes
|
||||||
|
• Low cost of keeping links open at only 0.44 bits per second
|
||||||
|
• Reliable sequential delivery with Channel and Buffer mechanisms
|
||||||
|
|
||||||
|
>> Reference Implementation
|
||||||
|
|
||||||
|
The Python code in this repository is the Reference Implementation of Reticulum. The Reticulum Protocol is defined entirely and authoritatively by this reference implementation, and its associated manual. It is maintained by Mark Qvist, identified by the Reticulum Identity `B333<bc7291552be7a58f361522990465165c>`b.
|
||||||
|
|
||||||
|
No second-order protocol specification derivatives currently do, or will ever exist. You can't get a better specification than one that is executable, functional, testable, deployed and operating 24/7 across the planet.
|
||||||
|
|
||||||
|
There is not, and never will be a separate, derived "formal spec", "RFC" or anything similar for Reticulum:
|
||||||
|
|
||||||
|
`!The reference implementation `*is`* the final, authoritative specification.`!
|
||||||
|
|
||||||
|
Compatibility with the Reticulum Protocol is defined as having full interoperability, and sufficient functional parity with this reference implementation. Any specific protocol implementation that achieves this is Reticulum. Any that does not is not Reticulum.
|
||||||
|
|
||||||
|
The reference implementation is licensed under the Reticulum License.
|
||||||
|
|
||||||
|
The Reticulum Protocol was dedicated to the Public Domain in 2016.
|
||||||
|
|
||||||
|
>> Examples of Reticulum Applications
|
||||||
|
|
||||||
|
If you want to quickly get an idea of what Reticulum can do, take a look at the `_`!`[Programs Using Reticulum`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/software.md]`!`_ section of the manual, or the following resources:
|
||||||
|
|
||||||
|
• `_`!`[LXMF`a8d24177d946de4f1f0a0fe1af9a1338:/page/repo.mu`g=reticulum|r=lxmf]`!`_ is a distributed, delay and disruption tolerant message transfer protocol built on Reticulum
|
||||||
|
|
||||||
|
• The `_`!`[LXST`a8d24177d946de4f1f0a0fe1af9a1338:/page/repo.mu`g=reticulum|r=lxst]`!`_ protocol and framework provides real-time audio and signals transport over Reticulum. It
|
||||||
|
includes primitives and utilities for building voice-based applications and hardware devices,
|
||||||
|
such as the `B333rnphone`b program, that can be used to build hardware telephones.
|
||||||
|
|
||||||
|
• For an off-grid, encrypted and resilient mesh communications platform, see `_`!`[Nomad Network`a8d24177d946de4f1f0a0fe1af9a1338:/page/repo.mu`g=reticulum|r=nomadnet]`!`_.
|
||||||
|
|
||||||
|
• The Android, Linux, macOS and Windows app `_`!`[Sideband`a8d24177d946de4f1f0a0fe1af9a1338:/page/repo.mu`g=reticulum|r=sideband]`!`_ has a graphical interface and many advanced
|
||||||
|
features, such as file transfers, image and voice messages, real-time voice calls, a distributed
|
||||||
|
telemetry system, mapping capabilities and full plugin extensibility.
|
||||||
|
|
||||||
|
• `_`!`[MeshChatX`c10d80b1a42fa958c37a6cc30dc04f53]`!`_ (`_`!`[source`5399f5a0212477618821e91e88ce053b:/page/repo.mu`g=quad4|r=MeshChatX]`_`!) is a full-featured LXMF client with many built-in tools and functionalities,
|
||||||
|
that also supports image and voice messages, file transfers and voice calls. It also includes a
|
||||||
|
built-in page browser for browsing Nomad Network nodes.
|
||||||
|
|
||||||
|
• You can use the included `_`!`[rnsh`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/using.md|anchor=the-rnsh-utility]`!`_ program to establish remote shell sessions over Reticulum.
|
||||||
|
|
||||||
|
>> Where can Reticulum be used?
|
||||||
|
|
||||||
|
Over practically any medium that can support at least a half-duplex channel with greater throughput than 5 bits per second, and an MTU of 500 bytes. Data radios, modems, LoRa radios, serial lines, AX.25 TNCs, amateur radio digital modes, WiFi and Ethernet devices, free-space optical links, and similar systems are all examples of the types of physical devices Reticulum can use.
|
||||||
|
|
||||||
|
An open-source LoRa-based interface called `_`!`[RNode`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/hardware.md|anchor=rnode]`!`_ has been designed specifically for use with Reticulum. It is possible to build yourself, or it can be purchased as a complete transceiver that just needs a USB connection to the host.
|
||||||
|
|
||||||
|
Reticulum can also be encapsulated over existing IP networks, so there's nothing stopping you from using it over wired Ethernet, your local WiFi network or the Internet, where it'll work just as well. In fact, one of the strengths of Reticulum is how easily it allows you to connect different mediums into a self-configuring, resilient and encrypted mesh, using any available mixture of available infrastructure.
|
||||||
|
|
||||||
|
As an example, it's possible to set up a Raspberry Pi connected to both a LoRa radio, a packet radio TNC and a WiFi network. Once the interfaces are configured, Reticulum will take care of the rest, and any device on the WiFi network can communicate with nodes on the LoRa and packet radio sides of the network, and vice versa.
|
||||||
|
|
||||||
|
>> How do I get started?
|
||||||
|
|
||||||
|
The best way to get started with the Reticulum Network Stack depends on what you want to do. For full details and examples, have a look at the `_`!`[Getting Started Fast`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/gettingstartedfast.md]`!`_ section of the `_`!`[Reticulum Manual`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/index.md]`!`_.
|
||||||
|
|
||||||
|
To simply install Reticulum and related utilities on your system, the easiest way is via `B333pip`b. You can then start any program that uses Reticulum, or start Reticulum as a system service with `_`!`[the rnsd utility`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/using.md|anchor=the-rnsd-utility]`!`_.
|
||||||
|
|
||||||
|
`B333
|
||||||
|
`=
|
||||||
|
pip install rns
|
||||||
|
`=
|
||||||
|
`b
|
||||||
|
|
||||||
|
If you are using an operating system that blocks normal user package installation via `B333pip`b, you can return `B333pip`b to normal behaviour by editing the `B333~/.config/pip/pip.conf`b file, and adding the following directive in the `B333[global]`b section:
|
||||||
|
|
||||||
|
`B333
|
||||||
|
`=
|
||||||
|
[global]
|
||||||
|
break-system-packages = true
|
||||||
|
`=
|
||||||
|
`b
|
||||||
|
|
||||||
|
Alternatively, you can use the `B333pipx`b tool to install Reticulum in an isolated environment:
|
||||||
|
|
||||||
|
`B333
|
||||||
|
`=
|
||||||
|
pipx install rns
|
||||||
|
`=
|
||||||
|
`b
|
||||||
|
|
||||||
|
When first started, Reticulum will create a default configuration file, providing basic connectivity to other Reticulum peers that might be locally reachable. The default config file contains a few examples, and references for creating a more complex configuration.
|
||||||
|
|
||||||
|
If you have an old version of `B333pip`b on your system, you may need to upgrade it first with `B333pip install pip --upgrade`b. If you no not already have `B333pip`b installed, you can install it using the package manager of your system with `B333sudo apt install python3-pip`b or similar.
|
||||||
|
|
||||||
|
For more detailed examples on how to expand communication over many mediums such as packet radio or LoRa, serial ports, or over fast IP links and the Internet using the UDP and TCP interfaces, take a look at the `_`!`[Supported Interfaces`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/interfaces.md]`!`_ section of the `_`!`[Reticulum Manual`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/index.md]`!`_.
|
||||||
|
|
||||||
|
>> Included Utilities
|
||||||
|
|
||||||
|
Reticulum includes a range of useful utilities for managing your networks, viewing status and information, and other tasks. You can read more about these programs in the `_`!`[Included Utility Programs`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/using.md|anchor=included-utility-programs]`!`_ section of the `_`!`[Reticulum Manual`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/index.md]`!`_.
|
||||||
|
|
||||||
|
• The system daemon `B333rnsd`b for running Reticulum as an always-available service
|
||||||
|
• An interface status utility called `B333rnstatus`b, that displays information about interfaces
|
||||||
|
• The path lookup and management tool `B333rnpath`b letting you view and modify path tables
|
||||||
|
• A diagnostics tool called `B333rnprobe`b for checking connectivity to destinations
|
||||||
|
• A simple file transfer program called `B333rncp`b making it easy to transfer files between systems
|
||||||
|
• The identity management and encryption utility `B333rnid`b let's you manage Identities and encrypt/decrypt files
|
||||||
|
• The `B333rnsh`b program allows you to establish fully interactive shell session with remote systems
|
||||||
|
• The remote command execution program `B333rnx`b let's you run simple commands and programs and retrieve output from remote systems
|
||||||
|
• The `B333rngit`b program provides a full multi-repository Git node for serving repositories over Reticulum
|
||||||
|
• The included `B333git-remote-rns`b helper allows you to interact with Git repositories over Reticulum
|
||||||
|
|
||||||
|
>> Supported interface types and devices
|
||||||
|
|
||||||
|
Reticulum implements a range of generalised interface types that covers most of the communications hardware that Reticulum can run over. If your hardware is not supported, it's `_`!`[simple to implement a custom interface module`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/interfaces.md|anchor=custom-interfaces]`!`_.
|
||||||
|
|
||||||
|
Currently, the following built-in interfaces are supported:
|
||||||
|
|
||||||
|
• Any Ethernet device
|
||||||
|
• LoRa using `_`!`[RNode`a8d24177d946de4f1f0a0fe1af9a1338:/page/repo.mu`g=reticulum|r=rnode_firmware]`!`_
|
||||||
|
• Packet Radio TNCs (with or without AX.25)
|
||||||
|
• KISS-compatible hardware and software modems
|
||||||
|
• Any device with a serial port
|
||||||
|
• TCP over IP networks
|
||||||
|
• UDP over IP networks
|
||||||
|
• External programs via stdio or pipes
|
||||||
|
• Custom hardware via stdio or pipes
|
||||||
|
|
||||||
|
>> Performance
|
||||||
|
|
||||||
|
`*All performance numbers here assume `B333rnsd`b running in fully interpreted Python mode, limited to a single CPU core.`*
|
||||||
|
|
||||||
|
Reticulum targets a `*very`* wide usable performance envelope, but prioritises functionality and performance on low-bandwidth mediums. The goal is to provide a dynamic performance envelope from 100 bits per second, to 10 gigabits per second on normal hardware.
|
||||||
|
|
||||||
|
Currently, the on-network, usable performance envelope is approximately 150 bits per second to 1 gigabit per second on modest hardware. Real-world performance will vary widely with the speed of your system, availability of cryptographic hardware acceleration, and other factors.
|
||||||
|
|
||||||
|
Raw routing throughput for a transport node hosted on a Raspberry Pi 5 is approximately 4 gigabits per second. Systems with modern crypto hardware acceleration can achieve transport throughput of 15 gigabits per second and higher.
|
||||||
|
|
||||||
|
While the reference implementation is perfectly capable of reaching 100+ Gbps, by utilizing multiple CPU cores and vectorized transport, performance beyond the current level is not higly prioritised, since RNS is already able to saturate available physical medium bandwidth in almost all real-world situations.
|
||||||
|
|
||||||
|
The primary objectives for RNS are `*reliability`*, `*correctness`*, `*security`*, `*proper traffic management`* and `*actually practical performance`*, not hollow on-paper "benchmarks" with little to no bearing on real-world usability.
|
||||||
|
|
||||||
|
All alternative-implementation maintainers are `*highly`* encouraged to adopt a similar approach, and especially to focus on getting their implementations working properly before publishing empirically dubious or factually wrong performance claims.
|
||||||
|
|
||||||
|
>> Current Status
|
||||||
|
|
||||||
|
All core protocol features are implemented and functioning, but additions will probably occur as real-world use is explored and understood. The API and wire-format can be considered stable.
|
||||||
|
|
||||||
|
>> Dependencies
|
||||||
|
|
||||||
|
The installation of the default `B333rns`b package requires only two external dependencies, listed below. Almost all systems and distributions have readily available packages for these dependencies, and when the `B333rns`b package is installed with `B333pip`b, they will be downloaded and installed as well.
|
||||||
|
|
||||||
|
• PyCA/cryptography
|
||||||
|
• pyserial
|
||||||
|
|
||||||
|
On more unusual systems, and in some rare cases, it might not be possible to install or even compile one or more of the above modules. In such situations, you can use the `B333rnspure`b package instead, which require no external dependencies for installation. Please note that the contents of the `B333rns`b and `B333rnspure`b packages are `*identical`*. The only difference is that the `B333rnspure`b package lists no dependencies required for installation.
|
||||||
|
|
||||||
|
No matter how Reticulum is installed and started, it will load external dependencies only if they are `*needed`* and `*available`*. If for example you want to use Reticulum on a system that cannot support `B333pyserial`b, it is perfectly possible to do so using the `B333rnspure`b package, but Reticulum will not be able to use serial-based interfaces. All other available modules will still be loaded when needed.
|
||||||
|
|
||||||
|
`!Please Note!`! If you use the `B333rnspure`b package to run Reticulum on systems that do not support PyCA/cryptography, it is important that you read and understand the `!Cryptographic Primitives`! section of this document.
|
||||||
|
|
||||||
|
>> Bootstrapping Connectivity
|
||||||
|
|
||||||
|
Reticulum is not a service you subscribe to, nor is it a single global network you "join". Reticulum provides functionality for discovering available public interfaces over the network itself, and the broader community has provided various directories of publicly available entrypoints to bootstrap connectivity.
|
||||||
|
|
||||||
|
To learn how to establish initial connectivity over Reticulum, read the `_`!`[Bootstrapping Connectivity`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/gettingstartedfast.md|anchor=bootstrapping-connectivity]`!`_ section of the manual.
|
||||||
|
|
||||||
|
If you already have a general idea of how this works, you can use community-run sites such as `_`!`[rns.recipes`9ce92808be498e9e05590ff27cbfdfe4]`!`_ and `_`!`[rmap.world`a4a5e861626ce97c9aa544d9ecdf6d22]`!`_ to find interface definitions for initial connectivity to the global distributed Reticulum backbone.
|
||||||
|
|
||||||
|
>> Public Testnet
|
||||||
|
|
||||||
|
`!`*Important!`! Historically, a developer-targeted testnet was made available by the Reticulum project itself. As the amount of global Reticulum nodes and entrypoints have grown to a substantial quantity, this public testnet, including the Amsterdam Testnet entrypoint, has now been decommissioned. If you still have instances that relied on this entrypoint for connectivity, transition to using the distributed backbone instead. Reticulum now includes a full on-network interface discovery and connectivity bootstrapping system. Read the `_`[Bootstrapping Connectivity`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=docs/markdown/gettingstartedfast.md|anchor=bootstrapping-connectivity]`_ section of the manual for pointers.`*
|
||||||
|
|
||||||
|
>> Support Reticulum
|
||||||
|
|
||||||
|
For this to be possible, I need your help. Please support the continued development of open, free and private communications systems by donating via one of the following channels:
|
||||||
|
|
||||||
|
• `!Monero`!
|
||||||
|
84FpY1QbxHcgdseePYNmhTHcrgMX4nFfBYtz2GKYToqHVVhJp8Eaw1Z1EedRnKD19b3B8NiLCGVxzKV17UMmmeEsCrPyA5w
|
||||||
|
|
||||||
|
• `!Bitcoin`!
|
||||||
|
bc1pgqgu8h8xvj4jtafslq396v7ju7hkgymyrzyqft4llfslz5vp99psqfk3a6
|
||||||
|
|
||||||
|
• `!Ethereum`!
|
||||||
|
0x91C421DdfB8a30a49A71d63447ddb54cEBe3465E
|
||||||
|
|
||||||
|
• `!Liberapay`!
|
||||||
|
`[https://liberapay.com/Reticulum/]
|
||||||
|
|
||||||
|
• `!Ko-Fi`!
|
||||||
|
`[https://ko-fi.com/markqvist]
|
||||||
|
|
||||||
|
>> Cryptographic Primitives
|
||||||
|
|
||||||
|
Reticulum uses a simple suite of efficient, strong and well-tested cryptographic primitives, with widely available implementations that can be used both on general-purpose CPUs and on microcontrollers.
|
||||||
|
|
||||||
|
One of the primary considerations for choosing this particular set of primitives is that they can be implemented `*safely`* with relatively few pitfalls, on practically all current computing platforms.
|
||||||
|
|
||||||
|
The primitives listed here `!are authoritative`!. Anything `*claiming`* to be Reticulum, but not using these exact primitives `FTA35050`!is not`!`f Reticulum, and possibly an intentionally compromised or weakened clone. The utilised primitives are:
|
||||||
|
|
||||||
|
• Reticulum Identity Keys are 512-bit Curve25519 keysets
|
||||||
|
• A 256-bit Ed25519 key for signatures
|
||||||
|
• A 256-bit X22519 key for ECDH key exchanges
|
||||||
|
• HKDF for key derivation
|
||||||
|
• Encrypted tokens are based on the `_`!`[Fernet spec`https://github.com/fernet/spec/]`!`_
|
||||||
|
• Ephemeral keys derived from an ECDH key exchange on Curve25519
|
||||||
|
• HMAC using SHA256 for message authentication
|
||||||
|
• IVs must be generated through `B333os.urandom()`b or better
|
||||||
|
• AES-256 in CBC mode with PKCS7 padding
|
||||||
|
• No Fernet version and timestamp metadata fields
|
||||||
|
• SHA-256
|
||||||
|
• SHA-512
|
||||||
|
|
||||||
|
In the default installation configuration, the `B333X25519`b, `B333Ed25519`b, and `B333AES-256-CBC`b primitives are provided by `_`!`[OpenSSL`https://www.openssl.org/]`!`_ (via the `_`!`[PyCA/cryptography`https://github.com/pyca/cryptography]`!`_ package). The hashing functions `B333SHA-256`b and `B333SHA-512`b are provided by the standard Python `_`!`[hashlib`https://docs.python.org/3/library/hashlib.html]`!`_. The `B333HKDF`b, `B333HMAC`b, `B333Token`b primitives, and the `B333PKCS7`b padding function are always provided by the following internal implementations:
|
||||||
|
|
||||||
|
• `_`!`[HKDF.py`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=RNS/Cryptography/HKDF.py]`!`_
|
||||||
|
• `_`!`[HMAC.py`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=RNS/Cryptography/HMAC.py]`!`_
|
||||||
|
• `_`!`[Token.py`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=RNS/Cryptography/Token.py]`!`_
|
||||||
|
• `_`!`[PKCS7.py`:/page/blob.mu`g=reticulum|r=reticulum|ref=HEAD|path=RNS/Cryptography/PKCS7.py]`!`_
|
||||||
|
|
||||||
|
Reticulum also includes a complete implementation of all necessary primitives in pure Python. If OpenSSL and PyCA are not available on the system when Reticulum is started, Reticulum will instead use the internal pure-python primitives. A trivial consequence of this is performance, with the OpenSSL backend being `*much`* faster. The most important consequence however, is the potential loss of security by using primitives that has not seen the same amount of scrutiny, testing and review as those from OpenSSL.
|
||||||
|
|
||||||
|
Please note that by default, installing Reticulum will `!require`! OpenSSL and PyCA to also be automatically installed if not already available. It is only possible to use the pure-python primitives if this requirement is specifically overridden by the user, for example by installing the `B333rnspure`b package instead of the normal `B333rns`b package, or by running directly from local source-code.
|
||||||
|
|
||||||
|
If you want to use the internal pure-python primitives, it is `!highly advisable`! that you have a good understanding of the risks that this pose, and make an informed decision on whether those risks are acceptable to you.
|
||||||
|
|
||||||
|
Having been in development for ten, and wider deployment for around five years, Reticulum is relatively young software, and should be considered as such. While it has been carefully built with cryptography best-practices very foremost in mind, there could very well still be privacy or security breaking bugs.
|
||||||
|
|
||||||
|
>> Acknowledgements & Credits
|
||||||
|
|
||||||
|
Reticulum can only exist because of the mountain of Open Source work it was built on top of, the contributions of everyone involved, and everyone that has supported the project through the years. To everyone who has helped, thank you so much.
|
||||||
|
|
||||||
|
A number of other modules and projects are either part of, or used by Reticulum. Sincere thanks to the authors and contributors of the following projects:
|
||||||
|
|
||||||
|
• `_`!`[PyCA/cryptography`https://github.com/pyca/cryptography]`!`_, `*BSD License`*
|
||||||
|
• `_`!`[Pure-25519`https://github.com/warner/python-pure25519]`!`_, by `_`!`[Brian Warner`https://github.com/warner]`!`_, `*MIT License`*
|
||||||
|
• `_`!`[Pysha2`https://github.com/thomdixon/pysha2]`!`_ by `_`!`[Thom Dixon`https://github.com/thomdixon]`!`_, `*MIT License`*
|
||||||
|
• `_`!`[Python AES-128`https://github.com/orgurar/python-aes]`!`_ by `_`!`[Or Gur Arie`https://github.com/orgurar]`!`_, `*MIT License`*
|
||||||
|
• `_`!`[Python AES-256`https://github.com/boppreh/aes]`!`_ by `_`!`[BoppreH`https://github.com/boppreh]`!`_, `*MIT License`*
|
||||||
|
• `_`!`[Curve25519.py`https://gist.github.com/nickovs/cc3c22d15f239a2640c185035c06f8a3]`!`_ by `_`!`[Nicko van Someren`https://gist.github.com/nickovs]`!`_, `*Public Domain`*
|
||||||
|
• `_`!`[I2Plib`https://github.com/l-n-s/i2plib]`!`_ by `_`!`[Viktor Villainov`https://github.com/l-n-s]`!`_
|
||||||
|
• `_`!`[PySerial`https://github.com/pyserial/pyserial]`!`_ by Chris Liechti, `*BSD License`*
|
||||||
|
• `_`!`[Configobj`https://github.com/DiffSK/configobj]`!`_ by Michael Foord, Nicola Larosa, Rob Dennis & Eli Courtwright, `*BSD License`*
|
||||||
|
• `_`!`[ifaddr`https://github.com/pydron/ifaddr]`!`_ by Stefan C. Mueller, `*MIT License`*
|
||||||
|
• `_`!`[Umsgpack.py`https://github.com/vsergeev/u-msgpack-python]`!`_ by `_`!`[Ivan A. Sergeev`https://github.com/vsergeev]`!`_
|
||||||
|
• `_`!`[rnsh`https://github.com/acehoss/rnsh]`!`_ by `_`!`[Aaron Heise`https://github.com/acehoss]`!`_
|
||||||
|
• `_`!`[Python`https://www.python.org]`!`_
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io and contributors.
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -45,7 +53,8 @@ class StreamDataMessage(MessageBase):
|
||||||
The stream id is limited to 2 bytes - 2 bit
|
The stream id is limited to 2 bytes - 2 bit
|
||||||
"""
|
"""
|
||||||
|
|
||||||
OVERHEAD = 2 + 6 # 2 for stream data message header, 6 for channel envelope
|
HEADER_LEN = 2 # 2 for stream data message header
|
||||||
|
OVERHEAD = HEADER_LEN + 6 # 6 for channel envelope
|
||||||
MAX_DATA_LEN = RNS.Link.MDU - OVERHEAD
|
MAX_DATA_LEN = RNS.Link.MDU - OVERHEAD
|
||||||
"""
|
"""
|
||||||
When the Buffer package is imported, this value is
|
When the Buffer package is imported, this value is
|
||||||
|
|
@ -84,7 +93,9 @@ class StreamDataMessage(MessageBase):
|
||||||
self.data = raw[2:]
|
self.data = raw[2:]
|
||||||
|
|
||||||
if self.compressed:
|
if self.compressed:
|
||||||
self.data = bz2.decompress(self.data)
|
decompressor = bz2.BZ2Decompressor()
|
||||||
|
self.data = decompressor.decompress(self.data, max_length=RawChannelWriter.MAX_CHUNK_LEN)
|
||||||
|
if not decompressor.eof: raise IOError("Decompressed buffer chunk exceeds maximum legitimate size")
|
||||||
|
|
||||||
|
|
||||||
class RawChannelReader(RawIOBase, AbstractContextManager):
|
class RawChannelReader(RawIOBase, AbstractContextManager):
|
||||||
|
|
@ -216,7 +227,7 @@ class RawChannelWriter(RawIOBase, AbstractContextManager):
|
||||||
self._stream_id = stream_id
|
self._stream_id = stream_id
|
||||||
self._channel = channel
|
self._channel = channel
|
||||||
self._eof = False
|
self._eof = False
|
||||||
self._mdu = channel.mdu - StreamDataMessage.OVERHEAD
|
self._mdu = channel.mdu - StreamDataMessage.HEADER_LEN
|
||||||
|
|
||||||
def write(self, __b: bytes) -> int | None:
|
def write(self, __b: bytes) -> int | None:
|
||||||
try:
|
try:
|
||||||
|
|
@ -232,7 +243,7 @@ class RawChannelWriter(RawIOBase, AbstractContextManager):
|
||||||
chunk_segment_length = int(chunk_len/comp_try)
|
chunk_segment_length = int(chunk_len/comp_try)
|
||||||
compressed_chunk = bz2.compress(__b[:chunk_segment_length])
|
compressed_chunk = bz2.compress(__b[:chunk_segment_length])
|
||||||
compressed_length = len(compressed_chunk)
|
compressed_length = len(compressed_chunk)
|
||||||
if compressed_length < StreamDataMessage.MAX_DATA_LEN and compressed_length < chunk_segment_length:
|
if compressed_length < self._mdu and compressed_length < chunk_segment_length:
|
||||||
comp_success = True
|
comp_success = True
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
|
@ -242,7 +253,7 @@ class RawChannelWriter(RawIOBase, AbstractContextManager):
|
||||||
chunk = compressed_chunk
|
chunk = compressed_chunk
|
||||||
processed_length = chunk_segment_length
|
processed_length = chunk_segment_length
|
||||||
else:
|
else:
|
||||||
chunk = bytes(__b[:StreamDataMessage.MAX_DATA_LEN])
|
chunk = bytes(__b[:self._mdu])
|
||||||
processed_length = len(chunk)
|
processed_length = len(chunk)
|
||||||
|
|
||||||
message = StreamDataMessage(self._stream_id, chunk, self._eof, comp_success)
|
message = StreamDataMessage(self._stream_id, chunk, self._eof, comp_success)
|
||||||
|
|
|
||||||
372
RNS/Channel.py
372
RNS/Channel.py
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io and contributors.
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -37,63 +45,6 @@ TPacket = TypeVar("TPacket")
|
||||||
class SystemMessageTypes(enum.IntEnum):
|
class SystemMessageTypes(enum.IntEnum):
|
||||||
SMT_STREAM_DATA = 0xff00
|
SMT_STREAM_DATA = 0xff00
|
||||||
|
|
||||||
class ChannelOutletBase(ABC, Generic[TPacket]):
|
|
||||||
"""
|
|
||||||
An abstract transport layer interface used by Channel.
|
|
||||||
|
|
||||||
DEPRECATED: This was created for testing; eventually
|
|
||||||
Channel will use Link or a LinkBase interface
|
|
||||||
directly.
|
|
||||||
"""
|
|
||||||
@abstractmethod
|
|
||||||
def send(self, raw: bytes) -> TPacket:
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def resend(self, packet: TPacket) -> TPacket:
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def mdu(self):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def rtt(self):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def is_usable(self):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_packet_state(self, packet: TPacket) -> MessageState:
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def timed_out(self):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def __str__(self):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def set_packet_timeout_callback(self, packet: TPacket, callback: Callable[[TPacket], None] | None,
|
|
||||||
timeout: float | None = None):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def set_packet_delivered_callback(self, packet: TPacket, callback: Callable[[TPacket], None] | None):
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_packet_id(self, packet: TPacket) -> any:
|
|
||||||
raise NotImplemented()
|
|
||||||
|
|
||||||
|
|
||||||
class CEType(enum.IntEnum):
|
class CEType(enum.IntEnum):
|
||||||
"""
|
"""
|
||||||
ChannelException type codes
|
ChannelException type codes
|
||||||
|
|
@ -105,7 +56,6 @@ class CEType(enum.IntEnum):
|
||||||
ME_ALREADY_SENT = 4
|
ME_ALREADY_SENT = 4
|
||||||
ME_TOO_BIG = 5
|
ME_TOO_BIG = 5
|
||||||
|
|
||||||
|
|
||||||
class ChannelException(Exception):
|
class ChannelException(Exception):
|
||||||
"""
|
"""
|
||||||
An exception thrown by Channel, with a type code.
|
An exception thrown by Channel, with a type code.
|
||||||
|
|
@ -114,7 +64,6 @@ class ChannelException(Exception):
|
||||||
super().__init__(args)
|
super().__init__(args)
|
||||||
self.type = ce_type
|
self.type = ce_type
|
||||||
|
|
||||||
|
|
||||||
class MessageState(enum.IntEnum):
|
class MessageState(enum.IntEnum):
|
||||||
"""
|
"""
|
||||||
Set of possible states for a Message
|
Set of possible states for a Message
|
||||||
|
|
@ -124,7 +73,6 @@ class MessageState(enum.IntEnum):
|
||||||
MSGSTATE_DELIVERED = 2
|
MSGSTATE_DELIVERED = 2
|
||||||
MSGSTATE_FAILED = 3
|
MSGSTATE_FAILED = 3
|
||||||
|
|
||||||
|
|
||||||
class MessageBase(abc.ABC):
|
class MessageBase(abc.ABC):
|
||||||
"""
|
"""
|
||||||
Base type for any messages sent or received on a Channel.
|
Base type for any messages sent or received on a Channel.
|
||||||
|
|
@ -136,7 +84,7 @@ class MessageBase(abc.ABC):
|
||||||
MSGTYPE = None
|
MSGTYPE = None
|
||||||
"""
|
"""
|
||||||
Defines a unique identifier for a message class.
|
Defines a unique identifier for a message class.
|
||||||
|
|
||||||
* Must be unique within all classes registered with a ``Channel``
|
* Must be unique within all classes registered with a ``Channel``
|
||||||
* Must be less than ``0xf000``. Values greater than or equal to ``0xf000`` are reserved.
|
* Must be less than ``0xf000``. Values greater than or equal to ``0xf000`` are reserved.
|
||||||
"""
|
"""
|
||||||
|
|
@ -162,7 +110,6 @@ class MessageBase(abc.ABC):
|
||||||
|
|
||||||
MessageCallbackType = NewType("MessageCallbackType", Callable[[MessageBase], bool])
|
MessageCallbackType = NewType("MessageCallbackType", Callable[[MessageBase], bool])
|
||||||
|
|
||||||
|
|
||||||
class Envelope:
|
class Envelope:
|
||||||
"""
|
"""
|
||||||
Internal wrapper used to transport messages over a channel and
|
Internal wrapper used to transport messages over a channel and
|
||||||
|
|
@ -172,8 +119,7 @@ class Envelope:
|
||||||
msgtype, self.sequence, length = struct.unpack(">HHH", self.raw[:6])
|
msgtype, self.sequence, length = struct.unpack(">HHH", self.raw[:6])
|
||||||
raw = self.raw[6:]
|
raw = self.raw[6:]
|
||||||
ctor = message_factories.get(msgtype, None)
|
ctor = message_factories.get(msgtype, None)
|
||||||
if ctor is None:
|
if ctor is None: raise ChannelException(CEType.ME_NOT_REGISTERED, f"Unable to find constructor for Channel MSGTYPE {hex(msgtype)}")
|
||||||
raise ChannelException(CEType.ME_NOT_REGISTERED, f"Unable to find constructor for Channel MSGTYPE {hex(msgtype)}")
|
|
||||||
message = ctor()
|
message = ctor()
|
||||||
message.unpack(raw)
|
message.unpack(raw)
|
||||||
self.unpacked = True
|
self.unpacked = True
|
||||||
|
|
@ -182,14 +128,13 @@ class Envelope:
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def pack(self) -> bytes:
|
def pack(self) -> bytes:
|
||||||
if self.message.__class__.MSGTYPE is None:
|
if self.message.__class__.MSGTYPE is None: raise ChannelException(CEType.ME_NO_MSG_TYPE, f"{self.message.__class__} lacks MSGTYPE")
|
||||||
raise ChannelException(CEType.ME_NO_MSG_TYPE, f"{self.message.__class__} lacks MSGTYPE")
|
|
||||||
data = self.message.pack()
|
data = self.message.pack()
|
||||||
self.raw = struct.pack(">HHH", self.message.MSGTYPE, self.sequence, len(data)) + data
|
self.raw = struct.pack(">HHH", self.message.MSGTYPE, self.sequence, len(data)) + data
|
||||||
self.packed = True
|
self.packed = True
|
||||||
return self.raw
|
return self.raw
|
||||||
|
|
||||||
def __init__(self, outlet: ChannelOutletBase, message: MessageBase = None, raw: bytes = None, sequence: int = None):
|
def __init__(self, outlet: LinkChannelOutlet, message: MessageBase = None, raw: bytes = None, sequence: int = None):
|
||||||
self.ts = time.time()
|
self.ts = time.time()
|
||||||
self.id = id(self)
|
self.id = id(self)
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
@ -247,11 +192,11 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
|
|
||||||
# The maximum window size for transfers on fast links
|
# The maximum window size for transfers on fast links
|
||||||
WINDOW_MAX_FAST = 48
|
WINDOW_MAX_FAST = 48
|
||||||
|
|
||||||
# For calculating maps and guard segments, this
|
# For calculating maps and guard segments, this
|
||||||
# must be set to the global maximum window.
|
# must be set to the global maximum window.
|
||||||
WINDOW_MAX = WINDOW_MAX_FAST
|
WINDOW_MAX = WINDOW_MAX_FAST
|
||||||
|
|
||||||
# If the fast rate is sustained for this many request
|
# If the fast rate is sustained for this many request
|
||||||
# rounds, the fast link window size will be allowed.
|
# rounds, the fast link window size will be allowed.
|
||||||
FAST_RATE_THRESHOLD = 10
|
FAST_RATE_THRESHOLD = 10
|
||||||
|
|
@ -270,13 +215,14 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
SEQ_MAX = 0xFFFF
|
SEQ_MAX = 0xFFFF
|
||||||
SEQ_MODULUS = SEQ_MAX+1
|
SEQ_MODULUS = SEQ_MAX+1
|
||||||
|
|
||||||
def __init__(self, outlet: ChannelOutletBase):
|
def __init__(self, outlet: LinkChannelOutlet):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@param outlet:
|
@param outlet:
|
||||||
"""
|
"""
|
||||||
self._outlet = outlet
|
self._outlet = outlet
|
||||||
self._lock = threading.RLock()
|
self._lock = threading.RLock()
|
||||||
|
self._send_lock = threading.Lock()
|
||||||
self._tx_ring: collections.deque[Envelope] = collections.deque()
|
self._tx_ring: collections.deque[Envelope] = collections.deque()
|
||||||
self._rx_ring: collections.deque[Envelope] = collections.deque()
|
self._rx_ring: collections.deque[Envelope] = collections.deque()
|
||||||
self._message_callbacks: [MessageCallbackType] = []
|
self._message_callbacks: [MessageCallbackType] = []
|
||||||
|
|
@ -298,11 +244,9 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
self.window_min = Channel.WINDOW_MIN
|
self.window_min = Channel.WINDOW_MIN
|
||||||
self.window_flexibility = Channel.WINDOW_FLEXIBILITY
|
self.window_flexibility = Channel.WINDOW_FLEXIBILITY
|
||||||
|
|
||||||
def __enter__(self) -> Channel:
|
def __enter__(self) -> Channel: return self
|
||||||
return self
|
|
||||||
|
|
||||||
def __exit__(self, __exc_type: Type[BaseException] | None, __exc_value: BaseException | None,
|
def __exit__(self, __exc_type: Type[BaseException] | None, __exc_value: BaseException | None, __traceback: TracebackType | None) -> bool | None:
|
||||||
__traceback: TracebackType | None) -> bool | None:
|
|
||||||
self._shutdown()
|
self._shutdown()
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -318,20 +262,13 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
|
|
||||||
def _register_message_type(self, message_class: Type[MessageBase], *, is_system_type: bool = False):
|
def _register_message_type(self, message_class: Type[MessageBase], *, is_system_type: bool = False):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if not issubclass(message_class, MessageBase):
|
if not issubclass(message_class, MessageBase): raise ChannelException(CEType.ME_INVALID_MSG_TYPE, f"{message_class} is not a subclass of {MessageBase}.")
|
||||||
raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
|
if message_class.MSGTYPE is None: raise ChannelException(CEType.ME_INVALID_MSG_TYPE, f"{message_class} has invalid MSGTYPE class attribute.")
|
||||||
f"{message_class} is not a subclass of {MessageBase}.")
|
if message_class.MSGTYPE >= 0xf000 and not is_system_type: raise ChannelException(CEType.ME_INVALID_MSG_TYPE, f"{message_class} has system-reserved message type.")
|
||||||
if message_class.MSGTYPE is None:
|
|
||||||
raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
|
try: message_class()
|
||||||
f"{message_class} has invalid MSGTYPE class attribute.")
|
|
||||||
if message_class.MSGTYPE >= 0xf000 and not is_system_type:
|
|
||||||
raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
|
|
||||||
f"{message_class} has system-reserved message type.")
|
|
||||||
try:
|
|
||||||
message_class()
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
raise ChannelException(CEType.ME_INVALID_MSG_TYPE,
|
raise ChannelException(CEType.ME_INVALID_MSG_TYPE, f"{message_class} raised an exception when constructed with no arguments: {ex}")
|
||||||
f"{message_class} raised an exception when constructed with no arguments: {ex}")
|
|
||||||
|
|
||||||
self._message_factories[message_class.MSGTYPE] = message_class
|
self._message_factories[message_class.MSGTYPE] = message_class
|
||||||
|
|
||||||
|
|
@ -374,27 +311,30 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
if envelope.packet is not None:
|
if envelope.packet is not None:
|
||||||
self._outlet.set_packet_timeout_callback(envelope.packet, None)
|
self._outlet.set_packet_timeout_callback(envelope.packet, None)
|
||||||
self._outlet.set_packet_delivered_callback(envelope.packet, None)
|
self._outlet.set_packet_delivered_callback(envelope.packet, None)
|
||||||
|
envelope.tracked = False
|
||||||
|
|
||||||
|
for envelope in self._rx_ring:
|
||||||
|
envelope.tracked = False
|
||||||
|
|
||||||
self._tx_ring.clear()
|
self._tx_ring.clear()
|
||||||
self._rx_ring.clear()
|
self._rx_ring.clear()
|
||||||
|
|
||||||
def _emplace_envelope(self, envelope: Envelope, ring: collections.deque[Envelope]) -> bool:
|
def _emplace_envelope(self, envelope: Envelope, ring: collections.deque[Envelope]) -> bool:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
for existing in ring:
|
|
||||||
|
|
||||||
|
for existing in ring:
|
||||||
if envelope.sequence == existing.sequence:
|
if envelope.sequence == existing.sequence:
|
||||||
RNS.log(f"Envelope: Emplacement of duplicate envelope with sequence "+str(envelope.sequence), RNS.LOG_EXTREME)
|
RNS.log(f"Envelope: Emplacement of duplicate envelope with sequence "+str(envelope.sequence), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if envelope.sequence < existing.sequence and not (self._next_rx_sequence - envelope.sequence) > (Channel.SEQ_MAX//2):
|
if envelope.sequence < existing.sequence and not (self._next_rx_sequence - envelope.sequence) > (Channel.SEQ_MAX//2):
|
||||||
ring.insert(i, envelope)
|
ring.insert(i, envelope)
|
||||||
|
|
||||||
envelope.tracked = True
|
envelope.tracked = True
|
||||||
return True
|
return True
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
envelope.tracked = True
|
envelope.tracked = True
|
||||||
ring.append(envelope)
|
ring.append(envelope)
|
||||||
|
|
||||||
|
|
@ -405,10 +345,8 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
|
|
||||||
for cb in cbs:
|
for cb in cbs:
|
||||||
try:
|
try:
|
||||||
if cb(message):
|
if cb(message): return
|
||||||
return
|
except Exception as e: RNS.log(f"Channel {str(self)} experienced an error while running a message callback. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Channel "+str(self)+" experienced an error while running a message callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
def _receive(self, raw: bytes):
|
def _receive(self, raw: bytes):
|
||||||
try:
|
try:
|
||||||
|
|
@ -420,16 +358,20 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
window_overflow = (self._next_rx_sequence+Channel.WINDOW_MAX) % Channel.SEQ_MODULUS
|
window_overflow = (self._next_rx_sequence+Channel.WINDOW_MAX) % Channel.SEQ_MODULUS
|
||||||
if window_overflow < self._next_rx_sequence:
|
if window_overflow < self._next_rx_sequence:
|
||||||
if envelope.sequence > window_overflow:
|
if envelope.sequence > window_overflow:
|
||||||
RNS.log("Invalid packet sequence ("+str(envelope.sequence)+") received on channel "+str(self), RNS.LOG_EXTREME)
|
RNS.log(f"Invalid packet sequence ({envelope.sequence}) received on channel {self}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
RNS.log("Invalid packet sequence ("+str(envelope.sequence)+") received on channel "+str(self), RNS.LOG_EXTREME)
|
RNS.log(f"Invalid packet sequence ({envelope.sequence}) received on channel {self}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
return
|
return
|
||||||
|
|
||||||
|
elif envelope.sequence > self._next_rx_sequence + self.WINDOW_MAX:
|
||||||
|
RNS.log(f"Invalid packet sequence ({envelope.sequence}) received on channel {self}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
return
|
||||||
|
|
||||||
is_new = self._emplace_envelope(envelope, self._rx_ring)
|
is_new = self._emplace_envelope(envelope, self._rx_ring)
|
||||||
|
|
||||||
if not is_new:
|
if not is_new:
|
||||||
RNS.log("Duplicate message received on channel "+str(self), RNS.LOG_EXTREME)
|
RNS.log("Duplicate message received on channel "+str(self), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
|
|
@ -445,11 +387,9 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
self._next_rx_sequence = (self._next_rx_sequence + 1) % Channel.SEQ_MODULUS
|
self._next_rx_sequence = (self._next_rx_sequence + 1) % Channel.SEQ_MODULUS
|
||||||
|
|
||||||
for e in contigous:
|
for e in contigous:
|
||||||
if not e.unpacked:
|
if not e.unpacked: m = e.unpack(self._message_factories)
|
||||||
m = e.unpack(self._message_factories)
|
else: m = e.message
|
||||||
else:
|
|
||||||
m = e.message
|
|
||||||
|
|
||||||
self._rx_ring.remove(e)
|
self._rx_ring.remove(e)
|
||||||
self._run_callbacks(m)
|
self._run_callbacks(m)
|
||||||
|
|
||||||
|
|
@ -462,67 +402,46 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
|
|
||||||
:return: True if ready
|
:return: True if ready
|
||||||
"""
|
"""
|
||||||
if not self._outlet.is_usable:
|
if not self._outlet.is_usable: return False
|
||||||
return False
|
|
||||||
|
|
||||||
with self._lock:
|
with self._lock:
|
||||||
outstanding = 0
|
outstanding = 0
|
||||||
for envelope in self._tx_ring:
|
for envelope in self._tx_ring:
|
||||||
if envelope.outlet == self._outlet:
|
if envelope.outlet == self._outlet:
|
||||||
if not envelope.packet or not self._outlet.get_packet_state(envelope.packet) == MessageState.MSGSTATE_DELIVERED:
|
if not envelope.packet or not self._outlet.get_packet_state(envelope.packet) == MessageState.MSGSTATE_DELIVERED:
|
||||||
outstanding += 1
|
outstanding += 1
|
||||||
|
|
||||||
if outstanding >= self.window:
|
if outstanding >= self.window: return False
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _packet_tx_op(self, packet: TPacket, op: Callable[[TPacket], bool]):
|
def _packet_tx_op(self, packet: TPacket, op: Callable[[TPacket], bool]):
|
||||||
|
target_id = self._outlet.get_packet_id(packet)
|
||||||
with self._lock:
|
with self._lock:
|
||||||
envelope = next(filter(lambda e: self._outlet.get_packet_id(e.packet) == self._outlet.get_packet_id(packet),
|
envelope = next(filter(lambda e: e.packet is not None and self._outlet.get_packet_id(e.packet) == target_id, self._tx_ring), None)
|
||||||
self._tx_ring), None)
|
|
||||||
|
|
||||||
if envelope and op(envelope):
|
if envelope and op(envelope):
|
||||||
envelope.tracked = False
|
envelope.tracked = False
|
||||||
if envelope in self._tx_ring:
|
if envelope in self._tx_ring:
|
||||||
self._tx_ring.remove(envelope)
|
self._tx_ring.remove(envelope)
|
||||||
|
|
||||||
if self.window < self.window_max:
|
if self.window < self.window_max: self.window += 1
|
||||||
self.window += 1
|
|
||||||
|
|
||||||
# TODO: Remove at some point
|
|
||||||
# RNS.log("Increased "+str(self)+" window to "+str(self.window), RNS.LOG_DEBUG)
|
|
||||||
|
|
||||||
if self._outlet.rtt != 0:
|
if self._outlet.rtt != 0:
|
||||||
if self._outlet.rtt > Channel.RTT_FAST:
|
if self._outlet.rtt > Channel.RTT_FAST:
|
||||||
self.fast_rate_rounds = 0
|
self.fast_rate_rounds = 0
|
||||||
|
if self._outlet.rtt > Channel.RTT_MEDIUM: self.medium_rate_rounds = 0
|
||||||
if self._outlet.rtt > Channel.RTT_MEDIUM:
|
|
||||||
self.medium_rate_rounds = 0
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.medium_rate_rounds += 1
|
self.medium_rate_rounds += 1
|
||||||
if self.window_max < Channel.WINDOW_MAX_MEDIUM and self.medium_rate_rounds == Channel.FAST_RATE_THRESHOLD:
|
if self.window_max < Channel.WINDOW_MAX_MEDIUM and self.medium_rate_rounds == Channel.FAST_RATE_THRESHOLD:
|
||||||
self.window_max = Channel.WINDOW_MAX_MEDIUM
|
self.window_max = Channel.WINDOW_MAX_MEDIUM
|
||||||
self.window_min = Channel.WINDOW_MIN_LIMIT_MEDIUM
|
self.window_min = Channel.WINDOW_MIN_LIMIT_MEDIUM
|
||||||
# TODO: Remove at some point
|
|
||||||
# RNS.log("Increased "+str(self)+" max window to "+str(self.window_max), RNS.LOG_DEBUG)
|
|
||||||
# RNS.log("Increased "+str(self)+" min window to "+str(self.window_min), RNS.LOG_DEBUG)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.fast_rate_rounds += 1
|
self.fast_rate_rounds += 1
|
||||||
if self.window_max < Channel.WINDOW_MAX_FAST and self.fast_rate_rounds == Channel.FAST_RATE_THRESHOLD:
|
if self.window_max < Channel.WINDOW_MAX_FAST and self.fast_rate_rounds == Channel.FAST_RATE_THRESHOLD:
|
||||||
self.window_max = Channel.WINDOW_MAX_FAST
|
self.window_max = Channel.WINDOW_MAX_FAST
|
||||||
self.window_min = Channel.WINDOW_MIN_LIMIT_FAST
|
self.window_min = Channel.WINDOW_MIN_LIMIT_FAST
|
||||||
# TODO: Remove at some point
|
|
||||||
# RNS.log("Increased "+str(self)+" max window to "+str(self.window_max), RNS.LOG_DEBUG)
|
|
||||||
# RNS.log("Increased "+str(self)+" min window to "+str(self.window_min), RNS.LOG_DEBUG)
|
|
||||||
|
|
||||||
|
else: RNS.log("Envelope not found in TX ring for "+str(self), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
else:
|
if not envelope: RNS.log("Spurious message received on "+str(self), RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
RNS.log("Envelope not found in TX ring for "+str(self), RNS.LOG_EXTREME)
|
|
||||||
if not envelope:
|
|
||||||
RNS.log("Spurious message received on "+str(self), RNS.LOG_EXTREME)
|
|
||||||
|
|
||||||
def _packet_delivered(self, packet: TPacket):
|
def _packet_delivered(self, packet: TPacket):
|
||||||
self._packet_tx_op(packet, lambda env: True)
|
self._packet_tx_op(packet, lambda env: True)
|
||||||
|
|
@ -531,44 +450,47 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
for envelope in self._tx_ring:
|
for envelope in self._tx_ring:
|
||||||
updated_timeout = self._get_packet_timeout_time(envelope.tries)
|
updated_timeout = self._get_packet_timeout_time(envelope.tries)
|
||||||
if envelope.packet and hasattr(envelope.packet, "receipt") and envelope.packet.receipt and envelope.packet.receipt.timeout:
|
if envelope.packet and hasattr(envelope.packet, "receipt") and envelope.packet.receipt and envelope.packet.receipt.timeout:
|
||||||
if updated_timeout > envelope.packet.receipt.timeout:
|
if updated_timeout > envelope.packet.receipt.timeout: envelope.packet.receipt.set_timeout(updated_timeout)
|
||||||
envelope.packet.receipt.set_timeout(updated_timeout)
|
|
||||||
|
|
||||||
def _get_packet_timeout_time(self, tries: int) -> float:
|
def _get_packet_timeout_time(self, tries: int) -> float:
|
||||||
to = pow(1.5, tries - 1) * max(self._outlet.rtt*2.5, 0.025) * (len(self._tx_ring)+1.5)
|
to = pow(1.5, tries - 1) * max(self._outlet.rtt*2.5, 0.025) * (len(self._tx_ring)+1.5)
|
||||||
return to
|
return to
|
||||||
|
|
||||||
def _packet_timeout(self, packet: TPacket):
|
def _packet_timeout(self, packet: TPacket):
|
||||||
def retry_envelope(envelope: Envelope) -> bool:
|
if self._outlet.get_packet_state(packet) == MessageState.MSGSTATE_DELIVERED: return
|
||||||
if envelope.tries >= self._max_tries:
|
|
||||||
RNS.log("Retry count exceeded on "+str(self)+", tearing down Link.", RNS.LOG_ERROR)
|
|
||||||
self._shutdown() # start on separate thread?
|
|
||||||
self._outlet.timed_out()
|
|
||||||
return True
|
|
||||||
|
|
||||||
envelope.tries += 1
|
target_id = self._outlet.get_packet_id(packet)
|
||||||
self._outlet.resend(envelope.packet)
|
envelope_to_resend: Envelope | None = None
|
||||||
self._outlet.set_packet_delivered_callback(envelope.packet, self._packet_delivered)
|
should_teardown = False
|
||||||
self._outlet.set_packet_timeout_callback(envelope.packet, self._packet_timeout, self._get_packet_timeout_time(envelope.tries))
|
with self._lock:
|
||||||
self._update_packet_timeouts()
|
envelope = next(filter(lambda e: e.packet is not None and self._outlet.get_packet_id(e.packet) == target_id, self._tx_ring), None)
|
||||||
|
if envelope is None: return
|
||||||
|
|
||||||
if self.window > self.window_min:
|
if envelope.tries >= self._max_tries: should_teardown = True
|
||||||
self.window -= 1
|
else:
|
||||||
# TODO: Remove at some point
|
envelope.tries += 1
|
||||||
# RNS.log("Decreased "+str(self)+" window to "+str(self.window), RNS.LOG_DEBUG)
|
envelope_to_resend = envelope
|
||||||
|
|
||||||
if self.window_max > (self.window_min+self.window_flexibility):
|
if self.window > self.window_min:
|
||||||
self.window_max -= 1
|
self.window -= 1
|
||||||
# TODO: Remove at some point
|
if self.window_max > (self.window_min+self.window_flexibility):
|
||||||
# RNS.log("Decreased "+str(self)+" max window to "+str(self.window_max), RNS.LOG_DEBUG)
|
self.window_max -= 1
|
||||||
|
|
||||||
# TODO: Remove at some point
|
if should_teardown:
|
||||||
# RNS.log("Decreased "+str(self)+" window to "+str(self.window), RNS.LOG_EXTREME)
|
RNS.log("Retry count exceeded on "+str(self)+", tearing down Link.", RNS.LOG_ERROR)
|
||||||
|
self._shutdown()
|
||||||
|
self._outlet.timed_out()
|
||||||
|
return
|
||||||
|
|
||||||
return False
|
if envelope_to_resend is not None:
|
||||||
|
self._outlet.resend(envelope_to_resend.packet)
|
||||||
|
with self._lock:
|
||||||
|
self._outlet.set_packet_delivered_callback(envelope_to_resend.packet, self._packet_delivered)
|
||||||
|
self._outlet.set_packet_timeout_callback(envelope_to_resend.packet, self._packet_timeout, self._get_packet_timeout_time(envelope_to_resend.tries))
|
||||||
|
self._update_packet_timeouts()
|
||||||
|
already_delivered = (self._outlet.get_packet_state(envelope_to_resend.packet) == MessageState.MSGSTATE_DELIVERED)
|
||||||
|
|
||||||
if self._outlet.get_packet_state(packet) != MessageState.MSGSTATE_DELIVERED:
|
if already_delivered: self._packet_delivered(envelope_to_resend.packet)
|
||||||
self._packet_tx_op(packet, retry_envelope)
|
|
||||||
|
|
||||||
def send(self, message: MessageBase) -> Envelope:
|
def send(self, message: MessageBase) -> Envelope:
|
||||||
"""
|
"""
|
||||||
|
|
@ -577,27 +499,33 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
|
|
||||||
:param message: an instance of a ``MessageBase`` subclass
|
:param message: an instance of a ``MessageBase`` subclass
|
||||||
"""
|
"""
|
||||||
envelope: Envelope | None = None
|
with self._send_lock:
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if not self.is_ready_to_send():
|
if not self.is_ready_to_send(): raise ChannelException(CEType.ME_LINK_NOT_READY, f"Link is not ready")
|
||||||
raise ChannelException(CEType.ME_LINK_NOT_READY, f"Link is not ready")
|
|
||||||
|
|
||||||
envelope = Envelope(self._outlet, message=message, sequence=self._next_sequence)
|
|
||||||
self._next_sequence = (self._next_sequence + 1) % Channel.SEQ_MODULUS
|
|
||||||
self._emplace_envelope(envelope, self._tx_ring)
|
|
||||||
|
|
||||||
if envelope is None:
|
reserved_sequence = self._next_sequence
|
||||||
raise BlockingIOError()
|
envelope = Envelope(self._outlet, message=message, sequence=reserved_sequence)
|
||||||
|
envelope.pack()
|
||||||
|
if len(envelope.raw) > self._outlet.mdu:
|
||||||
|
raise ChannelException(CEType.ME_TOO_BIG, f"Packed message too big for packet: {len(envelope.raw)} > {self._outlet.mdu}")
|
||||||
|
self._next_sequence = (reserved_sequence + 1) % Channel.SEQ_MODULUS
|
||||||
|
|
||||||
envelope.pack()
|
envelope.packet = self._outlet.send(envelope.raw)
|
||||||
if len(envelope.raw) > self._outlet.mdu:
|
|
||||||
raise ChannelException(CEType.ME_TOO_BIG, f"Packed message too big for packet: {len(envelope.raw)} > {self._outlet.mdu}")
|
if envelope.packet is None or getattr(envelope.packet, "raw", None) is None or (hasattr(envelope.packet, "receipt") and envelope.packet.receipt is None):
|
||||||
|
with self._lock: self._next_sequence = reserved_sequence
|
||||||
envelope.packet = self._outlet.send(envelope.raw)
|
raise ChannelException(CEType.ME_LINK_NOT_READY, "Outlet did not transmit packet")
|
||||||
envelope.tries += 1
|
|
||||||
self._outlet.set_packet_delivered_callback(envelope.packet, self._packet_delivered)
|
with self._lock:
|
||||||
self._outlet.set_packet_timeout_callback(envelope.packet, self._packet_timeout, self._get_packet_timeout_time(envelope.tries))
|
self._emplace_envelope(envelope, self._tx_ring)
|
||||||
self._update_packet_timeouts()
|
envelope.tries += 1
|
||||||
|
self._outlet.set_packet_delivered_callback(envelope.packet, self._packet_delivered)
|
||||||
|
self._outlet.set_packet_timeout_callback(envelope.packet, self._packet_timeout, self._get_packet_timeout_time(envelope.tries))
|
||||||
|
self._update_packet_timeouts()
|
||||||
|
already_delivered = (self._outlet.get_packet_state(envelope.packet) == MessageState.MSGSTATE_DELIVERED)
|
||||||
|
|
||||||
|
# Prevent _tx_ring envelope leak
|
||||||
|
if already_delivered: self._packet_delivered(envelope.packet)
|
||||||
|
|
||||||
return envelope
|
return envelope
|
||||||
|
|
||||||
|
|
@ -612,14 +540,13 @@ class Channel(contextlib.AbstractContextManager):
|
||||||
:return: number of bytes available
|
:return: number of bytes available
|
||||||
"""
|
"""
|
||||||
mdu = self._outlet.mdu - 6 # sizeof(msgtype) + sizeof(length) + sizeof(sequence)
|
mdu = self._outlet.mdu - 6 # sizeof(msgtype) + sizeof(length) + sizeof(sequence)
|
||||||
if mdu > 0xFFFF:
|
if mdu > 0xFFFF: mdu = 0xFFFF
|
||||||
mdu = 0xFFFF
|
|
||||||
return mdu
|
return mdu
|
||||||
|
|
||||||
|
|
||||||
class LinkChannelOutlet(ChannelOutletBase):
|
class LinkChannelOutlet(ABC, Generic[TPacket]):
|
||||||
"""
|
"""
|
||||||
An implementation of ChannelOutletBase for RNS.Link.
|
A channel outlet implementation for RNS.Link.
|
||||||
Allows Channel to send packets over an RNS Link with
|
Allows Channel to send packets over an RNS Link with
|
||||||
Packets.
|
Packets.
|
||||||
|
|
||||||
|
|
@ -630,68 +557,51 @@ class LinkChannelOutlet(ChannelOutletBase):
|
||||||
|
|
||||||
def send(self, raw: bytes) -> RNS.Packet:
|
def send(self, raw: bytes) -> RNS.Packet:
|
||||||
packet = RNS.Packet(self.link, raw, context=RNS.Packet.CHANNEL)
|
packet = RNS.Packet(self.link, raw, context=RNS.Packet.CHANNEL)
|
||||||
if self.link.status == RNS.Link.ACTIVE:
|
if self.link.status == RNS.Link.ACTIVE: packet.send()
|
||||||
packet.send()
|
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
def resend(self, packet: RNS.Packet) -> RNS.Packet:
|
def resend(self, packet: RNS.Packet) -> RNS.Packet:
|
||||||
receipt = packet.resend()
|
receipt = packet.resend()
|
||||||
if not receipt:
|
if not receipt: RNS.log("Failed to resend packet", RNS.LOG_ERROR)
|
||||||
RNS.log("Failed to resend packet", RNS.LOG_ERROR)
|
|
||||||
return packet
|
return packet
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def mdu(self):
|
def mdu(self): return self.link.mdu
|
||||||
return self.link.mdu
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def rtt(self):
|
def rtt(self): return self.link.rtt
|
||||||
return self.link.rtt
|
|
||||||
|
|
||||||
|
# TODO: Initial implementation had issues
|
||||||
|
# proxying this to link status. Should
|
||||||
|
# return False when link is not ACTIVE.
|
||||||
|
# Investigate what happened here.
|
||||||
@property
|
@property
|
||||||
def is_usable(self):
|
def is_usable(self): return True
|
||||||
return True # had issues looking at Link.status
|
|
||||||
|
|
||||||
def get_packet_state(self, packet: TPacket) -> MessageState:
|
def get_packet_state(self, packet: TPacket) -> MessageState:
|
||||||
if packet.receipt == None:
|
if packet.receipt == None:
|
||||||
return MessageState.MSGSTATE_FAILED
|
return MessageState.MSGSTATE_FAILED
|
||||||
|
|
||||||
status = packet.receipt.get_status()
|
status = packet.receipt.get_status()
|
||||||
if status == RNS.PacketReceipt.SENT:
|
if status == RNS.PacketReceipt.SENT: return MessageState.MSGSTATE_SENT
|
||||||
return MessageState.MSGSTATE_SENT
|
if status == RNS.PacketReceipt.DELIVERED: return MessageState.MSGSTATE_DELIVERED
|
||||||
if status == RNS.PacketReceipt.DELIVERED:
|
if status == RNS.PacketReceipt.FAILED: return MessageState.MSGSTATE_FAILED
|
||||||
return MessageState.MSGSTATE_DELIVERED
|
else: raise Exception(f"Unexpected receipt state: {status}")
|
||||||
if status == RNS.PacketReceipt.FAILED:
|
|
||||||
return MessageState.MSGSTATE_FAILED
|
|
||||||
else:
|
|
||||||
raise Exception(f"Unexpected receipt state: {status}")
|
|
||||||
|
|
||||||
def timed_out(self):
|
def set_packet_timeout_callback(self, packet: RNS.Packet, callback: Callable[[RNS.Packet], None] | None, timeout: float | None = None):
|
||||||
self.link.teardown()
|
if timeout and packet.receipt: packet.receipt.set_timeout(timeout)
|
||||||
|
def inner(receipt: RNS.PacketReceipt): callback(packet)
|
||||||
def __str__(self):
|
if packet and packet.receipt: packet.receipt.set_timeout_callback(inner if callback else None)
|
||||||
return f"{self.__class__.__name__}({self.link})"
|
|
||||||
|
|
||||||
def set_packet_timeout_callback(self, packet: RNS.Packet, callback: Callable[[RNS.Packet], None] | None,
|
|
||||||
timeout: float | None = None):
|
|
||||||
if timeout and packet.receipt:
|
|
||||||
packet.receipt.set_timeout(timeout)
|
|
||||||
|
|
||||||
def inner(receipt: RNS.PacketReceipt):
|
|
||||||
callback(packet)
|
|
||||||
|
|
||||||
if packet and packet.receipt:
|
|
||||||
packet.receipt.set_timeout_callback(inner if callback else None)
|
|
||||||
|
|
||||||
def set_packet_delivered_callback(self, packet: RNS.Packet, callback: Callable[[RNS.Packet], None] | None):
|
def set_packet_delivered_callback(self, packet: RNS.Packet, callback: Callable[[RNS.Packet], None] | None):
|
||||||
def inner(receipt: RNS.PacketReceipt):
|
def inner(receipt: RNS.PacketReceipt): callback(packet)
|
||||||
callback(packet)
|
if packet and packet.receipt: packet.receipt.set_delivery_callback(inner if callback else None)
|
||||||
|
|
||||||
if packet and packet.receipt:
|
|
||||||
packet.receipt.set_delivery_callback(inner if callback else None)
|
|
||||||
|
|
||||||
def get_packet_id(self, packet: RNS.Packet) -> any:
|
def get_packet_id(self, packet: RNS.Packet) -> any:
|
||||||
if packet and hasattr(packet, "get_hash") and callable(packet.get_hash):
|
if packet and getattr(packet, "raw", None) is not None and hasattr(packet, "get_hash") and callable(packet.get_hash):
|
||||||
return packet.get_hash()
|
return packet.get_hash()
|
||||||
else:
|
else: return None
|
||||||
return None
|
|
||||||
|
def timed_out(self): self.link.teardown()
|
||||||
|
|
||||||
|
def __str__(self): return f"{self.__class__.__name__}({self.link})"
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -24,23 +32,57 @@ import RNS.Cryptography.Provider as cp
|
||||||
import RNS.vendor.platformutils as pu
|
import RNS.vendor.platformutils as pu
|
||||||
|
|
||||||
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
||||||
from .aes import AES
|
from .aes import AES128
|
||||||
|
from .aes import AES256
|
||||||
|
|
||||||
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
||||||
|
if pu.cryptography_old_api(): from cryptography.hazmat.backends import default_backend
|
||||||
if pu.cryptography_old_api():
|
|
||||||
from cryptography.hazmat.backends import default_backend
|
|
||||||
|
|
||||||
|
|
||||||
class AES_128_CBC:
|
class AES_128_CBC:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encrypt(plaintext, key, iv):
|
def encrypt(plaintext, key, iv):
|
||||||
|
if len(key) != 16: raise ValueError(f"Invalid key length {len(key)*8} for AES_128_CBC")
|
||||||
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
||||||
cipher = AES(key)
|
cipher = AES128(key)
|
||||||
return cipher.encrypt(plaintext, iv)
|
return cipher.encrypt(plaintext, iv)
|
||||||
|
|
||||||
|
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
|
if not pu.cryptography_old_api():
|
||||||
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
||||||
|
else:
|
||||||
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
||||||
|
|
||||||
|
encryptor = cipher.encryptor()
|
||||||
|
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
|
||||||
|
return ciphertext
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def decrypt(ciphertext, key, iv):
|
||||||
|
if len(key) != 16: raise ValueError(f"Invalid key length {len(key)*8} for AES_128_CBC")
|
||||||
|
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
||||||
|
cipher = AES128(key)
|
||||||
|
return cipher.decrypt(ciphertext, iv)
|
||||||
|
|
||||||
|
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
|
if not pu.cryptography_old_api():
|
||||||
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
||||||
|
else:
|
||||||
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
|
||||||
|
|
||||||
|
decryptor = cipher.decryptor()
|
||||||
|
plaintext = decryptor.update(ciphertext) + decryptor.finalize()
|
||||||
|
return plaintext
|
||||||
|
|
||||||
|
class AES_256_CBC:
|
||||||
|
@staticmethod
|
||||||
|
def encrypt(plaintext, key, iv):
|
||||||
|
if len(key) != 32: raise ValueError(f"Invalid key length {len(key)*8} for AES_256_CBC")
|
||||||
|
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
||||||
|
cipher = AES256(key)
|
||||||
|
return cipher.encrypt_cbc(plaintext, iv)
|
||||||
|
|
||||||
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
if not pu.cryptography_old_api():
|
if not pu.cryptography_old_api():
|
||||||
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
|
||||||
|
|
@ -53,9 +95,10 @@ class AES_128_CBC:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def decrypt(ciphertext, key, iv):
|
def decrypt(ciphertext, key, iv):
|
||||||
|
if len(key) != 32: raise ValueError(f"Invalid key length {len(key)*8} for AES_256_CBC")
|
||||||
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
if cp.PROVIDER == cp.PROVIDER_INTERNAL:
|
||||||
cipher = AES(key)
|
cipher = AES256(key)
|
||||||
return cipher.decrypt(ciphertext, iv)
|
return cipher.decrypt_cbc(ciphertext, iv)
|
||||||
|
|
||||||
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
if not pu.cryptography_old_api():
|
if not pu.cryptography_old_api():
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,33 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from .pure25519 import ed25519_oop as ed25519
|
from .pure25519 import ed25519_oop as ed25519
|
||||||
|
|
||||||
|
|
@ -5,7 +35,6 @@ class Ed25519PrivateKey:
|
||||||
def __init__(self, seed):
|
def __init__(self, seed):
|
||||||
self.seed = seed
|
self.seed = seed
|
||||||
self.sk = ed25519.SigningKey(self.seed)
|
self.sk = ed25519.SigningKey(self.seed)
|
||||||
#self.vk = self.sk.get_verifying_key()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def generate(cls):
|
def generate(cls):
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -24,8 +32,59 @@ import hashlib
|
||||||
from math import ceil
|
from math import ceil
|
||||||
from RNS.Cryptography import HMAC
|
from RNS.Cryptography import HMAC
|
||||||
|
|
||||||
|
# Precomputed byte-permutation tables used to key the inner
|
||||||
|
# and outer hashing states of the HMAC construction
|
||||||
|
trans_5C = bytes((x ^ 0x5C) for x in range(256))
|
||||||
|
trans_36 = bytes((x ^ 0x36) for x in range(256))
|
||||||
|
|
||||||
|
# Precomputed HKDF counter bytes
|
||||||
|
_counters = [bytes([(i + 1) % 0x100]) for i in range(0x100)]
|
||||||
|
|
||||||
def hkdf(length=None, derive_from=None, salt=None, context=None):
|
def hkdf(length=None, derive_from=None, salt=None, context=None):
|
||||||
hash_len = 32
|
hash_len = 32
|
||||||
|
hash_block_size = 64
|
||||||
|
|
||||||
|
if length == None or length < 1: raise ValueError("Invalid output key length")
|
||||||
|
if derive_from == None or derive_from == "": raise ValueError("Cannot derive key from empty input material")
|
||||||
|
|
||||||
|
if salt == None or len(salt) == 0: salt = bytes([0] * hash_len)
|
||||||
|
if context == None: context = b""
|
||||||
|
|
||||||
|
pseudorandom_key = HMAC.new(salt, derive_from).digest()
|
||||||
|
|
||||||
|
# The pseudorandom key is always a 32-byte SHA-256 digest, so the
|
||||||
|
# HMAC key schedule below never needs the long-key hashing branch.
|
||||||
|
key = pseudorandom_key.ljust(hash_block_size, b"\x00")
|
||||||
|
|
||||||
|
# Precompute the keyed inner and outer hashing states of the
|
||||||
|
# HMAC construction once. Each expansion block then only requires
|
||||||
|
# two cheap hash-object copies instead of constructing a complete
|
||||||
|
# HMAC object per 32 bytes of output. This is possible since the
|
||||||
|
# PRK is **always** 32-byte SHA-256 HMAC output.
|
||||||
|
inner_state = hashlib.sha256(key.translate(trans_36))
|
||||||
|
outer_state = hashlib.sha256(key.translate(trans_5C))
|
||||||
|
|
||||||
|
# Expand
|
||||||
|
has_context = len(context) > 0
|
||||||
|
block = b""
|
||||||
|
derived = bytearray()
|
||||||
|
|
||||||
|
for i in range(ceil(length / hash_len)):
|
||||||
|
inner = inner_state.copy()
|
||||||
|
inner.update(block)
|
||||||
|
if has_context: inner.update(context)
|
||||||
|
inner.update(_counters[i & 0xFF])
|
||||||
|
|
||||||
|
outer = outer_state.copy()
|
||||||
|
outer.update(inner.digest())
|
||||||
|
|
||||||
|
block = outer.digest()
|
||||||
|
derived += block
|
||||||
|
|
||||||
|
return bytes(derived[:length])
|
||||||
|
|
||||||
|
def hkdf_legacy(length=None, derive_from=None, salt=None, context=None):
|
||||||
|
hash_len = 32
|
||||||
|
|
||||||
def hmac_sha256(key, data):
|
def hmac_sha256(key, data):
|
||||||
return HMAC.new(key, data).digest()
|
return HMAC.new(key, data).digest()
|
||||||
|
|
|
||||||
|
|
@ -180,4 +180,9 @@ def digest(key, msg, digest):
|
||||||
outer.update(key.translate(trans_5C))
|
outer.update(key.translate(trans_5C))
|
||||||
inner.update(msg)
|
inner.update(msg)
|
||||||
outer.update(inner.digest())
|
outer.update(inner.digest())
|
||||||
return outer.digest()
|
return outer.digest()
|
||||||
|
|
||||||
|
def _cd_internal(a, b): return a == b
|
||||||
|
try: from hmac import compare_digest as _cd
|
||||||
|
except ImportError: _cd = _cd_internal
|
||||||
|
compare_digest = _cd
|
||||||
|
|
@ -1,4 +1,34 @@
|
||||||
import importlib
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
if importlib.util.find_spec('hashlib') != None:
|
if importlib.util.find_spec('hashlib') != None:
|
||||||
import hashlib
|
import hashlib
|
||||||
else:
|
else:
|
||||||
|
|
@ -32,3 +62,9 @@ def sha512(data):
|
||||||
digest.update(data)
|
digest.update(data)
|
||||||
|
|
||||||
return digest.digest()
|
return digest.digest()
|
||||||
|
|
||||||
|
def file_sha256(file):
|
||||||
|
if not hashlib: raise SystemError("The hashlib module is not available on this system")
|
||||||
|
# TODO: Could implement fallback for old snakes here
|
||||||
|
if not hasattr(hashlib, "file_digest"): raise SystemError("The file_digest method is not available on this system. This functionality requires Python 3.11 or later.")
|
||||||
|
else: return hashlib.file_digest(file, "sha256").digest()
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,47 @@
|
||||||
import importlib
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
|
||||||
PROVIDER_NONE = 0x00
|
PROVIDER_NONE = 0x00
|
||||||
PROVIDER_INTERNAL = 0x01
|
PROVIDER_INTERNAL = 0x01
|
||||||
PROVIDER_PYCA = 0x02
|
PROVIDER_PYCA = 0x02
|
||||||
|
|
||||||
|
FORCE_INTERNAL = False
|
||||||
PROVIDER = PROVIDER_NONE
|
PROVIDER = PROVIDER_NONE
|
||||||
|
|
||||||
pyca_v = None
|
pyca_v = None
|
||||||
use_pyca = False
|
use_pyca = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if importlib.util.find_spec('cryptography') != None:
|
if not FORCE_INTERNAL and importlib.util.find_spec('cryptography') != None:
|
||||||
import cryptography
|
import cryptography
|
||||||
pyca_v = cryptography.__version__
|
pyca_v = cryptography.__version__
|
||||||
v = pyca_v.split(".")
|
v = pyca_v.split(".")
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,33 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
from cryptography.hazmat.primitives import serialization
|
from cryptography.hazmat.primitives import serialization
|
||||||
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
|
||||||
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
|
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey, X25519PublicKey
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -25,7 +33,9 @@ import time
|
||||||
|
|
||||||
from RNS.Cryptography import HMAC
|
from RNS.Cryptography import HMAC
|
||||||
from RNS.Cryptography import PKCS7
|
from RNS.Cryptography import PKCS7
|
||||||
|
from RNS.Cryptography import AES
|
||||||
from RNS.Cryptography.AES import AES_128_CBC
|
from RNS.Cryptography.AES import AES_128_CBC
|
||||||
|
from RNS.Cryptography.AES import AES_256_CBC
|
||||||
|
|
||||||
class Token():
|
class Token():
|
||||||
"""
|
"""
|
||||||
|
|
@ -40,71 +50,65 @@ class Token():
|
||||||
TOKEN_OVERHEAD = 48 # Bytes
|
TOKEN_OVERHEAD = 48 # Bytes
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def generate_key():
|
def generate_key(mode=AES_256_CBC):
|
||||||
return os.urandom(32)
|
if mode == AES_128_CBC: return os.urandom(32)
|
||||||
|
elif mode == AES_256_CBC: return os.urandom(64)
|
||||||
|
else: raise TypeError(f"Invalid token mode: {mode}")
|
||||||
|
|
||||||
def __init__(self, key = None):
|
def __init__(self, key=None, mode=AES):
|
||||||
if key == None:
|
if key == None: raise ValueError("Token key cannot be None")
|
||||||
raise ValueError("Token key cannot be None")
|
|
||||||
|
|
||||||
if len(key) != 32:
|
if mode == AES:
|
||||||
raise ValueError("Token key must be 32 bytes, not "+str(len(key)))
|
if len(key) == 32:
|
||||||
|
self.mode = AES_128_CBC
|
||||||
self._signing_key = key[:16]
|
self._signing_key = key[:16]
|
||||||
self._encryption_key = key[16:]
|
self._encryption_key = key[16:]
|
||||||
|
|
||||||
|
elif len(key) == 64:
|
||||||
|
self.mode = AES_256_CBC
|
||||||
|
self._signing_key = key[:32]
|
||||||
|
self._encryption_key = key[32:]
|
||||||
|
|
||||||
|
else: raise ValueError("Token key must be 128 or 256 bits, not "+str(len(key)*8))
|
||||||
|
|
||||||
|
else: raise TypeError(f"Invalid token mode: {mode}")
|
||||||
|
|
||||||
|
|
||||||
def verify_hmac(self, token):
|
def verify_hmac(self, token):
|
||||||
if len(token) <= 32:
|
if len(token) <= 32: raise ValueError("Cannot verify HMAC on token of only "+str(len(token))+" bytes")
|
||||||
raise ValueError("Cannot verify HMAC on token of only "+str(len(token))+" bytes")
|
|
||||||
else:
|
else:
|
||||||
received_hmac = token[-32:]
|
received_hmac = token[-32:]
|
||||||
expected_hmac = HMAC.new(self._signing_key, token[:-32]).digest()
|
expected_hmac = HMAC.new(self._signing_key, token[:-32]).digest()
|
||||||
|
|
||||||
if received_hmac == expected_hmac:
|
if HMAC.compare_digest(received_hmac, expected_hmac): return True
|
||||||
return True
|
else: return False
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def encrypt(self, data = None):
|
def encrypt(self, data = None):
|
||||||
|
if not isinstance(data, bytes): raise TypeError("Token plaintext input must be bytes")
|
||||||
iv = os.urandom(16)
|
iv = os.urandom(16)
|
||||||
current_time = int(time.time())
|
|
||||||
|
|
||||||
if not isinstance(data, bytes):
|
ciphertext = self.mode.encrypt(
|
||||||
raise TypeError("Token plaintext input must be bytes")
|
|
||||||
|
|
||||||
ciphertext = AES_128_CBC.encrypt(
|
|
||||||
plaintext = PKCS7.pad(data),
|
plaintext = PKCS7.pad(data),
|
||||||
key = self._encryption_key,
|
key = self._encryption_key,
|
||||||
iv = iv,
|
iv = iv)
|
||||||
)
|
|
||||||
|
|
||||||
signed_parts = iv+ciphertext
|
signed_parts = iv+ciphertext
|
||||||
|
|
||||||
return signed_parts + HMAC.new(self._signing_key, signed_parts).digest()
|
return signed_parts + HMAC.new(self._signing_key, signed_parts).digest()
|
||||||
|
|
||||||
|
|
||||||
def decrypt(self, token = None):
|
def decrypt(self, token = None):
|
||||||
if not isinstance(token, bytes):
|
if not isinstance(token, bytes): raise TypeError("Token must be bytes")
|
||||||
raise TypeError("Token must be bytes")
|
if not self.verify_hmac(token): raise ValueError("Token HMAC was invalid")
|
||||||
|
|
||||||
if not self.verify_hmac(token):
|
|
||||||
raise ValueError("Token HMAC was invalid")
|
|
||||||
|
|
||||||
iv = token[:16]
|
iv = token[:16]
|
||||||
ciphertext = token[16:-32]
|
ciphertext = token[16:-32]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
plaintext = PKCS7.unpad(
|
return PKCS7.unpad(
|
||||||
AES_128_CBC.decrypt(
|
self.mode.decrypt(
|
||||||
ciphertext,
|
ciphertext = ciphertext,
|
||||||
self._encryption_key,
|
key = self._encryption_key,
|
||||||
iv,
|
iv = iv))
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return plaintext
|
except Exception as e: raise ValueError(f"Could not decrypt token: {e}")
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Could not decrypt token")
|
|
||||||
|
|
|
||||||
|
|
@ -82,10 +82,13 @@ def _fix_secret(n):
|
||||||
n |= 64 << 8 * 31
|
n |= 64 << 8 * 31
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
def _fix_base_point(n):
|
||||||
|
n &= ~(2**255)
|
||||||
|
return n
|
||||||
|
|
||||||
def curve25519(base_point_raw, secret_raw):
|
def curve25519(base_point_raw, secret_raw):
|
||||||
"""Raise the base point to a given power"""
|
"""Raise the base point to a given power"""
|
||||||
base_point = _unpack_number(base_point_raw)
|
base_point = _fix_base_point(_unpack_number(base_point_raw))
|
||||||
secret = _fix_secret(_unpack_number(secret_raw))
|
secret = _fix_secret(_unpack_number(secret_raw))
|
||||||
return _pack_number(_raw_curve25519(base_point, secret))
|
return _pack_number(_raw_curve25519(base_point, secret))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,33 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import glob
|
import glob
|
||||||
|
|
||||||
|
|
@ -22,5 +52,7 @@ elif cp.PROVIDER == cp.PROVIDER_PYCA:
|
||||||
|
|
||||||
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
modules = py_modules+pyc_modules
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
__all__ = list(set([os.path.basename(f).replace(".pyc", "").replace(".py", "") for f in modules if not (f.endswith("__init__.py") or f.endswith("__init__.pyc"))]))
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
|
|
@ -1 +1,2 @@
|
||||||
from .aes import AES
|
from .aes128 import AES128
|
||||||
|
from .aes256 import AES256
|
||||||
|
|
@ -1,271 +0,0 @@
|
||||||
# MIT License
|
|
||||||
|
|
||||||
# Copyright (c) 2021 Or Gur Arie
|
|
||||||
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
|
||||||
# in the Software without restriction, including without limitation the rights
|
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
|
||||||
# furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
|
||||||
# copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
# SOFTWARE.
|
|
||||||
|
|
||||||
from .utils import *
|
|
||||||
|
|
||||||
|
|
||||||
class AES:
|
|
||||||
# AES-128 block size
|
|
||||||
block_size = 16
|
|
||||||
# AES-128 encrypts messages with 10 rounds
|
|
||||||
_rounds = 10
|
|
||||||
|
|
||||||
|
|
||||||
# initiate the AES objecy
|
|
||||||
def __init__(self, key):
|
|
||||||
"""
|
|
||||||
Initializes the object with a given key.
|
|
||||||
"""
|
|
||||||
# make sure key length is right
|
|
||||||
assert len(key) == AES.block_size
|
|
||||||
|
|
||||||
# ExpandKey
|
|
||||||
self._round_keys = self._expand_key(key)
|
|
||||||
|
|
||||||
|
|
||||||
# will perform the AES ExpandKey phase
|
|
||||||
def _expand_key(self, master_key):
|
|
||||||
"""
|
|
||||||
Expands and returns a list of key matrices for the given master_key.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Initialize round keys with raw key material.
|
|
||||||
key_columns = bytes2matrix(master_key)
|
|
||||||
iteration_size = len(master_key) // 4
|
|
||||||
|
|
||||||
# Each iteration has exactly as many columns as the key material.
|
|
||||||
i = 1
|
|
||||||
while len(key_columns) < (self._rounds + 1) * 4:
|
|
||||||
# Copy previous word.
|
|
||||||
word = list(key_columns[-1])
|
|
||||||
|
|
||||||
# Perform schedule_core once every "row".
|
|
||||||
if len(key_columns) % iteration_size == 0:
|
|
||||||
# Circular shift.
|
|
||||||
word.append(word.pop(0))
|
|
||||||
# Map to S-BOX.
|
|
||||||
word = [s_box[b] for b in word]
|
|
||||||
# XOR with first byte of R-CON, since the others bytes of R-CON are 0.
|
|
||||||
word[0] ^= r_con[i]
|
|
||||||
i += 1
|
|
||||||
elif len(master_key) == 32 and len(key_columns) % iteration_size == 4:
|
|
||||||
# Run word through S-box in the fourth iteration when using a
|
|
||||||
# 256-bit key.
|
|
||||||
word = [s_box[b] for b in word]
|
|
||||||
|
|
||||||
# XOR with equivalent word from previous iteration.
|
|
||||||
word = bytes(i^j for i, j in zip(word, key_columns[-iteration_size]))
|
|
||||||
key_columns.append(word)
|
|
||||||
|
|
||||||
# Group key words in 4x4 byte matrices.
|
|
||||||
return [key_columns[4*i : 4*(i+1)] for i in range(len(key_columns) // 4)]
|
|
||||||
|
|
||||||
|
|
||||||
# encrypt a single block of data with AES
|
|
||||||
def _encrypt_block(self, plaintext):
|
|
||||||
"""
|
|
||||||
Encrypts a single block of 16 byte long plaintext.
|
|
||||||
"""
|
|
||||||
# length of a single block
|
|
||||||
assert len(plaintext) == AES.block_size
|
|
||||||
|
|
||||||
# perform on a matrix
|
|
||||||
state = bytes2matrix(plaintext)
|
|
||||||
|
|
||||||
# AddRoundKey
|
|
||||||
add_round_key(state, self._round_keys[0])
|
|
||||||
|
|
||||||
# 9 main rounds
|
|
||||||
for i in range(1, self._rounds):
|
|
||||||
# SubBytes
|
|
||||||
sub_bytes(state)
|
|
||||||
# ShiftRows
|
|
||||||
shift_rows(state)
|
|
||||||
# MixCols
|
|
||||||
mix_columns(state)
|
|
||||||
# AddRoundKey
|
|
||||||
add_round_key(state, self._round_keys[i])
|
|
||||||
|
|
||||||
# last round, w/t AddRoundKey step
|
|
||||||
sub_bytes(state)
|
|
||||||
shift_rows(state)
|
|
||||||
add_round_key(state, self._round_keys[-1])
|
|
||||||
|
|
||||||
# return the encrypted matrix as bytes
|
|
||||||
return matrix2bytes(state)
|
|
||||||
|
|
||||||
|
|
||||||
# decrypt a single block of data with AES
|
|
||||||
def _decrypt_block(self, ciphertext):
|
|
||||||
"""
|
|
||||||
Decrypts a single block of 16 byte long ciphertext.
|
|
||||||
"""
|
|
||||||
# length of a single block
|
|
||||||
assert len(ciphertext) == AES.block_size
|
|
||||||
|
|
||||||
# perform on a matrix
|
|
||||||
state = bytes2matrix(ciphertext)
|
|
||||||
|
|
||||||
# in reverse order, last round is first
|
|
||||||
add_round_key(state, self._round_keys[-1])
|
|
||||||
inv_shift_rows(state)
|
|
||||||
inv_sub_bytes(state)
|
|
||||||
|
|
||||||
for i in range(self._rounds - 1, 0, -1):
|
|
||||||
# nain rounds
|
|
||||||
add_round_key(state, self._round_keys[i])
|
|
||||||
inv_mix_columns(state)
|
|
||||||
inv_shift_rows(state)
|
|
||||||
inv_sub_bytes(state)
|
|
||||||
|
|
||||||
# initial AddRoundKey phase
|
|
||||||
add_round_key(state, self._round_keys[0])
|
|
||||||
|
|
||||||
# return bytes
|
|
||||||
return matrix2bytes(state)
|
|
||||||
|
|
||||||
|
|
||||||
# will encrypt the entire data
|
|
||||||
def encrypt(self, plaintext, iv):
|
|
||||||
"""
|
|
||||||
Encrypts `plaintext` using CBC mode and PKCS#7 padding, with the given
|
|
||||||
initialization vector (iv).
|
|
||||||
"""
|
|
||||||
# iv length must be same as block size
|
|
||||||
assert len(iv) == AES.block_size
|
|
||||||
|
|
||||||
assert len(plaintext) % AES.block_size == 0
|
|
||||||
|
|
||||||
ciphertext_blocks = []
|
|
||||||
|
|
||||||
previous = iv
|
|
||||||
for plaintext_block in split_blocks(plaintext):
|
|
||||||
# in CBC mode every block is XOR'd with the previous block
|
|
||||||
xorred = xor_bytes(plaintext_block, previous)
|
|
||||||
|
|
||||||
# encrypt current block
|
|
||||||
block = self._encrypt_block(xorred)
|
|
||||||
previous = block
|
|
||||||
|
|
||||||
# append to ciphertext
|
|
||||||
ciphertext_blocks.append(block)
|
|
||||||
|
|
||||||
# return as bytes
|
|
||||||
return b''.join(ciphertext_blocks)
|
|
||||||
|
|
||||||
|
|
||||||
# will decrypt the entire data
|
|
||||||
def decrypt(self, ciphertext, iv):
|
|
||||||
"""
|
|
||||||
Decrypts `ciphertext` using CBC mode and PKCS#7 padding, with the given
|
|
||||||
initialization vector (iv).
|
|
||||||
"""
|
|
||||||
# iv length must be same as block size
|
|
||||||
assert len(iv) == AES.block_size
|
|
||||||
|
|
||||||
plaintext_blocks = []
|
|
||||||
|
|
||||||
previous = iv
|
|
||||||
for ciphertext_block in split_blocks(ciphertext):
|
|
||||||
# in CBC mode every block is XOR'd with the previous block
|
|
||||||
xorred = xor_bytes(previous, self._decrypt_block(ciphertext_block))
|
|
||||||
|
|
||||||
# append plaintext
|
|
||||||
plaintext_blocks.append(xorred)
|
|
||||||
previous = ciphertext_block
|
|
||||||
|
|
||||||
return b''.join(plaintext_blocks)
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
|
||||||
# modules and classes requiered for test only
|
|
||||||
import os
|
|
||||||
class bcolors:
|
|
||||||
OK = '\033[92m' #GREEN
|
|
||||||
WARNING = '\033[93m' #YELLOW
|
|
||||||
FAIL = '\033[91m' #RED
|
|
||||||
RESET = '\033[0m' #RESET COLOR
|
|
||||||
|
|
||||||
# will test AES class by performing an encryption / decryption
|
|
||||||
print("AES Tests")
|
|
||||||
print("=========")
|
|
||||||
|
|
||||||
# generate a secret key and print details
|
|
||||||
key = os.urandom(AES.block_size)
|
|
||||||
_aes = AES(key)
|
|
||||||
print(f"Algorithm: AES-CBC-{AES.block_size*8}")
|
|
||||||
print(f"Secret Key: {key.hex()}")
|
|
||||||
print()
|
|
||||||
|
|
||||||
# test single block encryption / decryption
|
|
||||||
iv = os.urandom(AES.block_size)
|
|
||||||
|
|
||||||
single_block_text = b"SingleBlock Text"
|
|
||||||
print("Single Block Tests")
|
|
||||||
print("------------------")
|
|
||||||
print(f"iv: {iv.hex()}")
|
|
||||||
|
|
||||||
print(f"plain text: '{single_block_text.decode()}'")
|
|
||||||
ciphertext_block = _aes._encrypt_block(single_block_text)
|
|
||||||
plaintext_block = _aes._decrypt_block(ciphertext_block)
|
|
||||||
print(f"Ciphertext Hex: {ciphertext_block.hex()}")
|
|
||||||
print(f"Plaintext: {plaintext_block.decode()}")
|
|
||||||
assert plaintext_block == single_block_text
|
|
||||||
print(bcolors.OK + "Single Block Test Passed Successfully" + bcolors.RESET)
|
|
||||||
print()
|
|
||||||
|
|
||||||
# test a less than a block length phrase
|
|
||||||
iv = os.urandom(AES.block_size)
|
|
||||||
|
|
||||||
short_text = b"Just Text"
|
|
||||||
print("Short Text Tests")
|
|
||||||
print("----------------")
|
|
||||||
print(f"iv: {iv.hex()}")
|
|
||||||
print(f"plain text: '{short_text.decode()}'")
|
|
||||||
ciphertext_short = _aes.encrypt(short_text, iv)
|
|
||||||
plaintext_short = _aes.decrypt(ciphertext_short, iv)
|
|
||||||
print(f"Ciphertext Hex: {ciphertext_short.hex()}")
|
|
||||||
print(f"Plaintext: {plaintext_short.decode()}")
|
|
||||||
assert short_text == plaintext_short
|
|
||||||
print(bcolors.OK + "Short Text Test Passed Successfully" + bcolors.RESET)
|
|
||||||
print()
|
|
||||||
|
|
||||||
# test an arbitrary length phrase
|
|
||||||
iv = os.urandom(AES.block_size)
|
|
||||||
|
|
||||||
text = b"This Text is longer than one block"
|
|
||||||
print("Arbitrary Length Tests")
|
|
||||||
print("----------------------")
|
|
||||||
print(f"iv: {iv.hex()}")
|
|
||||||
print(f"plain text: '{text.decode()}'")
|
|
||||||
ciphertext = _aes.encrypt(text, iv)
|
|
||||||
plaintext = _aes.decrypt(ciphertext, iv)
|
|
||||||
print(f"Ciphertext Hex: {ciphertext.hex()}")
|
|
||||||
print(f"Plaintext: {plaintext.decode()}")
|
|
||||||
assert text == plaintext
|
|
||||||
print(bcolors.OK + "Arbitrary Length Text Test Passed Successfully" + bcolors.RESET)
|
|
||||||
print()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# test AES class
|
|
||||||
test()
|
|
||||||
326
RNS/Cryptography/aes/aes128.py
Normal file
326
RNS/Cryptography/aes/aes128.py
Normal file
|
|
@ -0,0 +1,326 @@
|
||||||
|
# MIT License
|
||||||
|
|
||||||
|
# Copyright (c) 2021 Or Gur Arie
|
||||||
|
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
## AES lookup tables
|
||||||
|
# resource: https://en.wikipedia.org/wiki/Rijndael_S-box
|
||||||
|
s_box = (
|
||||||
|
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
|
||||||
|
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
|
||||||
|
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
|
||||||
|
0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
|
||||||
|
0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
|
||||||
|
0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
|
||||||
|
0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
|
||||||
|
0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
|
||||||
|
0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
|
||||||
|
0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
|
||||||
|
0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
|
||||||
|
0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
|
||||||
|
0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
|
||||||
|
0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
|
||||||
|
0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
|
||||||
|
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16,
|
||||||
|
)
|
||||||
|
|
||||||
|
inv_s_box = (
|
||||||
|
0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
|
||||||
|
0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
|
||||||
|
0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
|
||||||
|
0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
|
||||||
|
0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
|
||||||
|
0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
|
||||||
|
0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
|
||||||
|
0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
|
||||||
|
0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
|
||||||
|
0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
|
||||||
|
0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
|
||||||
|
0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
|
||||||
|
0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
|
||||||
|
0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
|
||||||
|
0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
|
||||||
|
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
## AES AddRoundKey
|
||||||
|
# Round constants https://en.wikipedia.org/wiki/AES_key_schedule#Round_constants
|
||||||
|
r_con = (
|
||||||
|
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40,
|
||||||
|
0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
|
||||||
|
0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
|
||||||
|
0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
|
||||||
|
)
|
||||||
|
|
||||||
|
def add_round_key(s, k):
|
||||||
|
for i in range(4):
|
||||||
|
for j in range(4):
|
||||||
|
s[i][j] ^= k[i][j]
|
||||||
|
|
||||||
|
|
||||||
|
## AES SubBytes
|
||||||
|
def sub_bytes(s):
|
||||||
|
for i in range(4):
|
||||||
|
for j in range(4):
|
||||||
|
s[i][j] = s_box[s[i][j]]
|
||||||
|
|
||||||
|
|
||||||
|
def inv_sub_bytes(s):
|
||||||
|
for i in range(4):
|
||||||
|
for j in range(4):
|
||||||
|
s[i][j] = inv_s_box[s[i][j]]
|
||||||
|
|
||||||
|
|
||||||
|
## AES ShiftRows
|
||||||
|
def shift_rows(s):
|
||||||
|
s[0][1], s[1][1], s[2][1], s[3][1] = s[1][1], s[2][1], s[3][1], s[0][1]
|
||||||
|
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
||||||
|
s[0][3], s[1][3], s[2][3], s[3][3] = s[3][3], s[0][3], s[1][3], s[2][3]
|
||||||
|
|
||||||
|
|
||||||
|
def inv_shift_rows(s):
|
||||||
|
s[0][1], s[1][1], s[2][1], s[3][1] = s[3][1], s[0][1], s[1][1], s[2][1]
|
||||||
|
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
||||||
|
s[0][3], s[1][3], s[2][3], s[3][3] = s[1][3], s[2][3], s[3][3], s[0][3]
|
||||||
|
|
||||||
|
|
||||||
|
## AES MixColumns
|
||||||
|
# learned from http://cs.ucsb.edu/~koc/cs178/projects/JT/aes.c
|
||||||
|
xtime = lambda a: (((a << 1) ^ 0x1B) & 0xFF) if (a & 0x80) else (a << 1)
|
||||||
|
|
||||||
|
|
||||||
|
def mix_single_column(a):
|
||||||
|
# see Sec 4.1.2 in The Design of Rijndael
|
||||||
|
t = a[0] ^ a[1] ^ a[2] ^ a[3]
|
||||||
|
u = a[0]
|
||||||
|
a[0] ^= t ^ xtime(a[0] ^ a[1])
|
||||||
|
a[1] ^= t ^ xtime(a[1] ^ a[2])
|
||||||
|
a[2] ^= t ^ xtime(a[2] ^ a[3])
|
||||||
|
a[3] ^= t ^ xtime(a[3] ^ u)
|
||||||
|
|
||||||
|
|
||||||
|
def mix_columns(s):
|
||||||
|
for i in range(4):
|
||||||
|
mix_single_column(s[i])
|
||||||
|
|
||||||
|
|
||||||
|
def inv_mix_columns(s):
|
||||||
|
# see Sec 4.1.3 in The Design of Rijndael
|
||||||
|
for i in range(4):
|
||||||
|
u = xtime(xtime(s[i][0] ^ s[i][2]))
|
||||||
|
v = xtime(xtime(s[i][1] ^ s[i][3]))
|
||||||
|
s[i][0] ^= u
|
||||||
|
s[i][1] ^= v
|
||||||
|
s[i][2] ^= u
|
||||||
|
s[i][3] ^= v
|
||||||
|
|
||||||
|
mix_columns(s)
|
||||||
|
|
||||||
|
## AES Bytes
|
||||||
|
def bytes2matrix(text):
|
||||||
|
""" Converts a 16-byte array into a 4x4 matrix. """
|
||||||
|
return [list(text[i:i+4]) for i in range(0, len(text), 4)]
|
||||||
|
|
||||||
|
def matrix2bytes(matrix):
|
||||||
|
""" Converts a 4x4 matrix into a 16-byte array. """
|
||||||
|
return bytes(sum(matrix, []))
|
||||||
|
|
||||||
|
|
||||||
|
def xor_bytes(a, b):
|
||||||
|
""" Returns a new byte array with the elements xor'ed. """
|
||||||
|
return bytes(i^j for i, j in zip(a, b))
|
||||||
|
|
||||||
|
|
||||||
|
def split_blocks(message, block_size=16, require_padding=True):
|
||||||
|
assert len(message) % block_size == 0 or not require_padding
|
||||||
|
return [message[i:i+16] for i in range(0, len(message), block_size)]
|
||||||
|
|
||||||
|
class AES128:
|
||||||
|
# AES-128 block size
|
||||||
|
block_size = 16
|
||||||
|
# AES-128 encrypts messages with 10 rounds
|
||||||
|
_rounds = 10
|
||||||
|
|
||||||
|
|
||||||
|
# initiate the AES objecy
|
||||||
|
def __init__(self, key):
|
||||||
|
"""
|
||||||
|
Initializes the object with a given key.
|
||||||
|
"""
|
||||||
|
# make sure key length is right
|
||||||
|
assert len(key) == AES128.block_size
|
||||||
|
|
||||||
|
# ExpandKey
|
||||||
|
self._round_keys = self._expand_key(key)
|
||||||
|
|
||||||
|
|
||||||
|
# will perform the AES ExpandKey phase
|
||||||
|
def _expand_key(self, master_key):
|
||||||
|
"""
|
||||||
|
Expands and returns a list of key matrices for the given master_key.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Initialize round keys with raw key material.
|
||||||
|
key_columns = bytes2matrix(master_key)
|
||||||
|
iteration_size = len(master_key) // 4
|
||||||
|
|
||||||
|
# Each iteration has exactly as many columns as the key material.
|
||||||
|
i = 1
|
||||||
|
while len(key_columns) < (self._rounds + 1) * 4:
|
||||||
|
# Copy previous word.
|
||||||
|
word = list(key_columns[-1])
|
||||||
|
|
||||||
|
# Perform schedule_core once every "row".
|
||||||
|
if len(key_columns) % iteration_size == 0:
|
||||||
|
# Circular shift.
|
||||||
|
word.append(word.pop(0))
|
||||||
|
# Map to S-BOX.
|
||||||
|
word = [s_box[b] for b in word]
|
||||||
|
# XOR with first byte of R-CON, since the others bytes of R-CON are 0.
|
||||||
|
word[0] ^= r_con[i]
|
||||||
|
i += 1
|
||||||
|
elif len(master_key) == 32 and len(key_columns) % iteration_size == 4:
|
||||||
|
# Run word through S-box in the fourth iteration when using a
|
||||||
|
# 256-bit key.
|
||||||
|
word = [s_box[b] for b in word]
|
||||||
|
|
||||||
|
# XOR with equivalent word from previous iteration.
|
||||||
|
word = bytes(i^j for i, j in zip(word, key_columns[-iteration_size]))
|
||||||
|
key_columns.append(word)
|
||||||
|
|
||||||
|
# Group key words in 4x4 byte matrices.
|
||||||
|
return [key_columns[4*i : 4*(i+1)] for i in range(len(key_columns) // 4)]
|
||||||
|
|
||||||
|
|
||||||
|
# encrypt a single block of data with AES
|
||||||
|
def _encrypt_block(self, plaintext):
|
||||||
|
"""
|
||||||
|
Encrypts a single block of 16 byte long plaintext.
|
||||||
|
"""
|
||||||
|
# length of a single block
|
||||||
|
assert len(plaintext) == AES128.block_size
|
||||||
|
|
||||||
|
# perform on a matrix
|
||||||
|
state = bytes2matrix(plaintext)
|
||||||
|
|
||||||
|
# AddRoundKey
|
||||||
|
add_round_key(state, self._round_keys[0])
|
||||||
|
|
||||||
|
# 9 main rounds
|
||||||
|
for i in range(1, self._rounds):
|
||||||
|
# SubBytes
|
||||||
|
sub_bytes(state)
|
||||||
|
# ShiftRows
|
||||||
|
shift_rows(state)
|
||||||
|
# MixCols
|
||||||
|
mix_columns(state)
|
||||||
|
# AddRoundKey
|
||||||
|
add_round_key(state, self._round_keys[i])
|
||||||
|
|
||||||
|
# last round, w/t AddRoundKey step
|
||||||
|
sub_bytes(state)
|
||||||
|
shift_rows(state)
|
||||||
|
add_round_key(state, self._round_keys[-1])
|
||||||
|
|
||||||
|
# return the encrypted matrix as bytes
|
||||||
|
return matrix2bytes(state)
|
||||||
|
|
||||||
|
|
||||||
|
# decrypt a single block of data with AES
|
||||||
|
def _decrypt_block(self, ciphertext):
|
||||||
|
"""
|
||||||
|
Decrypts a single block of 16 byte long ciphertext.
|
||||||
|
"""
|
||||||
|
# length of a single block
|
||||||
|
assert len(ciphertext) == AES128.block_size
|
||||||
|
|
||||||
|
# perform on a matrix
|
||||||
|
state = bytes2matrix(ciphertext)
|
||||||
|
|
||||||
|
# in reverse order, last round is first
|
||||||
|
add_round_key(state, self._round_keys[-1])
|
||||||
|
inv_shift_rows(state)
|
||||||
|
inv_sub_bytes(state)
|
||||||
|
|
||||||
|
for i in range(self._rounds - 1, 0, -1):
|
||||||
|
# nain rounds
|
||||||
|
add_round_key(state, self._round_keys[i])
|
||||||
|
inv_mix_columns(state)
|
||||||
|
inv_shift_rows(state)
|
||||||
|
inv_sub_bytes(state)
|
||||||
|
|
||||||
|
# initial AddRoundKey phase
|
||||||
|
add_round_key(state, self._round_keys[0])
|
||||||
|
|
||||||
|
# return bytes
|
||||||
|
return matrix2bytes(state)
|
||||||
|
|
||||||
|
|
||||||
|
# will encrypt the entire data
|
||||||
|
def encrypt(self, plaintext, iv):
|
||||||
|
"""
|
||||||
|
Encrypts `plaintext` using CBC mode and PKCS#7 padding, with the given
|
||||||
|
initialization vector (iv).
|
||||||
|
"""
|
||||||
|
# iv length must be same as block size
|
||||||
|
assert len(iv) == AES128.block_size
|
||||||
|
|
||||||
|
assert len(plaintext) % AES128.block_size == 0
|
||||||
|
|
||||||
|
ciphertext_blocks = []
|
||||||
|
|
||||||
|
previous = iv
|
||||||
|
for plaintext_block in split_blocks(plaintext):
|
||||||
|
# in CBC mode every block is XOR'd with the previous block
|
||||||
|
xorred = xor_bytes(plaintext_block, previous)
|
||||||
|
|
||||||
|
# encrypt current block
|
||||||
|
block = self._encrypt_block(xorred)
|
||||||
|
previous = block
|
||||||
|
|
||||||
|
# append to ciphertext
|
||||||
|
ciphertext_blocks.append(block)
|
||||||
|
|
||||||
|
# return as bytes
|
||||||
|
return b''.join(ciphertext_blocks)
|
||||||
|
|
||||||
|
|
||||||
|
# will decrypt the entire data
|
||||||
|
def decrypt(self, ciphertext, iv):
|
||||||
|
"""
|
||||||
|
Decrypts `ciphertext` using CBC mode and PKCS#7 padding, with the given
|
||||||
|
initialization vector (iv).
|
||||||
|
"""
|
||||||
|
# iv length must be same as block size
|
||||||
|
assert len(iv) == AES128.block_size
|
||||||
|
|
||||||
|
plaintext_blocks = []
|
||||||
|
|
||||||
|
previous = iv
|
||||||
|
for ciphertext_block in split_blocks(ciphertext):
|
||||||
|
# in CBC mode every block is XOR'd with the previous block
|
||||||
|
xorred = xor_bytes(previous, self._decrypt_block(ciphertext_block))
|
||||||
|
|
||||||
|
# append plaintext
|
||||||
|
plaintext_blocks.append(xorred)
|
||||||
|
previous = ciphertext_block
|
||||||
|
|
||||||
|
return b''.join(plaintext_blocks)
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
# MIT License
|
# MIT License
|
||||||
|
#
|
||||||
# Copyright (c) 2021 Or Gur Arie
|
# Copyright (c) 2024 BoppreH
|
||||||
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
# in the Software without restriction, including without limitation the rights
|
# in the Software without restriction, including without limitation the rights
|
||||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
# copies or substantial portions of the Software.
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
|
@ -20,12 +20,6 @@
|
||||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
'''
|
|
||||||
Utils class for AES encryption / decryption
|
|
||||||
'''
|
|
||||||
|
|
||||||
## AES lookup tables
|
|
||||||
# resource: https://en.wikipedia.org/wiki/Rijndael_S-box
|
|
||||||
s_box = (
|
s_box = (
|
||||||
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
|
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
|
||||||
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
|
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
|
||||||
|
|
@ -64,53 +58,33 @@ inv_s_box = (
|
||||||
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D,
|
0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
## AES AddRoundKey
|
|
||||||
# Round constants https://en.wikipedia.org/wiki/AES_key_schedule#Round_constants
|
|
||||||
r_con = (
|
|
||||||
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40,
|
|
||||||
0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
|
|
||||||
0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
|
|
||||||
0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
|
|
||||||
)
|
|
||||||
|
|
||||||
def add_round_key(s, k):
|
|
||||||
for i in range(4):
|
|
||||||
for j in range(4):
|
|
||||||
s[i][j] ^= k[i][j]
|
|
||||||
|
|
||||||
|
|
||||||
## AES SubBytes
|
|
||||||
def sub_bytes(s):
|
def sub_bytes(s):
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
for j in range(4):
|
for j in range(4):
|
||||||
s[i][j] = s_box[s[i][j]]
|
s[i][j] = s_box[s[i][j]]
|
||||||
|
|
||||||
|
|
||||||
def inv_sub_bytes(s):
|
def inv_sub_bytes(s):
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
for j in range(4):
|
for j in range(4):
|
||||||
s[i][j] = inv_s_box[s[i][j]]
|
s[i][j] = inv_s_box[s[i][j]]
|
||||||
|
|
||||||
|
|
||||||
## AES ShiftRows
|
|
||||||
def shift_rows(s):
|
def shift_rows(s):
|
||||||
s[0][1], s[1][1], s[2][1], s[3][1] = s[1][1], s[2][1], s[3][1], s[0][1]
|
s[0][1], s[1][1], s[2][1], s[3][1] = s[1][1], s[2][1], s[3][1], s[0][1]
|
||||||
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
||||||
s[0][3], s[1][3], s[2][3], s[3][3] = s[3][3], s[0][3], s[1][3], s[2][3]
|
s[0][3], s[1][3], s[2][3], s[3][3] = s[3][3], s[0][3], s[1][3], s[2][3]
|
||||||
|
|
||||||
|
|
||||||
def inv_shift_rows(s):
|
def inv_shift_rows(s):
|
||||||
s[0][1], s[1][1], s[2][1], s[3][1] = s[3][1], s[0][1], s[1][1], s[2][1]
|
s[0][1], s[1][1], s[2][1], s[3][1] = s[3][1], s[0][1], s[1][1], s[2][1]
|
||||||
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
|
||||||
s[0][3], s[1][3], s[2][3], s[3][3] = s[1][3], s[2][3], s[3][3], s[0][3]
|
s[0][3], s[1][3], s[2][3], s[3][3] = s[1][3], s[2][3], s[3][3], s[0][3]
|
||||||
|
|
||||||
|
def add_round_key(s, k):
|
||||||
|
for i in range(4):
|
||||||
|
for j in range(4):
|
||||||
|
s[i][j] ^= k[i][j]
|
||||||
|
|
||||||
## AES MixColumns
|
|
||||||
# learned from http://cs.ucsb.edu/~koc/cs178/projects/JT/aes.c
|
|
||||||
xtime = lambda a: (((a << 1) ^ 0x1B) & 0xFF) if (a & 0x80) else (a << 1)
|
xtime = lambda a: (((a << 1) ^ 0x1B) & 0xFF) if (a & 0x80) else (a << 1)
|
||||||
|
|
||||||
|
|
||||||
def mix_single_column(a):
|
def mix_single_column(a):
|
||||||
# see Sec 4.1.2 in The Design of Rijndael
|
# see Sec 4.1.2 in The Design of Rijndael
|
||||||
t = a[0] ^ a[1] ^ a[2] ^ a[3]
|
t = a[0] ^ a[1] ^ a[2] ^ a[3]
|
||||||
|
|
@ -120,12 +94,10 @@ def mix_single_column(a):
|
||||||
a[2] ^= t ^ xtime(a[2] ^ a[3])
|
a[2] ^= t ^ xtime(a[2] ^ a[3])
|
||||||
a[3] ^= t ^ xtime(a[3] ^ u)
|
a[3] ^= t ^ xtime(a[3] ^ u)
|
||||||
|
|
||||||
|
|
||||||
def mix_columns(s):
|
def mix_columns(s):
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
mix_single_column(s[i])
|
mix_single_column(s[i])
|
||||||
|
|
||||||
|
|
||||||
def inv_mix_columns(s):
|
def inv_mix_columns(s):
|
||||||
# see Sec 4.1.3 in The Design of Rijndael
|
# see Sec 4.1.3 in The Design of Rijndael
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
|
|
@ -138,22 +110,127 @@ def inv_mix_columns(s):
|
||||||
|
|
||||||
mix_columns(s)
|
mix_columns(s)
|
||||||
|
|
||||||
|
r_con = (
|
||||||
## AES Bytes
|
0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40,
|
||||||
def bytes2matrix(text):
|
0x80, 0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
|
||||||
""" Converts a 16-byte array into a 4x4 matrix. """
|
0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
|
||||||
return [list(text[i:i+4]) for i in range(0, len(text), 4)]
|
0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
|
||||||
|
)
|
||||||
def matrix2bytes(matrix):
|
|
||||||
""" Converts a 4x4 matrix into a 16-byte array. """
|
|
||||||
return bytes(sum(matrix, []))
|
|
||||||
|
|
||||||
|
|
||||||
def xor_bytes(a, b):
|
def bytes2matrix(text): return [list(text[i:i+4]) for i in range(0, len(text), 4)]
|
||||||
""" Returns a new byte array with the elements xor'ed. """
|
def matrix2bytes(matrix): return bytes(sum(matrix, []))
|
||||||
return bytes(i^j for i, j in zip(a, b))
|
def xor_bytes(a, b): return bytes(i^j for i, j in zip(a, b))
|
||||||
|
|
||||||
|
def inc_bytes(a):
|
||||||
|
out = list(a)
|
||||||
|
for i in reversed(range(len(out))):
|
||||||
|
if out[i] == 0xFF:
|
||||||
|
out[i] = 0
|
||||||
|
else:
|
||||||
|
out[i] += 1
|
||||||
|
break
|
||||||
|
return bytes(out)
|
||||||
|
|
||||||
def split_blocks(message, block_size=16, require_padding=True):
|
def split_blocks(message, block_size=16, require_padding=True):
|
||||||
assert len(message) % block_size == 0 or not require_padding
|
assert len(message) % block_size == 0 or not require_padding
|
||||||
return [message[i:i+16] for i in range(0, len(message), block_size)]
|
return [message[i:i+16] for i in range(0, len(message), block_size)]
|
||||||
|
|
||||||
|
class AES256:
|
||||||
|
rounds_by_key_size = {32: 14}
|
||||||
|
def __init__(self, master_key):
|
||||||
|
assert len(master_key) in AES256.rounds_by_key_size
|
||||||
|
self.n_rounds = AES256.rounds_by_key_size[len(master_key)]
|
||||||
|
self._key_matrices = self._expand_key(master_key)
|
||||||
|
|
||||||
|
def _expand_key(self, master_key):
|
||||||
|
# Initialize round keys with raw key material.
|
||||||
|
key_columns = bytes2matrix(master_key)
|
||||||
|
iteration_size = len(master_key) // 4
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
while len(key_columns) < (self.n_rounds + 1) * 4:
|
||||||
|
# Copy previous word.
|
||||||
|
word = list(key_columns[-1])
|
||||||
|
|
||||||
|
# Perform schedule_core once every "row".
|
||||||
|
if len(key_columns) % iteration_size == 0:
|
||||||
|
# Circular shift.
|
||||||
|
word.append(word.pop(0))
|
||||||
|
# Map to S-BOX.
|
||||||
|
word = [s_box[b] for b in word]
|
||||||
|
# XOR with first byte of R-CON, since the others bytes of R-CON are 0.
|
||||||
|
word[0] ^= r_con[i]
|
||||||
|
i += 1
|
||||||
|
elif len(master_key) == 32 and len(key_columns) % iteration_size == 4:
|
||||||
|
# Run word through S-box in the fourth iteration when using a
|
||||||
|
# 256-bit key.
|
||||||
|
word = [s_box[b] for b in word]
|
||||||
|
|
||||||
|
# XOR with equivalent word from previous iteration.
|
||||||
|
word = xor_bytes(word, key_columns[-iteration_size])
|
||||||
|
key_columns.append(word)
|
||||||
|
|
||||||
|
# Group key words in 4x4 byte matrices.
|
||||||
|
return [key_columns[4*i : 4*(i+1)] for i in range(len(key_columns) // 4)]
|
||||||
|
|
||||||
|
def encrypt_block(self, plaintext):
|
||||||
|
assert len(plaintext) == 16
|
||||||
|
|
||||||
|
plain_state = bytes2matrix(plaintext)
|
||||||
|
|
||||||
|
add_round_key(plain_state, self._key_matrices[0])
|
||||||
|
|
||||||
|
for i in range(1, self.n_rounds):
|
||||||
|
sub_bytes(plain_state)
|
||||||
|
shift_rows(plain_state)
|
||||||
|
mix_columns(plain_state)
|
||||||
|
add_round_key(plain_state, self._key_matrices[i])
|
||||||
|
|
||||||
|
sub_bytes(plain_state)
|
||||||
|
shift_rows(plain_state)
|
||||||
|
add_round_key(plain_state, self._key_matrices[-1])
|
||||||
|
|
||||||
|
return matrix2bytes(plain_state)
|
||||||
|
|
||||||
|
def decrypt_block(self, ciphertext):
|
||||||
|
assert len(ciphertext) == 16
|
||||||
|
|
||||||
|
cipher_state = bytes2matrix(ciphertext)
|
||||||
|
|
||||||
|
add_round_key(cipher_state, self._key_matrices[-1])
|
||||||
|
inv_shift_rows(cipher_state)
|
||||||
|
inv_sub_bytes(cipher_state)
|
||||||
|
|
||||||
|
for i in range(self.n_rounds - 1, 0, -1):
|
||||||
|
add_round_key(cipher_state, self._key_matrices[i])
|
||||||
|
inv_mix_columns(cipher_state)
|
||||||
|
inv_shift_rows(cipher_state)
|
||||||
|
inv_sub_bytes(cipher_state)
|
||||||
|
|
||||||
|
add_round_key(cipher_state, self._key_matrices[0])
|
||||||
|
|
||||||
|
return matrix2bytes(cipher_state)
|
||||||
|
|
||||||
|
def encrypt_cbc(self, plaintext, iv):
|
||||||
|
if len(iv) != 16: raise ValueError(f"Invalid IV length: {len(iv)}")
|
||||||
|
blocks = []
|
||||||
|
previous = iv
|
||||||
|
for plaintext_block in split_blocks(plaintext):
|
||||||
|
block = self.encrypt_block(xor_bytes(plaintext_block, previous))
|
||||||
|
blocks.append(block)
|
||||||
|
previous = block
|
||||||
|
|
||||||
|
return b''.join(blocks)
|
||||||
|
|
||||||
|
def decrypt_cbc(self, ciphertext, iv):
|
||||||
|
if len(iv) != 16: raise ValueError(f"Invalid IV length: {len(iv)}")
|
||||||
|
blocks = []
|
||||||
|
previous = iv
|
||||||
|
for ciphertext_block in split_blocks(ciphertext):
|
||||||
|
blocks.append(xor_bytes(previous, self.decrypt_block(ciphertext_block)))
|
||||||
|
previous = ciphertext_block
|
||||||
|
|
||||||
|
return b''.join(blocks)
|
||||||
|
|
||||||
|
__all__ = ["AES256"]
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2024 Mark Qvist / unsigned.io and contributors
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -27,6 +35,8 @@ import threading
|
||||||
import RNS
|
import RNS
|
||||||
|
|
||||||
from RNS.Cryptography import Token
|
from RNS.Cryptography import Token
|
||||||
|
from RNS.Stamps import generate_stamp, store_destination_stamp, load_destination_stamp
|
||||||
|
from RNS.Stamps import cost_to_bytes, quantize_cost
|
||||||
from .vendor import umsgpack as umsgpack
|
from .vendor import umsgpack as umsgpack
|
||||||
|
|
||||||
class Callbacks:
|
class Callbacks:
|
||||||
|
|
@ -138,6 +148,14 @@ class Destination:
|
||||||
|
|
||||||
return Destination.hash(identity, app_name, *aspects)
|
return Destination.hash(identity, app_name, *aspects)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def name_hash(full_name):
|
||||||
|
components = Destination.app_and_aspects_from_name(full_name)
|
||||||
|
app_name = str(components[0])
|
||||||
|
aspects = f".{'.'.join(components[1])}" if components[1] else ""
|
||||||
|
name = f"{app_name}{aspects}".encode("utf-8")
|
||||||
|
return RNS.Identity.full_hash(name)[:(RNS.Identity.NAME_HASH_LENGTH//8)]
|
||||||
|
|
||||||
def __init__(self, identity, direction, type, app_name, *aspects):
|
def __init__(self, identity, direction, type, app_name, *aspects):
|
||||||
# Check input values and build name string
|
# Check input values and build name string
|
||||||
if "." in app_name: raise ValueError("Dots can't be used in app names")
|
if "." in app_name: raise ValueError("Dots can't be used in app names")
|
||||||
|
|
@ -147,9 +165,15 @@ class Destination:
|
||||||
self.accept_link_requests = True
|
self.accept_link_requests = True
|
||||||
self.callbacks = Callbacks()
|
self.callbacks = Callbacks()
|
||||||
self.request_handlers = {}
|
self.request_handlers = {}
|
||||||
|
self.max_request_size = None
|
||||||
self.type = type
|
self.type = type
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
self.proof_strategy = Destination.PROVE_NONE
|
self.proof_strategy = Destination.PROVE_NONE
|
||||||
|
self.stamp = None
|
||||||
|
self.stamp_value = None
|
||||||
|
self.stamp_generation_lock = threading.Lock()
|
||||||
|
self._target_value = RNS.Transport.DESTINATION_VALUE
|
||||||
|
self._link_cost = None
|
||||||
self.ratchets = None
|
self.ratchets = None
|
||||||
self.ratchets_path = None
|
self.ratchets_path = None
|
||||||
self.ratchet_interval = Destination.RATCHET_INTERVAL
|
self.ratchet_interval = Destination.RATCHET_INTERVAL
|
||||||
|
|
@ -181,6 +205,13 @@ class Destination:
|
||||||
self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)]
|
self.name_hash = RNS.Identity.full_hash(self.expand_name(None, app_name, *aspects).encode("utf-8"))[:(RNS.Identity.NAME_HASH_LENGTH//8)]
|
||||||
self.hexhash = self.hash.hex()
|
self.hexhash = self.hash.hex()
|
||||||
|
|
||||||
|
# Load existing stamp if available
|
||||||
|
if self.direction == Destination.IN and self.identity != None and self.identity.prv != None and RNS.Transport.STAMP_DESTINATIONS:
|
||||||
|
stamp_entry = load_destination_stamp(self.hash, self.stamp_material(), RNS.Transport.DESTINATION_COST)
|
||||||
|
if stamp_entry != None:
|
||||||
|
self.stamp, self.stamp_value = stamp_entry
|
||||||
|
RNS.log(f"Loaded stamp with value {RNS.prettycost(self.stamp_value)} for {self}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
self.default_app_data = None
|
self.default_app_data = None
|
||||||
self.callback = None
|
self.callback = None
|
||||||
self.proofcallback = None
|
self.proofcallback = None
|
||||||
|
|
@ -192,22 +223,26 @@ class Destination:
|
||||||
"""
|
"""
|
||||||
:returns: A human-readable representation of the destination including addressable hash and full name.
|
:returns: A human-readable representation of the destination including addressable hash and full name.
|
||||||
"""
|
"""
|
||||||
return "<"+self.name+"/"+self.hexhash+">"
|
return "<"+self.name+":"+self.hexhash+">"
|
||||||
|
|
||||||
def _clean_ratchets(self):
|
def _clean_ratchets(self):
|
||||||
if self.ratchets != None:
|
if self.ratchets != None:
|
||||||
if len (self.ratchets) > self.retained_ratchets:
|
if len (self.ratchets) > self.retained_ratchets:
|
||||||
self.ratchets = self.ratchets[:Destination.RATCHET_COUNT]
|
self.ratchets = self.ratchets[:self.retained_ratchets]
|
||||||
|
|
||||||
def _persist_ratchets(self):
|
def _persist_ratchets(self):
|
||||||
try:
|
try:
|
||||||
with self.ratchet_file_lock:
|
with self.ratchet_file_lock:
|
||||||
|
temp_write_path = self.ratchets_path+".tmp"
|
||||||
packed_ratchets = umsgpack.packb(self.ratchets)
|
packed_ratchets = umsgpack.packb(self.ratchets)
|
||||||
persisted_data = {"signature": self.sign(packed_ratchets), "ratchets": packed_ratchets}
|
persisted_data = {"signature": self.sign(packed_ratchets), "ratchets": packed_ratchets}
|
||||||
ratchets_file = open(self.ratchets_path, "wb")
|
ratchets_file = open(temp_write_path, "wb")
|
||||||
ratchets_file.write(umsgpack.packb(persisted_data))
|
ratchets_file.write(umsgpack.packb(persisted_data))
|
||||||
ratchets_file.close()
|
ratchets_file.close()
|
||||||
|
if os.path.isfile(self.ratchets_path): os.unlink(self.ratchets_path)
|
||||||
|
os.rename(temp_write_path, self.ratchets_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
RNS.trace_exception(e)
|
||||||
self.ratchets = None
|
self.ratchets = None
|
||||||
self.ratchets_path = None
|
self.ratchets_path = None
|
||||||
raise OSError("Could not write ratchet file contents for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
raise OSError("Could not write ratchet file contents for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
@ -228,6 +263,26 @@ class Destination:
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _await_stamp(self):
|
||||||
|
with self.stamp_generation_lock:
|
||||||
|
if self.stamp == None or self.stamp_value == None or self.stamp_value < quantize_cost(self.target_value):
|
||||||
|
stamp_entry = load_destination_stamp(self.hash, self.stamp_material(), self.target_value)
|
||||||
|
if stamp_entry != None: self.stamp, self.stamp_value = stamp_entry
|
||||||
|
else:
|
||||||
|
stamp, stamp_value = generate_stamp(self.stamp_material(), stamp_cost=self.target_value,
|
||||||
|
expand_rounds=0, size=RNS.Identity.STAMPLENGTH)
|
||||||
|
if stamp == None:
|
||||||
|
RNS.log(f"Destination stamp generation for {RNS.prettyhexrep(self.hash)} failed or was aborted", RNS.LOG_DEBUG)
|
||||||
|
return False
|
||||||
|
|
||||||
|
self.stamp = stamp
|
||||||
|
self.stamp_value = stamp_value
|
||||||
|
store_destination_stamp(self.hash, stamp, stamp_value)
|
||||||
|
RNS.log(f"Generated and persisted stamp with value {self.stamp_value} for {self}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
if self.stamp == None: return False
|
||||||
|
else: return True
|
||||||
|
|
||||||
def announce(self, app_data=None, path_response=False, attached_interface=None, tag=None, send=True):
|
def announce(self, app_data=None, path_response=False, attached_interface=None, tag=None, send=True):
|
||||||
"""
|
"""
|
||||||
Creates an announce packet for this destination and broadcasts it on all
|
Creates an announce packet for this destination and broadcasts it on all
|
||||||
|
|
@ -236,12 +291,20 @@ class Destination:
|
||||||
:param app_data: *bytes* containing the app_data.
|
:param app_data: *bytes* containing the app_data.
|
||||||
:param path_response: Internal flag used by :ref:`RNS.Transport<api-transport>`. Ignore.
|
:param path_response: Internal flag used by :ref:`RNS.Transport<api-transport>`. Ignore.
|
||||||
"""
|
"""
|
||||||
if self.type != Destination.SINGLE:
|
if self.type != Destination.SINGLE: raise TypeError("Only SINGLE destination types can be announced")
|
||||||
raise TypeError("Only SINGLE destination types can be announced")
|
if self.direction != Destination.IN: raise TypeError("Only IN destination types can be announced")
|
||||||
|
|
||||||
if self.direction != Destination.IN:
|
if send and RNS.Transport.STAMP_DESTINATIONS and self.stamp == None:
|
||||||
raise TypeError("Only IN destination types can be announced")
|
# If no stamp is available, queue for background generation
|
||||||
|
# and announce emission once ready.
|
||||||
|
RNS.Transport.enqueue_stamped_announce(self, app_data=app_data, path_response=path_response,
|
||||||
|
attached_interface=attached_interface, tag=tag)
|
||||||
|
else:
|
||||||
|
# Otherwise, dispatch immediately.
|
||||||
|
return self._announce(app_data=app_data, path_response=path_response,
|
||||||
|
attached_interface=attached_interface, tag=tag, send=send)
|
||||||
|
|
||||||
|
def _announce(self, app_data=None, path_response=False, attached_interface=None, tag=None, send=True):
|
||||||
ratchet = b""
|
ratchet = b""
|
||||||
now = time.time()
|
now = time.time()
|
||||||
stale_responses = []
|
stale_responses = []
|
||||||
|
|
@ -281,35 +344,77 @@ class Destination:
|
||||||
returned_app_data = self.default_app_data()
|
returned_app_data = self.default_app_data()
|
||||||
if isinstance(returned_app_data, bytes):
|
if isinstance(returned_app_data, bytes):
|
||||||
app_data = returned_app_data
|
app_data = returned_app_data
|
||||||
|
|
||||||
|
if RNS.Transport.STAMP_DESTINATIONS:
|
||||||
|
if not self._await_stamp():
|
||||||
|
RNS.log(f"Could not generate destination stamp for {RNS.prettyhexrep(self.hash)}, announce emission cancelled", RNS.LOG_WARNING)
|
||||||
|
return None
|
||||||
|
|
||||||
|
cost_bytes = cost_to_bytes(self.get_link_cost())
|
||||||
|
ext_payload = bytes([RNS.Identity.EXTPAYLOADMARKER, self.get_ext_flags()])+self.stamp+bytes([cost_bytes[1], cost_bytes[0]])
|
||||||
|
|
||||||
|
if app_data == None: app_data = ext_payload
|
||||||
|
else: app_data = app_data+ext_payload
|
||||||
|
|
||||||
signed_data = self.hash+self.identity.get_public_key()+self.name_hash+random_hash+ratchet
|
signed_data = self.hash+self.identity.get_public_key()+self.name_hash+random_hash+ratchet
|
||||||
if app_data != None:
|
if app_data != None: signed_data += app_data
|
||||||
signed_data += app_data
|
|
||||||
|
|
||||||
signature = self.identity.sign(signed_data)
|
signature = self.identity.sign(signed_data)
|
||||||
announce_data = self.identity.get_public_key()+self.name_hash+random_hash+ratchet+signature
|
announce_data = self.identity.get_public_key()+self.name_hash+random_hash+ratchet+signature
|
||||||
|
|
||||||
if app_data != None:
|
if app_data != None: announce_data += app_data
|
||||||
announce_data += app_data
|
|
||||||
|
|
||||||
self.path_responses[tag] = [time.time(), announce_data]
|
self.path_responses[tag] = [time.time(), announce_data]
|
||||||
|
|
||||||
if path_response:
|
if path_response: announce_context = RNS.Packet.PATH_RESPONSE
|
||||||
announce_context = RNS.Packet.PATH_RESPONSE
|
else: announce_context = RNS.Packet.NONE
|
||||||
else:
|
|
||||||
announce_context = RNS.Packet.NONE
|
|
||||||
|
|
||||||
if ratchet:
|
if ratchet: context_flag = RNS.Packet.FLAG_SET
|
||||||
context_flag = RNS.Packet.FLAG_SET
|
else: context_flag = RNS.Packet.FLAG_UNSET
|
||||||
else:
|
|
||||||
context_flag = RNS.Packet.FLAG_UNSET
|
|
||||||
|
|
||||||
announce_packet = RNS.Packet(self, announce_data, RNS.Packet.ANNOUNCE, context = announce_context,
|
announce_packet = RNS.Packet(self, announce_data, RNS.Packet.ANNOUNCE, context = announce_context,
|
||||||
attached_interface = attached_interface, context_flag=context_flag)
|
attached_interface = attached_interface, context_flag=context_flag)
|
||||||
if send:
|
|
||||||
announce_packet.send()
|
if send: announce_packet.send()
|
||||||
else:
|
else: return announce_packet
|
||||||
return announce_packet
|
|
||||||
|
def stamp_material(self):
|
||||||
|
return self.hash+self.identity.get_public_key()+self.name_hash
|
||||||
|
|
||||||
|
def get_ext_flags(self):
|
||||||
|
return 0x00
|
||||||
|
|
||||||
|
@property
|
||||||
|
def target_value(self):
|
||||||
|
return min(RNS.Transport.STAMP_COST_CAP, max(float(self._target_value), RNS.Transport.DESTINATION_VALUE))
|
||||||
|
|
||||||
|
def set_value(self, value):
|
||||||
|
"""
|
||||||
|
Sets the target stamp value for this destination.
|
||||||
|
|
||||||
|
:param value: The target value as a *float*.
|
||||||
|
"""
|
||||||
|
self._target_value = min(RNS.Transport.STAMP_COST_CAP, max(float(value), RNS.Transport.DESTINATION_VALUE))
|
||||||
|
|
||||||
|
def set_link_cost(self, cost):
|
||||||
|
"""
|
||||||
|
Sets the required link establishment stamp cost for this destination. Set to 0 to disable.
|
||||||
|
|
||||||
|
:param cost: The required cost as a *float*.
|
||||||
|
"""
|
||||||
|
self._link_cost = max(0, min(float(cost), RNS.Transport.LINK_COST_CAP))
|
||||||
|
|
||||||
|
def get_link_cost(self):
|
||||||
|
"""
|
||||||
|
Returns the link request stamp cost to be announced for this
|
||||||
|
destination, as a float. If no destination-specific cost has
|
||||||
|
been set, the default link request cost is returned.
|
||||||
|
|
||||||
|
:returns: The announced link request cost as a *float*.
|
||||||
|
"""
|
||||||
|
cost = self._link_cost
|
||||||
|
if cost == None: cost = RNS.Transport.LR_COST
|
||||||
|
return max(0.0, min(float(cost), RNS.Transport.STAMP_COST_CAP))
|
||||||
|
|
||||||
def accepts_links(self, accepts = None):
|
def accepts_links(self, accepts = None):
|
||||||
"""
|
"""
|
||||||
|
|
@ -318,13 +423,10 @@ class Destination:
|
||||||
:param accepts: If ``True`` or ``False``, this method sets whether the destination accepts incoming link requests. If not provided or ``None``, the method returns whether the destination currently accepts link requests.
|
:param accepts: If ``True`` or ``False``, this method sets whether the destination accepts incoming link requests. If not provided or ``None``, the method returns whether the destination currently accepts link requests.
|
||||||
:returns: ``True`` or ``False`` depending on whether the destination accepts incoming link requests, if the *accepts* parameter is not provided or ``None``.
|
:returns: ``True`` or ``False`` depending on whether the destination accepts incoming link requests, if the *accepts* parameter is not provided or ``None``.
|
||||||
"""
|
"""
|
||||||
if accepts == None:
|
if accepts == None: return self.accept_link_requests
|
||||||
return self.accept_link_requests
|
|
||||||
|
|
||||||
if accepts:
|
if accepts: self.accept_link_requests = True
|
||||||
self.accept_link_requests = True
|
else: self.accept_link_requests = False
|
||||||
else:
|
|
||||||
self.accept_link_requests = False
|
|
||||||
|
|
||||||
def set_link_established_callback(self, callback):
|
def set_link_established_callback(self, callback):
|
||||||
"""
|
"""
|
||||||
|
|
@ -360,12 +462,22 @@ class Destination:
|
||||||
|
|
||||||
:param proof_strategy: One of ``RNS.Destination.PROVE_NONE``, ``RNS.Destination.PROVE_ALL`` or ``RNS.Destination.PROVE_APP``. If ``RNS.Destination.PROVE_APP`` is set, the `proof_requested_callback` will be called to determine whether a proof should be sent or not.
|
:param proof_strategy: One of ``RNS.Destination.PROVE_NONE``, ``RNS.Destination.PROVE_ALL`` or ``RNS.Destination.PROVE_APP``. If ``RNS.Destination.PROVE_APP`` is set, the `proof_requested_callback` will be called to determine whether a proof should be sent or not.
|
||||||
"""
|
"""
|
||||||
if not proof_strategy in Destination.proof_strategies:
|
if not proof_strategy in Destination.proof_strategies: raise TypeError("Unsupported proof strategy")
|
||||||
raise TypeError("Unsupported proof strategy")
|
else: self.proof_strategy = proof_strategy
|
||||||
else:
|
|
||||||
self.proof_strategy = proof_strategy
|
|
||||||
|
|
||||||
def register_request_handler(self, path, response_generator = None, allow = ALLOW_NONE, allowed_list = None):
|
def set_max_request_size(self, max_request_size):
|
||||||
|
"""
|
||||||
|
Sets the maximum accepted request size for registered request handlers.
|
||||||
|
|
||||||
|
:param max_request_size: The maximum accepted request size in bytes, as an integer.
|
||||||
|
:raises: ``TypeError`` or ``ValueError`` if any of the argument is invalid.
|
||||||
|
"""
|
||||||
|
try: max_request_size = int(max_request_size)
|
||||||
|
except: raise TypeError("Invalid maximum request size specified")
|
||||||
|
if max_request_size < 0: raise ValueError("Maximum request size cannot be negative")
|
||||||
|
self.max_request_size = max_request_size
|
||||||
|
|
||||||
|
def register_request_handler(self, path, response_generator = None, allow = ALLOW_NONE, allowed_list = None, auto_compress = True):
|
||||||
"""
|
"""
|
||||||
Registers a request handler.
|
Registers a request handler.
|
||||||
|
|
||||||
|
|
@ -373,17 +485,15 @@ class Destination:
|
||||||
:param response_generator: A function or method with the signature *response_generator(path, data, request_id, link_id, remote_identity, requested_at)* to be called. Whatever this funcion returns will be sent as a response to the requester. If the function returns ``None``, no response will be sent.
|
:param response_generator: A function or method with the signature *response_generator(path, data, request_id, link_id, remote_identity, requested_at)* to be called. Whatever this funcion returns will be sent as a response to the requester. If the function returns ``None``, no response will be sent.
|
||||||
:param allow: One of ``RNS.Destination.ALLOW_NONE``, ``RNS.Destination.ALLOW_ALL`` or ``RNS.Destination.ALLOW_LIST``. If ``RNS.Destination.ALLOW_LIST`` is set, the request handler will only respond to requests for identified peers in the supplied list.
|
:param allow: One of ``RNS.Destination.ALLOW_NONE``, ``RNS.Destination.ALLOW_ALL`` or ``RNS.Destination.ALLOW_LIST``. If ``RNS.Destination.ALLOW_LIST`` is set, the request handler will only respond to requests for identified peers in the supplied list.
|
||||||
:param allowed_list: A list of *bytes-like* :ref:`RNS.Identity<api-identity>` hashes.
|
:param allowed_list: A list of *bytes-like* :ref:`RNS.Identity<api-identity>` hashes.
|
||||||
|
:param auto_compress: If ``True`` or ``False``, determines whether automatic compression of responses should be carried out. If set to an integer value, responses will only be auto-compressed if under this size in bytes. If omitted, the default compression settings will be followed.
|
||||||
:raises: ``ValueError`` if any of the supplied arguments are invalid.
|
:raises: ``ValueError`` if any of the supplied arguments are invalid.
|
||||||
"""
|
"""
|
||||||
if path == None or path == "":
|
if path == None or path == "": raise ValueError("Invalid path specified")
|
||||||
raise ValueError("Invalid path specified")
|
elif not callable(response_generator): raise ValueError("Invalid response generator specified")
|
||||||
elif not callable(response_generator):
|
elif not allow in Destination.request_policies: raise ValueError("Invalid request policy")
|
||||||
raise ValueError("Invalid response generator specified")
|
|
||||||
elif not allow in Destination.request_policies:
|
|
||||||
raise ValueError("Invalid request policy")
|
|
||||||
else:
|
else:
|
||||||
path_hash = RNS.Identity.truncated_hash(path.encode("utf-8"))
|
path_hash = RNS.Identity.truncated_hash(path.encode("utf-8"))
|
||||||
request_handler = [path, response_generator, allow, allowed_list]
|
request_handler = [path, response_generator, allow, allowed_list, auto_compress]
|
||||||
self.request_handlers[path_hash] = request_handler
|
self.request_handlers[path_hash] = request_handler
|
||||||
|
|
||||||
def deregister_request_handler(self, path):
|
def deregister_request_handler(self, path):
|
||||||
|
|
@ -407,19 +517,20 @@ class Destination:
|
||||||
else:
|
else:
|
||||||
plaintext = self.decrypt(packet.data)
|
plaintext = self.decrypt(packet.data)
|
||||||
packet.ratchet_id = self.latest_ratchet_id
|
packet.ratchet_id = self.latest_ratchet_id
|
||||||
if plaintext != None:
|
if plaintext == None: return False
|
||||||
|
else:
|
||||||
if packet.packet_type == RNS.Packet.DATA:
|
if packet.packet_type == RNS.Packet.DATA:
|
||||||
if self.callbacks.packet != None:
|
if self.callbacks.packet != None:
|
||||||
try:
|
try: self.callbacks.packet(plaintext, packet)
|
||||||
self.callbacks.packet(plaintext, packet)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Error while executing receive callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Error while executing receive callback from "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def incoming_link_request(self, data, packet):
|
def incoming_link_request(self, data, packet):
|
||||||
if self.accept_link_requests:
|
if self.accept_link_requests:
|
||||||
link = RNS.Link.validate_request(self, data, packet)
|
link = RNS.Link.validate_request(self, data, packet)
|
||||||
if link != None:
|
if link != None: self.links.append(link)
|
||||||
self.links.append(link)
|
|
||||||
|
|
||||||
def _reload_ratchets(self, ratchets_path):
|
def _reload_ratchets(self, ratchets_path):
|
||||||
if os.path.isfile(ratchets_path):
|
if os.path.isfile(ratchets_path):
|
||||||
|
|
@ -449,7 +560,12 @@ class Destination:
|
||||||
self.ratchets = None
|
self.ratchets = None
|
||||||
self.ratchets_path = None
|
self.ratchets_path = None
|
||||||
RNS.trace_exception(e)
|
RNS.trace_exception(e)
|
||||||
|
RNS.log(f"The ratchet file located at {ratchets_path} could not be loaded. This could indicate that the ratchet file has become corrupt.", RNS.LOG_CRITICAL)
|
||||||
|
RNS.log(f"You can attempt to manually recover the ratchet file, or simply remove it to have Reticulum recreate it on the next use.", RNS.LOG_CRITICAL)
|
||||||
|
RNS.log(f"If re-initialize this ratchet file, make sure to send an announce for the relevant destination as soon as possible,", RNS.LOG_CRITICAL)
|
||||||
|
RNS.log(f"so that the new ratchet information is synchronized to the network.", RNS.LOG_CRITICAL)
|
||||||
raise OSError("Could not read ratchet file contents for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
raise OSError("Could not read ratchet file contents for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log("No existing ratchet data found, initialising new ratchet file for "+str(self), RNS.LOG_DEBUG)
|
RNS.log("No existing ratchet data found, initialising new ratchet file for "+str(self), RNS.LOG_DEBUG)
|
||||||
self.ratchets = []
|
self.ratchets = []
|
||||||
|
|
@ -475,7 +591,6 @@ class Destination:
|
||||||
self.latest_ratchet_time = 0
|
self.latest_ratchet_time = 0
|
||||||
self._reload_ratchets(ratchets_path)
|
self._reload_ratchets(ratchets_path)
|
||||||
|
|
||||||
# TODO: Remove at some point
|
|
||||||
RNS.log("Ratchets enabled on "+str(self), RNS.LOG_DEBUG)
|
RNS.log("Ratchets enabled on "+str(self), RNS.LOG_DEBUG)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -629,7 +744,7 @@ class Destination:
|
||||||
RNS.log(f"Decryption still failing after ratchet reload. The contained exception was: {e}", RNS.LOG_ERROR)
|
RNS.log(f"Decryption still failing after ratchet reload. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
RNS.log("Decryption succeeded after ratchet reload", RNS.LOG_NOTICE)
|
if decrypted: RNS.log("Decryption succeeded after ratchet reload", RNS.LOG_NOTICE)
|
||||||
|
|
||||||
return decrypted
|
return decrypted
|
||||||
|
|
||||||
|
|
|
||||||
1035
RNS/Discovery.py
Normal file
1035
RNS/Discovery.py
Normal file
File diff suppressed because it is too large
Load diff
906
RNS/Identity.py
906
RNS/Identity.py
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -70,7 +78,7 @@ class AX25KISSInterface(Interface):
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -120,7 +128,9 @@ class AX25KISSInterface(Interface):
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.flow_control = flow_control
|
self.flow_control = flow_control
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
|
self.reconnecting = False
|
||||||
self.flow_control_timeout = 5
|
self.flow_control_timeout = 5
|
||||||
self.flow_control_locked = time.time()
|
self.flow_control_locked = time.time()
|
||||||
|
|
||||||
|
|
@ -143,14 +153,18 @@ class AX25KISSInterface(Interface):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
if self.serial.is_open: self.configure_device()
|
||||||
|
else: raise IOError("Could not open serial port")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
RNS.log(f"Could not open serial port for {self}", RNS.LOG_ERROR)
|
||||||
raise e
|
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting:
|
||||||
|
thread = threading.Thread(target=self.reconnect_port)
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
if self.serial.is_open:
|
|
||||||
self.configure_device()
|
|
||||||
else:
|
|
||||||
raise IOError("Could not open serial port")
|
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
||||||
|
|
@ -168,6 +182,12 @@ class AX25KISSInterface(Interface):
|
||||||
dsrdtr = False,
|
dsrdtr = False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
try: self.serial.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close port for {self} at detach: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def configure_device(self):
|
def configure_device(self):
|
||||||
# Allow time for interface to initialise before config
|
# Allow time for interface to initialise before config
|
||||||
sleep(2.0)
|
sleep(2.0)
|
||||||
|
|
@ -246,9 +266,7 @@ class AX25KISSInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
if (len(data) > AX25.HEADER_SIZE):
|
if (len(data) > AX25.HEADER_SIZE): self.owner.inbound(data[AX25.HEADER_SIZE:], self)
|
||||||
self.rxb += len(data)
|
|
||||||
self.owner.inbound(data[AX25.HEADER_SIZE:], self)
|
|
||||||
|
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self,data):
|
||||||
|
|
@ -364,30 +382,28 @@ class AX25KISSInterface(Interface):
|
||||||
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
self.serial.close()
|
try: self.serial.close()
|
||||||
self.reconnect_port()
|
except Exception as e: RNS.log(f"Could not close port for {self}", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting: self.reconnect_port()
|
||||||
|
|
||||||
def reconnect_port(self):
|
def reconnect_port(self):
|
||||||
while not self.online:
|
self.reconnecting = True
|
||||||
|
while not self.online and not self.detached:
|
||||||
try:
|
try:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
except Exception as e: RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
self.reconnecting = False
|
||||||
|
if self.online: RNS.log("Reconnected serial port for "+str(self))
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self): return False
|
||||||
return False
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "AX25KISSInterface["+self.name+"]"
|
||||||
return "AX25KISSInterface["+self.name+"]"
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -63,7 +71,7 @@ class KISSInterface(Interface):
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
import importlib
|
import importlib.util
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
self.on_android = True
|
self.on_android = True
|
||||||
if importlib.util.find_spec('usbserial4a') != None:
|
if importlib.util.find_spec('usbserial4a') != None:
|
||||||
|
|
@ -120,9 +128,11 @@ class KISSInterface(Interface):
|
||||||
self.beacon_d = beacon_data.encode("utf-8")
|
self.beacon_d = beacon_data.encode("utf-8")
|
||||||
self.first_tx = None
|
self.first_tx = None
|
||||||
self.bitrate = KISSInterface.BITRATE_GUESS
|
self.bitrate = KISSInterface.BITRATE_GUESS
|
||||||
|
self.reconnecting = False
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.flow_control = flow_control
|
self.flow_control = flow_control
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
self.flow_control_timeout = 5
|
self.flow_control_timeout = 5
|
||||||
self.flow_control_locked = time.time()
|
self.flow_control_locked = time.time()
|
||||||
|
|
@ -140,15 +150,23 @@ class KISSInterface(Interface):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
if self.serial.is_open: self.configure_device()
|
||||||
|
else: raise IOError("Could not open serial port")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
|
RNS.log(f"Could not open serial port for {self}", RNS.LOG_ERROR)
|
||||||
raise e
|
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
||||||
if self.serial.is_open:
|
if not self.detached and not self.reconnecting:
|
||||||
self.configure_device()
|
thread = threading.Thread(target=self.reconnect_port)
|
||||||
else:
|
thread.daemon = True
|
||||||
raise IOError("Could not open serial port")
|
thread.start()
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
try: self.serial.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close port for {self} at detach: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
RNS.log("Opening serial port "+self.port+"...")
|
RNS.log("Opening serial port "+self.port+"...")
|
||||||
|
|
@ -408,24 +426,23 @@ class KISSInterface(Interface):
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
self.serial.close()
|
try: self.serial.close()
|
||||||
self.reconnect_port()
|
except Exception as e: RNS.log(f"Could not close port for {self}", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting: self.reconnect_port()
|
||||||
|
|
||||||
def reconnect_port(self):
|
def reconnect_port(self):
|
||||||
|
self.reconnecting = True
|
||||||
while not self.online:
|
while not self.online:
|
||||||
try:
|
try:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
except Exception as e: RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
self.reconnecting = False
|
||||||
|
if self.online: RNS.log("Reconnected serial port for "+str(self))
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self): return False
|
||||||
return False
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "KISSInterface["+self.name+"]"
|
||||||
return "KISSInterface["+self.name+"]"
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -24,6 +32,7 @@ from RNS.Interfaces.Interface import Interface
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import socket
|
||||||
import time
|
import time
|
||||||
import math
|
import math
|
||||||
import RNS
|
import RNS
|
||||||
|
|
@ -65,6 +74,7 @@ class KISS():
|
||||||
CMD_STAT_PHYPRM = 0x26
|
CMD_STAT_PHYPRM = 0x26
|
||||||
CMD_STAT_BAT = 0x27
|
CMD_STAT_BAT = 0x27
|
||||||
CMD_STAT_CSMA = 0x28
|
CMD_STAT_CSMA = 0x28
|
||||||
|
CMD_STAT_TEMP = 0x29
|
||||||
CMD_BLINK = 0x30
|
CMD_BLINK = 0x30
|
||||||
CMD_RANDOM = 0x40
|
CMD_RANDOM = 0x40
|
||||||
CMD_FB_EXT = 0x41
|
CMD_FB_EXT = 0x41
|
||||||
|
|
@ -272,12 +282,9 @@ class RNodeInterface(Interface):
|
||||||
if (port != None or device_serial != None) and (enable_bluetooth or disable_bluetooth or pairing_mode):
|
if (port != None or device_serial != None) and (enable_bluetooth or disable_bluetooth or pairing_mode):
|
||||||
serial = None
|
serial = None
|
||||||
bluetooth_state = None
|
bluetooth_state = None
|
||||||
if pairing_mode:
|
if pairing_mode: bluetooth_state = 0x01
|
||||||
bluetooth_state = 0x01
|
elif enable_bluetooth: bluetooth_state = 0x01
|
||||||
elif enable_bluetooth:
|
elif disable_bluetooth: bluetooth_state = 0x00
|
||||||
bluetooth_state = 0x01
|
|
||||||
elif disable_bluetooth:
|
|
||||||
bluetooth_state = 0x00
|
|
||||||
|
|
||||||
if port != None:
|
if port != None:
|
||||||
RNS.log("Opening serial port "+port+"...")
|
RNS.log("Opening serial port "+port+"...")
|
||||||
|
|
@ -355,7 +362,9 @@ class RNodeInterface(Interface):
|
||||||
target_device_address = c["target_device_address"] if "target_device_address" in c else None
|
target_device_address = c["target_device_address"] if "target_device_address" in c else None
|
||||||
ble_name = c["ble_name"] if "ble_name" in c else None
|
ble_name = c["ble_name"] if "ble_name" in c else None
|
||||||
ble_addr = c["ble_addr"] if "ble_addr" in c else None
|
ble_addr = c["ble_addr"] if "ble_addr" in c else None
|
||||||
|
tcp_host = c["tcp_host"] if "tcp_host" in c else None
|
||||||
force_ble = c["force_ble"] if "force_ble" in c else False
|
force_ble = c["force_ble"] if "force_ble" in c else False
|
||||||
|
force_tcp = c["force_tcp"] if "force_tcp" in c else False
|
||||||
frequency = int(c["frequency"]) if "frequency" in c else 0
|
frequency = int(c["frequency"]) if "frequency" in c else 0
|
||||||
bandwidth = int(c["bandwidth"]) if "bandwidth" in c else 0
|
bandwidth = int(c["bandwidth"]) if "bandwidth" in c else 0
|
||||||
txpower = int(c["txpower"]) if "txpower" in c else 0
|
txpower = int(c["txpower"]) if "txpower" in c else 0
|
||||||
|
|
@ -368,7 +377,7 @@ class RNodeInterface(Interface):
|
||||||
lt_alock = float(c["airtime_limit_long"]) if "airtime_limit_long" in c and c["airtime_limit_long"] != None else None
|
lt_alock = float(c["airtime_limit_long"]) if "airtime_limit_long" in c and c["airtime_limit_long"] != None else None
|
||||||
port = c["port"] if "port" in c else None
|
port = c["port"] if "port" in c else None
|
||||||
|
|
||||||
import importlib
|
import importlib.util
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
self.on_android = True
|
self.on_android = True
|
||||||
if importlib.util.find_spec('usbserial4a') != None:
|
if importlib.util.find_spec('usbserial4a') != None:
|
||||||
|
|
@ -427,6 +436,14 @@ class RNodeInterface(Interface):
|
||||||
self.ble_rx_queue= b""
|
self.ble_rx_queue= b""
|
||||||
self.ble_tx_queue= b""
|
self.ble_tx_queue= b""
|
||||||
|
|
||||||
|
self.tcp = None
|
||||||
|
self.use_tcp = False
|
||||||
|
self.tcp_host = tcp_host
|
||||||
|
self.tcp_rx_queue= b""
|
||||||
|
self.tcp_tx_queue= b""
|
||||||
|
self.tcp_rx_lock = threading.Lock()
|
||||||
|
self.tcp_tx_lock = threading.Lock()
|
||||||
|
|
||||||
self.frequency = frequency
|
self.frequency = frequency
|
||||||
self.bandwidth = bandwidth
|
self.bandwidth = bandwidth
|
||||||
self.txpower = txpower
|
self.txpower = txpower
|
||||||
|
|
@ -436,6 +453,7 @@ class RNodeInterface(Interface):
|
||||||
self.bitrate = 0
|
self.bitrate = 0
|
||||||
self.st_alock = st_alock
|
self.st_alock = st_alock
|
||||||
self.lt_alock = lt_alock
|
self.lt_alock = lt_alock
|
||||||
|
self.cpu_temp = None
|
||||||
self.platform = None
|
self.platform = None
|
||||||
self.display = None
|
self.display = None
|
||||||
self.mcu = None
|
self.mcu = None
|
||||||
|
|
@ -448,6 +466,7 @@ class RNodeInterface(Interface):
|
||||||
self.first_tx = None
|
self.first_tx = None
|
||||||
self.reconnect_w = RNodeInterface.RECONNECT_WAIT
|
self.reconnect_w = RNodeInterface.RECONNECT_WAIT
|
||||||
self.reconnect_lock = threading.Lock()
|
self.reconnect_lock = threading.Lock()
|
||||||
|
self.awaiting_ble_reset = False
|
||||||
|
|
||||||
self.r_frequency = None
|
self.r_frequency = None
|
||||||
self.r_bandwidth = None
|
self.r_bandwidth = None
|
||||||
|
|
@ -478,6 +497,10 @@ class RNodeInterface(Interface):
|
||||||
self.r_csma_cw_max = None
|
self.r_csma_cw_max = None
|
||||||
self.r_current_rssi = None
|
self.r_current_rssi = None
|
||||||
self.r_noise_floor = None
|
self.r_noise_floor = None
|
||||||
|
self.r_interference = None
|
||||||
|
self.r_interference_l = None
|
||||||
|
self.r_temperature = None
|
||||||
|
self.reports_phy_stats = True
|
||||||
|
|
||||||
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
||||||
self.r_battery_percent = 0
|
self.r_battery_percent = 0
|
||||||
|
|
@ -493,21 +516,22 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.flow_control = flow_control
|
self.flow_control = flow_control
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
self.announce_rate_target = None
|
self.announce_rate_target = None
|
||||||
self.last_port_io = 0
|
self.last_port_io = 0
|
||||||
self.port_io_timeout = RNodeInterface.PORT_IO_TIMEOUT
|
self.port_io_timeout = RNodeInterface.PORT_IO_TIMEOUT
|
||||||
self.last_imagedata = None
|
self.last_imagedata = None
|
||||||
|
|
||||||
if force_ble or self.ble_addr != None or self.ble_name != None:
|
if force_ble or self.ble_addr != None or self.ble_name != None: self.use_ble = True
|
||||||
self.use_ble = True
|
if force_tcp or self.tcp_host != None: self.use_tcp = True
|
||||||
|
|
||||||
self.validcfg = True
|
self.validcfg = True
|
||||||
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
|
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
|
||||||
RNS.log("Invalid frequency configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid frequency configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
if (self.txpower < 0 or self.txpower > 22):
|
if (self.txpower < 0 or self.txpower > 37):
|
||||||
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
|
|
@ -550,10 +574,8 @@ class RNodeInterface(Interface):
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
|
||||||
if self.serial != None:
|
if self.serial != None:
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
else: raise IOError("Could not open serial port")
|
||||||
else:
|
|
||||||
raise IOError("Could not open serial port")
|
|
||||||
elif self.bt_manager != None:
|
elif self.bt_manager != None:
|
||||||
if self.bt_manager.connected:
|
if self.bt_manager.connected:
|
||||||
self.configure_device()
|
self.configure_device()
|
||||||
|
|
@ -571,12 +593,9 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def read_mux(self, len=None):
|
def read_mux(self, len=None):
|
||||||
if self.serial != None:
|
if self.serial != None: return self.serial.read()
|
||||||
return self.serial.read()
|
elif self.bt_manager != None: return self.bt_manager.read()
|
||||||
elif self.bt_manager != None:
|
else: raise IOError("No ports available for reading")
|
||||||
return self.bt_manager.read()
|
|
||||||
else:
|
|
||||||
raise IOError("No ports available for reading")
|
|
||||||
|
|
||||||
def write_mux(self, data):
|
def write_mux(self, data):
|
||||||
if self.serial != None:
|
if self.serial != None:
|
||||||
|
|
@ -591,17 +610,19 @@ class RNodeInterface(Interface):
|
||||||
else:
|
else:
|
||||||
raise IOError("No ports available for writing")
|
raise IOError("No ports available for writing")
|
||||||
|
|
||||||
# def reset_ble(self):
|
def reset_ble(self):
|
||||||
# RNS.log(f"Clearing previous connection instance: "+str(self.ble))
|
if not self.awaiting_ble_reset: return
|
||||||
# del self.ble
|
else:
|
||||||
# self.ble = None
|
RNS.log(f"Clearing previous connection instance: "+str(self.ble), RNS.LOG_DEBUG)
|
||||||
# self.serial = None
|
self.ble = None
|
||||||
# self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
self.serial = None
|
||||||
# self.serial = self.ble
|
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
||||||
# RNS.log(f"New connection instance: "+str(self.ble))
|
self.serial = self.ble
|
||||||
|
self.awaiting_ble_reset = False
|
||||||
|
RNS.log(f"New connection instance: "+str(self.ble), RNS.LOG_DEBUG)
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
if not self.use_ble:
|
if not self.use_ble and not self.use_tcp:
|
||||||
if self.port != None:
|
if self.port != None:
|
||||||
RNS.log("Opening serial port "+self.port+"...")
|
RNS.log("Opening serial port "+self.port+"...")
|
||||||
# Get device parameters
|
# Get device parameters
|
||||||
|
|
@ -669,7 +690,7 @@ class RNodeInterface(Interface):
|
||||||
if self.bt_manager != None:
|
if self.bt_manager != None:
|
||||||
self.bt_manager.connect_any_device()
|
self.bt_manager.connect_any_device()
|
||||||
|
|
||||||
else:
|
elif self.use_ble:
|
||||||
if self.ble == None:
|
if self.ble == None:
|
||||||
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
||||||
self.serial = self.ble
|
self.serial = self.ble
|
||||||
|
|
@ -678,6 +699,24 @@ class RNodeInterface(Interface):
|
||||||
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
elif self.use_tcp:
|
||||||
|
RNS.log(f"Opening TCP connection for {self}...")
|
||||||
|
if self.tcp != None and self.tcp.running == False:
|
||||||
|
self.tcp.close()
|
||||||
|
self.tcp.cleanup()
|
||||||
|
self.tcp = None
|
||||||
|
|
||||||
|
if self.tcp == None:
|
||||||
|
self.tcp = TCPConnection(owner=self, target_host=self.tcp_host)
|
||||||
|
self.serial = self.tcp
|
||||||
|
|
||||||
|
open_time = time.time()
|
||||||
|
while not self.tcp.connected and time.time() < open_time + self.tcp.CONNECT_TIMEOUT:
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise TypeError("No valid device connection type defined for RNode interface")
|
||||||
|
|
||||||
|
|
||||||
def configure_device(self):
|
def configure_device(self):
|
||||||
self.resetRadioState()
|
self.resetRadioState()
|
||||||
|
|
@ -685,17 +724,19 @@ class RNodeInterface(Interface):
|
||||||
thread = threading.Thread(target=self.readLoop, daemon=True).start()
|
thread = threading.Thread(target=self.readLoop, daemon=True).start()
|
||||||
|
|
||||||
self.detect()
|
self.detect()
|
||||||
if not self.use_ble:
|
if self.use_tcp:
|
||||||
sleep(0.5)
|
tcp_detect_timeout = 5.0
|
||||||
else:
|
detect_time = time.time()
|
||||||
|
while not self.detected and time.time() < detect_time + tcp_detect_timeout: time.sleep(0.1)
|
||||||
|
if not self.detected: RNS.log(f"RNode detect timed out over TCP", RNS.LOG_ERROR)
|
||||||
|
elif self.use_ble:
|
||||||
ble_detect_timeout = 5
|
ble_detect_timeout = 5
|
||||||
detect_time = time.time()
|
detect_time = time.time()
|
||||||
while not self.detected and time.time() < detect_time + ble_detect_timeout:
|
while not self.detected and time.time() < detect_time + ble_detect_timeout: time.sleep(0.1)
|
||||||
time.sleep(0.1)
|
if self.detected: detect_time = RNS.prettytime(time.time()-detect_time)
|
||||||
if self.detected:
|
else: RNS.log(f"RNode detect timed out over {self.port}", RNS.LOG_ERROR)
|
||||||
detect_time = RNS.prettytime(time.time()-detect_time)
|
else:
|
||||||
else:
|
sleep(0.2)
|
||||||
RNS.log(f"RNode detect timed out over {self.port}", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
if not self.detected:
|
if not self.detected:
|
||||||
raise IOError("Could not detect device")
|
raise IOError("Could not detect device")
|
||||||
|
|
@ -708,11 +749,19 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
if self.serial != None and self.port != None:
|
if self.serial != None and self.port != None:
|
||||||
self.timeout = 200
|
self.timeout = 200
|
||||||
RNS.log("Serial port "+self.port+" is now open")
|
RNS.log(f"Serial port {self.port} is now open")
|
||||||
|
|
||||||
if self.bt_manager != None and self.bt_manager.connected:
|
if self.bt_manager != None and self.bt_manager.connected:
|
||||||
self.timeout = 1500
|
self.timeout = 1500
|
||||||
RNS.log("Bluetooth connection to RNode now open")
|
RNS.log(f"Bluetooth connection to RNode now open")
|
||||||
|
|
||||||
|
if self.ble != None and self.ble.connected:
|
||||||
|
self.timeout = 1500
|
||||||
|
RNS.log(f"BLE connection {self.port} to RNode now open")
|
||||||
|
|
||||||
|
if self.tcp != None and self.tcp.connected:
|
||||||
|
self.timeout = 1500
|
||||||
|
RNS.log(f"TCP connection tcp://{self.tcp_host} to RNode now open")
|
||||||
|
|
||||||
RNS.log("Configuring RNode interface...", RNS.LOG_VERBOSE)
|
RNS.log("Configuring RNode interface...", RNS.LOG_VERBOSE)
|
||||||
self.initRadio()
|
self.initRadio()
|
||||||
|
|
@ -964,10 +1013,8 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
def validateRadioState(self):
|
def validateRadioState(self):
|
||||||
RNS.log("Waiting for radio configuration validation for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Waiting for radio configuration validation for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
if not self.platform == KISS.PLATFORM_ESP32:
|
if not self.platform == KISS.PLATFORM_ESP32: sleep(1.00);
|
||||||
sleep(1.00);
|
else: sleep(2.00);
|
||||||
else:
|
|
||||||
sleep(2.00);
|
|
||||||
|
|
||||||
self.validcfg = True
|
self.validcfg = True
|
||||||
if (self.r_frequency != None and abs(self.frequency - int(self.r_frequency)) > 100):
|
if (self.r_frequency != None and abs(self.frequency - int(self.r_frequency)) > 100):
|
||||||
|
|
@ -1264,10 +1311,12 @@ class RNodeInterface(Interface):
|
||||||
self.r_channel_load_long = cul/100.0
|
self.r_channel_load_long = cul/100.0
|
||||||
self.r_current_rssi = crs-RNodeInterface.RSSI_OFFSET
|
self.r_current_rssi = crs-RNodeInterface.RSSI_OFFSET
|
||||||
self.r_noise_floor = nfl-RNodeInterface.RSSI_OFFSET
|
self.r_noise_floor = nfl-RNodeInterface.RSSI_OFFSET
|
||||||
|
|
||||||
if ntf == 0xFF:
|
if ntf == 0xFF:
|
||||||
self.r_interference = None
|
self.r_interference = None
|
||||||
else:
|
else:
|
||||||
self.r_interference = ntf-RNodeInterface.RSSI_OFFSET
|
self.r_interference = ntf-RNodeInterface.RSSI_OFFSET
|
||||||
|
self.r_interference_l = [time.time(), self.r_interference]
|
||||||
|
|
||||||
if self.r_interference != None:
|
if self.r_interference != None:
|
||||||
RNS.log(f"{self} Radio detected interference at {self.r_interference} dBm", RNS.LOG_DEBUG)
|
RNS.log(f"{self} Radio detected interference at {self.r_interference} dBm", RNS.LOG_DEBUG)
|
||||||
|
|
@ -1347,6 +1396,22 @@ class RNodeInterface(Interface):
|
||||||
bat_percent = 0
|
bat_percent = 0
|
||||||
self.r_battery_state = command_buffer[0]
|
self.r_battery_state = command_buffer[0]
|
||||||
self.r_battery_percent = bat_percent
|
self.r_battery_percent = bat_percent
|
||||||
|
elif (command == KISS.CMD_STAT_TEMP):
|
||||||
|
if (byte == KISS.FESC):
|
||||||
|
escape = True
|
||||||
|
else:
|
||||||
|
if (escape):
|
||||||
|
if (byte == KISS.TFEND):
|
||||||
|
byte = KISS.FEND
|
||||||
|
if (byte == KISS.TFESC):
|
||||||
|
byte = KISS.FESC
|
||||||
|
escape = False
|
||||||
|
command_buffer = command_buffer+bytes([byte])
|
||||||
|
if (len(command_buffer) == 1):
|
||||||
|
temp = command_buffer[0]-120
|
||||||
|
if temp >= -30 and temp <= 90: self.r_temperature = temp
|
||||||
|
else: self.r_temperature = None
|
||||||
|
self.cpu_temp = self.r_temperature
|
||||||
elif (command == KISS.CMD_RANDOM):
|
elif (command == KISS.CMD_RANDOM):
|
||||||
self.r_random = byte
|
self.r_random = byte
|
||||||
elif (command == KISS.CMD_PLATFORM):
|
elif (command == KISS.CMD_PLATFORM):
|
||||||
|
|
@ -1417,7 +1482,7 @@ class RNodeInterface(Interface):
|
||||||
if got == 0:
|
if got == 0:
|
||||||
time_since_last = int(time.time()*1000) - last_read_ms
|
time_since_last = int(time.time()*1000) - last_read_ms
|
||||||
if len(data_buffer) > 0 and time_since_last > self.timeout:
|
if len(data_buffer) > 0 and time_since_last > self.timeout:
|
||||||
RNS.log(str(self)+" serial read timeout in command "+str(command), RNS.LOG_WARNING)
|
RNS.log(f"{self} device read timeout in command {command} after {RNS.prettytime(self.timeout/1000.0)}", RNS.LOG_WARNING)
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
in_frame = False
|
in_frame = False
|
||||||
command = KISS.CMD_UNKNOWN
|
command = KISS.CMD_UNKNOWN
|
||||||
|
|
@ -1428,25 +1493,25 @@ class RNodeInterface(Interface):
|
||||||
if time.time() > self.first_tx + self.id_interval:
|
if time.time() > self.first_tx + self.id_interval:
|
||||||
RNS.log("Interface "+str(self)+" is transmitting beacon data: "+str(self.id_callsign.decode("utf-8")), RNS.LOG_DEBUG)
|
RNS.log("Interface "+str(self)+" is transmitting beacon data: "+str(self.id_callsign.decode("utf-8")), RNS.LOG_DEBUG)
|
||||||
self.process_outgoing(self.id_callsign)
|
self.process_outgoing(self.id_callsign)
|
||||||
|
|
||||||
if (time.time() - self.last_port_io > self.port_io_timeout):
|
|
||||||
self.detect()
|
|
||||||
|
|
||||||
if (time.time() - self.last_port_io > self.port_io_timeout*3):
|
|
||||||
raise IOError("Connected port for "+str(self)+" became unresponsive")
|
|
||||||
|
|
||||||
if self.bt_manager != None:
|
if self.use_tcp:
|
||||||
sleep(0.08)
|
if self.tcp and self.tcp.connected:
|
||||||
|
if time.time() > self.tcp.last_write + self.tcp.ACTIVITY_KEEPALIVE:
|
||||||
|
self.detect()
|
||||||
|
|
||||||
|
if (time.time() - self.last_port_io > self.port_io_timeout): self.detect()
|
||||||
|
if (time.time() - self.last_port_io > self.port_io_timeout*3): raise IOError("Connected port for "+str(self)+" became unresponsive")
|
||||||
|
if self.bt_manager != None or self.ble != None: sleep(0.08)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.online = False
|
self.online = False
|
||||||
RNS.log("A serial port occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
if not self.detached:
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
|
|
||||||
|
|
@ -1498,12 +1563,22 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
self.detached = True
|
self.detached = True
|
||||||
self.disable_external_framebuffer()
|
try:
|
||||||
self.setRadioState(KISS.RADIO_STATE_OFF)
|
self.disable_external_framebuffer()
|
||||||
self.leave()
|
self.setRadioState(KISS.RADIO_STATE_OFF)
|
||||||
|
self.leave()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"An error occurred while detaching {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if self.use_ble:
|
if self.use_ble:
|
||||||
self.ble.close()
|
self.ble.close()
|
||||||
|
self.ble.cleanup()
|
||||||
|
elif self.use_tcp:
|
||||||
|
time.sleep(0.5)
|
||||||
|
self.tcp.close()
|
||||||
|
else:
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self):
|
||||||
return False
|
return False
|
||||||
|
|
@ -1524,6 +1599,17 @@ class RNodeInterface(Interface):
|
||||||
def get_battery_percent(self):
|
def get_battery_percent(self):
|
||||||
return self.r_battery_percent
|
return self.r_battery_percent
|
||||||
|
|
||||||
|
def tcp_receive(self, data):
|
||||||
|
with self.tcp_rx_lock: self.tcp_rx_queue += data
|
||||||
|
|
||||||
|
def tcp_waiting(self): return len(self.tcp_tx_queue) > 0
|
||||||
|
|
||||||
|
def get_tcp_waiting(self, n):
|
||||||
|
with self.tcp_tx_lock:
|
||||||
|
data = self.tcp_tx_queue[:n]
|
||||||
|
self.tcp_tx_queue = self.tcp_tx_queue[n:]
|
||||||
|
return data
|
||||||
|
|
||||||
def ble_receive(self, data):
|
def ble_receive(self, data):
|
||||||
with self.ble_rx_lock:
|
with self.ble_rx_lock:
|
||||||
self.ble_rx_queue += data
|
self.ble_rx_queue += data
|
||||||
|
|
@ -1538,7 +1624,7 @@ class RNodeInterface(Interface):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "RNodeInterface["+str(self.name)+"]"
|
return f"RNodeInterface[{self.name}]"
|
||||||
|
|
||||||
class BLEConnection(BluetoothDispatcher):
|
class BLEConnection(BluetoothDispatcher):
|
||||||
UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
|
UART_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
|
||||||
|
|
@ -1550,7 +1636,7 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
|
|
||||||
MTU_TIMEOUT = 4.0
|
MTU_TIMEOUT = 4.0
|
||||||
CONNECT_TIMEOUT = 7.0
|
CONNECT_TIMEOUT = 7.0
|
||||||
RECONNECT_WAIT = 1.0
|
RECONNECT_WAIT = 2.5
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_open(self):
|
def is_open(self):
|
||||||
|
|
@ -1571,6 +1657,9 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
self.owner.ble_rx_queue = b""
|
self.owner.ble_rx_queue = b""
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
self.should_run = False
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
try:
|
try:
|
||||||
if self.connected:
|
if self.connected:
|
||||||
|
|
@ -1651,13 +1740,17 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
self.write_thread = None
|
self.write_thread = None
|
||||||
|
|
||||||
def connection_job(self):
|
def connection_job(self):
|
||||||
|
ble_devices = []
|
||||||
while self.should_run:
|
while self.should_run:
|
||||||
if self.bt_manager.bt_enabled():
|
if self.bt_manager.bt_enabled():
|
||||||
if self.ble_device == None:
|
if not self.connected:
|
||||||
self.ble_device = self.find_target_device()
|
if len(ble_devices) == 0:
|
||||||
|
ble_devices = self.find_target_devices()
|
||||||
|
|
||||||
|
if len(ble_devices) > 0: self.ble_device = ble_devices.pop()
|
||||||
|
else: self.ble_device = None
|
||||||
|
|
||||||
if self.ble_device != None:
|
if self.ble_device != None:
|
||||||
if not self.connected:
|
|
||||||
if self.was_connected:
|
if self.was_connected:
|
||||||
RNS.log(f"Throttling BLE reconnect for {BLEConnection.RECONNECT_WAIT} seconds", RNS.LOG_DEBUG)
|
RNS.log(f"Throttling BLE reconnect for {BLEConnection.RECONNECT_WAIT} seconds", RNS.LOG_DEBUG)
|
||||||
time.sleep(BLEConnection.RECONNECT_WAIT)
|
time.sleep(BLEConnection.RECONNECT_WAIT)
|
||||||
|
|
@ -1669,7 +1762,7 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
RNS.log("Bluetooth was disabled, closing active BLE device connection", RNS.LOG_ERROR)
|
RNS.log("Bluetooth was disabled, closing active BLE device connection", RNS.LOG_ERROR)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
time.sleep(2)
|
time.sleep(1)
|
||||||
|
|
||||||
def connect_device(self):
|
def connect_device(self):
|
||||||
if self.ble_device != None and self.bt_manager.bt_enabled():
|
if self.ble_device != None and self.bt_manager.bt_enabled():
|
||||||
|
|
@ -1687,12 +1780,15 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
else:
|
else:
|
||||||
RNS.log(f"BLE device connection timed out for {self.owner}", RNS.LOG_DEBUG)
|
RNS.log(f"BLE device connection timed out for {self.owner}", RNS.LOG_DEBUG)
|
||||||
if self.mtu_requested_time:
|
if self.mtu_requested_time:
|
||||||
RNS.log("MTU update timeout, tearing down connection")
|
RNS.log("MTU update timeout, tearing down connection and resetting BLE dispatcher")
|
||||||
self.owner.hw_errors.append({"error": KISS.ERROR_INVALID_BLE_MTU, "description": "The Bluetooth Low Energy transfer MTU could not be configured for the connected device, and communication has failed. Restart Reticulum and any connected applications to retry connecting."})
|
self.owner.hw_errors.append({"error": KISS.ERROR_INVALID_BLE_MTU, "description": "The Bluetooth Low Energy transfer MTU could not be configured for the connected device, and communication has failed. Restart Reticulum and any connected applications to retry connecting."})
|
||||||
self.close()
|
self.close()
|
||||||
|
self.close_gatt()
|
||||||
self.should_run = False
|
self.should_run = False
|
||||||
|
self.owner.awaiting_ble_reset = True
|
||||||
self.close_gatt()
|
|
||||||
|
else:
|
||||||
|
self.close_gatt()
|
||||||
|
|
||||||
self.connect_job_running = False
|
self.connect_job_running = False
|
||||||
|
|
||||||
|
|
@ -1702,15 +1798,17 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
self.ble_device = None
|
self.ble_device = None
|
||||||
self.close_gatt()
|
self.close_gatt()
|
||||||
|
|
||||||
def find_target_device(self):
|
def find_target_devices(self):
|
||||||
found_device = None
|
found_device = None
|
||||||
potential_devices = self.bt_manager.get_paired_devices()
|
potential_devices = self.bt_manager.get_paired_devices()
|
||||||
|
suitable_devices = []
|
||||||
|
|
||||||
if self.target_bt_addr != None:
|
if self.target_bt_addr != None:
|
||||||
for device in potential_devices:
|
for device in potential_devices:
|
||||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||||
if str(device.getAddress()).replace(":", "").lower() == str(self.target_bt_addr).replace(":", "").lower():
|
if str(device.getAddress()).replace(":", "").lower() == str(self.target_bt_addr).replace(":", "").lower():
|
||||||
found_device = device
|
found_device = device
|
||||||
|
suitable_devices.append(device)
|
||||||
break
|
break
|
||||||
|
|
||||||
if not found_device and self.target_name != None:
|
if not found_device and self.target_name != None:
|
||||||
|
|
@ -1718,6 +1816,7 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||||
if device.getName().lower() == self.target_name.lower():
|
if device.getName().lower() == self.target_name.lower():
|
||||||
found_device = device
|
found_device = device
|
||||||
|
suitable_devices.append(device)
|
||||||
break
|
break
|
||||||
|
|
||||||
if not found_device:
|
if not found_device:
|
||||||
|
|
@ -1725,9 +1824,9 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
if (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_LE) or (device.getType() == AndroidBluetoothManager.DEVICE_TYPE_DUAL):
|
||||||
if device.getName().startswith("RNode "):
|
if device.getName().startswith("RNode "):
|
||||||
found_device = device
|
found_device = device
|
||||||
break
|
suitable_devices.append(device)
|
||||||
|
|
||||||
return found_device
|
return suitable_devices
|
||||||
|
|
||||||
def on_connection_state_change(self, status, state):
|
def on_connection_state_change(self, status, state):
|
||||||
if status == GATT_SUCCESS and state:
|
if status == GATT_SUCCESS and state:
|
||||||
|
|
@ -1771,4 +1870,139 @@ class BLEConnection(BluetoothDispatcher):
|
||||||
def on_characteristic_changed(self, characteristic):
|
def on_characteristic_changed(self, characteristic):
|
||||||
if characteristic.getUuid().toString() == BLEConnection.UART_TX_CHAR_UUID:
|
if characteristic.getUuid().toString() == BLEConnection.UART_TX_CHAR_UUID:
|
||||||
recvd = bytes(characteristic.getValue())
|
recvd = bytes(characteristic.getValue())
|
||||||
self.owner.ble_receive(recvd)
|
self.owner.ble_receive(recvd)
|
||||||
|
|
||||||
|
class TCPConnection():
|
||||||
|
TARGET_PORT = 7633
|
||||||
|
CONNECT_TIMEOUT = 5.0
|
||||||
|
INITIAL_CONNECT_TIMEOUT = 5.0
|
||||||
|
RECONNECT_WAIT = 4.0
|
||||||
|
ACTIVITY_TIMEOUT = 6.0
|
||||||
|
ACTIVITY_KEEPALIVE = ACTIVITY_TIMEOUT-2.5
|
||||||
|
|
||||||
|
TCP_USER_TIMEOUT = 24
|
||||||
|
TCP_PROBE_AFTER = 5
|
||||||
|
TCP_PROBE_INTERVAL = 2
|
||||||
|
TCP_PROBES = 12
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_open(self):
|
||||||
|
return self.connected
|
||||||
|
|
||||||
|
@property
|
||||||
|
def in_waiting(self):
|
||||||
|
buflen = len(self.owner.tcp_rx_queue)
|
||||||
|
return buflen > 0
|
||||||
|
|
||||||
|
def write(self, data_bytes):
|
||||||
|
if self.connected and self.socket:
|
||||||
|
with self.owner.tcp_tx_lock:
|
||||||
|
if len(self.owner.tcp_tx_queue) > 0:
|
||||||
|
self.socket.sendall(self.owner.tcp_tx_queue)
|
||||||
|
self.owner.tcp_tx_queue = b""
|
||||||
|
|
||||||
|
self.socket.sendall(data_bytes)
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
else:
|
||||||
|
with self.owner.tcp_tx_lock: self.owner.tcp_tx_queue += data_bytes
|
||||||
|
|
||||||
|
return len(data_bytes)
|
||||||
|
|
||||||
|
def read(self, n=4096):
|
||||||
|
with self.owner.tcp_rx_lock:
|
||||||
|
data = self.owner.tcp_rx_queue[:n]
|
||||||
|
self.owner.tcp_rx_queue = self.owner.tcp_rx_queue[n:]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.connected:
|
||||||
|
RNS.log(f"Disconnecting TCP socket for {self.owner}", RNS.LOG_DEBUG)
|
||||||
|
self.must_disconnect = True
|
||||||
|
if self.socket: self.socket.close()
|
||||||
|
|
||||||
|
def __init__(self, owner=None, target_host=None):
|
||||||
|
self.owner = owner
|
||||||
|
self.target_host = target_host
|
||||||
|
self.connected = False
|
||||||
|
self.reconnecting = False
|
||||||
|
self.running = False
|
||||||
|
self.should_run = False
|
||||||
|
self.must_disconnect = False
|
||||||
|
self.connect_job_running = False
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
self.should_run = True
|
||||||
|
self.connection_thread = threading.Thread(target=self.initial_connect, daemon=True).start()
|
||||||
|
|
||||||
|
def set_timeouts_linux(self):
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(self.TCP_USER_TIMEOUT * 1000))
|
||||||
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, int(self.TCP_PROBE_AFTER))
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, int(self.TCP_PROBE_INTERVAL))
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, int(self.TCP_PROBES))
|
||||||
|
|
||||||
|
def set_timeouts_osx(self):
|
||||||
|
if hasattr(socket, "TCP_KEEPALIVE"): TCP_KEEPIDLE = socket.TCP_KEEPALIVE
|
||||||
|
else: TCP_KEEPIDLE = 0x10
|
||||||
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(self.TCP_PROBE_AFTER))
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
try:
|
||||||
|
if self.socket: self.socket.close()
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Error while disconnecting TCP socket on cleanup for {self.owner}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
self.should_run = False
|
||||||
|
|
||||||
|
def initial_connect(self):
|
||||||
|
if self.connect(initial=True): threading.Thread(target=self.read_loop, daemon=True).start()
|
||||||
|
|
||||||
|
def connect(self, initial=False):
|
||||||
|
try:
|
||||||
|
if initial:
|
||||||
|
RNS.log(f"Establishing TCP connection to device for {self.owner}...", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
address_info = socket.getaddrinfo(self.target_host, self.TARGET_PORT, proto=socket.IPPROTO_TCP)[0]
|
||||||
|
address_family = address_info[0]
|
||||||
|
target_address = address_info[4]
|
||||||
|
|
||||||
|
self.socket = socket.socket(address_family, socket.SOCK_STREAM)
|
||||||
|
self.socket.settimeout(self.INITIAL_CONNECT_TIMEOUT)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
|
self.socket.connect(target_address)
|
||||||
|
self.socket.settimeout(None)
|
||||||
|
self.connected = True
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
RNS.log(f"TCP connection to device for {self.owner} established", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
if RNS.vendor.platformutils.is_linux(): self.set_timeouts_linux()
|
||||||
|
elif RNS.vendor.platformutils.is_darwin(): self.set_timeouts_osx()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if initial:
|
||||||
|
RNS.log(f"TCP connection to device for {self.owner} could not be established: {e}", RNS.LOG_ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
else: raise e
|
||||||
|
|
||||||
|
def read_loop(self):
|
||||||
|
try:
|
||||||
|
data_in = b""
|
||||||
|
while not self.must_disconnect:
|
||||||
|
if self.socket: data_in = self.socket.recv(4096)
|
||||||
|
else: data_in = b""
|
||||||
|
|
||||||
|
if len(data_in) > 0: self.owner.tcp_receive(data_in)
|
||||||
|
else:
|
||||||
|
self.connected = False
|
||||||
|
RNS.log(f"The TCP socket for {self} was closed", RNS.LOG_WARNING)
|
||||||
|
break
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.connected = False
|
||||||
|
RNS.log(f"A TCP read error occurred for {self}, the contained exception was: {e}", RNS.LOG_WARNING)
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -53,7 +61,7 @@ class SerialInterface(Interface):
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
import importlib
|
import importlib.util
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
self.on_android = True
|
self.on_android = True
|
||||||
if importlib.util.find_spec('usbserial4a') != None:
|
if importlib.util.find_spec('usbserial4a') != None:
|
||||||
|
|
@ -87,6 +95,7 @@ class SerialInterface(Interface):
|
||||||
raise ValueError("No port specified for serial interface")
|
raise ValueError("No port specified for serial interface")
|
||||||
|
|
||||||
self.HW_MTU = 564
|
self.HW_MTU = 564
|
||||||
|
self.shared_medium = True
|
||||||
|
|
||||||
self.pyserial = serial
|
self.pyserial = serial
|
||||||
self.serial = None
|
self.serial = None
|
||||||
|
|
@ -100,6 +109,7 @@ class SerialInterface(Interface):
|
||||||
self.timeout = 100
|
self.timeout = 100
|
||||||
self.online = False
|
self.online = False
|
||||||
self.bitrate = self.speed
|
self.bitrate = self.speed
|
||||||
|
self.reconnecting = False
|
||||||
|
|
||||||
if parity.lower() == "e" or parity.lower() == "even":
|
if parity.lower() == "e" or parity.lower() == "even":
|
||||||
self.parity = "E"
|
self.parity = "E"
|
||||||
|
|
@ -109,15 +119,23 @@ class SerialInterface(Interface):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
if self.serial.is_open: self.configure_device()
|
||||||
|
else: raise IOError("Could not open serial port")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
RNS.log(f"Could not open serial port for {self}", RNS.LOG_ERROR)
|
||||||
raise e
|
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
||||||
if self.serial.is_open:
|
if not self.detached and not self.reconnecting:
|
||||||
self.configure_device()
|
thread = threading.Thread(target=self.reconnect_port)
|
||||||
else:
|
thread.daemon = True
|
||||||
raise IOError("Could not open serial port")
|
thread.start()
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
try: self.serial.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close port for {self} at detach: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
RNS.log("Opening serial port "+self.port+"...")
|
RNS.log("Opening serial port "+self.port+"...")
|
||||||
|
|
@ -249,24 +267,23 @@ class SerialInterface(Interface):
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
self.serial.close()
|
try: self.serial.close()
|
||||||
self.reconnect_port()
|
except Exception as e: RNS.log(f"Could not close port for {self}", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting: self.reconnect_port()
|
||||||
|
|
||||||
def reconnect_port(self):
|
def reconnect_port(self):
|
||||||
while not self.online:
|
self.reconnecting = True
|
||||||
|
while not self.online and not self.detached:
|
||||||
try:
|
try:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
except Exception as e: RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
self.reconnecting = False
|
||||||
|
if self.online: RNS.log("Reconnected serial port for "+str(self))
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self): return False
|
||||||
return False
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "SerialInterface["+self.name+"]"
|
||||||
return "SerialInterface["+self.name+"]"
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -25,5 +33,7 @@ import glob
|
||||||
|
|
||||||
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
modules = py_modules+pyc_modules
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
__all__ = list(set([os.path.basename(f).replace(".pyc", "").replace(".py", "") for f in modules if not (f.endswith("__init__.py") or f.endswith("__init__.pyc"))]))
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2024 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -33,6 +41,9 @@ import RNS
|
||||||
|
|
||||||
|
|
||||||
class AutoInterface(Interface):
|
class AutoInterface(Interface):
|
||||||
|
HW_MTU = 1196
|
||||||
|
FIXED_MTU = True
|
||||||
|
|
||||||
DEFAULT_DISCOVERY_PORT = 29716
|
DEFAULT_DISCOVERY_PORT = 29716
|
||||||
DEFAULT_DATA_PORT = 42671
|
DEFAULT_DATA_PORT = 42671
|
||||||
DEFAULT_GROUP_ID = "reticulum".encode("utf-8")
|
DEFAULT_GROUP_ID = "reticulum".encode("utf-8")
|
||||||
|
|
@ -47,11 +58,14 @@ class AutoInterface(Interface):
|
||||||
MULTICAST_PERMANENT_ADDRESS_TYPE = "0"
|
MULTICAST_PERMANENT_ADDRESS_TYPE = "0"
|
||||||
MULTICAST_TEMPORARY_ADDRESS_TYPE = "1"
|
MULTICAST_TEMPORARY_ADDRESS_TYPE = "1"
|
||||||
|
|
||||||
PEERING_TIMEOUT = 7.5
|
PEERING_TIMEOUT = 22.0
|
||||||
|
ANNOUNCE_INTERVAL = 1.6
|
||||||
|
PEER_JOB_INTERVAL = 4.0
|
||||||
|
MCAST_ECHO_TIMEOUT = 6.5
|
||||||
|
|
||||||
ALL_IGNORE_IFS = ["lo0"]
|
ALL_IGNORE_IFS = ["lo0"]
|
||||||
DARWIN_IGNORE_IFS = ["awdl0", "llw0", "lo0", "en5"]
|
DARWIN_IGNORE_IFS = ["awdl0", "llw0", "lo0", "en5"]
|
||||||
ANDROID_IGNORE_IFS = ["dummy0", "lo", "tun0"]
|
ANDROID_IGNORE_IFS = ["dummy0", "lo", "tun0", "rmnet0", "rmnet1", "rmnet2", "rmnet3", "rmnet4", "rmnet5", "rmnet6", "rmnet7"]
|
||||||
|
|
||||||
BITRATE_GUESS = 10*1000*1000
|
BITRATE_GUESS = 10*1000*1000
|
||||||
|
|
||||||
|
|
@ -79,7 +93,6 @@ class AutoInterface(Interface):
|
||||||
return ifas
|
return ifas
|
||||||
|
|
||||||
def interface_name_to_index(self, ifname):
|
def interface_name_to_index(self, ifname):
|
||||||
|
|
||||||
# socket.if_nametoindex doesn't work with uuid interface names on windows, it wants the ethernet_0 style
|
# socket.if_nametoindex doesn't work with uuid interface names on windows, it wants the ethernet_0 style
|
||||||
# we will just get the index from netinfo instead as it seems to work
|
# we will just get the index from netinfo instead as it seems to work
|
||||||
if RNS.vendor.platformutils.is_windows():
|
if RNS.vendor.platformutils.is_windows():
|
||||||
|
|
@ -99,38 +112,44 @@ class AutoInterface(Interface):
|
||||||
ignored_interfaces = c.as_list("ignored_devices") if "ignored_devices" in c else None
|
ignored_interfaces = c.as_list("ignored_devices") if "ignored_devices" in c else None
|
||||||
configured_bitrate = c["configured_bitrate"] if "configured_bitrate" in c else None
|
configured_bitrate = c["configured_bitrate"] if "configured_bitrate" in c else None
|
||||||
|
|
||||||
from RNS.vendor.ifaddr import niwrapper
|
from RNS.Interfaces import netinfo
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.netinfo = niwrapper
|
self.netinfo = netinfo
|
||||||
|
|
||||||
self.HW_MTU = 1064
|
|
||||||
|
|
||||||
|
self.HW_MTU = AutoInterface.HW_MTU
|
||||||
self.IN = True
|
self.IN = True
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.owner = owner
|
||||||
self.online = False
|
self.online = False
|
||||||
|
self.final_init_done = False
|
||||||
self.peers = {}
|
self.peers = {}
|
||||||
self.link_local_addresses = []
|
self.link_local_addresses = []
|
||||||
self.adopted_interfaces = {}
|
self.adopted_interfaces = {}
|
||||||
self.interface_servers = {}
|
self.interface_servers = {}
|
||||||
|
self.discovery_sockets = []
|
||||||
self.multicast_echoes = {}
|
self.multicast_echoes = {}
|
||||||
|
self.initial_echoes = {}
|
||||||
self.timed_out_interfaces = {}
|
self.timed_out_interfaces = {}
|
||||||
|
self.spawned_interfaces = {}
|
||||||
|
self.write_lock = threading.Lock()
|
||||||
self.mif_deque = deque(maxlen=AutoInterface.MULTI_IF_DEQUE_LEN)
|
self.mif_deque = deque(maxlen=AutoInterface.MULTI_IF_DEQUE_LEN)
|
||||||
self.mif_deque_times = deque(maxlen=AutoInterface.MULTI_IF_DEQUE_LEN)
|
self.mif_deque_times = deque(maxlen=AutoInterface.MULTI_IF_DEQUE_LEN)
|
||||||
self.carrier_changed = False
|
self.carrier_changed = False
|
||||||
|
|
||||||
self.outbound_udp_socket = None
|
self.outbound_udp_socket = None
|
||||||
|
|
||||||
self.announce_rate_target = None
|
self.announce_rate_target = None
|
||||||
self.announce_interval = AutoInterface.PEERING_TIMEOUT/6.0
|
self.announce_interval = AutoInterface.ANNOUNCE_INTERVAL
|
||||||
self.peer_job_interval = AutoInterface.PEERING_TIMEOUT*1.1
|
self.peer_job_interval = AutoInterface.PEER_JOB_INTERVAL
|
||||||
self.peering_timeout = AutoInterface.PEERING_TIMEOUT
|
self.peering_timeout = AutoInterface.PEERING_TIMEOUT
|
||||||
self.multicast_echo_timeout = AutoInterface.PEERING_TIMEOUT/2
|
self.multicast_echo_timeout = AutoInterface.MCAST_ECHO_TIMEOUT
|
||||||
|
self.reverse_peering_interval = self.announce_interval*3.25
|
||||||
|
|
||||||
# Increase peering timeout on Android, due to potential
|
# Increase peering timeout on Android, due to potential
|
||||||
# low-power modes implemented on many chipsets.
|
# low-power modes implemented on many chipsets.
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
self.peering_timeout *= 3
|
self.peering_timeout *= 1.25
|
||||||
|
|
||||||
if allowed_interfaces == None:
|
if allowed_interfaces == None:
|
||||||
self.allowed_interfaces = []
|
self.allowed_interfaces = []
|
||||||
|
|
@ -152,6 +171,8 @@ class AutoInterface(Interface):
|
||||||
else:
|
else:
|
||||||
self.discovery_port = discovery_port
|
self.discovery_port = discovery_port
|
||||||
|
|
||||||
|
self.unicast_discovery_port = self.discovery_port+1
|
||||||
|
|
||||||
if multicast_address_type == None:
|
if multicast_address_type == None:
|
||||||
self.multicast_address_type = AutoInterface.MULTICAST_TEMPORARY_ADDRESS_TYPE
|
self.multicast_address_type = AutoInterface.MULTICAST_TEMPORARY_ADDRESS_TYPE
|
||||||
elif str(multicast_address_type).lower() == "temporary":
|
elif str(multicast_address_type).lower() == "temporary":
|
||||||
|
|
@ -227,33 +248,50 @@ class AutoInterface(Interface):
|
||||||
if link_local_addr == None:
|
if link_local_addr == None:
|
||||||
RNS.log(str(self)+" No link-local IPv6 address configured for "+str(ifname)+", skipping interface", RNS.LOG_EXTREME)
|
RNS.log(str(self)+" No link-local IPv6 address configured for "+str(ifname)+", skipping interface", RNS.LOG_EXTREME)
|
||||||
else:
|
else:
|
||||||
mcast_addr = self.mcast_discovery_address
|
RNS.log(str(self)+" Creating unicast discovery listener on "+str(ifname)+" with address "+str(link_local_addr), RNS.LOG_EXTREME)
|
||||||
RNS.log(str(self)+" Creating multicast discovery listener on "+str(ifname)+" with address "+str(mcast_addr), RNS.LOG_EXTREME)
|
|
||||||
|
|
||||||
# Struct with interface index
|
# Struct with interface index
|
||||||
if_struct = struct.pack("I", self.interface_name_to_index(ifname))
|
if_struct = struct.pack("I", self.interface_name_to_index(ifname))
|
||||||
|
|
||||||
# Set up multicast socket
|
# Set up unicast discovery socket
|
||||||
|
unicast_discovery_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
unicast_discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
if hasattr(socket, "SO_REUSEPORT"): unicast_discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
|
|
||||||
|
# Bind unicast discovery socket
|
||||||
|
if RNS.vendor.platformutils.is_windows():
|
||||||
|
# Windows throws "[WinError 10049] The requested address is not valid in its context"
|
||||||
|
# when trying to use the multicast address as host, or when providing interface index
|
||||||
|
# passing an empty host appears to work, but probably not exactly how we want it to...
|
||||||
|
unicast_discovery_socket.bind(('', self.unicast_discovery_port))
|
||||||
|
|
||||||
|
else:
|
||||||
|
addr_info = socket.getaddrinfo(link_local_addr+"%"+ifname, self.unicast_discovery_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
unicast_discovery_socket.bind(addr_info[0][4])
|
||||||
|
|
||||||
|
self.discovery_sockets.append(unicast_discovery_socket)
|
||||||
|
|
||||||
|
mcast_addr = self.mcast_discovery_address
|
||||||
|
RNS.log(str(self)+" Creating multicast discovery listener on "+str(ifname)+" with address "+str(mcast_addr), RNS.LOG_EXTREME)
|
||||||
|
|
||||||
|
# Set up multicast discovery socket
|
||||||
discovery_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
discovery_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
if hasattr(socket, "SO_REUSEPORT"):
|
if hasattr(socket, "SO_REUSEPORT"): discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
||||||
discovery_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
|
|
||||||
discovery_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, if_struct)
|
discovery_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, if_struct)
|
||||||
|
|
||||||
# Join multicast group
|
# Join multicast group
|
||||||
mcast_group = socket.inet_pton(socket.AF_INET6, mcast_addr) + if_struct
|
mcast_group = socket.inet_pton(socket.AF_INET6, mcast_addr) + if_struct
|
||||||
discovery_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mcast_group)
|
discovery_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mcast_group)
|
||||||
|
|
||||||
# Bind socket
|
# Bind multicast socket
|
||||||
if RNS.vendor.platformutils.is_windows():
|
if RNS.vendor.platformutils.is_windows():
|
||||||
|
# Windows throws "[WinError 10049] The requested address is not valid in its context"
|
||||||
# window throws "[WinError 10049] The requested address is not valid in its context"
|
|
||||||
# when trying to use the multicast address as host, or when providing interface index
|
# when trying to use the multicast address as host, or when providing interface index
|
||||||
# passing an empty host appears to work, but probably not exactly how we want it to...
|
# passing an empty host appears to work, but probably not exactly how we want it to...
|
||||||
discovery_socket.bind(('', self.discovery_port))
|
discovery_socket.bind(('', self.discovery_port))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
if self.discovery_scope == AutoInterface.SCOPE_LINK:
|
if self.discovery_scope == AutoInterface.SCOPE_LINK:
|
||||||
addr_info = socket.getaddrinfo(mcast_addr+"%"+ifname, self.discovery_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
addr_info = socket.getaddrinfo(mcast_addr+"%"+ifname, self.discovery_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
else:
|
else:
|
||||||
|
|
@ -261,13 +299,15 @@ class AutoInterface(Interface):
|
||||||
|
|
||||||
discovery_socket.bind(addr_info[0][4])
|
discovery_socket.bind(addr_info[0][4])
|
||||||
|
|
||||||
# Set up thread for discovery packets
|
self.discovery_sockets.append(discovery_socket)
|
||||||
def discovery_loop():
|
|
||||||
self.discovery_handler(discovery_socket, ifname)
|
|
||||||
|
|
||||||
thread = threading.Thread(target=discovery_loop)
|
# Set up thread for multicast discovery packets
|
||||||
thread.daemon = True
|
def discovery_loop(): self.discovery_handler(discovery_socket, ifname)
|
||||||
thread.start()
|
thread = threading.Thread(target=discovery_loop, daemon=True).start()
|
||||||
|
|
||||||
|
# Set up thread for unicast discovery packets
|
||||||
|
def unicast_discovery_loop(): self.discovery_handler(unicast_discovery_socket, ifname, announce=False)
|
||||||
|
thread = threading.Thread(target=unicast_discovery_loop, daemon=True).start()
|
||||||
|
|
||||||
suitable_interfaces += 1
|
suitable_interfaces += 1
|
||||||
|
|
||||||
|
|
@ -288,51 +328,53 @@ class AutoInterface(Interface):
|
||||||
else:
|
else:
|
||||||
self.bitrate = AutoInterface.BITRATE_GUESS
|
self.bitrate = AutoInterface.BITRATE_GUESS
|
||||||
|
|
||||||
peering_wait = self.announce_interval*1.2
|
def final_init(self):
|
||||||
RNS.log(str(self)+" discovering peers for "+str(round(peering_wait, 2))+" seconds...", RNS.LOG_VERBOSE)
|
peering_wait = self.announce_interval*1.2
|
||||||
|
RNS.log(str(self)+" discovering peers for "+str(round(peering_wait, 2))+" seconds...", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
self.owner = owner
|
socketserver.UDPServer.address_family = socket.AF_INET6
|
||||||
socketserver.UDPServer.address_family = socket.AF_INET6
|
|
||||||
|
|
||||||
for ifname in self.adopted_interfaces:
|
for ifname in self.adopted_interfaces:
|
||||||
local_addr = self.adopted_interfaces[ifname]+"%"+str(self.interface_name_to_index(ifname))
|
local_addr = self.adopted_interfaces[ifname]+"%"+str(self.interface_name_to_index(ifname))
|
||||||
addr_info = socket.getaddrinfo(local_addr, self.data_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
addr_info = socket.getaddrinfo(local_addr, self.data_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
address = addr_info[0][4]
|
address = addr_info[0][4]
|
||||||
|
|
||||||
udp_server = socketserver.UDPServer(address, self.handler_factory(self.process_incoming))
|
udp_server = socketserver.UDPServer(address, self.handler_factory(self.process_incoming))
|
||||||
self.interface_servers[ifname] = udp_server
|
self.interface_servers[ifname] = udp_server
|
||||||
|
|
||||||
thread = threading.Thread(target=udp_server.serve_forever)
|
|
||||||
thread.daemon = True
|
|
||||||
thread.start()
|
|
||||||
|
|
||||||
job_thread = threading.Thread(target=self.peer_jobs)
|
|
||||||
job_thread.daemon = True
|
|
||||||
job_thread.start()
|
|
||||||
|
|
||||||
time.sleep(peering_wait)
|
|
||||||
|
|
||||||
self.online = True
|
|
||||||
|
|
||||||
|
|
||||||
def discovery_handler(self, socket, ifname):
|
|
||||||
def announce_loop():
|
|
||||||
self.announce_handler(ifname)
|
|
||||||
|
|
||||||
thread = threading.Thread(target=announce_loop)
|
thread = threading.Thread(target=udp_server.serve_forever)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
job_thread = threading.Thread(target=self.peer_jobs)
|
||||||
|
job_thread.daemon = True
|
||||||
|
job_thread.start()
|
||||||
|
|
||||||
|
time.sleep(peering_wait)
|
||||||
|
|
||||||
|
self.online = True
|
||||||
|
self.final_init_done = True
|
||||||
|
|
||||||
|
def discovery_handler(self, socket, ifname, announce=True):
|
||||||
|
def announce_loop(): self.announce_handler(ifname)
|
||||||
|
|
||||||
while True:
|
if announce:
|
||||||
|
thread = threading.Thread(target=announce_loop)
|
||||||
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
while not self.detached:
|
||||||
data, ipv6_src = socket.recvfrom(1024)
|
data, ipv6_src = socket.recvfrom(1024)
|
||||||
expected_hash = RNS.Identity.full_hash(self.group_id+ipv6_src[0].encode("utf-8"))
|
if self.final_init_done and data and ipv6_src:
|
||||||
if data == expected_hash:
|
peering_hash = data[:RNS.Identity.HASHLENGTH//8]
|
||||||
self.add_peer(ipv6_src[0], ifname)
|
expected_hash = RNS.Identity.full_hash(self.group_id+ipv6_src[0].encode("utf-8"))
|
||||||
else:
|
if peering_hash == expected_hash:
|
||||||
RNS.log(str(self)+" received peering packet on "+str(ifname)+" from "+str(ipv6_src[0])+", but authentication hash was incorrect.", RNS.LOG_DEBUG)
|
self.add_peer(ipv6_src[0], ifname)
|
||||||
|
else:
|
||||||
|
RNS.log(str(self)+" received peering packet on "+str(ifname)+" from "+str(ipv6_src[0])+", but authentication hash was incorrect.", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
def peer_jobs(self):
|
def peer_jobs(self):
|
||||||
while True:
|
while not self.detached:
|
||||||
time.sleep(self.peer_job_interval)
|
time.sleep(self.peer_job_interval)
|
||||||
now = time.time()
|
now = time.time()
|
||||||
timed_out_peers = []
|
timed_out_peers = []
|
||||||
|
|
@ -347,8 +389,24 @@ class AutoInterface(Interface):
|
||||||
# Remove any timed out peers
|
# Remove any timed out peers
|
||||||
for peer_addr in timed_out_peers:
|
for peer_addr in timed_out_peers:
|
||||||
removed_peer = self.peers.pop(peer_addr)
|
removed_peer = self.peers.pop(peer_addr)
|
||||||
|
if peer_addr in self.spawned_interfaces:
|
||||||
|
spawned_interface = self.spawned_interfaces[peer_addr]
|
||||||
|
spawned_interface.detach()
|
||||||
|
spawned_interface.teardown()
|
||||||
RNS.log(str(self)+" removed peer "+str(peer_addr)+" on "+str(removed_peer[0]), RNS.LOG_DEBUG)
|
RNS.log(str(self)+" removed peer "+str(peer_addr)+" on "+str(removed_peer[0]), RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
# Send reverse peering packets
|
||||||
|
for peer_addr in self.peers:
|
||||||
|
try:
|
||||||
|
peer = self.peers[peer_addr]
|
||||||
|
ifname = peer[0]
|
||||||
|
last_outbound = peer[2]
|
||||||
|
if now > last_outbound+self.reverse_peering_interval:
|
||||||
|
self.reverse_announce(ifname, peer_addr)
|
||||||
|
peer[2] = time.time()
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Error while sending reverse peering packet to {peer_addr}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
for ifname in self.adopted_interfaces:
|
for ifname in self.adopted_interfaces:
|
||||||
# Check that the link-local address has not changed
|
# Check that the link-local address has not changed
|
||||||
try:
|
try:
|
||||||
|
|
@ -375,14 +433,22 @@ class AutoInterface(Interface):
|
||||||
if ifname in self.interface_servers:
|
if ifname in self.interface_servers:
|
||||||
RNS.log("Shutting down previous UDP listener for "+str(self)+" "+str(ifname), RNS.LOG_DEBUG)
|
RNS.log("Shutting down previous UDP listener for "+str(self)+" "+str(ifname), RNS.LOG_DEBUG)
|
||||||
previous_server = self.interface_servers[ifname]
|
previous_server = self.interface_servers[ifname]
|
||||||
def shutdown_server():
|
def shutdown_server(): previous_server.shutdown()
|
||||||
previous_server.shutdown()
|
|
||||||
threading.Thread(target=shutdown_server, daemon=True).start()
|
threading.Thread(target=shutdown_server, daemon=True).start()
|
||||||
|
|
||||||
RNS.log("Starting new UDP listener for "+str(self)+" "+str(ifname), RNS.LOG_DEBUG)
|
RNS.log("Starting new UDP listener for "+str(self)+" "+str(ifname), RNS.LOG_DEBUG)
|
||||||
|
|
||||||
udp_server = socketserver.UDPServer(listen_address, self.handler_factory(self.process_incoming))
|
retry_delay = 1.25
|
||||||
self.interface_servers[ifname] = udp_server
|
listener_started = False
|
||||||
|
while not listener_started:
|
||||||
|
try:
|
||||||
|
time.sleep(retry_delay)
|
||||||
|
udp_server = socketserver.UDPServer(listen_address, self.handler_factory(self.process_incoming))
|
||||||
|
self.interface_servers[ifname] = udp_server
|
||||||
|
listener_started = True
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not start new UDP listener for {self} on {listen_address}: {e}", RNS.LOG_WARNING)
|
||||||
|
RNS.log(f"Retrying in {retry_delay} seconds", RNS.LOG_WARNING)
|
||||||
|
|
||||||
thread = threading.Thread(target=udp_server.serve_forever)
|
thread = threading.Thread(target=udp_server.serve_forever)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
|
|
@ -394,9 +460,10 @@ class AutoInterface(Interface):
|
||||||
RNS.log("Could not get device information while updating link-local addresses for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Could not get device information while updating link-local addresses for "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
# Check multicast echo timeouts
|
# Check multicast echo timeouts
|
||||||
last_multicast_echo = 0
|
last_multicast_echo = 0
|
||||||
if ifname in self.multicast_echoes:
|
multicast_echo_received = False
|
||||||
last_multicast_echo = self.multicast_echoes[ifname]
|
if ifname in self.multicast_echoes: last_multicast_echo = self.multicast_echoes[ifname]
|
||||||
|
if ifname in self.initial_echoes: multicast_echo_received = True
|
||||||
|
|
||||||
if now - last_multicast_echo > self.multicast_echo_timeout:
|
if now - last_multicast_echo > self.multicast_echo_timeout:
|
||||||
if ifname in self.timed_out_interfaces and self.timed_out_interfaces[ifname] == False:
|
if ifname in self.timed_out_interfaces and self.timed_out_interfaces[ifname] == False:
|
||||||
|
|
@ -408,13 +475,32 @@ class AutoInterface(Interface):
|
||||||
self.carrier_changed = True
|
self.carrier_changed = True
|
||||||
RNS.log(str(self)+" Carrier recovered on "+str(ifname), RNS.LOG_WARNING)
|
RNS.log(str(self)+" Carrier recovered on "+str(ifname), RNS.LOG_WARNING)
|
||||||
self.timed_out_interfaces[ifname] = False
|
self.timed_out_interfaces[ifname] = False
|
||||||
|
|
||||||
|
if not multicast_echo_received:
|
||||||
|
RNS.log(f"{self} No multicast echoes received on {ifname}. The networking hardware or a firewall may be blocking multicast traffic.", RNS.LOG_ERROR)
|
||||||
|
# else:
|
||||||
|
# RNS.log(f"{self} Initial multicast echo on {ifname} received {RNS.prettytime(time.time()-self.initial_echoes[ifname])} ago.", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
|
||||||
def announce_handler(self, ifname):
|
def announce_handler(self, ifname):
|
||||||
while True:
|
while not self.detached:
|
||||||
self.peer_announce(ifname)
|
self.peer_announce(ifname)
|
||||||
time.sleep(self.announce_interval)
|
time.sleep(self.announce_interval)
|
||||||
|
|
||||||
|
def reverse_announce(self, ifname, peer_addr):
|
||||||
|
try:
|
||||||
|
link_local_address = self.adopted_interfaces[ifname]
|
||||||
|
discovery_token = RNS.Identity.full_hash(self.group_id+link_local_address.encode("utf-8"))
|
||||||
|
announce_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
addr_info = socket.getaddrinfo(f"{peer_addr}%{ifname}", self.unicast_discovery_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
|
|
||||||
|
ifis = struct.pack("I", self.interface_name_to_index(ifname))
|
||||||
|
announce_socket.sendto(discovery_token, addr_info[0][4])
|
||||||
|
announce_socket.close()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not send reverse peering packet to {peer_addr} on {ifname}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def peer_announce(self, ifname):
|
def peer_announce(self, ifname):
|
||||||
try:
|
try:
|
||||||
link_local_address = self.adopted_interfaces[ifname]
|
link_local_address = self.adopted_interfaces[ifname]
|
||||||
|
|
@ -433,6 +519,10 @@ class AutoInterface(Interface):
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def peer_count(self):
|
||||||
|
return len(self.spawned_interfaces)
|
||||||
|
|
||||||
def add_peer(self, addr, ifname):
|
def add_peer(self, addr, ifname):
|
||||||
if addr in self.link_local_addresses:
|
if addr in self.link_local_addresses:
|
||||||
ifname = None
|
ifname = None
|
||||||
|
|
@ -442,63 +532,173 @@ class AutoInterface(Interface):
|
||||||
|
|
||||||
if ifname != None:
|
if ifname != None:
|
||||||
self.multicast_echoes[ifname] = time.time()
|
self.multicast_echoes[ifname] = time.time()
|
||||||
|
if not ifname in self.initial_echoes: self.initial_echoes[ifname] = time.time()
|
||||||
else:
|
else:
|
||||||
RNS.log(str(self)+" received multicast echo on unexpected interface "+str(ifname), RNS.LOG_WARNING)
|
RNS.log(str(self)+" received multicast echo on unexpected interface "+str(ifname), RNS.LOG_WARNING)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if not addr in self.peers:
|
if not addr in self.peers:
|
||||||
self.peers[addr] = [ifname, time.time()]
|
self.peers[addr] = [ifname, time.time(), time.time()]
|
||||||
|
|
||||||
|
spawned_interface = AutoInterfacePeer(self, addr, ifname)
|
||||||
|
spawned_interface.OUT = self.OUT
|
||||||
|
spawned_interface.IN = self.IN
|
||||||
|
|
||||||
|
spawned_interface.ingress_control = self.ingress_control
|
||||||
|
spawned_interface.ic_max_held_announces = self.ic_max_held_announces
|
||||||
|
spawned_interface.ic_burst_hold = self.ic_burst_hold
|
||||||
|
spawned_interface.ic_burst_freq = self.ic_burst_freq
|
||||||
|
spawned_interface.ic_burst_freq_new = self.ic_burst_freq_new
|
||||||
|
spawned_interface.ic_new_time = self.ic_new_time
|
||||||
|
spawned_interface.ic_burst_penalty = self.ic_burst_penalty
|
||||||
|
spawned_interface.ic_held_release_interval = self.ic_held_release_interval
|
||||||
|
|
||||||
|
spawned_interface.egress_control = self.egress_control
|
||||||
|
spawned_interface.ec_pr_freq = self.ec_pr_freq
|
||||||
|
spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new
|
||||||
|
spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq
|
||||||
|
|
||||||
|
spawned_interface._destination_cost = self._destination_cost
|
||||||
|
spawned_interface._pr_cost = self._pr_cost
|
||||||
|
|
||||||
|
spawned_interface.parent_interface = self
|
||||||
|
spawned_interface.bitrate = self.bitrate
|
||||||
|
|
||||||
|
spawned_interface.ifac_size = self.ifac_size
|
||||||
|
spawned_interface.ifac_netname = self.ifac_netname
|
||||||
|
spawned_interface.ifac_netkey = self.ifac_netkey
|
||||||
|
if spawned_interface.ifac_netname != None or spawned_interface.ifac_netkey != None:
|
||||||
|
ifac_origin = b""
|
||||||
|
if spawned_interface.ifac_netname != None:
|
||||||
|
ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netname.encode("utf-8"))
|
||||||
|
if spawned_interface.ifac_netkey != None:
|
||||||
|
ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netkey.encode("utf-8"))
|
||||||
|
|
||||||
|
ifac_origin_hash = RNS.Identity.full_hash(ifac_origin)
|
||||||
|
spawned_interface.ifac_key = RNS.Cryptography.hkdf(
|
||||||
|
length=64,
|
||||||
|
derive_from=ifac_origin_hash,
|
||||||
|
salt=RNS.Reticulum.IFAC_SALT,
|
||||||
|
context=None
|
||||||
|
)
|
||||||
|
spawned_interface.ifac_identity = RNS.Identity.from_bytes(spawned_interface.ifac_key)
|
||||||
|
spawned_interface.ifac_signature = spawned_interface.ifac_identity.sign(RNS.Identity.full_hash(spawned_interface.ifac_key))
|
||||||
|
|
||||||
|
spawned_interface.announce_rate_target = self.announce_rate_target
|
||||||
|
spawned_interface.announce_rate_grace = self.announce_rate_grace
|
||||||
|
spawned_interface.announce_rate_penalty = self.announce_rate_penalty
|
||||||
|
spawned_interface.mode = self.mode
|
||||||
|
spawned_interface.gravity = self.gravity
|
||||||
|
spawned_interface.recursive_prs = self.recursive_prs
|
||||||
|
spawned_interface.announces_from_internal = self.announces_from_internal
|
||||||
|
spawned_interface.announces_to_internal = self.announces_to_internal
|
||||||
|
spawned_interface.announce_cap = self.announce_cap
|
||||||
|
spawned_interface.HW_MTU = self.HW_MTU
|
||||||
|
spawned_interface.online = True
|
||||||
|
RNS.Transport.add_interface(spawned_interface)
|
||||||
|
if addr in self.spawned_interfaces:
|
||||||
|
self.spawned_interfaces[addr].detach()
|
||||||
|
self.spawned_interfaces[addr].teardown()
|
||||||
|
if addr in self.spawned_interfaces: self.spawned_interfaces.pop(addr)
|
||||||
|
self.spawned_interfaces[addr] = spawned_interface
|
||||||
|
|
||||||
RNS.log(str(self)+" added peer "+str(addr)+" on "+str(ifname), RNS.LOG_DEBUG)
|
RNS.log(str(self)+" added peer "+str(addr)+" on "+str(ifname), RNS.LOG_DEBUG)
|
||||||
else:
|
else:
|
||||||
self.refresh_peer(addr)
|
self.refresh_peer(addr)
|
||||||
|
|
||||||
def refresh_peer(self, addr):
|
def refresh_peer(self, addr):
|
||||||
self.peers[addr][1] = time.time()
|
try: self.peers[addr][1] = time.time()
|
||||||
|
except Exception as e: RNS.log(f"An error occurred while refreshing peer {addr} on {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data, addr=None):
|
||||||
if self.online:
|
if self.online and addr in self.spawned_interfaces:
|
||||||
|
self.spawned_interfaces[addr].process_incoming(data, addr)
|
||||||
|
|
||||||
|
def process_outgoing(self, data): pass
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.online = False
|
||||||
|
self.detached = True
|
||||||
|
|
||||||
|
RNS.log(f"Shutting down all UDP listeners for {self}", RNS.LOG_NOTICE)
|
||||||
|
for ifname in self.interface_servers:
|
||||||
|
try:
|
||||||
|
self.interface_servers[ifname].server_close()
|
||||||
|
self.interface_servers[ifname].shutdown()
|
||||||
|
except Exception as e: RNS.log(f"Could not shut down UDP listener {ifname} for {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
for s in self.discovery_sockets:
|
||||||
|
try: s.close()
|
||||||
|
except Exception as e: RNS.log(f"Error while shutting down discovery socket {s} for {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
def __str__(self): return f"AutoInterface[{self.name}]"
|
||||||
|
|
||||||
|
class AutoInterfacePeer(Interface):
|
||||||
|
|
||||||
|
def __init__(self, owner, addr, ifname):
|
||||||
|
super().__init__()
|
||||||
|
self.owner = owner
|
||||||
|
self.parent_interface = owner
|
||||||
|
self.addr = addr
|
||||||
|
self.ifname = ifname
|
||||||
|
self.peer_addr = None
|
||||||
|
self.addr_info = None
|
||||||
|
self.HW_MTU = self.owner.HW_MTU
|
||||||
|
self.FIXED_MTU = self.owner.FIXED_MTU
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"AutoInterfacePeer[{self.ifname}/{self.addr}]"
|
||||||
|
|
||||||
|
def process_incoming(self, data, addr=None):
|
||||||
|
if self.online and self.owner.online:
|
||||||
data_hash = RNS.Identity.full_hash(data)
|
data_hash = RNS.Identity.full_hash(data)
|
||||||
deque_hit = False
|
deque_hit = False
|
||||||
if data_hash in self.mif_deque:
|
if data_hash in self.owner.mif_deque:
|
||||||
for te in self.mif_deque_times:
|
for te in self.owner.mif_deque_times:
|
||||||
if te[0] == data_hash and time.time() < te[1]+AutoInterface.MULTI_IF_DEQUE_TTL:
|
if te[0] == data_hash and time.time() < te[1]+AutoInterface.MULTI_IF_DEQUE_TTL:
|
||||||
deque_hit = True
|
deque_hit = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if not deque_hit:
|
if not deque_hit:
|
||||||
self.mif_deque.append(data_hash)
|
self.owner.refresh_peer(self.addr)
|
||||||
self.mif_deque_times.append([data_hash, time.time()])
|
self.owner.mif_deque.append(data_hash)
|
||||||
self.rxb += len(data)
|
self.owner.mif_deque_times.append([data_hash, time.time()])
|
||||||
self.owner.inbound(data, self)
|
self.owner.owner.inbound(data, self)
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self, data):
|
||||||
if self.online:
|
if self.online:
|
||||||
for peer in self.peers:
|
with self.owner.write_lock:
|
||||||
try:
|
try:
|
||||||
if self.outbound_udp_socket == None:
|
if self.owner.outbound_udp_socket == None: self.owner.outbound_udp_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
self.outbound_udp_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
if self.peer_addr == None: self.peer_addr = str(self.addr)+"%"+str(self.owner.interface_name_to_index(self.ifname))
|
||||||
|
if self.addr_info == None: self.addr_info = socket.getaddrinfo(self.peer_addr, self.owner.data_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
peer_addr = str(peer)+"%"+str(self.interface_name_to_index(self.peers[peer][0]))
|
self.owner.outbound_udp_socket.sendto(data, self.addr_info[0][4])
|
||||||
addr_info = socket.getaddrinfo(peer_addr, self.data_port, socket.AF_INET6, socket.SOCK_DGRAM)
|
self.txb += len(data)
|
||||||
self.outbound_udp_socket.sendto(data, addr_info[0][4])
|
self.owner.txb += len(data)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
|
||||||
self.txb += len(data)
|
|
||||||
|
|
||||||
|
|
||||||
# Until per-device sub-interfacing is implemented,
|
|
||||||
# ingress limiting should be disabled on AutoInterface
|
|
||||||
def should_ingress_limit(self):
|
|
||||||
return False
|
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
self.online = False
|
self.online = False
|
||||||
|
self.detached = True
|
||||||
|
|
||||||
|
def teardown(self):
|
||||||
|
if not self.detached:
|
||||||
|
RNS.log(f"The interface {self} experienced an unrecoverable error and is being torn down.", RNS.LOG_ERROR)
|
||||||
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
|
|
||||||
def __str__(self):
|
else: RNS.log(f"The interface {self} is being torn down.", RNS.LOG_VERBOSE)
|
||||||
return "AutoInterface["+self.name+"]"
|
|
||||||
|
self.online = False
|
||||||
|
self.OUT = False
|
||||||
|
self.IN = False
|
||||||
|
|
||||||
|
if self.addr in self.owner.spawned_interfaces:
|
||||||
|
try: self.owner.spawned_interfaces.pop(self.addr)
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not remove {self} from parent interface on detach. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
RNS.Transport.remove_interface(self)
|
||||||
|
|
||||||
class AutoInterfaceHandler(socketserver.BaseRequestHandler):
|
class AutoInterfaceHandler(socketserver.BaseRequestHandler):
|
||||||
def __init__(self, callback, *args, **keys):
|
def __init__(self, callback, *args, **keys):
|
||||||
|
|
@ -507,4 +707,5 @@ class AutoInterfaceHandler(socketserver.BaseRequestHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
data = self.request[0]
|
data = self.request[0]
|
||||||
self.callback(data)
|
addr = self.client_address[0]
|
||||||
|
self.callback(data, addr)
|
||||||
1147
RNS/Interfaces/BackboneInterface.py
Normal file
1147
RNS/Interfaces/BackboneInterface.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2024 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -80,8 +88,7 @@ class I2PController:
|
||||||
self.ready = False
|
self.ready = False
|
||||||
|
|
||||||
self.storagepath = rns_storagepath+"/i2p"
|
self.storagepath = rns_storagepath+"/i2p"
|
||||||
if not os.path.isdir(self.storagepath):
|
if not os.path.isdir(self.storagepath): os.makedirs(self.storagepath)
|
||||||
os.makedirs(self.storagepath)
|
|
||||||
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
|
@ -102,28 +109,22 @@ class I2PController:
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.ready = False
|
self.ready = False
|
||||||
RNS.log("Exception on event loop for "+str(self)+": "+str(e), RNS.LOG_ERROR)
|
RNS.log("Exception on event loop for "+str(self)+": "+str(e), RNS.LOG_ERROR)
|
||||||
finally:
|
finally: self.loop.close()
|
||||||
self.loop.close()
|
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
for i2ptunnel in self.i2plib_tunnels:
|
for i2ptunnel in self.i2plib_tunnels:
|
||||||
if hasattr(i2ptunnel, "stop") and callable(i2ptunnel.stop):
|
if hasattr(i2ptunnel, "stop") and callable(i2ptunnel.stop): i2ptunnel.stop()
|
||||||
i2ptunnel.stop()
|
|
||||||
|
|
||||||
if hasattr(asyncio.Task, "all_tasks") and callable(asyncio.Task.all_tasks):
|
if hasattr(asyncio.Task, "all_tasks") and callable(asyncio.Task.all_tasks):
|
||||||
for task in asyncio.Task.all_tasks(loop=self.loop):
|
for task in asyncio.Task.all_tasks(loop=self.loop): task.cancel()
|
||||||
task.cancel()
|
|
||||||
|
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
|
|
||||||
self.loop.stop()
|
self.loop.stop()
|
||||||
|
|
||||||
|
|
||||||
def get_free_port(self):
|
def get_free_port(self):
|
||||||
return self.i2plib.utils.get_free_port()
|
return self.i2plib.utils.get_free_port()
|
||||||
|
|
||||||
|
|
||||||
def stop_tunnel(self, i2ptunnel):
|
def stop_tunnel(self, i2ptunnel):
|
||||||
if hasattr(i2ptunnel, "stop") and callable(i2ptunnel.stop):
|
if hasattr(i2ptunnel, "stop") and callable(i2ptunnel.stop):
|
||||||
i2ptunnel.stop()
|
i2ptunnel.stop()
|
||||||
|
|
@ -151,10 +152,9 @@ class I2PController:
|
||||||
tn = self.i2plib_tunnels[i2p_destination]
|
tn = self.i2plib_tunnels[i2p_destination]
|
||||||
if tn != None and hasattr(tn, "status"):
|
if tn != None and hasattr(tn, "status"):
|
||||||
|
|
||||||
RNS.log("Waiting for status from I2P control process", RNS.LOG_EXTREME)
|
RNS.log("Waiting for status from I2P control process", RNS.LOG_DEBUG)
|
||||||
while not tn.status["setup_ran"]:
|
while not tn.status["setup_ran"]: time.sleep(0.1)
|
||||||
time.sleep(0.1)
|
RNS.log("Got status from I2P control process", RNS.LOG_DEBUG)
|
||||||
RNS.log("Got status from I2P control process", RNS.LOG_EXTREME)
|
|
||||||
|
|
||||||
if tn.status["setup_failed"]:
|
if tn.status["setup_failed"]:
|
||||||
self.stop_tunnel(tn)
|
self.stop_tunnel(tn)
|
||||||
|
|
@ -164,29 +164,19 @@ class I2PController:
|
||||||
if owner.socket != None:
|
if owner.socket != None:
|
||||||
if hasattr(owner.socket, "close"):
|
if hasattr(owner.socket, "close"):
|
||||||
if callable(owner.socket.close):
|
if callable(owner.socket.close):
|
||||||
try:
|
try: owner.socket.shutdown(socket.SHUT_RDWR)
|
||||||
owner.socket.shutdown(socket.SHUT_RDWR)
|
except Exception as e: RNS.log("Error while shutting down socket for "+str(owner)+": "+str(e))
|
||||||
except Exception as e:
|
try: owner.socket.close()
|
||||||
RNS.log("Error while shutting down socket for "+str(owner)+": "+str(e))
|
except Exception as e: RNS.log("Error while closing socket for "+str(owner)+": "+str(e))
|
||||||
|
|
||||||
try:
|
|
||||||
owner.socket.close()
|
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while closing socket for "+str(owner)+": "+str(e))
|
|
||||||
self.client_tunnels[i2p_destination] = True
|
self.client_tunnels[i2p_destination] = True
|
||||||
owner.awaiting_i2p_tunnel = False
|
owner.awaiting_i2p_tunnel = False
|
||||||
|
|
||||||
RNS.log(str(owner)+" tunnel setup complete", RNS.LOG_VERBOSE)
|
RNS.log(str(owner)+" tunnel setup complete", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
else:
|
else: raise IOError("Got no status response from SAM API")
|
||||||
raise IOError("Got no status response from SAM API")
|
|
||||||
|
|
||||||
except ConnectionRefusedError as e:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
except ConnectionAbortedError as e:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
|
except ConnectionRefusedError as e: raise e
|
||||||
|
except ConnectionAbortedError as e: raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Unexpected error type from I2P SAM: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Unexpected error type from I2P SAM: "+str(e), RNS.LOG_ERROR)
|
||||||
raise e
|
raise e
|
||||||
|
|
@ -198,54 +188,33 @@ class I2PController:
|
||||||
|
|
||||||
if i2ptunnel.status["setup_ran"] == False:
|
if i2ptunnel.status["setup_ran"] == False:
|
||||||
RNS.log(str(self)+" I2P tunnel setup did not complete", RNS.LOG_ERROR)
|
RNS.log(str(self)+" I2P tunnel setup did not complete", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif i2p_exception != None:
|
elif i2p_exception != None:
|
||||||
RNS.log("An error ocurred while setting up I2P tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
RNS.log("An error ocurred while setting up I2P tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
|
if isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.CantReachPeer): RNS.log("The I2P daemon can't reach peer "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
if isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.CantReachPeer):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedDest): RNS.log("The I2P daemon reported that the destination is already in use", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon can't reach peer "+str(i2p_destination), RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedId): RNS.log("The I2P daemon reported that the ID is arleady in use", RNS.LOG_ERROR)
|
||||||
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidId): RNS.log("The I2P daemon reported that the stream session ID doesn't exist", RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedDest):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidKey): RNS.log("The I2P daemon reported that the key for "+str(i2p_destination)+" is invalid", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon reported that the destination is already in use", RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.KeyNotFound): RNS.log("The I2P daemon could not find the key for "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.PeerNotFound): RNS.log("The I2P daemon could not find the peer "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedId):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.I2PError): RNS.log("The I2P daemon experienced an unspecified error", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon reported that the ID is arleady in use", RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.Timeout): RNS.log("I2P daemon timed out while setting up client tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
|
else: RNS.log(f"Unspecified I2P daemon error: {i2p_exception}", RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidId):
|
|
||||||
RNS.log("The I2P daemon reported that the stream session ID doesn't exist", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidKey):
|
|
||||||
RNS.log("The I2P daemon reported that the key for "+str(i2p_destination)+" is invalid", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.KeyNotFound):
|
|
||||||
RNS.log("The I2P daemon could not find the key for "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.PeerNotFound):
|
|
||||||
RNS.log("The I2P daemon mould not find the peer "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.I2PError):
|
|
||||||
RNS.log("The I2P daemon experienced an unspecified error", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.Timeout):
|
|
||||||
RNS.log("I2P daemon timed out while setting up client tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Resetting I2P tunnel and retrying later", RNS.LOG_ERROR)
|
RNS.log("Resetting I2P tunnel and retrying later", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif i2ptunnel.status["setup_failed"] == True:
|
elif i2ptunnel.status["setup_failed"] == True:
|
||||||
RNS.log(str(self)+" Unspecified I2P tunnel setup error, resetting I2P tunnel", RNS.LOG_ERROR)
|
RNS.log(str(self)+" Unspecified I2P tunnel setup error, resetting I2P tunnel", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log(str(self)+" Got no status from SAM API, resetting I2P tunnel", RNS.LOG_ERROR)
|
RNS.log(str(self)+" Got no status from SAM API, resetting I2P tunnel", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
@ -266,10 +235,8 @@ class I2PController:
|
||||||
i2p_keyfile_nf = self.storagepath+"/"+RNS.hexrep(i2p_dest_hash_nf, delimit=False)+".i2p"
|
i2p_keyfile_nf = self.storagepath+"/"+RNS.hexrep(i2p_dest_hash_nf, delimit=False)+".i2p"
|
||||||
|
|
||||||
# Use old format if a key is already present
|
# Use old format if a key is already present
|
||||||
if os.path.isfile(i2p_keyfile_of):
|
if os.path.isfile(i2p_keyfile_of): i2p_keyfile = i2p_keyfile_of
|
||||||
i2p_keyfile = i2p_keyfile_of
|
else: i2p_keyfile = i2p_keyfile_nf
|
||||||
else:
|
|
||||||
i2p_keyfile = i2p_keyfile_nf
|
|
||||||
|
|
||||||
i2p_dest = None
|
i2p_dest = None
|
||||||
if not os.path.isfile(i2p_keyfile):
|
if not os.path.isfile(i2p_keyfile):
|
||||||
|
|
@ -304,8 +271,7 @@ class I2PController:
|
||||||
asyncio.run_coroutine_threadsafe(tunnel_up(), self.loop).result()
|
asyncio.run_coroutine_threadsafe(tunnel_up(), self.loop).result()
|
||||||
self.server_tunnels[i2p_b32] = True
|
self.server_tunnels[i2p_b32] = True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e: raise e
|
||||||
raise e
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
i2ptunnel = self.i2plib_tunnels[i2p_b32]
|
i2ptunnel = self.i2plib_tunnels[i2p_b32]
|
||||||
|
|
@ -314,66 +280,45 @@ class I2PController:
|
||||||
|
|
||||||
if i2ptunnel.status["setup_ran"] == False:
|
if i2ptunnel.status["setup_ran"] == False:
|
||||||
RNS.log(str(self)+" I2P tunnel setup did not complete", RNS.LOG_ERROR)
|
RNS.log(str(self)+" I2P tunnel setup did not complete", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif i2p_exception != None:
|
elif i2p_exception != None:
|
||||||
RNS.log("An error ocurred while setting up I2P tunnel", RNS.LOG_ERROR)
|
RNS.log("An error ocurred while setting up I2P tunnel", RNS.LOG_ERROR)
|
||||||
|
if isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.CantReachPeer): RNS.log("The I2P daemon can't reach peer "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
if isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.CantReachPeer):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedDest): RNS.log("The I2P daemon reported that the destination is already in use", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon can't reach peer "+str(i2p_destination), RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedId): RNS.log("The I2P daemon reported that the ID is arleady in use", RNS.LOG_ERROR)
|
||||||
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidId): RNS.log("The I2P daemon reported that the stream session ID doesn't exist", RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedDest):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidKey): RNS.log("The I2P daemon reported that the key for "+str(i2p_destination)+" is invalid", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon reported that the destination is already in use", RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.KeyNotFound): RNS.log("The I2P daemon could not find the key for "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.PeerNotFound): RNS.log("The I2P daemon could not find the peer "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.DuplicatedId):
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.I2PError): RNS.log("The I2P daemon experienced an unspecified error", RNS.LOG_ERROR)
|
||||||
RNS.log("The I2P daemon reported that the ID is arleady in use", RNS.LOG_ERROR)
|
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.Timeout): RNS.log("I2P daemon timed out while setting up client tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
||||||
|
else: RNS.log(f"Unspecified I2P daemon error: {i2p_exception}", RNS.LOG_ERROR)
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidId):
|
|
||||||
RNS.log("The I2P daemon reported that the stream session ID doesn't exist", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.InvalidKey):
|
|
||||||
RNS.log("The I2P daemon reported that the key for "+str(i2p_destination)+" is invalid", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.KeyNotFound):
|
|
||||||
RNS.log("The I2P daemon could not find the key for "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.PeerNotFound):
|
|
||||||
RNS.log("The I2P daemon mould not find the peer "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.I2PError):
|
|
||||||
RNS.log("The I2P daemon experienced an unspecified error", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
elif isinstance(i2p_exception, RNS.vendor.i2plib.exceptions.Timeout):
|
|
||||||
RNS.log("I2P daemon timed out while setting up client tunnel to "+str(i2p_destination), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Resetting I2P tunnel and retrying later", RNS.LOG_ERROR)
|
RNS.log("Resetting I2P tunnel and retrying later", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif i2ptunnel.status["setup_failed"] == True:
|
elif i2ptunnel.status["setup_failed"] == True:
|
||||||
RNS.log(str(self)+" Unspecified I2P tunnel setup error, resetting I2P tunnel", RNS.LOG_ERROR)
|
RNS.log(str(self)+" Unspecified I2P tunnel setup error, resetting I2P tunnel", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log(str(self)+" Got no status from SAM API, resetting I2P tunnel", RNS.LOG_ERROR)
|
RNS.log(str(self)+" Got no status from SAM API, resetting I2P tunnel", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.stop_tunnel(i2ptunnel)
|
self.stop_tunnel(i2ptunnel)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
def get_loop(self):
|
def get_loop(self): return asyncio.get_event_loop()
|
||||||
return asyncio.get_event_loop()
|
|
||||||
|
|
||||||
|
|
||||||
class ThreadingI2PServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadingI2PServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class I2PInterfacePeer(Interface):
|
class I2PInterfacePeer(Interface):
|
||||||
RECONNECT_WAIT = 15
|
RECONNECT_WAIT = 15
|
||||||
RECONNECT_MAX_TRIES = None
|
RECONNECT_MAX_TRIES = None
|
||||||
|
|
@ -408,9 +353,11 @@ class I2PInterfacePeer(Interface):
|
||||||
self.online = False
|
self.online = False
|
||||||
self.detached = False
|
self.detached = False
|
||||||
self.kiss_framing = False
|
self.kiss_framing = False
|
||||||
|
self.tunnels_allowed = True
|
||||||
self.i2p_tunneled = True
|
self.i2p_tunneled = True
|
||||||
self.i2p_dest = None
|
self.i2p_dest = None
|
||||||
self.i2p_tunnel_ready = False
|
self.i2p_tunnel_ready = False
|
||||||
|
self.lossless_medium = True
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
self.bitrate = I2PInterface.BITRATE_GUESS
|
self.bitrate = I2PInterface.BITRATE_GUESS
|
||||||
self.last_read = 0
|
self.last_read = 0
|
||||||
|
|
@ -423,18 +370,11 @@ class I2PInterfacePeer(Interface):
|
||||||
self.ifac_netkey = self.parent_interface.ifac_netkey
|
self.ifac_netkey = self.parent_interface.ifac_netkey
|
||||||
if self.ifac_netname != None or self.ifac_netkey != None:
|
if self.ifac_netname != None or self.ifac_netkey != None:
|
||||||
ifac_origin = b""
|
ifac_origin = b""
|
||||||
if self.ifac_netname != None:
|
if self.ifac_netname != None: ifac_origin += RNS.Identity.full_hash(self.ifac_netname.encode("utf-8"))
|
||||||
ifac_origin += RNS.Identity.full_hash(self.ifac_netname.encode("utf-8"))
|
if self.ifac_netkey != None: ifac_origin += RNS.Identity.full_hash(self.ifac_netkey.encode("utf-8"))
|
||||||
if self.ifac_netkey != None:
|
|
||||||
ifac_origin += RNS.Identity.full_hash(self.ifac_netkey.encode("utf-8"))
|
|
||||||
|
|
||||||
ifac_origin_hash = RNS.Identity.full_hash(ifac_origin)
|
ifac_origin_hash = RNS.Identity.full_hash(ifac_origin)
|
||||||
self.ifac_key = RNS.Cryptography.hkdf(
|
self.ifac_key = RNS.Cryptography.hkdf(length=64, derive_from=ifac_origin_hash, salt=RNS.Reticulum.IFAC_SALT, context=None)
|
||||||
length=64,
|
|
||||||
derive_from=ifac_origin_hash,
|
|
||||||
salt=RNS.Reticulum.IFAC_SALT,
|
|
||||||
context=None
|
|
||||||
)
|
|
||||||
self.ifac_identity = RNS.Identity.from_bytes(self.ifac_key)
|
self.ifac_identity = RNS.Identity.from_bytes(self.ifac_key)
|
||||||
self.ifac_signature = self.ifac_identity.sign(RNS.Identity.full_hash(self.ifac_key))
|
self.ifac_signature = self.ifac_identity.sign(RNS.Identity.full_hash(self.ifac_key))
|
||||||
|
|
||||||
|
|
@ -442,10 +382,8 @@ class I2PInterfacePeer(Interface):
|
||||||
self.announce_rate_grace = None
|
self.announce_rate_grace = None
|
||||||
self.announce_rate_penalty = None
|
self.announce_rate_penalty = None
|
||||||
|
|
||||||
if max_reconnect_tries == None:
|
if max_reconnect_tries == None: self.max_reconnect_tries = I2PInterfacePeer.RECONNECT_MAX_TRIES
|
||||||
self.max_reconnect_tries = I2PInterfacePeer.RECONNECT_MAX_TRIES
|
else: self.max_reconnect_tries = max_reconnect_tries
|
||||||
else:
|
|
||||||
self.max_reconnect_tries = max_reconnect_tries
|
|
||||||
|
|
||||||
if connected_socket != None:
|
if connected_socket != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
|
|
@ -453,15 +391,12 @@ class I2PInterfacePeer(Interface):
|
||||||
self.target_port = None
|
self.target_port = None
|
||||||
self.socket = connected_socket
|
self.socket = connected_socket
|
||||||
|
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux": self.set_timeouts_linux()
|
||||||
self.set_timeouts_linux()
|
elif platform.system() == "Darwin": self.set_timeouts_osx()
|
||||||
elif platform.system() == "Darwin":
|
|
||||||
self.set_timeouts_osx()
|
|
||||||
|
|
||||||
elif target_i2p_dest != None:
|
elif target_i2p_dest != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
self.initiator = True
|
self.initiator = True
|
||||||
|
|
||||||
self.bind_ip = "127.0.0.1"
|
self.bind_ip = "127.0.0.1"
|
||||||
|
|
||||||
self.awaiting_i2p_tunnel = True
|
self.awaiting_i2p_tunnel = True
|
||||||
|
|
@ -489,13 +424,9 @@ class I2PInterfacePeer(Interface):
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def wait_job():
|
def wait_job():
|
||||||
while self.awaiting_i2p_tunnel:
|
while self.awaiting_i2p_tunnel: time.sleep(0.25)
|
||||||
time.sleep(0.25)
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
if not self.kiss_framing:
|
|
||||||
self.wants_tunnel = True
|
|
||||||
|
|
||||||
if not self.connect(initial=True):
|
if not self.connect(initial=True):
|
||||||
thread = threading.Thread(target=self.reconnect)
|
thread = threading.Thread(target=self.reconnect)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
|
|
@ -509,7 +440,6 @@ class I2PInterfacePeer(Interface):
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
|
||||||
def set_timeouts_linux(self):
|
def set_timeouts_linux(self):
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(I2PInterfacePeer.I2P_USER_TIMEOUT * 1000))
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(I2PInterfacePeer.I2P_USER_TIMEOUT * 1000))
|
||||||
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
|
|
@ -518,10 +448,8 @@ class I2PInterfacePeer(Interface):
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, int(I2PInterfacePeer.I2P_PROBES))
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, int(I2PInterfacePeer.I2P_PROBES))
|
||||||
|
|
||||||
def set_timeouts_osx(self):
|
def set_timeouts_osx(self):
|
||||||
if hasattr(socket, "TCP_KEEPALIVE"):
|
if hasattr(socket, "TCP_KEEPALIVE"): TCP_KEEPIDLE = socket.TCP_KEEPALIVE
|
||||||
TCP_KEEPIDLE = socket.TCP_KEEPALIVE
|
else: TCP_KEEPIDLE = 0x10
|
||||||
else:
|
|
||||||
TCP_KEEPIDLE = 0x10
|
|
||||||
|
|
||||||
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(I2PInterfacePeer.I2P_PROBE_AFTER))
|
self.socket.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(I2PInterfacePeer.I2P_PROBE_AFTER))
|
||||||
|
|
@ -529,16 +457,12 @@ class I2PInterfacePeer(Interface):
|
||||||
def shutdown_socket(self, target_socket):
|
def shutdown_socket(self, target_socket):
|
||||||
if callable(target_socket.close):
|
if callable(target_socket.close):
|
||||||
try:
|
try:
|
||||||
if socket != None:
|
if socket != None: target_socket.shutdown(socket.SHUT_RDWR)
|
||||||
target_socket.shutdown(socket.SHUT_RDWR)
|
except Exception as e: RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if socket != None:
|
if socket != None: target_socket.close()
|
||||||
target_socket.close()
|
except Exception as e: RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
RNS.log("Detaching "+str(self), RNS.LOG_DEBUG)
|
RNS.log("Detaching "+str(self), RNS.LOG_DEBUG)
|
||||||
|
|
@ -547,22 +471,39 @@ class I2PInterfacePeer(Interface):
|
||||||
if callable(self.socket.close):
|
if callable(self.socket.close):
|
||||||
self.detached = True
|
self.detached = True
|
||||||
|
|
||||||
try:
|
try: self.socket.shutdown(socket.SHUT_RDWR)
|
||||||
self.socket.shutdown(socket.SHUT_RDWR)
|
except Exception as e: RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
try:
|
try: self.socket.close()
|
||||||
self.socket.close()
|
except Exception as e: RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
self.socket = None
|
self.socket = None
|
||||||
|
|
||||||
|
def early_synthesis(self, packet):
|
||||||
|
if not self.detached:
|
||||||
|
if not packet.packed: packet.pack()
|
||||||
|
if self.ifac_identity: raw = RNS.Transport.handle_outgoing_ifac(self, packet.raw);
|
||||||
|
else: raw = packet.raw
|
||||||
|
|
||||||
|
if self.kiss_framing: frame = bytes([KISS.FEND])+bytes([KISS.CMD_DATA])+KISS.escape(raw)+bytes([KISS.FEND])
|
||||||
|
else: frame = bytes([HDLC.FLAG])+HDLC.escape(raw)+bytes([HDLC.FLAG])
|
||||||
|
|
||||||
|
self.socket.sendall(frame)
|
||||||
|
self.txb += len(frame)
|
||||||
|
self.last_write = time.time()
|
||||||
|
if hasattr(self, "parent_interface") and self.parent_interface != None and self.parent_count: self.parent_interface.txb += len(frame)
|
||||||
|
|
||||||
def connect(self, initial=False):
|
def connect(self, initial=False):
|
||||||
try:
|
try:
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.socket.connect((self.target_ip, self.target_port))
|
self.socket.connect((self.target_ip, self.target_port))
|
||||||
|
|
||||||
|
if not self.kiss_framing:
|
||||||
|
self.wants_tunnel = True
|
||||||
|
self.remote_transport = None
|
||||||
|
self.synthesis_received = False
|
||||||
|
RNS.Transport.synthesize_tunnel(self)
|
||||||
|
|
||||||
self.online = True
|
self.online = True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -570,24 +511,16 @@ class I2PInterfacePeer(Interface):
|
||||||
if not self.awaiting_i2p_tunnel:
|
if not self.awaiting_i2p_tunnel:
|
||||||
RNS.log("Initial connection for "+str(self)+" could not be established: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Initial connection for "+str(self)+" could not be established: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.log("Leaving unconnected and retrying connection in "+str(I2PInterfacePeer.RECONNECT_WAIT)+" seconds.", RNS.LOG_ERROR)
|
RNS.log("Leaving unconnected and retrying connection in "+str(I2PInterfacePeer.RECONNECT_WAIT)+" seconds.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
else: raise e
|
||||||
else:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux": self.set_timeouts_linux()
|
||||||
self.set_timeouts_linux()
|
elif platform.system() == "Darwin": self.set_timeouts_osx()
|
||||||
elif platform.system() == "Darwin":
|
|
||||||
self.set_timeouts_osx()
|
|
||||||
|
|
||||||
self.online = True
|
self.online = True
|
||||||
self.writing = False
|
self.writing = False
|
||||||
self.never_connected = False
|
self.never_connected = False
|
||||||
|
|
||||||
if not self.kiss_framing and self.wants_tunnel:
|
|
||||||
RNS.Transport.synthesize_tunnel(self)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def reconnect(self):
|
def reconnect(self):
|
||||||
|
|
@ -604,14 +537,10 @@ class I2PInterfacePeer(Interface):
|
||||||
self.teardown()
|
self.teardown()
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try: self.connect()
|
||||||
self.connect()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if not self.awaiting_i2p_tunnel:
|
if not self.awaiting_i2p_tunnel: RNS.log("Connection attempt for "+str(self)+" failed: "+str(e), RNS.LOG_DEBUG)
|
||||||
RNS.log("Connection attempt for "+str(self)+" failed: "+str(e), RNS.LOG_DEBUG)
|
else: RNS.log(str(self)+" still waiting for I2P tunnel to appear", RNS.LOG_VERBOSE)
|
||||||
else:
|
|
||||||
RNS.log(str(self)+" still waiting for I2P tunnel to appear", RNS.LOG_VERBOSE)
|
|
||||||
|
|
||||||
if not self.never_connected:
|
if not self.never_connected:
|
||||||
RNS.log(str(self)+" Re-established connection via I2P tunnel", RNS.LOG_INFO)
|
RNS.log(str(self)+" Re-established connection via I2P tunnel", RNS.LOG_INFO)
|
||||||
|
|
@ -620,40 +549,31 @@ class I2PInterfacePeer(Interface):
|
||||||
thread = threading.Thread(target=self.read_loop)
|
thread = threading.Thread(target=self.read_loop)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
if not self.kiss_framing:
|
|
||||||
RNS.Transport.synthesize_tunnel(self)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log("Attempt to reconnect on a non-initiator I2P interface. This should not happen.", RNS.LOG_ERROR)
|
RNS.log("Attempt to reconnect on a non-initiator I2P interface. This should not happen.", RNS.LOG_ERROR)
|
||||||
raise IOError("Attempt to reconnect on a non-initiator I2P interface")
|
raise IOError("Attempt to reconnect on a non-initiator I2P interface")
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None and self.parent_count:
|
|
||||||
self.parent_interface.rxb += len(data)
|
|
||||||
|
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
if self.online:
|
if self.online:
|
||||||
while self.writing:
|
while self.writing: time.sleep(0.001)
|
||||||
time.sleep(0.001)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.writing = True
|
self.writing = True
|
||||||
|
|
||||||
if self.kiss_framing:
|
if self.kiss_framing: data = bytes([KISS.FEND])+bytes([KISS.CMD_DATA])+KISS.escape(data)+bytes([KISS.FEND])
|
||||||
data = bytes([KISS.FEND])+bytes([KISS.CMD_DATA])+KISS.escape(data)+bytes([KISS.FEND])
|
else: data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
|
||||||
else:
|
|
||||||
data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
|
|
||||||
|
|
||||||
self.socket.sendall(data)
|
self.socket.sendall(data)
|
||||||
self.writing = False
|
self.writing = False
|
||||||
self.txb += len(data)
|
self.txb += len(data)
|
||||||
self.last_write = time.time()
|
self.last_write = time.time()
|
||||||
|
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None and self.parent_count:
|
if hasattr(self, "parent_interface") and self.parent_interface != None and self.parent_count: self.parent_interface.txb += len(data)
|
||||||
self.parent_interface.txb += len(data)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Exception occurred while transmitting via "+str(self)+", tearing down interface", RNS.LOG_ERROR)
|
RNS.log("Exception occurred while transmitting via "+str(self)+", tearing down interface", RNS.LOG_ERROR)
|
||||||
|
|
@ -662,8 +582,7 @@ class I2PInterfacePeer(Interface):
|
||||||
|
|
||||||
|
|
||||||
def read_watchdog(self):
|
def read_watchdog(self):
|
||||||
while self.wd_reset:
|
while self.wd_reset: time.sleep(0.25)
|
||||||
time.sleep(0.25)
|
|
||||||
|
|
||||||
should_run = True
|
should_run = True
|
||||||
try:
|
try:
|
||||||
|
|
@ -671,17 +590,13 @@ class I2PInterfacePeer(Interface):
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
if (time.time()-self.last_read > I2PInterfacePeer.I2P_PROBE_AFTER*2):
|
if (time.time()-self.last_read > I2PInterfacePeer.I2P_PROBE_AFTER*2):
|
||||||
if self.i2p_tunnel_state != I2PInterfacePeer.TUNNEL_STATE_STALE:
|
if self.i2p_tunnel_state != I2PInterfacePeer.TUNNEL_STATE_STALE: RNS.log("I2P tunnel became unresponsive", RNS.LOG_DEBUG)
|
||||||
RNS.log("I2P tunnel became unresponsive", RNS.LOG_DEBUG)
|
|
||||||
|
|
||||||
self.i2p_tunnel_state = I2PInterfacePeer.TUNNEL_STATE_STALE
|
self.i2p_tunnel_state = I2PInterfacePeer.TUNNEL_STATE_STALE
|
||||||
else:
|
else: self.i2p_tunnel_state = I2PInterfacePeer.TUNNEL_STATE_ACTIVE
|
||||||
self.i2p_tunnel_state = I2PInterfacePeer.TUNNEL_STATE_ACTIVE
|
|
||||||
|
|
||||||
if (time.time()-self.last_write > I2PInterfacePeer.I2P_PROBE_AFTER*1):
|
if (time.time()-self.last_write > I2PInterfacePeer.I2P_PROBE_AFTER*1):
|
||||||
try:
|
try:
|
||||||
if self.socket != None:
|
if self.socket != None: self.socket.sendall(bytes([HDLC.FLAG, HDLC.FLAG]))
|
||||||
self.socket.sendall(bytes([HDLC.FLAG, HDLC.FLAG]))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("An error ocurred while sending I2P keepalive. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("An error ocurred while sending I2P keepalive. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
self.shutdown_socket(self.socket)
|
self.shutdown_socket(self.socket)
|
||||||
|
|
@ -690,22 +605,17 @@ class I2PInterfacePeer(Interface):
|
||||||
if (time.time()-self.last_read > I2PInterfacePeer.I2P_READ_TIMEOUT):
|
if (time.time()-self.last_read > I2PInterfacePeer.I2P_READ_TIMEOUT):
|
||||||
RNS.log("I2P socket is unresponsive, restarting...", RNS.LOG_WARNING)
|
RNS.log("I2P socket is unresponsive, restarting...", RNS.LOG_WARNING)
|
||||||
if self.socket != None:
|
if self.socket != None:
|
||||||
try:
|
try: self.socket.shutdown(socket.SHUT_RDWR)
|
||||||
self.socket.shutdown(socket.SHUT_RDWR)
|
except Exception as e: RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while shutting down socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
try:
|
try: self.socket.close()
|
||||||
self.socket.close()
|
except Exception as e: RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while closing socket for "+str(self)+": "+str(e))
|
|
||||||
|
|
||||||
should_run = False
|
should_run = False
|
||||||
|
|
||||||
self.wd_reset = False
|
self.wd_reset = False
|
||||||
|
|
||||||
finally:
|
finally: self.wd_reset = False
|
||||||
self.wd_reset = False
|
|
||||||
|
|
||||||
def read_loop(self):
|
def read_loop(self):
|
||||||
try:
|
try:
|
||||||
|
|
@ -776,7 +686,6 @@ class I2PInterfacePeer(Interface):
|
||||||
data_buffer = data_buffer+bytes([byte])
|
data_buffer = data_buffer+bytes([byte])
|
||||||
else:
|
else:
|
||||||
self.online = False
|
self.online = False
|
||||||
|
|
||||||
self.wd_reset = True
|
self.wd_reset = True
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
self.wd_reset = False
|
self.wd_reset = False
|
||||||
|
|
@ -798,15 +707,12 @@ class I2PInterfacePeer(Interface):
|
||||||
if self.initiator:
|
if self.initiator:
|
||||||
RNS.log("Attempting to reconnect...", RNS.LOG_WARNING)
|
RNS.log("Attempting to reconnect...", RNS.LOG_WARNING)
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
else:
|
else: self.teardown()
|
||||||
self.teardown()
|
|
||||||
|
|
||||||
def teardown(self):
|
def teardown(self):
|
||||||
if self.initiator and not self.detached:
|
if self.initiator and not self.detached:
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is being torn down. Restart Reticulum to attempt to open this interface again.", RNS.LOG_ERROR)
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is being torn down. Restart Reticulum to attempt to open this interface again.", RNS.LOG_ERROR)
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log("The interface "+str(self)+" is being torn down.", RNS.LOG_VERBOSE)
|
RNS.log("The interface "+str(self)+" is being torn down.", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
|
|
@ -818,10 +724,7 @@ class I2PInterfacePeer(Interface):
|
||||||
while self in self.parent_interface.spawned_interfaces:
|
while self in self.parent_interface.spawned_interfaces:
|
||||||
self.parent_interface.spawned_interfaces.remove(self)
|
self.parent_interface.spawned_interfaces.remove(self)
|
||||||
|
|
||||||
if self in RNS.Transport.interfaces:
|
if not self.initiator: RNS.Transport.remove_interface(self)
|
||||||
if not self.initiator:
|
|
||||||
RNS.Transport.interfaces.remove(self)
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "I2PInterfacePeer["+str(self.name)+"]"
|
return "I2PInterfacePeer["+str(self.name)+"]"
|
||||||
|
|
@ -832,8 +735,7 @@ class I2PInterface(Interface):
|
||||||
DEFAULT_IFAC_SIZE = 16
|
DEFAULT_IFAC_SIZE = 16
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def clients(self):
|
def clients(self): return len(self.spawned_interfaces)
|
||||||
return len(self.spawned_interfaces)
|
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
@ -854,6 +756,8 @@ class I2PInterface(Interface):
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.connectable = connectable
|
self.connectable = connectable
|
||||||
self.i2p_tunneled = True
|
self.i2p_tunneled = True
|
||||||
|
self.tunnels_allowed = True
|
||||||
|
self.peers = peers
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
|
|
||||||
self.b32 = None
|
self.b32 = None
|
||||||
|
|
@ -863,7 +767,6 @@ class I2PInterface(Interface):
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
self.receives = True
|
self.receives = True
|
||||||
self.bind_ip = "127.0.0.1"
|
self.bind_ip = "127.0.0.1"
|
||||||
self.bind_port = self.i2p.get_free_port()
|
self.bind_port = self.i2p.get_free_port()
|
||||||
|
|
@ -872,6 +775,8 @@ class I2PInterface(Interface):
|
||||||
self.ifac_size = ifac_size
|
self.ifac_size = ifac_size
|
||||||
self.ifac_netname = ifac_netname
|
self.ifac_netname = ifac_netname
|
||||||
self.ifac_netkey = ifac_netkey
|
self.ifac_netkey = ifac_netkey
|
||||||
|
self.supports_discovery = True
|
||||||
|
self.lossless_medium = True
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
|
|
||||||
|
|
@ -886,11 +791,9 @@ class I2PInterface(Interface):
|
||||||
RNS.log("I2P controller did not become available in time, waiting for controller", RNS.LOG_VERBOSE)
|
RNS.log("I2P controller did not become available in time, waiting for controller", RNS.LOG_VERBOSE)
|
||||||
i2p_notready_warning = True
|
i2p_notready_warning = True
|
||||||
|
|
||||||
while not self.i2p.ready:
|
while not self.i2p.ready: time.sleep(0.25)
|
||||||
time.sleep(0.25)
|
|
||||||
|
|
||||||
if i2p_notready_warning == True:
|
if i2p_notready_warning == True: RNS.log("I2P controller ready, continuing setup", RNS.LOG_VERBOSE)
|
||||||
RNS.log("I2P controller ready, continuing setup", RNS.LOG_VERBOSE)
|
|
||||||
|
|
||||||
def handlerFactory(callback):
|
def handlerFactory(callback):
|
||||||
def createHandler(*args, **keys):
|
def createHandler(*args, **keys):
|
||||||
|
|
@ -904,6 +807,7 @@ class I2PInterface(Interface):
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
|
def final_init(self):
|
||||||
if self.connectable:
|
if self.connectable:
|
||||||
def tunnel_job():
|
def tunnel_job():
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -923,22 +827,83 @@ class I2PInterface(Interface):
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
if peers != None:
|
threading.Thread(target=self._spawn_peers, daemon=True).start()
|
||||||
for peer_addr in peers:
|
|
||||||
interface_name = self.name+" to "+peer_addr
|
def _spawn_peers(self):
|
||||||
peer_interface = I2PInterfacePeer(self, self.owner, interface_name, peer_addr)
|
time.sleep(1.5)
|
||||||
peer_interface.OUT = True
|
if self.peers != None:
|
||||||
peer_interface.IN = True
|
for peer_addr in self.peers:
|
||||||
peer_interface.parent_interface = self
|
try:
|
||||||
peer_interface.parent_count = False
|
interface_name = self.name+" to "+peer_addr
|
||||||
RNS.Transport.interfaces.append(peer_interface)
|
peer_interface = I2PInterfacePeer(self, self.owner, interface_name, peer_addr)
|
||||||
|
peer_interface.OUT = self.OUT
|
||||||
|
peer_interface.IN = self.IN
|
||||||
|
peer_interface.parent_interface = self
|
||||||
|
peer_interface.parent_count = False
|
||||||
|
|
||||||
|
peer_interface.mode = self.mode
|
||||||
|
peer_interface.bitrate = self.bitrate
|
||||||
|
peer_interface.recursive_prs = self.recursive_prs
|
||||||
|
peer_interface.gravity = self.gravity
|
||||||
|
peer_interface.HW_MTU = self.HW_MTU
|
||||||
|
|
||||||
|
peer_interface.announces_to_internal = self.announces_to_internal
|
||||||
|
peer_interface.announces_from_internal = self.announces_from_internal
|
||||||
|
peer_interface.announce_cap = self.announce_cap
|
||||||
|
|
||||||
|
peer_interface.ingress_control = self.ingress_control
|
||||||
|
peer_interface.ic_max_held_announces = self.ic_max_held_announces
|
||||||
|
peer_interface.ic_burst_hold = self.ic_burst_hold
|
||||||
|
peer_interface.ic_burst_freq = self.ic_burst_freq
|
||||||
|
peer_interface.ic_burst_freq_new = self.ic_burst_freq_new
|
||||||
|
peer_interface.ic_new_time = self.ic_new_time
|
||||||
|
peer_interface.ic_burst_penalty = self.ic_burst_penalty
|
||||||
|
peer_interface.ic_held_release_interval = self.ic_held_release_interval
|
||||||
|
|
||||||
|
peer_interface.egress_control = self.egress_control
|
||||||
|
peer_interface.ec_pr_freq = self.ec_pr_freq
|
||||||
|
peer_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new
|
||||||
|
peer_interface.ic_pr_burst_freq = self.ic_pr_burst_freq
|
||||||
|
|
||||||
|
peer_interface.announce_rate_target = self.announce_rate_target
|
||||||
|
peer_interface.announce_rate_grace = self.announce_rate_grace
|
||||||
|
peer_interface.announce_rate_penalty = self.announce_rate_penalty
|
||||||
|
|
||||||
|
peer_interface._destination_cost = self._destination_cost
|
||||||
|
peer_interface._pr_cost = self._pr_cost
|
||||||
|
|
||||||
|
RNS.Transport.add_interface(peer_interface)
|
||||||
|
|
||||||
|
except Exception as e: RNS.log(f"Could not create interface for {self} to {peer_addr}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def incoming_connection(self, handler):
|
def incoming_connection(self, handler):
|
||||||
RNS.log("Accepting incoming I2P connection", RNS.LOG_VERBOSE)
|
RNS.log("Accepting incoming I2P connection", RNS.LOG_VERBOSE)
|
||||||
interface_name = "Connected peer on "+self.name
|
interface_name = "Connected peer on "+self.name
|
||||||
spawned_interface = I2PInterfacePeer(self, self.owner, interface_name, connected_socket=handler.request)
|
spawned_interface = I2PInterfacePeer(self, self.owner, interface_name, connected_socket=handler.request)
|
||||||
spawned_interface.OUT = True
|
spawned_interface.OUT = self.OUT
|
||||||
spawned_interface.IN = True
|
spawned_interface.IN = self.IN
|
||||||
|
|
||||||
|
spawned_interface.ingress_control = self.ingress_control
|
||||||
|
spawned_interface.ic_max_held_announces = self.ic_max_held_announces
|
||||||
|
spawned_interface.ic_burst_hold = self.ic_burst_hold
|
||||||
|
spawned_interface.ic_burst_freq = self.ic_burst_freq
|
||||||
|
spawned_interface.ic_burst_freq_new = self.ic_burst_freq_new
|
||||||
|
spawned_interface.ic_new_time = self.ic_new_time
|
||||||
|
spawned_interface.ic_burst_penalty = self.ic_burst_penalty
|
||||||
|
spawned_interface.ic_held_release_interval = self.ic_held_release_interval
|
||||||
|
|
||||||
|
spawned_interface.announces_to_internal = self.announces_to_internal
|
||||||
|
spawned_interface.announces_from_internal = self.announces_from_internal
|
||||||
|
spawned_interface.announce_cap = self.announce_cap
|
||||||
|
|
||||||
|
spawned_interface.egress_control = self.egress_control
|
||||||
|
spawned_interface.ec_pr_freq = self.ec_pr_freq
|
||||||
|
spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new
|
||||||
|
spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq
|
||||||
|
|
||||||
|
spawned_interface._destination_cost = self._destination_cost
|
||||||
|
spawned_interface._pr_cost = self._pr_cost
|
||||||
|
|
||||||
spawned_interface.parent_interface = self
|
spawned_interface.parent_interface = self
|
||||||
spawned_interface.online = True
|
spawned_interface.online = True
|
||||||
spawned_interface.bitrate = self.bitrate
|
spawned_interface.bitrate = self.bitrate
|
||||||
|
|
@ -948,18 +913,11 @@ class I2PInterface(Interface):
|
||||||
spawned_interface.ifac_netkey = self.ifac_netkey
|
spawned_interface.ifac_netkey = self.ifac_netkey
|
||||||
if spawned_interface.ifac_netname != None or spawned_interface.ifac_netkey != None:
|
if spawned_interface.ifac_netname != None or spawned_interface.ifac_netkey != None:
|
||||||
ifac_origin = b""
|
ifac_origin = b""
|
||||||
if spawned_interface.ifac_netname != None:
|
if spawned_interface.ifac_netname != None: ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netname.encode("utf-8"))
|
||||||
ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netname.encode("utf-8"))
|
if spawned_interface.ifac_netkey != None: ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netkey.encode("utf-8"))
|
||||||
if spawned_interface.ifac_netkey != None:
|
|
||||||
ifac_origin += RNS.Identity.full_hash(spawned_interface.ifac_netkey.encode("utf-8"))
|
|
||||||
|
|
||||||
ifac_origin_hash = RNS.Identity.full_hash(ifac_origin)
|
ifac_origin_hash = RNS.Identity.full_hash(ifac_origin)
|
||||||
spawned_interface.ifac_key = RNS.Cryptography.hkdf(
|
spawned_interface.ifac_key = RNS.Cryptography.hkdf(length=64, derive_from=ifac_origin_hash, salt=RNS.Reticulum.IFAC_SALT, context=None)
|
||||||
length=64,
|
|
||||||
derive_from=ifac_origin_hash,
|
|
||||||
salt=RNS.Reticulum.IFAC_SALT,
|
|
||||||
context=None
|
|
||||||
)
|
|
||||||
spawned_interface.ifac_identity = RNS.Identity.from_bytes(spawned_interface.ifac_key)
|
spawned_interface.ifac_identity = RNS.Identity.from_bytes(spawned_interface.ifac_key)
|
||||||
spawned_interface.ifac_signature = spawned_interface.ifac_identity.sign(RNS.Identity.full_hash(spawned_interface.ifac_key))
|
spawned_interface.ifac_signature = spawned_interface.ifac_identity.sign(RNS.Identity.full_hash(spawned_interface.ifac_key))
|
||||||
|
|
||||||
|
|
@ -967,34 +925,48 @@ class I2PInterface(Interface):
|
||||||
spawned_interface.announce_rate_grace = self.announce_rate_grace
|
spawned_interface.announce_rate_grace = self.announce_rate_grace
|
||||||
spawned_interface.announce_rate_penalty = self.announce_rate_penalty
|
spawned_interface.announce_rate_penalty = self.announce_rate_penalty
|
||||||
spawned_interface.mode = self.mode
|
spawned_interface.mode = self.mode
|
||||||
|
spawned_interface.recursive_prs = self.recursive_prs
|
||||||
|
spawned_interface.gravity = self.gravity
|
||||||
|
spawned_interface.hop_limiter = self.hop_limiter
|
||||||
spawned_interface.HW_MTU = self.HW_MTU
|
spawned_interface.HW_MTU = self.HW_MTU
|
||||||
RNS.log("Spawned new I2PInterface Peer: "+str(spawned_interface), RNS.LOG_VERBOSE)
|
RNS.log("Spawned new I2PInterface Peer: "+str(spawned_interface), RNS.LOG_VERBOSE)
|
||||||
RNS.Transport.interfaces.append(spawned_interface)
|
RNS.Transport.add_interface(spawned_interface)
|
||||||
while spawned_interface in self.spawned_interfaces:
|
while spawned_interface in self.spawned_interfaces: self.spawned_interfaces.remove(spawned_interface)
|
||||||
self.spawned_interfaces.remove(spawned_interface)
|
|
||||||
self.spawned_interfaces.append(spawned_interface)
|
self.spawned_interfaces.append(spawned_interface)
|
||||||
spawned_interface.read_loop()
|
spawned_interface.read_loop()
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def received_announce(self, from_spawned=False):
|
def received_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.ia_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.ia_freq_deque.append(time.time())
|
||||||
|
self.arxb += size
|
||||||
|
|
||||||
def sent_announce(self, from_spawned=False):
|
def sent_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.oa_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.oa_freq_deque.append(time.time())
|
||||||
|
self.atxb += size
|
||||||
|
|
||||||
|
def received_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.ip_freq_deque.append(time.time())
|
||||||
|
self.prxb += size
|
||||||
|
|
||||||
|
def sent_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.op_freq_deque.append(time.time())
|
||||||
|
self.ptxb += size
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
RNS.log("Detaching "+str(self), RNS.LOG_DEBUG)
|
RNS.log("Detaching "+str(self), RNS.LOG_DEBUG)
|
||||||
self.i2p.stop()
|
self.i2p.stop()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "I2PInterface["+self.name+"]"
|
||||||
return "I2PInterface["+self.name+"]"
|
|
||||||
|
|
||||||
class I2PInterfaceHandler(socketserver.BaseRequestHandler):
|
class I2PInterfaceHandler(socketserver.BaseRequestHandler):
|
||||||
def __init__(self, callback, *args, **keys):
|
def __init__(self, callback, *args, **keys):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
|
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
|
||||||
|
|
||||||
def handle(self):
|
def handle(self): self.callback(handler=self)
|
||||||
self.callback(handler=self)
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -31,7 +39,6 @@ class Interface:
|
||||||
OUT = False
|
OUT = False
|
||||||
FWD = False
|
FWD = False
|
||||||
RPT = False
|
RPT = False
|
||||||
name = None
|
|
||||||
|
|
||||||
# Interface mode definitions
|
# Interface mode definitions
|
||||||
MODE_FULL = 0x01
|
MODE_FULL = 0x01
|
||||||
|
|
@ -40,59 +47,167 @@ class Interface:
|
||||||
MODE_ROAMING = 0x04
|
MODE_ROAMING = 0x04
|
||||||
MODE_BOUNDARY = 0x05
|
MODE_BOUNDARY = 0x05
|
||||||
MODE_GATEWAY = 0x06
|
MODE_GATEWAY = 0x06
|
||||||
|
MODE_INTERNAL = 0x07
|
||||||
|
|
||||||
# Which interface modes a Transport Node should
|
# Which interface modes a Transport Node should
|
||||||
# actively discover paths for.
|
# actively discover paths for.
|
||||||
DISCOVER_PATHS_FOR = [MODE_ACCESS_POINT, MODE_GATEWAY, MODE_ROAMING]
|
DISCOVER_PATHS_FOR = [MODE_ACCESS_POINT, MODE_GATEWAY, MODE_ROAMING, MODE_INTERNAL]
|
||||||
|
BOUNDARY_SEARCH_MODES = [MODE_BOUNDARY, MODE_GATEWAY]
|
||||||
|
|
||||||
# How many samples to use for announce
|
# How many samples to use for announce
|
||||||
# frequency calculations
|
# frequency calculations
|
||||||
IA_FREQ_SAMPLES = 6
|
IA_FREQ_SAMPLES = 48
|
||||||
OA_FREQ_SAMPLES = 6
|
OA_FREQ_SAMPLES = 48
|
||||||
|
IP_FREQ_SAMPLES = 48
|
||||||
|
OP_FREQ_SAMPLES = 48
|
||||||
|
|
||||||
|
AR_MINFREQ_HZ = 0.1
|
||||||
|
PR_MINFREQ_HZ = 0.1
|
||||||
|
AR_FREQ_DECAY = 1/AR_MINFREQ_HZ
|
||||||
|
PR_FREQ_DECAY = 1/PR_MINFREQ_HZ
|
||||||
|
|
||||||
# Maximum amount of ingress limited announces
|
# Maximum amount of ingress limited announces
|
||||||
# to hold at any given time.
|
# to hold at any given time.
|
||||||
MAX_HELD_ANNOUNCES = 256
|
MAX_HELD_ANNOUNCES = 64
|
||||||
|
|
||||||
# How long a spawned interface will be
|
# Control parameters
|
||||||
# considered to be newly created. Two
|
DEFAULT_GRAVITY = 0
|
||||||
# hours by default.
|
|
||||||
IC_NEW_TIME = 2*60*60
|
IC_NEW_TIME = 2*60*60
|
||||||
IC_BURST_FREQ_NEW = 3.5
|
IC_BURST_FREQ_NEW = 3
|
||||||
IC_BURST_FREQ = 12
|
IC_BURST_FREQ = 10
|
||||||
IC_BURST_HOLD = 1*60
|
IC_PR_BURST_FREQ_NEW = 3
|
||||||
IC_BURST_PENALTY = 5*60
|
IC_PR_BURST_FREQ = 8
|
||||||
IC_HELD_RELEASE_INTERVAL = 30
|
IC_BURST_HOLD = 15
|
||||||
|
IC_BURST_PENALTY = 15
|
||||||
|
IC_HELD_RELEASE_INTERVAL = 5
|
||||||
|
IC_DEQUE_MIN_SAMPLE = 2
|
||||||
|
EC_BURST_MIN_SAMPLES = 2
|
||||||
|
EC_PR_FREQ = 5
|
||||||
|
EGRESS_CONTROL = False
|
||||||
|
|
||||||
|
# Default announce rate targets
|
||||||
|
DEFAULT_AR_TARGET = 3600
|
||||||
|
DEFAULT_AR_PENALTY = 0
|
||||||
|
DEFAULT_AR_GRACE = 5
|
||||||
|
|
||||||
AUTOCONFIGURE_MTU = False
|
AUTOCONFIGURE_MTU = False
|
||||||
|
FIXED_MTU = False
|
||||||
|
DEFAULT_IFAC_SIZE = 16
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
self.name = None
|
||||||
self.rxb = 0
|
self.rxb = 0
|
||||||
self.txb = 0
|
self.txb = 0
|
||||||
|
self.frxb = 0
|
||||||
|
self.ftxb = 0
|
||||||
|
self.arxb = 0
|
||||||
|
self.atxb = 0
|
||||||
|
self.arxc = 0
|
||||||
|
self.atxc = 0
|
||||||
|
self.afrxc = 0
|
||||||
|
self.prxb = 0
|
||||||
|
self.ptxb = 0
|
||||||
|
self.prxc = 0
|
||||||
|
self.ptxc = 0
|
||||||
|
self.gravity = 0
|
||||||
self.created = time.time()
|
self.created = time.time()
|
||||||
self.detached = False
|
self.detached = False
|
||||||
self.online = False
|
self.online = False
|
||||||
self.bitrate = 62500
|
self.bitrate = 62500
|
||||||
self.HW_MTU = None
|
self.HW_MTU = None
|
||||||
|
self.__hash = None
|
||||||
|
|
||||||
self.ingress_control = True
|
self.announce_cap = RNS.Reticulum.ANNOUNCE_CAP/100.0
|
||||||
self.ic_max_held_announces = Interface.MAX_HELD_ANNOUNCES
|
self.announce_allowed_at = 0
|
||||||
self.ic_burst_hold = Interface.IC_BURST_HOLD
|
self.supports_discovery = False
|
||||||
self.ic_burst_active = False
|
self.discoverable = False
|
||||||
self.ic_burst_activated = 0
|
self.last_discovery_announce = 0
|
||||||
self.ic_held_release = 0
|
self.bootstrap_only = False
|
||||||
self.ic_burst_freq_new = Interface.IC_BURST_FREQ_NEW
|
self.recursive_prs = False
|
||||||
self.ic_burst_freq = Interface.IC_BURST_FREQ
|
self.announces_from_internal = True
|
||||||
self.ic_new_time = Interface.IC_NEW_TIME
|
self.announces_to_internal = None
|
||||||
self.ic_burst_penalty = Interface.IC_BURST_PENALTY
|
self.is_local_shared_instance = False
|
||||||
self.ic_held_release_interval = Interface.IC_HELD_RELEASE_INTERVAL
|
self.parent_interface = None
|
||||||
self.held_announces = {}
|
self.spawned_interfaces = None
|
||||||
|
self.tunnel_id = None
|
||||||
|
self.local_tunnel_id = None
|
||||||
|
self.tunnels_allowed = False
|
||||||
|
self.synthesis_received = False
|
||||||
|
self.synthesis_emitted = False
|
||||||
|
self.wants_tunnel = False
|
||||||
|
self.remote_transport = None
|
||||||
|
self.ingress_control = True
|
||||||
|
self.hop_limiter = None
|
||||||
|
self.phy_keepalive = False
|
||||||
|
self.shared_medium = False
|
||||||
|
self.lossless_medium = False
|
||||||
|
|
||||||
|
self.remote_impl = None
|
||||||
|
self.remote_vers = None
|
||||||
|
self.remote_dst_cost = None
|
||||||
|
self.remote_pr_cost = None
|
||||||
|
self.remote_is_tn = None
|
||||||
|
|
||||||
|
self.ic_burst_active = False
|
||||||
|
self.ic_burst_activated = 0
|
||||||
|
self.ic_burst_sustained = 0
|
||||||
|
self.ic_pr_burst_active = False
|
||||||
|
self.ic_pr_burst_activated = 0
|
||||||
|
self.ic_pr_burst_sustained = 0
|
||||||
|
self.ic_pr_burst_cooldown = 0
|
||||||
|
self.ic_held_release = 0
|
||||||
|
self.ic_max_held_announces = RNS.Reticulum.get_instance()._default_ic_max_held_announces()
|
||||||
|
self.ic_burst_hold = RNS.Reticulum.get_instance()._default_ic_burst_hold()
|
||||||
|
self.ic_burst_freq_new = RNS.Reticulum.get_instance()._default_ic_burst_freq_new()
|
||||||
|
self.ic_burst_freq = RNS.Reticulum.get_instance()._default_ic_burst_freq()
|
||||||
|
self.ic_pr_burst_freq_new = RNS.Reticulum.get_instance()._default_ic_pr_burst_freq_new()
|
||||||
|
self.ic_pr_burst_freq = RNS.Reticulum.get_instance()._default_ic_pr_burst_freq()
|
||||||
|
self.ic_new_time = RNS.Reticulum.get_instance()._default_ic_new_time()
|
||||||
|
self.ic_burst_penalty = RNS.Reticulum.get_instance()._default_ic_burst_penalty()
|
||||||
|
self.ic_held_release_interval = RNS.Reticulum.get_instance()._default_ic_held_release_interval()
|
||||||
|
self.ec_pr_freq = RNS.Reticulum.get_instance()._default_ec_pr_freq()
|
||||||
|
self.egress_control = RNS.Reticulum.get_instance()._default_egress_control()
|
||||||
|
self.announce_queue = []
|
||||||
|
self.held_announces = {}
|
||||||
|
self.ifac_identity = None
|
||||||
|
self.transmit_buffer = None
|
||||||
|
|
||||||
self.ia_freq_deque = deque(maxlen=Interface.IA_FREQ_SAMPLES)
|
self.ia_freq_deque = deque(maxlen=Interface.IA_FREQ_SAMPLES)
|
||||||
self.oa_freq_deque = deque(maxlen=Interface.OA_FREQ_SAMPLES)
|
self.oa_freq_deque = deque(maxlen=Interface.OA_FREQ_SAMPLES)
|
||||||
|
self.ip_freq_deque = deque(maxlen=Interface.IA_FREQ_SAMPLES)
|
||||||
|
self.op_freq_deque = deque(maxlen=Interface.OA_FREQ_SAMPLES)
|
||||||
|
|
||||||
|
self.protocol_violations = 0
|
||||||
|
self.ifac_violations = 0
|
||||||
|
self.packet_filter_hits = 0
|
||||||
|
self.hop_limiter_hits = 0
|
||||||
|
self.dp_ingress_tcount = 0
|
||||||
|
self.dp_ingress_bytes = 0
|
||||||
|
self.dp_ingress_packets = 0
|
||||||
|
self.dp_ingress_hold = None
|
||||||
|
self.dp_ingress_gated = False
|
||||||
|
|
||||||
|
self.tx_hwm = 4*1024*1024
|
||||||
|
self.tx_stalled = False
|
||||||
|
self.tx_drops = 0
|
||||||
|
self.tx_dropped_bytes = 0
|
||||||
|
self._dp_ec_prev_sent = 0
|
||||||
|
self._dp_ec_zero_ticks = 0
|
||||||
|
self._dp_ec_last_drain = time.time()
|
||||||
|
|
||||||
|
self._destination_cost = None
|
||||||
|
self._pr_cost = None
|
||||||
|
|
||||||
|
self.reports_phy_stats = False
|
||||||
|
self.r_stat_rssi = None
|
||||||
|
self.r_stat_snr = None
|
||||||
|
self.r_stat_q = None
|
||||||
|
|
||||||
|
self.is_connected_to_shared_instance = False
|
||||||
|
|
||||||
def get_hash(self):
|
def get_hash(self):
|
||||||
return RNS.Identity.full_hash(str(self).encode("utf-8"))
|
if not self.__hash: self.__hash = RNS.Identity.full_hash(str(self).encode("utf-8"))
|
||||||
|
return self.__hash
|
||||||
|
|
||||||
# This is a generic function for determining when an interface
|
# This is a generic function for determining when an interface
|
||||||
# should activate ingress limiting. Since this can vary for
|
# should activate ingress limiting. Since this can vary for
|
||||||
|
|
@ -104,132 +219,247 @@ class Interface:
|
||||||
ia_freq = self.incoming_announce_frequency()
|
ia_freq = self.incoming_announce_frequency()
|
||||||
|
|
||||||
if self.ic_burst_active:
|
if self.ic_burst_active:
|
||||||
if ia_freq < freq_threshold and time.time() > self.ic_burst_activated+self.ic_burst_hold:
|
if ia_freq < freq_threshold and time.time() > self.ic_burst_activated+self.ic_burst_hold and time.time() > self.ic_burst_sustained+self.ic_burst_hold:
|
||||||
self.ic_burst_active = False
|
if len(self.ia_freq_deque) >= self.IC_DEQUE_MIN_SAMPLE: self.ic_burst_active = False
|
||||||
self.ic_held_release = time.time() + self.ic_burst_penalty
|
else:
|
||||||
|
if ia_freq >= freq_threshold: self.ic_burst_sustained = time.time()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if ia_freq > freq_threshold:
|
if ia_freq > freq_threshold:
|
||||||
self.ic_burst_active = True
|
self.ic_burst_active = True
|
||||||
self.ic_burst_activated = time.time()
|
self.ic_burst_activated = time.time()
|
||||||
|
self.ic_burst_sustained = time.time()
|
||||||
|
self.ic_held_release = time.time() + self.ic_burst_penalty
|
||||||
return True
|
return True
|
||||||
|
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
|
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
|
def should_ingress_limit_pr(self):
|
||||||
|
if self.ingress_control:
|
||||||
|
freq_threshold = self.ic_pr_burst_freq_new if self.age() < self.ic_new_time else self.ic_pr_burst_freq
|
||||||
|
ip_freq = self.incoming_pr_frequency()
|
||||||
|
|
||||||
|
if self.ic_pr_burst_active:
|
||||||
|
if ip_freq < freq_threshold and time.time() > self.ic_pr_burst_activated+self.ic_burst_hold and time.time() > self.ic_pr_burst_sustained+self.ic_burst_hold:
|
||||||
|
if self.ic_pr_burst_cooldown <= 0: self.ic_pr_burst_active = False
|
||||||
|
else: self.ic_pr_burst_cooldown -= 1
|
||||||
|
else:
|
||||||
|
self.ic_pr_burst_cooldown = 3
|
||||||
|
if ip_freq >= freq_threshold: self.ic_pr_burst_sustained = time.time()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
else:
|
||||||
|
if ip_freq > freq_threshold:
|
||||||
|
self.ic_pr_burst_active = True
|
||||||
|
self.ic_pr_burst_activated = time.time()
|
||||||
|
self.ic_pr_burst_sustained = time.time()
|
||||||
|
self.ic_pr_burst_cooldown = 3
|
||||||
|
return True
|
||||||
|
|
||||||
|
else: return False
|
||||||
|
|
||||||
|
else: return False
|
||||||
|
|
||||||
|
def should_egress_limit_pr(self):
|
||||||
|
if self.egress_control:
|
||||||
|
freq_threshold = self.ec_pr_freq
|
||||||
|
op_freq = self.outgoing_pr_frequency(preemptive=True)
|
||||||
|
|
||||||
|
if op_freq > freq_threshold:
|
||||||
|
if len(self.op_freq_deque) >= self.EC_BURST_MIN_SAMPLES: return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def optimise_mtu(self):
|
def optimise_mtu(self):
|
||||||
if self.AUTOCONFIGURE_MTU:
|
if self.AUTOCONFIGURE_MTU:
|
||||||
if self.bitrate > 16_000_000:
|
if self.bitrate >= 1_000_000_000: self.HW_MTU = 524288
|
||||||
self.HW_MTU = 262144
|
elif self.bitrate >= 750_000_000: self.HW_MTU = 262144
|
||||||
elif self.bitrate > 8_000_000:
|
elif self.bitrate >= 400_000_000: self.HW_MTU = 131072
|
||||||
self.HW_MTU = 131072
|
elif self.bitrate >= 200_000_000: self.HW_MTU = 65536
|
||||||
elif self.bitrate > 4_000_000:
|
elif self.bitrate >= 100_000_000: self.HW_MTU = 32768
|
||||||
self.HW_MTU = 65536
|
elif self.bitrate >= 10_000_000: self.HW_MTU = 16384
|
||||||
elif self.bitrate > 2_000_000:
|
elif self.bitrate >= 5_000_000: self.HW_MTU = 8192
|
||||||
self.HW_MTU = 32768
|
elif self.bitrate >= 2_000_000: self.HW_MTU = 4096
|
||||||
elif self.bitrate > 1_000_000:
|
elif self.bitrate >= 1_000_000: self.HW_MTU = 2048
|
||||||
self.HW_MTU = 16384
|
elif self.bitrate >= 62_500: self.HW_MTU = 1024
|
||||||
elif self.bitrate > 500_000:
|
else: self.HW_MTU = None
|
||||||
self.HW_MTU = 8192
|
|
||||||
elif self.bitrate > 250_000:
|
|
||||||
self.HW_MTU = 4096
|
|
||||||
elif self.bitrate > 125_000:
|
|
||||||
self.HW_MTU = 2048
|
|
||||||
elif self.bitrate > 62_500:
|
|
||||||
self.HW_MTU = 1024
|
|
||||||
else:
|
|
||||||
self.HW_MTU = None
|
|
||||||
|
|
||||||
RNS.log(f"{self} hardware MTU set to {self.HW_MTU}", RNS.LOG_DEBUG) # TODO: Remove debug
|
RNS.log(f"{self} hardware MTU set to {self.HW_MTU}", RNS.LOG_EXTREME)
|
||||||
|
|
||||||
def age(self):
|
def age(self):
|
||||||
return time.time()-self.created
|
return time.time()-self.created
|
||||||
|
|
||||||
def hold_announce(self, announce_packet):
|
def hold_announce(self, announce_packet):
|
||||||
if announce_packet.destination_hash in self.held_announces:
|
if announce_packet.hops >= RNS.Transport.PATHFINDER_M-1:
|
||||||
|
return self.filtered_announce()
|
||||||
|
elif announce_packet.destination_hash in self.held_announces:
|
||||||
self.held_announces[announce_packet.destination_hash] = announce_packet
|
self.held_announces[announce_packet.destination_hash] = announce_packet
|
||||||
|
return self.filtered_announce()
|
||||||
elif not len(self.held_announces) >= self.ic_max_held_announces:
|
elif not len(self.held_announces) >= self.ic_max_held_announces:
|
||||||
self.held_announces[announce_packet.destination_hash] = announce_packet
|
self.held_announces[announce_packet.destination_hash] = announce_packet
|
||||||
|
return True
|
||||||
|
else: return self.filtered_announce()
|
||||||
|
|
||||||
def process_held_announces(self):
|
def process_held_announces(self):
|
||||||
try:
|
try:
|
||||||
if not self.should_ingress_limit() and len(self.held_announces) > 0 and time.time() > self.ic_held_release:
|
if len(self.held_announces) > 0 and time.time() > self.ic_held_release:
|
||||||
freq_threshold = self.ic_burst_freq_new if self.age() < self.ic_new_time else self.ic_burst_freq
|
freq_threshold = self.ic_burst_freq_new if self.age() < self.ic_new_time else self.ic_burst_freq
|
||||||
ia_freq = self.incoming_announce_frequency()
|
ia_freq = self.incoming_announce_frequency()
|
||||||
if ia_freq < freq_threshold:
|
if ia_freq < freq_threshold:
|
||||||
selected_announce_packet = None
|
selected_announce_packet = None
|
||||||
min_hops = RNS.Transport.PATHFINDER_M
|
min_hops = RNS.Transport.PATHFINDER_M
|
||||||
for destination_hash in self.held_announces:
|
cull_entries = []
|
||||||
announce_packet = self.held_announces[destination_hash]
|
for destination_hash in self.held_announces.copy():
|
||||||
|
announce_packet = self.held_announces.get(destination_hash)
|
||||||
|
if not announce_packet: continue
|
||||||
|
|
||||||
|
# If we can determine that a held announce has become
|
||||||
|
# useless for any potential path table updates, we'll
|
||||||
|
# remove it immediately instead of waiting for it to
|
||||||
|
# eventually get released, freeing up other held queue
|
||||||
|
# slots for potentially useful new announces.
|
||||||
|
path_entry = RNS.Transport.path_table.get(destination_hash)
|
||||||
|
if path_entry and not RNS.Transport.announce_prefilter(announce_packet, path_entry):
|
||||||
|
cull_entries.append(destination_hash)
|
||||||
|
continue
|
||||||
|
|
||||||
if announce_packet.hops < min_hops:
|
if announce_packet.hops < min_hops:
|
||||||
min_hops = announce_packet.hops
|
min_hops = announce_packet.hops
|
||||||
selected_announce_packet = announce_packet
|
selected_announce_packet = announce_packet
|
||||||
|
|
||||||
|
for destination_hash in cull_entries: self.held_announces.pop(destination_hash, None)
|
||||||
|
|
||||||
if selected_announce_packet != None:
|
if selected_announce_packet != None:
|
||||||
RNS.log("Releasing held announce packet "+str(selected_announce_packet)+" from "+str(self), RNS.LOG_EXTREME)
|
RNS.log("Releasing held announce packet "+str(selected_announce_packet)+" from "+str(self), RNS.LOG_EXTREME)
|
||||||
self.ic_held_release = time.time() + self.ic_held_release_interval
|
self.ic_held_release = time.time() + self.ic_held_release_interval
|
||||||
self.held_announces.pop(selected_announce_packet.destination_hash)
|
self.held_announces.pop(selected_announce_packet.destination_hash, None)
|
||||||
def release():
|
RNS.Transport.inbound(selected_announce_packet.raw, selected_announce_packet.receiving_interface, tc=RNS.Transport.TC_INGRESS_LIMITED, ifac_handled=True)
|
||||||
RNS.Transport.inbound(selected_announce_packet.raw, selected_announce_packet.receiving_interface)
|
|
||||||
threading.Thread(target=release, daemon=True).start()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("An error occurred while processing held announces for "+str(self), RNS.LOG_ERROR)
|
RNS.log("An error occurred while processing held announces for "+str(self), RNS.LOG_ERROR)
|
||||||
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
def received_announce(self):
|
def received_announce(self, size=0, from_spawned=False):
|
||||||
|
self.arxc += 1; self.arxb += size
|
||||||
self.ia_freq_deque.append(time.time())
|
self.ia_freq_deque.append(time.time())
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
if self.parent_interface != None:
|
||||||
self.parent_interface.received_announce(from_spawned=True)
|
self.parent_interface.received_announce(size=size, from_spawned=True)
|
||||||
|
|
||||||
def sent_announce(self):
|
def sent_announce(self, size=0, from_spawned=False):
|
||||||
|
self.atxc += 1; self.atxb += size
|
||||||
self.oa_freq_deque.append(time.time())
|
self.oa_freq_deque.append(time.time())
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
if self.parent_interface != None:
|
||||||
self.parent_interface.sent_announce(from_spawned=True)
|
self.parent_interface.sent_announce(size=size, from_spawned=True)
|
||||||
|
|
||||||
|
def filtered_announce(self, size=0, from_spawned=False):
|
||||||
|
self.afrxc += 1
|
||||||
|
RNS.Transport.filtered_arxc += 1
|
||||||
|
if self.parent_interface != None: self.parent_interface.afrxc += 1
|
||||||
|
return None
|
||||||
|
|
||||||
|
def received_path_request(self, size=0, from_spawned=False):
|
||||||
|
self.prxc += 1; self.prxb += size
|
||||||
|
self.ip_freq_deque.append(time.time())
|
||||||
|
if self.parent_interface != None:
|
||||||
|
self.parent_interface.received_path_request(size=size, from_spawned=True)
|
||||||
|
|
||||||
|
def sent_path_request(self, size=0, from_spawned=False):
|
||||||
|
self.ptxc += 1; self.ptxb += size
|
||||||
|
self.op_freq_deque.append(time.time())
|
||||||
|
if self.parent_interface != None:
|
||||||
|
self.parent_interface.sent_path_request(size=size, from_spawned=True)
|
||||||
|
|
||||||
|
def protocol_violation(self, description=None, level=None):
|
||||||
|
if level == None: level = RNS.LOG_DEBUG
|
||||||
|
self.protocol_violations += 1
|
||||||
|
RNS.log(f"Protocol violation on {self}: {description}", level) if RNS.sl(level) else None
|
||||||
|
return None
|
||||||
|
|
||||||
|
def ifac_violation(self, description=None):
|
||||||
|
self.ifac_violations += 1
|
||||||
|
RNS.log(f"IFAC violation on {self}: {description}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
return None
|
||||||
|
|
||||||
|
def packet_filter_hit(self):
|
||||||
|
self.packet_filter_hits += 1
|
||||||
|
return None
|
||||||
|
|
||||||
|
def hop_limiter_hit(self):
|
||||||
|
self.hop_limiter_hits += 1
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def destination_cost(self):
|
||||||
|
if not self._destination_cost == None: return self._destination_cost
|
||||||
|
else: return RNS.Transport.DESTINATION_COST
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pr_cost(self):
|
||||||
|
if not self._pr_cost == None: return self._pr_cost
|
||||||
|
else: return RNS.Transport.PR_COST
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ic_burst_count(self): return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ic_pr_burst_count(self): return None
|
||||||
|
|
||||||
def incoming_announce_frequency(self):
|
def incoming_announce_frequency(self):
|
||||||
if not len(self.ia_freq_deque) > 1:
|
n = len(self.ia_freq_deque)
|
||||||
return 0
|
if not n > self.IC_DEQUE_MIN_SAMPLE: return 0
|
||||||
else:
|
else:
|
||||||
dq_len = len(self.ia_freq_deque)
|
oldest = self.ia_freq_deque[0]
|
||||||
delta_sum = 0
|
span = time.time() - oldest
|
||||||
for i in range(1,dq_len):
|
if span > self.AR_FREQ_DECAY: self.ia_freq_deque.popleft()
|
||||||
delta_sum += self.ia_freq_deque[i]-self.ia_freq_deque[i-1]
|
if span <= 0: return 0
|
||||||
delta_sum += time.time() - self.ia_freq_deque[dq_len-1]
|
hz = n / span
|
||||||
|
return hz
|
||||||
if delta_sum == 0:
|
|
||||||
avg = 0
|
|
||||||
else:
|
|
||||||
avg = 1/(delta_sum/(dq_len))
|
|
||||||
|
|
||||||
return avg
|
|
||||||
|
|
||||||
def outgoing_announce_frequency(self):
|
def outgoing_announce_frequency(self):
|
||||||
if not len(self.oa_freq_deque) > 1:
|
n = len(self.oa_freq_deque)
|
||||||
return 0
|
if not len(self.oa_freq_deque) > 1: return 0
|
||||||
else:
|
else:
|
||||||
dq_len = len(self.oa_freq_deque)
|
oldest = self.oa_freq_deque[0]
|
||||||
delta_sum = 0
|
span = time.time() - oldest
|
||||||
for i in range(1,dq_len):
|
if span > self.AR_FREQ_DECAY: self.oa_freq_deque.popleft()
|
||||||
delta_sum += self.oa_freq_deque[i]-self.oa_freq_deque[i-1]
|
if span <= 0: return 0
|
||||||
delta_sum += time.time() - self.oa_freq_deque[dq_len-1]
|
hz = n / span
|
||||||
|
return hz
|
||||||
if delta_sum == 0:
|
|
||||||
avg = 0
|
|
||||||
else:
|
|
||||||
avg = 1/(delta_sum/(dq_len))
|
|
||||||
|
|
||||||
return avg
|
def incoming_pr_frequency(self):
|
||||||
|
n = len(self.ip_freq_deque)
|
||||||
|
if not n > self.IC_DEQUE_MIN_SAMPLE: return 0
|
||||||
|
else:
|
||||||
|
oldest = self.ip_freq_deque[0]
|
||||||
|
span = time.time() - oldest
|
||||||
|
if span > self.PR_FREQ_DECAY: self.ip_freq_deque.popleft()
|
||||||
|
if span <= 0: return 0
|
||||||
|
hz = n / span
|
||||||
|
return hz
|
||||||
|
|
||||||
|
def outgoing_pr_frequency(self, preemptive=False):
|
||||||
|
n = len(self.op_freq_deque)+(1 if preemptive else 0)
|
||||||
|
if not len(self.op_freq_deque) > 1: return 0
|
||||||
|
else:
|
||||||
|
oldest = self.op_freq_deque[0]
|
||||||
|
span = time.time() - oldest
|
||||||
|
if span > self.PR_FREQ_DECAY: self.op_freq_deque.popleft()
|
||||||
|
if span <= 0: return 0
|
||||||
|
hz = n / span
|
||||||
|
return hz
|
||||||
|
|
||||||
def process_announce_queue(self):
|
def process_announce_queue(self):
|
||||||
if not hasattr(self, "announce_cap"):
|
if not self.announce_queue: return
|
||||||
self.announce_cap = RNS.Reticulum.ANNOUNCE_CAP
|
|
||||||
|
|
||||||
if hasattr(self, "announce_queue"):
|
if not hasattr(self, "announce_cap"):
|
||||||
|
self.announce_cap = RNS.Reticulum.ANNOUNCE_CAP/100.0
|
||||||
|
|
||||||
|
if self.announce_queue:
|
||||||
try:
|
try:
|
||||||
now = time.time()
|
now = time.time()
|
||||||
stale = []
|
stale = []
|
||||||
|
|
@ -248,12 +478,13 @@ class Interface:
|
||||||
selected = entries[0]
|
selected = entries[0]
|
||||||
|
|
||||||
now = time.time()
|
now = time.time()
|
||||||
tx_time = (len(selected["raw"])*8) / self.bitrate
|
tx_size = len(selected["raw"])
|
||||||
|
tx_time = (tx_size*8) / self.bitrate
|
||||||
wait_time = (tx_time / self.announce_cap)
|
wait_time = (tx_time / self.announce_cap)
|
||||||
self.announce_allowed_at = now + wait_time
|
self.announce_allowed_at = now + wait_time
|
||||||
|
|
||||||
self.process_outgoing(selected["raw"])
|
RNS.Transport.transmit(self, selected["raw"])
|
||||||
self.sent_announce()
|
self.sent_announce(tx_size)
|
||||||
|
|
||||||
if selected in self.announce_queue:
|
if selected in self.announce_queue:
|
||||||
self.announce_queue.remove(selected)
|
self.announce_queue.remove(selected)
|
||||||
|
|
@ -267,16 +498,20 @@ class Interface:
|
||||||
RNS.log("Error while processing announce queue on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Error while processing announce queue on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.log("The announce queue for this interface has been cleared.", RNS.LOG_ERROR)
|
RNS.log("The announce queue for this interface has been cleared.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
def final_init(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def teardown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_config_obj(config_in):
|
def get_config_obj(config_in):
|
||||||
if type(config_in) == ConfigObj:
|
if type(config_in) == ConfigObj: return config_in
|
||||||
return config_in
|
|
||||||
else:
|
else:
|
||||||
try:
|
try: return ConfigObj(config_in)
|
||||||
return ConfigObj(config_in)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log(f"Could not parse supplied configuration data. The contained exception was: {e}", RNS.LOG_ERROR)
|
RNS.log(f"Could not parse supplied configuration data. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
raise SystemError("Invalid configuration data supplied")
|
raise SystemError("Invalid configuration data supplied")
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -63,7 +71,7 @@ class KISSInterface(Interface):
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -114,7 +122,9 @@ class KISSInterface(Interface):
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.flow_control = flow_control
|
self.flow_control = flow_control
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
|
self.reconnecting = False
|
||||||
self.flow_control_timeout = 5
|
self.flow_control_timeout = 5
|
||||||
self.flow_control_locked = time.time()
|
self.flow_control_locked = time.time()
|
||||||
|
|
||||||
|
|
@ -131,15 +141,17 @@ class KISSInterface(Interface):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
if self.serial.is_open: self.configure_device()
|
||||||
|
else: raise IOError("Could not open serial port")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
|
RNS.log(f"Could not open serial port for {self}", RNS.LOG_ERROR)
|
||||||
raise e
|
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
||||||
if self.serial.is_open:
|
if not self.detached and not self.reconnecting:
|
||||||
self.configure_device()
|
thread = threading.Thread(target=self.reconnect_port)
|
||||||
else:
|
thread.daemon = True
|
||||||
raise IOError("Could not open serial port")
|
thread.start()
|
||||||
|
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
||||||
|
|
@ -157,6 +169,11 @@ class KISSInterface(Interface):
|
||||||
dsrdtr = False,
|
dsrdtr = False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
try: self.serial.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close port for {self} at detach: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def configure_device(self):
|
def configure_device(self):
|
||||||
# Allow time for interface to initialise before config
|
# Allow time for interface to initialise before config
|
||||||
|
|
@ -237,7 +254,7 @@ class KISSInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -351,30 +368,28 @@ class KISSInterface(Interface):
|
||||||
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
self.serial.close()
|
try: self.serial.close()
|
||||||
self.reconnect_port()
|
except Exception as e: RNS.log(f"Could not close port for {self}", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting: self.reconnect_port()
|
||||||
|
|
||||||
def reconnect_port(self):
|
def reconnect_port(self):
|
||||||
while not self.online:
|
self.reconnecting = True
|
||||||
|
while not self.online and not self.detached:
|
||||||
try:
|
try:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
except Exception as e: RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
self.reconnecting = False
|
||||||
|
if self.online: RNS.log("Reconnected serial port for "+str(self))
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self): return False
|
||||||
return False
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "KISSInterface["+self.name+"]"
|
||||||
return "KISSInterface["+self.name+"]"
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -21,6 +29,9 @@
|
||||||
# SOFTWARE.
|
# SOFTWARE.
|
||||||
|
|
||||||
from RNS.Interfaces.Interface import Interface
|
from RNS.Interfaces.Interface import Interface
|
||||||
|
from RNS.Interfaces.BackboneInterface import BackboneInterface
|
||||||
|
from RNS.Interfaces.util.TransmitBuffer import TransmitBuffer
|
||||||
|
from RNS.Interfaces.util.HDLC import HDLC, ReceiveBuffer
|
||||||
import socketserver
|
import socketserver
|
||||||
import threading
|
import threading
|
||||||
import socket
|
import socket
|
||||||
|
|
@ -30,17 +41,6 @@ import os
|
||||||
import RNS
|
import RNS
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
class HDLC():
|
|
||||||
FLAG = 0x7E
|
|
||||||
ESC = 0x7D
|
|
||||||
ESC_MASK = 0x20
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def escape(data):
|
|
||||||
data = data.replace(bytes([HDLC.ESC]), bytes([HDLC.ESC, HDLC.ESC^HDLC.ESC_MASK]))
|
|
||||||
data = data.replace(bytes([HDLC.FLAG]), bytes([HDLC.ESC, HDLC.FLAG^HDLC.ESC_MASK]))
|
|
||||||
return data
|
|
||||||
|
|
||||||
class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
def server_bind(self):
|
def server_bind(self):
|
||||||
if RNS.vendor.platformutils.is_windows():
|
if RNS.vendor.platformutils.is_windows():
|
||||||
|
|
@ -53,13 +53,20 @@ class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||||
class LocalClientInterface(Interface):
|
class LocalClientInterface(Interface):
|
||||||
RECONNECT_WAIT = 8
|
RECONNECT_WAIT = 8
|
||||||
AUTOCONFIGURE_MTU = True
|
AUTOCONFIGURE_MTU = True
|
||||||
|
CLIENT_SLEEP_PAUSE_TIMEOUT = 12
|
||||||
|
|
||||||
def __init__(self, owner, name, target_port = None, connected_socket=None):
|
def __init__(self, owner, name, target_port = None, connected_socket=None, socket_path=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
self.HW_MTU = 262144
|
self.owner = owner
|
||||||
|
self.bitrate = 1_000_000_000
|
||||||
self.online = False
|
self.epoll_backend = False
|
||||||
|
self.HW_MTU = 262144
|
||||||
|
self.online = False
|
||||||
|
self.ifac_size = self.DEFAULT_IFAC_SIZE
|
||||||
|
|
||||||
|
if socket_path != None and RNS.Reticulum.get_instance().use_af_unix: self.socket_path = f"\0rns/{socket_path}"
|
||||||
|
else: self.socket_path = None
|
||||||
|
|
||||||
self.IN = True
|
self.IN = True
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
|
|
@ -70,24 +77,41 @@ class LocalClientInterface(Interface):
|
||||||
self.detached = False
|
self.detached = False
|
||||||
self.name = name
|
self.name = name
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
|
self.transmit_buffer = TransmitBuffer()
|
||||||
|
self.receive_buffer = ReceiveBuffer(mtu=lambda: self.HW_MTU, min_frame_len=RNS.Reticulum.HEADER_MINSIZE,
|
||||||
|
max_frame_len=lambda: self.HW_MTU, on_frame=self.process_incoming)
|
||||||
|
|
||||||
|
if RNS.vendor.platformutils.use_epoll(): self.epoll_backend = True
|
||||||
|
|
||||||
|
self.pause_on_client_sleep = False
|
||||||
|
|
||||||
if connected_socket != None:
|
if connected_socket != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
self.target_ip = None
|
self.target_ip = None
|
||||||
self.target_port = None
|
self.target_port = None
|
||||||
self.socket = connected_socket
|
self.socket = connected_socket
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
|
||||||
|
if self.socket.family == socket.AF_INET:
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
|
|
||||||
self.is_connected_to_shared_instance = False
|
self.is_connected_to_shared_instance = False
|
||||||
|
|
||||||
|
if RNS.vendor.platformutils.is_android():
|
||||||
|
self.pause_on_client_sleep = True
|
||||||
|
self.pause_timeout = time.time() + self.CLIENT_SLEEP_PAUSE_TIMEOUT
|
||||||
|
|
||||||
|
elif self.socket_path != None:
|
||||||
|
self.receives = True
|
||||||
|
self.target_ip = None
|
||||||
|
self.target_port = None
|
||||||
|
self.connect()
|
||||||
|
|
||||||
elif target_port != None:
|
elif target_port != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
self.target_ip = "127.0.0.1"
|
self.target_ip = "127.0.0.1"
|
||||||
self.target_port = target_port
|
self.target_port = target_port
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
self.owner = owner
|
|
||||||
self.bitrate = 1_000_000_000
|
|
||||||
self.online = True
|
self.online = True
|
||||||
self.writing = False
|
self.writing = False
|
||||||
|
|
||||||
|
|
@ -98,22 +122,31 @@ class LocalClientInterface(Interface):
|
||||||
self.announce_rate_penalty = None
|
self.announce_rate_penalty = None
|
||||||
|
|
||||||
if connected_socket == None:
|
if connected_socket == None:
|
||||||
thread = threading.Thread(target=self.read_loop)
|
if not self.epoll_backend:
|
||||||
thread.daemon = True
|
thread = threading.Thread(target=self.read_loop)
|
||||||
thread.start()
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
if self.socket_path != None:
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||||
self.socket.connect((self.target_ip, self.target_port))
|
self.socket.connect(self.socket_path)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
|
self.socket.connect((self.target_ip, self.target_port))
|
||||||
|
|
||||||
self.online = True
|
self.online = True
|
||||||
self.is_connected_to_shared_instance = True
|
self.is_connected_to_shared_instance = True
|
||||||
self.never_connected = False
|
self.never_connected = False
|
||||||
|
|
||||||
|
if RNS.vendor.platformutils.is_android(): self.phy_keepalive = True
|
||||||
|
if self.epoll_backend: BackboneInterface.add_client_socket(self.socket, self)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -137,9 +170,11 @@ class LocalClientInterface(Interface):
|
||||||
RNS.log("Reconnected socket for "+str(self)+".", RNS.LOG_INFO)
|
RNS.log("Reconnected socket for "+str(self)+".", RNS.LOG_INFO)
|
||||||
|
|
||||||
self.reconnecting = False
|
self.reconnecting = False
|
||||||
thread = threading.Thread(target=self.read_loop)
|
if not self.epoll_backend:
|
||||||
thread.daemon = True
|
thread = threading.Thread(target=self.read_loop)
|
||||||
thread.start()
|
thread.daemon = True
|
||||||
|
thread.start()
|
||||||
|
|
||||||
def job():
|
def job():
|
||||||
time.sleep(LocalClientInterface.RECONNECT_WAIT+2)
|
time.sleep(LocalClientInterface.RECONNECT_WAIT+2)
|
||||||
RNS.Transport.shared_connection_reappeared()
|
RNS.Transport.shared_connection_reappeared()
|
||||||
|
|
@ -150,37 +185,59 @@ class LocalClientInterface(Interface):
|
||||||
raise IOError("Attempt to reconnect on a non-initiator local interface")
|
raise IOError("Attempt to reconnect on a non-initiator local interface")
|
||||||
|
|
||||||
|
|
||||||
|
def send_keepalive(self):
|
||||||
|
if self.online:
|
||||||
|
RNS.log(f"Sending keepalive on {self}", RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
|
try:
|
||||||
|
if self.epoll_backend:
|
||||||
|
self.transmit_buffer.append(bytes([HDLC.FLAG])+bytes([HDLC.FLAG]))
|
||||||
|
BackboneInterface.tx_ready(self)
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.writing = True
|
||||||
|
data = bytes([HDLC.FLAG])+bytes([HDLC.FLAG])
|
||||||
|
self.socket.sendall(data)
|
||||||
|
self.writing = False
|
||||||
|
|
||||||
|
except Exception as e: RNS.log(f"Exception occurred while sending keepalive on {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
try: self.owner.inbound(data, self)
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
|
||||||
self.parent_interface.rxb += len(data)
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.owner.inbound(data, self)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log(f"An error in the processing of an incoming frame for {self}: {e}", RNS.LOG_ERROR)
|
RNS.log(f"An error occurred in the processing of an incoming frame for {self}: {e}", RNS.LOG_ERROR)
|
||||||
RNS.trace_exception(e)
|
RNS.trace_exception(e)
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
|
if self.pause_on_client_sleep and time.time() > self.pause_timeout:
|
||||||
|
RNS.log(f"TX paused for LocalInterface client, dropping outbound packet", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None # TODO: Remove
|
||||||
|
return
|
||||||
|
|
||||||
if self.online:
|
if self.online:
|
||||||
try:
|
try:
|
||||||
self.writing = True
|
if self.epoll_backend:
|
||||||
|
frame = HDLC.frame(data)
|
||||||
|
if self.tx_stalled or not self.transmit_buffer.append(frame, self.tx_hwm):
|
||||||
|
self.tx_drops += 1
|
||||||
|
self.tx_dropped_bytes += len(frame)
|
||||||
|
RNS.log(f"Egress control dropping outbound frame of {RNS.prettysize(len(frame))} on {self}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
else: BackboneInterface.tx_ready(self)
|
||||||
|
|
||||||
if self._force_bitrate:
|
else:
|
||||||
if not hasattr(self, "send_lock"):
|
self.writing = True
|
||||||
self.send_lock = Lock()
|
|
||||||
|
|
||||||
with self.send_lock:
|
if self._force_bitrate:
|
||||||
# RNS.log(f"Simulating latency of {RNS.prettytime(s)} for {len(data)} bytes", RNS.LOG_EXTREME)
|
if not hasattr(self, "send_lock"): self.send_lock = Lock()
|
||||||
s = len(data) / self.bitrate * 8
|
with self.send_lock:
|
||||||
time.sleep(s)
|
# RNS.log(f"Simulating latency of {RNS.prettytime(s)} for {len(data)} bytes", RNS.LOG_EXTREME)
|
||||||
|
s = len(data) / self.bitrate * 8
|
||||||
|
time.sleep(s)
|
||||||
|
|
||||||
data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
|
data = HDLC.frame(data)
|
||||||
self.socket.sendall(data)
|
self.socket.sendall(data)
|
||||||
self.writing = False
|
self.writing = False
|
||||||
self.txb += len(data)
|
self.txb += len(data)
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
||||||
self.parent_interface.txb += len(data)
|
self.parent_interface.txb += len(data)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Exception occurred while transmitting via "+str(self)+", tearing down interface", RNS.LOG_ERROR)
|
RNS.log("Exception occurred while transmitting via "+str(self)+", tearing down interface", RNS.LOG_ERROR)
|
||||||
|
|
@ -188,48 +245,52 @@ class LocalClientInterface(Interface):
|
||||||
RNS.trace_exception(e)
|
RNS.trace_exception(e)
|
||||||
self.teardown()
|
self.teardown()
|
||||||
|
|
||||||
|
def handle_hdlc(self, data_in):
|
||||||
|
self.receive_buffer.feed(data_in)
|
||||||
|
|
||||||
|
def receive(self, data_in):
|
||||||
|
try:
|
||||||
|
if len(data_in) > 0: self.handle_hdlc(data_in)
|
||||||
|
else:
|
||||||
|
self.online = False
|
||||||
|
if self.is_connected_to_shared_instance and not self.detached:
|
||||||
|
RNS.log("Socket for "+str(self)+" was closed, attempting to reconnect...", RNS.LOG_WARNING)
|
||||||
|
RNS.Transport.shared_connection_disappeared()
|
||||||
|
# TODO: Potentially run this in a thread, but since if we get here,
|
||||||
|
# there's no other connectivity left to block anyway, it might be
|
||||||
|
# unnecessary.
|
||||||
|
self.reconnect()
|
||||||
|
else: self.teardown(nowarning=True)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.online = False
|
||||||
|
RNS.log("An interface error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
RNS.log("Tearing down "+str(self), RNS.LOG_ERROR)
|
||||||
|
self.teardown()
|
||||||
|
|
||||||
|
if self.pause_on_client_sleep: self.pause_timeout = time.time() + self.CLIENT_SLEEP_PAUSE_TIMEOUT
|
||||||
|
|
||||||
def read_loop(self):
|
def read_loop(self):
|
||||||
try:
|
try:
|
||||||
in_frame = False
|
self.receive_buffer.reset()
|
||||||
escape = False
|
|
||||||
frame_buffer = b""
|
|
||||||
data_in = b""
|
data_in = b""
|
||||||
data_buffer = b""
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data_in = self.socket.recv(4096)
|
data_in = self.socket.recv(4096)
|
||||||
if len(data_in) > 0:
|
if len(data_in) > 0: self.handle_hdlc(data_in)
|
||||||
frame_buffer += data_in
|
|
||||||
flags_remaining = True
|
|
||||||
while flags_remaining:
|
|
||||||
frame_start = frame_buffer.find(HDLC.FLAG)
|
|
||||||
if frame_start != -1:
|
|
||||||
frame_end = frame_buffer.find(HDLC.FLAG, frame_start+1)
|
|
||||||
if frame_end != -1:
|
|
||||||
frame = frame_buffer[frame_start+1:frame_end]
|
|
||||||
frame = frame.replace(bytes([HDLC.ESC, HDLC.FLAG ^ HDLC.ESC_MASK]), bytes([HDLC.FLAG]))
|
|
||||||
frame = frame.replace(bytes([HDLC.ESC, HDLC.ESC ^ HDLC.ESC_MASK]), bytes([HDLC.ESC]))
|
|
||||||
if len(frame) > RNS.Reticulum.HEADER_MINSIZE:
|
|
||||||
self.process_incoming(frame)
|
|
||||||
frame_buffer = frame_buffer[frame_end:]
|
|
||||||
else:
|
|
||||||
flags_remaining = False
|
|
||||||
else:
|
|
||||||
flags_remaining = False
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.online = False
|
self.online = False
|
||||||
if self.is_connected_to_shared_instance and not self.detached:
|
if self.is_connected_to_shared_instance and not self.detached:
|
||||||
RNS.log("Socket for "+str(self)+" was closed, attempting to reconnect...", RNS.LOG_WARNING)
|
RNS.log("Socket for "+str(self)+" was closed, attempting to reconnect...", RNS.LOG_WARNING)
|
||||||
RNS.Transport.shared_connection_disappeared()
|
RNS.Transport.shared_connection_disappeared()
|
||||||
|
# TODO: Potentially run this in a thread, but since if we get here,
|
||||||
|
# there's no other connectivity left to block anyway, it might be
|
||||||
|
# unnecessary.
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
else:
|
else:
|
||||||
self.teardown(nowarning=True)
|
self.teardown(nowarning=True)
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.online = False
|
self.online = False
|
||||||
RNS.log("An interface error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("An interface error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
@ -262,15 +323,15 @@ class LocalClientInterface(Interface):
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.IN = False
|
self.IN = False
|
||||||
|
|
||||||
if self in RNS.Transport.interfaces:
|
RNS.Transport.remove_interface(self)
|
||||||
RNS.Transport.interfaces.remove(self)
|
|
||||||
|
|
||||||
if self in RNS.Transport.local_client_interfaces:
|
if self in RNS.Transport.local_client_interfaces:
|
||||||
RNS.Transport.local_client_interfaces.remove(self)
|
RNS.Transport.local_client_interfaces.remove(self)
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
||||||
self.parent_interface.clients -= 1
|
self.parent_interface.clients -= 1
|
||||||
if hasattr(RNS.Transport, "owner") and RNS.Transport.owner != None:
|
if hasattr(RNS.Transport, "owner") and RNS.Transport.owner != None:
|
||||||
RNS.Transport.owner._should_persist_data()
|
background = not self.detached
|
||||||
|
RNS.Transport.owner._should_persist_data(background=background)
|
||||||
|
|
||||||
if nowarning == False:
|
if nowarning == False:
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is being torn down. Restart Reticulum to attempt to open this interface again.", RNS.LOG_ERROR)
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is being torn down. Restart Reticulum to attempt to open this interface again.", RNS.LOG_ERROR)
|
||||||
|
|
@ -285,86 +346,146 @@ class LocalClientInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "LocalInterface["+str(self.target_port)+"]"
|
if self.socket_path: return "LocalInterface["+str(self.socket_path.replace("\0", ""))+"]"
|
||||||
|
else: return "LocalInterface["+str(self.target_port)+"]"
|
||||||
|
|
||||||
|
|
||||||
class LocalServerInterface(Interface):
|
class LocalServerInterface(Interface):
|
||||||
AUTOCONFIGURE_MTU = True
|
AUTOCONFIGURE_MTU = True
|
||||||
|
|
||||||
def __init__(self, owner, bindport=None):
|
def __init__(self, owner, bindport=None, socket_path=None):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.epoll_backend = False
|
||||||
self.online = False
|
self.online = False
|
||||||
self.clients = 0
|
self.clients = 0
|
||||||
|
self.ifac_size = self.DEFAULT_IFAC_SIZE
|
||||||
|
|
||||||
|
if socket_path != None and RNS.Reticulum.get_instance().use_af_unix: self.socket_path = f"\0rns/{socket_path}"
|
||||||
|
else: self.socket_path = None
|
||||||
|
|
||||||
self.IN = True
|
self.IN = True
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.name = "Reticulum"
|
self.name = "Reticulum"
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
|
|
||||||
if (bindport != None):
|
if RNS.vendor.platformutils.use_epoll():
|
||||||
|
self.epoll_backend = True
|
||||||
|
|
||||||
|
if socket_path != None and self.epoll_backend:
|
||||||
|
self.receives = True
|
||||||
|
self.bind_ip = None
|
||||||
|
self.bind_port = None
|
||||||
|
|
||||||
|
self.owner = owner
|
||||||
|
self.is_local_shared_instance = True
|
||||||
|
BackboneInterface.add_listener(self, self.socket_path, socket_type=socket.AF_UNIX)
|
||||||
|
|
||||||
|
elif bindport != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
self.bind_ip = "127.0.0.1"
|
self.bind_ip = "127.0.0.1"
|
||||||
self.bind_port = bindport
|
self.bind_port = bindport
|
||||||
|
|
||||||
def handlerFactory(callback):
|
|
||||||
def createHandler(*args, **keys):
|
|
||||||
return LocalInterfaceHandler(callback, *args, **keys)
|
|
||||||
return createHandler
|
|
||||||
|
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.is_local_shared_instance = True
|
self.is_local_shared_instance = True
|
||||||
|
|
||||||
address = (self.bind_ip, self.bind_port)
|
address = (self.bind_ip, self.bind_port)
|
||||||
|
if self.epoll_backend: BackboneInterface.add_listener(self, address)
|
||||||
|
else:
|
||||||
|
def handlerFactory(callback):
|
||||||
|
def createHandler(*args, **keys):
|
||||||
|
return LocalInterfaceHandler(callback, *args, **keys)
|
||||||
|
return createHandler
|
||||||
|
|
||||||
self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection))
|
self.server = ThreadingTCPServer(address, handlerFactory(self.incoming_connection))
|
||||||
self.server.daemon_threads = True
|
self.server.daemon_threads = True
|
||||||
|
thread = threading.Thread(target=self.server.serve_forever)
|
||||||
thread = threading.Thread(target=self.server.serve_forever)
|
thread.daemon = True
|
||||||
thread.daemon = True
|
thread.start()
|
||||||
thread.start()
|
|
||||||
|
|
||||||
self.announce_rate_target = None
|
|
||||||
self.announce_rate_grace = None
|
|
||||||
self.announce_rate_penalty = None
|
|
||||||
|
|
||||||
self.bitrate = 1000*1000*1000
|
|
||||||
self.online = True
|
|
||||||
|
|
||||||
|
self.announce_rate_target = None
|
||||||
|
self.announce_rate_grace = None
|
||||||
|
self.announce_rate_penalty = None
|
||||||
|
|
||||||
|
self.HW_MTU = 262144
|
||||||
|
self.bitrate = 1000*1000*1000
|
||||||
|
self.online = True
|
||||||
|
|
||||||
def incoming_connection(self, handler):
|
def incoming_connection(self, handler):
|
||||||
interface_name = str(str(handler.client_address[1]))
|
if self.epoll_backend:
|
||||||
spawned_interface = LocalClientInterface(self.owner, name=interface_name, connected_socket=handler.request)
|
client_socket = handler
|
||||||
spawned_interface.OUT = self.OUT
|
if client_socket.family == socket.AF_INET:
|
||||||
spawned_interface.IN = self.IN
|
interface_name = str(str(client_socket.getpeername()[1]))
|
||||||
spawned_interface.target_ip = handler.client_address[0]
|
elif client_socket.family == socket.AF_UNIX:
|
||||||
spawned_interface.target_port = str(handler.client_address[1])
|
interface_name = f"{self.clients}@{self.socket_path}"
|
||||||
spawned_interface.parent_interface = self
|
|
||||||
spawned_interface.bitrate = self.bitrate
|
spawned_interface = LocalClientInterface(self.owner, name=interface_name, connected_socket=client_socket)
|
||||||
if hasattr(self, "_force_bitrate"):
|
spawned_interface.OUT = self.OUT
|
||||||
spawned_interface._force_bitrate = self._force_bitrate
|
spawned_interface.IN = self.IN
|
||||||
# RNS.log("Accepting new connection to shared instance: "+str(spawned_interface), RNS.LOG_EXTREME)
|
spawned_interface.socket = client_socket
|
||||||
RNS.Transport.interfaces.append(spawned_interface)
|
spawned_interface.parent_interface = self
|
||||||
RNS.Transport.local_client_interfaces.append(spawned_interface)
|
spawned_interface.bitrate = self.bitrate
|
||||||
self.clients += 1
|
|
||||||
spawned_interface.read_loop()
|
if client_socket.family == socket.AF_INET:
|
||||||
|
spawned_interface.target_ip = client_socket.getpeername()[0]
|
||||||
|
spawned_interface.target_port = str(client_socket.getpeername()[1])
|
||||||
|
|
||||||
|
elif client_socket.family == socket.AF_UNIX:
|
||||||
|
spawned_interface.target_ip = None
|
||||||
|
spawned_interface.target_port = interface_name
|
||||||
|
spawned_interface.socket_path = self.socket_path
|
||||||
|
|
||||||
|
if hasattr(self, "_force_bitrate"): spawned_interface._force_bitrate = self._force_bitrate
|
||||||
|
RNS.Transport.add_interface(spawned_interface)
|
||||||
|
RNS.Transport.local_client_interfaces.append(spawned_interface)
|
||||||
|
BackboneInterface.add_client_socket(client_socket, spawned_interface)
|
||||||
|
self.clients += 1
|
||||||
|
return True
|
||||||
|
|
||||||
|
else:
|
||||||
|
interface_name = str(str(handler.client_address[1]))
|
||||||
|
spawned_interface = LocalClientInterface(self.owner, name=interface_name, connected_socket=handler.request)
|
||||||
|
spawned_interface.OUT = self.OUT
|
||||||
|
spawned_interface.IN = self.IN
|
||||||
|
spawned_interface.target_ip = handler.client_address[0]
|
||||||
|
spawned_interface.target_port = str(handler.client_address[1])
|
||||||
|
spawned_interface.parent_interface = self
|
||||||
|
spawned_interface.bitrate = self.bitrate
|
||||||
|
if hasattr(self, "_force_bitrate"): spawned_interface._force_bitrate = self._force_bitrate
|
||||||
|
RNS.Transport.add_interface(spawned_interface)
|
||||||
|
RNS.Transport.local_client_interfaces.append(spawned_interface)
|
||||||
|
self.clients += 1
|
||||||
|
spawned_interface.read_loop()
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def received_announce(self, from_spawned=False):
|
def received_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.ia_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.ia_freq_deque.append(time.time())
|
||||||
|
self.arxb += size
|
||||||
|
|
||||||
def sent_announce(self, from_spawned=False):
|
def sent_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.oa_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.oa_freq_deque.append(time.time())
|
||||||
|
self.atxb += size
|
||||||
|
|
||||||
|
def received_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.ip_freq_deque.append(time.time())
|
||||||
|
self.prxb += size
|
||||||
|
|
||||||
|
def sent_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.op_freq_deque.append(time.time())
|
||||||
|
self.ptxb += size
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Shared Instance["+str(self.bind_port)+"]"
|
if self.socket_path: return "Shared Instance["+str(self.socket_path.replace("\0", ""))+"]"
|
||||||
|
else: return "Shared Instance["+str(self.bind_port)+"]"
|
||||||
|
|
||||||
class LocalInterfaceHandler(socketserver.BaseRequestHandler):
|
class LocalInterfaceHandler(socketserver.BaseRequestHandler):
|
||||||
def __init__(self, callback, *args, **keys):
|
def __init__(self, callback, *args, **keys):
|
||||||
self.callback = callback
|
self.callback = callback
|
||||||
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
|
socketserver.BaseRequestHandler.__init__(self, *args, **keys)
|
||||||
|
|
||||||
def handle(self):
|
def handle(self): self.callback(handler=self)
|
||||||
self.callback(handler=self)
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -76,6 +84,7 @@ class PipeInterface(Interface):
|
||||||
self.pipe_is_open = False
|
self.pipe_is_open = False
|
||||||
self.bitrate = PipeInterface.BITRATE_GUESS
|
self.bitrate = PipeInterface.BITRATE_GUESS
|
||||||
self.respawn_delay = respawn_delay
|
self.respawn_delay = respawn_delay
|
||||||
|
self.shared_medium = True
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_pipe()
|
self.open_pipe()
|
||||||
|
|
@ -111,7 +120,7 @@ class PipeInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -24,6 +32,7 @@ from RNS.Interfaces.Interface import Interface
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
import socket
|
||||||
import time
|
import time
|
||||||
import math
|
import math
|
||||||
import RNS
|
import RNS
|
||||||
|
|
@ -56,6 +65,7 @@ class KISS():
|
||||||
CMD_STAT_PHYPRM = 0x26
|
CMD_STAT_PHYPRM = 0x26
|
||||||
CMD_STAT_BAT = 0x27
|
CMD_STAT_BAT = 0x27
|
||||||
CMD_STAT_CSMA = 0x28
|
CMD_STAT_CSMA = 0x28
|
||||||
|
CMD_STAT_TEMP = 0x29
|
||||||
CMD_BLINK = 0x30
|
CMD_BLINK = 0x30
|
||||||
CMD_RANDOM = 0x40
|
CMD_RANDOM = 0x40
|
||||||
CMD_FB_EXT = 0x41
|
CMD_FB_EXT = 0x41
|
||||||
|
|
@ -126,7 +136,7 @@ class RNodeInterface(Interface):
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
||||||
|
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -150,8 +160,11 @@ class RNodeInterface(Interface):
|
||||||
lt_alock = float(c["airtime_limit_long"]) if "airtime_limit_long" in c else None
|
lt_alock = float(c["airtime_limit_long"]) if "airtime_limit_long" in c else None
|
||||||
|
|
||||||
force_ble = False
|
force_ble = False
|
||||||
ble_name = None
|
ble_name = None
|
||||||
ble_addr = None
|
ble_addr = None
|
||||||
|
|
||||||
|
force_tcp = False
|
||||||
|
tcp_host = None
|
||||||
|
|
||||||
port = c["port"] if "port" in c else None
|
port = c["port"] if "port" in c else None
|
||||||
|
|
||||||
|
|
@ -159,6 +172,7 @@ class RNodeInterface(Interface):
|
||||||
raise ValueError("No port specified for RNode interface")
|
raise ValueError("No port specified for RNode interface")
|
||||||
|
|
||||||
if port != None:
|
if port != None:
|
||||||
|
tcp_uri_scheme = "tcp://"
|
||||||
ble_uri_scheme = "ble://"
|
ble_uri_scheme = "ble://"
|
||||||
if port.lower().startswith(ble_uri_scheme):
|
if port.lower().startswith(ble_uri_scheme):
|
||||||
force_ble = True
|
force_ble = True
|
||||||
|
|
@ -171,6 +185,13 @@ class RNodeInterface(Interface):
|
||||||
else:
|
else:
|
||||||
ble_name = ble_string
|
ble_name = ble_string
|
||||||
|
|
||||||
|
elif port.lower().startswith(tcp_uri_scheme):
|
||||||
|
force_tcp = True
|
||||||
|
tcp_string = port[len(tcp_uri_scheme):]
|
||||||
|
port = None
|
||||||
|
if len(tcp_string) == 0: pass
|
||||||
|
else: tcp_host = tcp_string
|
||||||
|
|
||||||
self.HW_MTU = 508
|
self.HW_MTU = 508
|
||||||
|
|
||||||
self.pyserial = serial
|
self.pyserial = serial
|
||||||
|
|
@ -187,6 +208,14 @@ class RNodeInterface(Interface):
|
||||||
self.reconnecting= False
|
self.reconnecting= False
|
||||||
self.hw_errors = []
|
self.hw_errors = []
|
||||||
|
|
||||||
|
self.use_tcp = False
|
||||||
|
self.tcp = None
|
||||||
|
self.tcp_host = tcp_host
|
||||||
|
self.tcp_rx_queue= b""
|
||||||
|
self.tcp_tx_queue= b""
|
||||||
|
self.tcp_rx_lock = threading.Lock()
|
||||||
|
self.tcp_tx_lock = threading.Lock()
|
||||||
|
|
||||||
self.use_ble = False
|
self.use_ble = False
|
||||||
self.ble_name = ble_name
|
self.ble_name = ble_name
|
||||||
self.ble_addr = ble_addr
|
self.ble_addr = ble_addr
|
||||||
|
|
@ -205,6 +234,7 @@ class RNodeInterface(Interface):
|
||||||
self.bitrate = 0
|
self.bitrate = 0
|
||||||
self.st_alock = st_alock
|
self.st_alock = st_alock
|
||||||
self.lt_alock = lt_alock
|
self.lt_alock = lt_alock
|
||||||
|
self.cpu_temp = None
|
||||||
self.platform = None
|
self.platform = None
|
||||||
self.display = None
|
self.display = None
|
||||||
self.mcu = None
|
self.mcu = None
|
||||||
|
|
@ -228,9 +258,11 @@ class RNodeInterface(Interface):
|
||||||
self.r_stat_tx = None
|
self.r_stat_tx = None
|
||||||
self.r_stat_rssi = None
|
self.r_stat_rssi = None
|
||||||
self.r_stat_snr = None
|
self.r_stat_snr = None
|
||||||
|
self.r_stat_q = None
|
||||||
self.r_st_alock = None
|
self.r_st_alock = None
|
||||||
self.r_lt_alock = None
|
self.r_lt_alock = None
|
||||||
self.r_random = None
|
self.r_random = None
|
||||||
|
self.r_modulation = "LoRa"
|
||||||
self.r_airtime_short = 0.0
|
self.r_airtime_short = 0.0
|
||||||
self.r_airtime_long = 0.0
|
self.r_airtime_long = 0.0
|
||||||
self.r_channel_load_short = 0.0
|
self.r_channel_load_short = 0.0
|
||||||
|
|
@ -246,9 +278,13 @@ class RNodeInterface(Interface):
|
||||||
self.r_csma_cw_max = None
|
self.r_csma_cw_max = None
|
||||||
self.r_current_rssi = None
|
self.r_current_rssi = None
|
||||||
self.r_noise_floor = None
|
self.r_noise_floor = None
|
||||||
|
self.r_interference = None
|
||||||
|
self.r_interference_l = None
|
||||||
|
self.reports_phy_stats = True
|
||||||
|
|
||||||
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
self.r_battery_state = RNodeInterface.BATTERY_STATE_UNKNOWN
|
||||||
self.r_battery_percent = 0
|
self.r_battery_percent = 0
|
||||||
|
self.r_temperature = None
|
||||||
self.r_framebuffer = b""
|
self.r_framebuffer = b""
|
||||||
self.r_framebuffer_readtime = 0
|
self.r_framebuffer_readtime = 0
|
||||||
self.r_framebuffer_latency = 0
|
self.r_framebuffer_latency = 0
|
||||||
|
|
@ -261,18 +297,20 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.flow_control = flow_control
|
self.flow_control = flow_control
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
self.announce_rate_target = None
|
self.announce_rate_target = None
|
||||||
|
self.supports_discovery = True
|
||||||
|
|
||||||
if force_ble or self.ble_addr != None or self.ble_name != None:
|
if force_ble or self.ble_addr != None or self.ble_name != None: self.use_ble = True
|
||||||
self.use_ble = True
|
if force_tcp or self.tcp_host != None: self.use_tcp = True
|
||||||
|
|
||||||
self.validcfg = True
|
self.validcfg = True
|
||||||
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
|
if (self.frequency < RNodeInterface.FREQ_MIN or self.frequency > RNodeInterface.FREQ_MAX):
|
||||||
RNS.log("Invalid frequency configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid frequency configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
if (self.txpower < 0 or self.txpower > 22):
|
if (self.txpower < 0 or self.txpower > 37):
|
||||||
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
|
|
@ -314,10 +352,8 @@ class RNodeInterface(Interface):
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
else: raise IOError("Could not open serial port")
|
||||||
else:
|
|
||||||
raise IOError("Could not open serial port")
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
||||||
|
|
@ -330,8 +366,8 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
if not self.use_ble:
|
if not self.use_ble and not self.use_tcp:
|
||||||
RNS.log("Opening serial port "+self.port+"...")
|
RNS.log(f"Opening serial port {self.port}...")
|
||||||
self.serial = self.pyserial.Serial(
|
self.serial = self.pyserial.Serial(
|
||||||
port = self.port,
|
port = self.port,
|
||||||
baudrate = self.speed,
|
baudrate = self.speed,
|
||||||
|
|
@ -347,19 +383,38 @@ class RNodeInterface(Interface):
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log(f"Opening BLE connection for {self}...")
|
if self.use_ble:
|
||||||
if self.ble != None and self.ble.running == False:
|
RNS.log(f"Opening BLE connection for {self}...")
|
||||||
self.ble.close()
|
self.timeout = 1250
|
||||||
self.ble.cleanup()
|
if self.ble != None and self.ble.running == False:
|
||||||
self.ble = None
|
RNS.log(f"Cleaning up previous BLE connection for {self}...")
|
||||||
|
self.ble.close()
|
||||||
|
self.ble.cleanup()
|
||||||
|
self.ble = None
|
||||||
|
|
||||||
if self.ble == None:
|
if self.ble == None:
|
||||||
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
self.ble = BLEConnection(owner=self, target_name=self.ble_name, target_bt_addr=self.ble_addr)
|
||||||
self.serial = self.ble
|
self.serial = self.ble
|
||||||
|
|
||||||
open_time = time.time()
|
open_time = time.time()
|
||||||
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
while not self.ble.connected and time.time() < open_time + self.ble.CONNECT_TIMEOUT:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
if self.use_tcp:
|
||||||
|
self.timeout = 1500
|
||||||
|
RNS.log(f"Opening TCP connection for {self}...")
|
||||||
|
if self.tcp != None and self.tcp.running == False:
|
||||||
|
self.tcp.close()
|
||||||
|
self.tcp.cleanup()
|
||||||
|
self.tcp = None
|
||||||
|
|
||||||
|
if self.tcp == None:
|
||||||
|
self.tcp = TCPConnection(owner=self, target_host=self.tcp_host)
|
||||||
|
self.serial = self.tcp
|
||||||
|
|
||||||
|
open_time = time.time()
|
||||||
|
while not self.tcp.connected and time.time() < open_time + self.tcp.CONNECT_TIMEOUT:
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
def reset_radio_state(self):
|
def reset_radio_state(self):
|
||||||
self.r_frequency = None
|
self.r_frequency = None
|
||||||
|
|
@ -380,38 +435,45 @@ class RNodeInterface(Interface):
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
self.detect()
|
self.detect()
|
||||||
if not self.use_ble:
|
if self.use_tcp:
|
||||||
sleep(0.2)
|
tcp_detect_timeout = 5.0
|
||||||
else:
|
|
||||||
ble_detect_timeout = 5
|
|
||||||
detect_time = time.time()
|
detect_time = time.time()
|
||||||
while not self.detected and time.time() < detect_time + ble_detect_timeout:
|
while not self.detected and time.time() < detect_time + tcp_detect_timeout: time.sleep(0.1)
|
||||||
time.sleep(0.1)
|
if not self.detected: RNS.log(f"RNode detect timed out over TCP", RNS.LOG_ERROR)
|
||||||
if self.detected:
|
elif self.use_ble:
|
||||||
detect_time = RNS.prettytime(time.time()-detect_time)
|
ble_detect_timeout = 5.0
|
||||||
else:
|
detect_time = time.time()
|
||||||
RNS.log(f"RNode detect timed out over {self.port}", RNS.LOG_ERROR)
|
while not self.detected and time.time() < detect_time + ble_detect_timeout: time.sleep(0.1)
|
||||||
|
if not self.detected:
|
||||||
|
RNS.log(f"RNode detect timed out over BLE", RNS.LOG_ERROR)
|
||||||
|
if self.ble:
|
||||||
|
self.ble.should_run = False
|
||||||
|
self.ble.must_disconnect = True
|
||||||
|
else:
|
||||||
|
sleep(0.2)
|
||||||
|
|
||||||
if not self.detected:
|
if not self.detected:
|
||||||
RNS.log("Could not detect device for "+str(self), RNS.LOG_ERROR)
|
RNS.log(f"Could not detect device for {self}", RNS.LOG_ERROR)
|
||||||
self.serial.close()
|
self.serial.close()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if self.platform == KISS.PLATFORM_ESP32 or self.platform == KISS.PLATFORM_NRF52:
|
if self.platform == KISS.PLATFORM_ESP32 or self.platform == KISS.PLATFORM_NRF52: self.display = True
|
||||||
self.display = True
|
|
||||||
|
|
||||||
RNS.log("Serial port "+self.port+" is now open")
|
if self.use_tcp: RNS.log(f"TCP connection to {self.tcp_host} is now open", RNS.LOG_VERBOSE)
|
||||||
RNS.log("Configuring RNode interface...", RNS.LOG_VERBOSE)
|
elif self.use_ble: RNS.log(f"BLE connection to {self} is now open", RNS.LOG_VERBOSE)
|
||||||
self.initRadio()
|
else: RNS.log(f"Serial port {self.port} is now open", RNS.LOG_VERBOSE)
|
||||||
if (self.validateRadioState()):
|
RNS.log("Configuring RNode interface...", RNS.LOG_VERBOSE)
|
||||||
self.interface_ready = True
|
self.initRadio()
|
||||||
RNS.log(str(self)+" is configured and powered up")
|
if (self.validateRadioState()):
|
||||||
sleep(0.3)
|
self.interface_ready = True
|
||||||
self.online = True
|
RNS.log(str(self)+" is configured and powered up")
|
||||||
else:
|
sleep(0.3)
|
||||||
RNS.log("After configuring "+str(self)+", the reported radio parameters did not match your configuration.", RNS.LOG_ERROR)
|
self.online = True
|
||||||
RNS.log("Make sure that your hardware actually supports the parameters specified in the configuration", RNS.LOG_ERROR)
|
else:
|
||||||
RNS.log("Aborting RNode startup", RNS.LOG_ERROR)
|
RNS.log("After configuring "+str(self)+", the reported radio parameters did not match your configuration.", RNS.LOG_ERROR)
|
||||||
self.serial.close()
|
RNS.log("Make sure that your hardware actually supports the parameters specified in the configuration", RNS.LOG_ERROR)
|
||||||
|
RNS.log("Aborting RNode startup", RNS.LOG_ERROR)
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
|
|
||||||
def initRadio(self):
|
def initRadio(self):
|
||||||
|
|
@ -424,8 +486,7 @@ class RNodeInterface(Interface):
|
||||||
self.setLTALock()
|
self.setLTALock()
|
||||||
self.setRadioState(KISS.RADIO_STATE_ON)
|
self.setRadioState(KISS.RADIO_STATE_ON)
|
||||||
|
|
||||||
if self.use_ble:
|
if self.use_ble: time.sleep(2)
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
def detect(self):
|
def detect(self):
|
||||||
kiss_command = bytes([KISS.FEND, KISS.CMD_DETECT, KISS.DETECT_REQ, KISS.FEND, KISS.CMD_FW_VERSION, 0x00, KISS.FEND, KISS.CMD_PLATFORM, 0x00, KISS.FEND, KISS.CMD_MCU, 0x00, KISS.FEND])
|
kiss_command = bytes([KISS.FEND, KISS.CMD_DETECT, KISS.DETECT_REQ, KISS.FEND, KISS.CMD_FW_VERSION, 0x00, KISS.FEND, KISS.CMD_PLATFORM, 0x00, KISS.FEND, KISS.CMD_MCU, 0x00, KISS.FEND])
|
||||||
|
|
@ -606,10 +667,9 @@ class RNodeInterface(Interface):
|
||||||
|
|
||||||
def validateRadioState(self):
|
def validateRadioState(self):
|
||||||
RNS.log("Waiting for radio configuration validation for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Waiting for radio configuration validation for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
if self.use_ble:
|
if self.use_ble: sleep(1.00)
|
||||||
sleep(1.00)
|
elif self.use_tcp: sleep(1.5)
|
||||||
else:
|
else: sleep(0.25)
|
||||||
sleep(0.25)
|
|
||||||
|
|
||||||
if self.use_ble and self.ble != None and self.ble.device_disappeared:
|
if self.use_ble and self.ble != None and self.ble.device_disappeared:
|
||||||
RNS.log(f"Device disappeared during radio state validation for {self}", RNS.LOG_ERROR)
|
RNS.log(f"Device disappeared during radio state validation for {self}", RNS.LOG_ERROR)
|
||||||
|
|
@ -632,10 +692,8 @@ class RNodeInterface(Interface):
|
||||||
RNS.log("Radio state mismatch", RNS.LOG_ERROR)
|
RNS.log("Radio state mismatch", RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
if (self.validcfg):
|
if (self.validcfg): return True
|
||||||
return True
|
else: return False
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def updateBitrate(self):
|
def updateBitrate(self):
|
||||||
|
|
@ -647,11 +705,8 @@ class RNodeInterface(Interface):
|
||||||
self.bitrate = 0
|
self.bitrate = 0
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
self.r_stat_rssi = None
|
|
||||||
self.r_stat_snr = None
|
|
||||||
|
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self,data):
|
||||||
datalen = len(data)
|
datalen = len(data)
|
||||||
|
|
@ -894,16 +949,35 @@ class RNodeInterface(Interface):
|
||||||
self.r_channel_load_long = cul/100.0
|
self.r_channel_load_long = cul/100.0
|
||||||
self.r_current_rssi = crs-RNodeInterface.RSSI_OFFSET
|
self.r_current_rssi = crs-RNodeInterface.RSSI_OFFSET
|
||||||
self.r_noise_floor = nfl-RNodeInterface.RSSI_OFFSET
|
self.r_noise_floor = nfl-RNodeInterface.RSSI_OFFSET
|
||||||
|
|
||||||
|
# TODO: Remove debug
|
||||||
|
# interference_log_threshold = 10
|
||||||
|
# if ntf == 0xFF:
|
||||||
|
# self.r_interference = None
|
||||||
|
# if self.r_noise_floor != None:
|
||||||
|
# # Filter potential false interference events due to LNA recalibration
|
||||||
|
# if self.r_interference_l != None:
|
||||||
|
# if self.r_interference_l[1] < self.r_noise_floor+interference_log_threshold:
|
||||||
|
# self.r_interference_l = None
|
||||||
|
# else:
|
||||||
|
# if self.r_noise_floor != None:
|
||||||
|
# interference = ntf-RNodeInterface.RSSI_OFFSET
|
||||||
|
# # Filter potential false interference events due to LNA recalibration
|
||||||
|
# if interference > self.r_noise_floor+interference_log_threshold:
|
||||||
|
# self.r_interference = ntf-RNodeInterface.RSSI_OFFSET
|
||||||
|
# self.r_interference_l = [time.time(), self.r_interference]
|
||||||
|
|
||||||
if ntf == 0xFF:
|
if ntf == 0xFF:
|
||||||
self.r_interference = None
|
self.r_interference = None
|
||||||
else:
|
else:
|
||||||
self.r_interference = ntf-RNodeInterface.RSSI_OFFSET
|
self.r_interference = ntf-RNodeInterface.RSSI_OFFSET
|
||||||
|
self.r_interference_l = [time.time(), self.r_interference]
|
||||||
|
|
||||||
if self.r_interference != None:
|
if self.r_interference != None:
|
||||||
RNS.log(f"{self} Radio detected interference at {self.r_interference} dBm", RNS.LOG_DEBUG)
|
RNS.log(f"{self} Radio detected interference at {self.r_interference} dBm", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
# TODO: Remove debug
|
# TODO: Remove debug
|
||||||
# RNS.log(f"RSSI: {self.r_current_rssi}, Noise floor: {self.r_noise_floor}, Interference: {self.r_interference}", RNS.LOG_EXTREME)
|
# RNS.log(f"RSSI: {self.r_current_rssi}, Noise floor: {self.r_noise_floor}, Interference: {self.r_interference}", RNS.LOG_DEBUG)
|
||||||
elif (command == KISS.CMD_STAT_PHYPRM):
|
elif (command == KISS.CMD_STAT_PHYPRM):
|
||||||
if (byte == KISS.FESC):
|
if (byte == KISS.FESC):
|
||||||
escape = True
|
escape = True
|
||||||
|
|
@ -977,6 +1051,22 @@ class RNodeInterface(Interface):
|
||||||
bat_percent = 0
|
bat_percent = 0
|
||||||
self.r_battery_state = command_buffer[0]
|
self.r_battery_state = command_buffer[0]
|
||||||
self.r_battery_percent = bat_percent
|
self.r_battery_percent = bat_percent
|
||||||
|
elif (command == KISS.CMD_STAT_TEMP):
|
||||||
|
if (byte == KISS.FESC):
|
||||||
|
escape = True
|
||||||
|
else:
|
||||||
|
if (escape):
|
||||||
|
if (byte == KISS.TFEND):
|
||||||
|
byte = KISS.FEND
|
||||||
|
if (byte == KISS.TFESC):
|
||||||
|
byte = KISS.FESC
|
||||||
|
escape = False
|
||||||
|
command_buffer = command_buffer+bytes([byte])
|
||||||
|
if (len(command_buffer) == 1):
|
||||||
|
temp = command_buffer[0]-120
|
||||||
|
if temp >= -30 and temp <= 90: self.r_temperature = temp
|
||||||
|
else: self.r_temperature = None
|
||||||
|
self.cpu_temp = self.r_temperature
|
||||||
elif (command == KISS.CMD_RANDOM):
|
elif (command == KISS.CMD_RANDOM):
|
||||||
self.r_random = byte
|
self.r_random = byte
|
||||||
elif (command == KISS.CMD_PLATFORM):
|
elif (command == KISS.CMD_PLATFORM):
|
||||||
|
|
@ -1046,7 +1136,7 @@ class RNodeInterface(Interface):
|
||||||
else:
|
else:
|
||||||
time_since_last = int(time.time()*1000) - last_read_ms
|
time_since_last = int(time.time()*1000) - last_read_ms
|
||||||
if len(data_buffer) > 0 and time_since_last > self.timeout:
|
if len(data_buffer) > 0 and time_since_last > self.timeout:
|
||||||
RNS.log(str(self)+" serial read timeout in command "+str(command), RNS.LOG_WARNING)
|
RNS.log(f"{self} device read timeout in command {command} after {RNS.prettytime(self.timeout/1000.0)}", RNS.LOG_WARNING)
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
in_frame = False
|
in_frame = False
|
||||||
command = KISS.CMD_UNKNOWN
|
command = KISS.CMD_UNKNOWN
|
||||||
|
|
@ -1058,23 +1148,26 @@ class RNodeInterface(Interface):
|
||||||
RNS.log("Interface "+str(self)+" is transmitting beacon data: "+str(self.id_callsign.decode("utf-8")), RNS.LOG_DEBUG)
|
RNS.log("Interface "+str(self)+" is transmitting beacon data: "+str(self.id_callsign.decode("utf-8")), RNS.LOG_DEBUG)
|
||||||
self.process_outgoing(self.id_callsign)
|
self.process_outgoing(self.id_callsign)
|
||||||
|
|
||||||
|
if self.use_tcp:
|
||||||
|
if self.tcp and self.tcp.connected:
|
||||||
|
if time.time() > self.tcp.last_write + self.tcp.ACTIVITY_KEEPALIVE:
|
||||||
|
self.detect()
|
||||||
|
|
||||||
sleep(0.08)
|
sleep(0.08)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.online = False
|
self.online = False
|
||||||
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
if not self.detached:
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
try:
|
try: self.serial.close()
|
||||||
self.serial.close()
|
except Exception as e: pass
|
||||||
except Exception as e:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if not self.detached and not self.reconnecting:
|
if not self.detached and not self.reconnecting:
|
||||||
self.reconnect_port()
|
self.reconnect_port()
|
||||||
|
|
@ -1086,23 +1179,34 @@ class RNodeInterface(Interface):
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.reconnecting = False
|
self.reconnecting = False
|
||||||
if self.online:
|
if self.online: RNS.log(f"Reconnected port for {self}")
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
self.detached = True
|
self.detached = True
|
||||||
self.disable_external_framebuffer()
|
try:
|
||||||
self.setRadioState(KISS.RADIO_STATE_OFF)
|
self.disable_external_framebuffer()
|
||||||
self.leave()
|
self.setRadioState(KISS.RADIO_STATE_OFF)
|
||||||
|
self.leave()
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"An error occurred while detaching {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if self.use_ble:
|
if self.use_ble:
|
||||||
self.ble.close()
|
self.ble.close()
|
||||||
|
self.ble.cleanup()
|
||||||
|
self.ble = None
|
||||||
|
elif self.use_tcp:
|
||||||
|
time.sleep(0.5)
|
||||||
|
self.tcp.close()
|
||||||
|
else:
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self):
|
||||||
return False
|
return False
|
||||||
|
|
@ -1123,6 +1227,17 @@ class RNodeInterface(Interface):
|
||||||
def get_battery_percent(self):
|
def get_battery_percent(self):
|
||||||
return self.r_battery_percent
|
return self.r_battery_percent
|
||||||
|
|
||||||
|
def tcp_receive(self, data):
|
||||||
|
with self.tcp_rx_lock: self.tcp_rx_queue += data
|
||||||
|
|
||||||
|
def tcp_waiting(self): return len(self.tcp_tx_queue) > 0
|
||||||
|
|
||||||
|
def get_tcp_waiting(self, n):
|
||||||
|
with self.tcp_tx_lock:
|
||||||
|
data = self.tcp_tx_queue[:n]
|
||||||
|
self.tcp_tx_queue = self.tcp_tx_queue[n:]
|
||||||
|
return data
|
||||||
|
|
||||||
def ble_receive(self, data):
|
def ble_receive(self, data):
|
||||||
with self.ble_rx_lock:
|
with self.ble_rx_lock:
|
||||||
self.ble_rx_queue += data
|
self.ble_rx_queue += data
|
||||||
|
|
@ -1137,7 +1252,7 @@ class RNodeInterface(Interface):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "RNodeInterface["+str(self.name)+"]"
|
return f"RNodeInterface[{self.name}]"
|
||||||
|
|
||||||
class BLEConnection():
|
class BLEConnection():
|
||||||
UART_SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
UART_SERVICE_UUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
|
||||||
|
|
@ -1189,8 +1304,9 @@ class BLEConnection():
|
||||||
self.must_disconnect = False
|
self.must_disconnect = False
|
||||||
self.connect_job_running = False
|
self.connect_job_running = False
|
||||||
self.device_disappeared = False
|
self.device_disappeared = False
|
||||||
|
self._windows_paired_addrs = None
|
||||||
|
|
||||||
import importlib
|
import importlib.util
|
||||||
if BLEConnection.bleak == None:
|
if BLEConnection.bleak == None:
|
||||||
if importlib.util.find_spec("bleak") != None:
|
if importlib.util.find_spec("bleak") != None:
|
||||||
import bleak
|
import bleak
|
||||||
|
|
@ -1199,8 +1315,8 @@ class BLEConnection():
|
||||||
import asyncio
|
import asyncio
|
||||||
BLEConnection.asyncio = asyncio
|
BLEConnection.asyncio = asyncio
|
||||||
else:
|
else:
|
||||||
RNS.log("Using the RNode interface over BLE requires a the \"bleak\" module to be installed.", RNS.LOG_CRITICAL)
|
RNS.log("Using the RNode interface over BLE requires the \"bleak\" module to be installed.", RNS.LOG_CRITICAL)
|
||||||
RNS.log("You can install one with the command: python3 -m pip install bleak", RNS.LOG_CRITICAL)
|
RNS.log("You can install it with the command: python3 -m pip install bleak", RNS.LOG_CRITICAL)
|
||||||
RNS.panic()
|
RNS.panic()
|
||||||
|
|
||||||
self.should_run = True
|
self.should_run = True
|
||||||
|
|
@ -1216,6 +1332,7 @@ class BLEConnection():
|
||||||
self.should_run = False
|
self.should_run = False
|
||||||
|
|
||||||
def connection_job(self):
|
def connection_job(self):
|
||||||
|
self.running = True
|
||||||
while self.should_run:
|
while self.should_run:
|
||||||
if self.ble_device == None:
|
if self.ble_device == None:
|
||||||
self.ble_device = self.find_target_device()
|
self.ble_device = self.find_target_device()
|
||||||
|
|
@ -1242,6 +1359,7 @@ class BLEConnection():
|
||||||
self.owner.ble_receive(data)
|
self.owner.ble_receive(data)
|
||||||
|
|
||||||
self.connected = True
|
self.connected = True
|
||||||
|
self.device_disappeared = False
|
||||||
self.ble_device = ble_client
|
self.ble_device = ble_client
|
||||||
self.last_client = ble_client
|
self.last_client = ble_client
|
||||||
self.owner.port = str(f"ble://{ble_client.address}")
|
self.owner.port = str(f"ble://{ble_client.address}")
|
||||||
|
|
@ -1276,6 +1394,8 @@ class BLEConnection():
|
||||||
|
|
||||||
def find_target_device(self):
|
def find_target_device(self):
|
||||||
RNS.log(f"Searching for attachable BLE device for {self.owner}...", RNS.LOG_EXTREME)
|
RNS.log(f"Searching for attachable BLE device for {self.owner}...", RNS.LOG_EXTREME)
|
||||||
|
if RNS.vendor.platformutils.is_windows():
|
||||||
|
self._windows_paired_addrs = self._get_windows_paired_ble_addresses()
|
||||||
def device_filter(device: self.bleak.backends.device.BLEDevice, adv: self.bleak.backends.scanner.AdvertisementData):
|
def device_filter(device: self.bleak.backends.device.BLEDevice, adv: self.bleak.backends.scanner.AdvertisementData):
|
||||||
if BLEConnection.UART_SERVICE_UUID.lower() in adv.service_uuids:
|
if BLEConnection.UART_SERVICE_UUID.lower() in adv.service_uuids:
|
||||||
if self.device_bonded(device):
|
if self.device_bonded(device):
|
||||||
|
|
@ -1307,6 +1427,9 @@ class BLEConnection():
|
||||||
|
|
||||||
def device_bonded(self, device):
|
def device_bonded(self, device):
|
||||||
try:
|
try:
|
||||||
|
if self._windows_paired_addrs is not None:
|
||||||
|
return device.address is not None and device.address.lower() in self._windows_paired_addrs
|
||||||
|
|
||||||
if hasattr(device, "details"):
|
if hasattr(device, "details"):
|
||||||
if "props" in device.details and "Bonded" in device.details["props"]:
|
if "props" in device.details and "Bonded" in device.details["props"]:
|
||||||
if device.details["props"]["Bonded"] == True:
|
if device.details["props"]["Bonded"] == True:
|
||||||
|
|
@ -1316,3 +1439,145 @@ class BLEConnection():
|
||||||
RNS.log(f"Error while determining device bond status for {device}, the contained exception was: {e}", RNS.LOG_ERROR)
|
RNS.log(f"Error while determining device bond status for {device}, the contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def _get_windows_paired_ble_addresses(self):
|
||||||
|
from winrt.windows.devices.bluetooth import BluetoothLEDevice
|
||||||
|
from winrt.windows.devices.enumeration import DeviceInformation
|
||||||
|
async def _query():
|
||||||
|
selector = BluetoothLEDevice.get_device_selector_from_pairing_state(True)
|
||||||
|
infos = await DeviceInformation.find_all_async_aqs_filter(selector)
|
||||||
|
return set(info.id.split("-")[-1].lower() for info in infos)
|
||||||
|
return self.asyncio.run(_query())
|
||||||
|
|
||||||
|
class TCPConnection():
|
||||||
|
TARGET_PORT = 7633
|
||||||
|
CONNECT_TIMEOUT = 5.0
|
||||||
|
INITIAL_CONNECT_TIMEOUT = 5.0
|
||||||
|
RECONNECT_WAIT = 4.0
|
||||||
|
ACTIVITY_TIMEOUT = 6.0
|
||||||
|
ACTIVITY_KEEPALIVE = ACTIVITY_TIMEOUT-2.5
|
||||||
|
|
||||||
|
TCP_USER_TIMEOUT = 24
|
||||||
|
TCP_PROBE_AFTER = 5
|
||||||
|
TCP_PROBE_INTERVAL = 2
|
||||||
|
TCP_PROBES = 12
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_open(self):
|
||||||
|
return self.connected
|
||||||
|
|
||||||
|
@property
|
||||||
|
def in_waiting(self):
|
||||||
|
buflen = len(self.owner.tcp_rx_queue)
|
||||||
|
return buflen > 0
|
||||||
|
|
||||||
|
def write(self, data_bytes):
|
||||||
|
if self.connected and self.socket:
|
||||||
|
with self.owner.tcp_tx_lock:
|
||||||
|
if len(self.owner.tcp_tx_queue) > 0:
|
||||||
|
self.socket.sendall(self.owner.tcp_tx_queue)
|
||||||
|
self.owner.tcp_tx_queue = b""
|
||||||
|
|
||||||
|
self.socket.sendall(data_bytes)
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
else:
|
||||||
|
with self.owner.tcp_tx_lock: self.owner.tcp_tx_queue += data_bytes
|
||||||
|
|
||||||
|
return len(data_bytes)
|
||||||
|
|
||||||
|
def read(self, n):
|
||||||
|
with self.owner.tcp_rx_lock:
|
||||||
|
data = self.owner.tcp_rx_queue[:n]
|
||||||
|
self.owner.tcp_rx_queue = self.owner.tcp_rx_queue[n:]
|
||||||
|
return data
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.connected:
|
||||||
|
RNS.log(f"Disconnecting TCP socket for {self.owner}", RNS.LOG_DEBUG)
|
||||||
|
self.must_disconnect = True
|
||||||
|
if self.socket: self.socket.close()
|
||||||
|
|
||||||
|
def __init__(self, owner=None, target_host=None):
|
||||||
|
self.owner = owner
|
||||||
|
self.target_host = target_host
|
||||||
|
self.connected = False
|
||||||
|
self.reconnecting = False
|
||||||
|
self.running = False
|
||||||
|
self.should_run = False
|
||||||
|
self.must_disconnect = False
|
||||||
|
self.connect_job_running = False
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
self.should_run = True
|
||||||
|
self.connection_thread = threading.Thread(target=self.initial_connect, daemon=True).start()
|
||||||
|
|
||||||
|
def set_timeouts_linux(self):
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, int(self.TCP_USER_TIMEOUT * 1000))
|
||||||
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, int(self.TCP_PROBE_AFTER))
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, int(self.TCP_PROBE_INTERVAL))
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, int(self.TCP_PROBES))
|
||||||
|
|
||||||
|
def set_timeouts_osx(self):
|
||||||
|
if hasattr(socket, "TCP_KEEPALIVE"): TCP_KEEPIDLE = socket.TCP_KEEPALIVE
|
||||||
|
else: TCP_KEEPIDLE = 0x10
|
||||||
|
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, TCP_KEEPIDLE, int(self.TCP_PROBE_AFTER))
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
try:
|
||||||
|
if self.socket: self.socket.close()
|
||||||
|
except Exception as e: RNS.log(f"Error while disconnecting TCP socket on cleanup for {self.owner}", RNS.LOG_ERROR)
|
||||||
|
self.should_run = False
|
||||||
|
|
||||||
|
def initial_connect(self):
|
||||||
|
if self.connect(initial=True): threading.Thread(target=self.read_loop, daemon=True).start()
|
||||||
|
|
||||||
|
def connect(self, initial=False):
|
||||||
|
try:
|
||||||
|
if initial:
|
||||||
|
RNS.log(f"Establishing TCP connection to device for {self.owner}...", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
address_info = socket.getaddrinfo(self.target_host, self.TARGET_PORT, proto=socket.IPPROTO_TCP)[0]
|
||||||
|
address_family = address_info[0]
|
||||||
|
target_address = address_info[4]
|
||||||
|
|
||||||
|
self.socket = socket.socket(address_family, socket.SOCK_STREAM)
|
||||||
|
self.socket.settimeout(self.INITIAL_CONNECT_TIMEOUT)
|
||||||
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
|
self.socket.connect(target_address)
|
||||||
|
self.socket.settimeout(None)
|
||||||
|
self.connected = True
|
||||||
|
self.last_write = time.time()
|
||||||
|
|
||||||
|
RNS.log(f"TCP connection to device for {self.owner} established", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
if RNS.vendor.platformutils.is_linux(): self.set_timeouts_linux()
|
||||||
|
elif RNS.vendor.platformutils.is_darwin(): self.set_timeouts_osx()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if initial:
|
||||||
|
RNS.log(f"TCP connection to device for {self.owner} could not be established: {e}", RNS.LOG_ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
else: raise e
|
||||||
|
|
||||||
|
def read_loop(self):
|
||||||
|
try:
|
||||||
|
data_in = b""
|
||||||
|
while not self.must_disconnect:
|
||||||
|
if self.socket: data_in = self.socket.recv(4096)
|
||||||
|
else: data_in = b""
|
||||||
|
|
||||||
|
if len(data_in) > 0: self.owner.tcp_receive(data_in)
|
||||||
|
else:
|
||||||
|
self.connected = False
|
||||||
|
RNS.log(f"The TCP socket for {self} was closed", RNS.LOG_WARNING)
|
||||||
|
break
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.connected = False
|
||||||
|
RNS.log(f"A TCP read error occurred for {self}, the contained exception was: {e}", RNS.LOG_WARNING)
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2024 Jacob Eva. Adapted from the RNodeInterface by Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -33,6 +41,8 @@ class KISS():
|
||||||
FESC = 0xDB
|
FESC = 0xDB
|
||||||
TFEND = 0xDC
|
TFEND = 0xDC
|
||||||
TFESC = 0xDD
|
TFESC = 0xDD
|
||||||
|
|
||||||
|
CMD_DATA = 0x00
|
||||||
|
|
||||||
CMD_UNKNOWN = 0xFE
|
CMD_UNKNOWN = 0xFE
|
||||||
CMD_FREQUENCY = 0x01
|
CMD_FREQUENCY = 0x01
|
||||||
|
|
@ -64,7 +74,7 @@ class KISS():
|
||||||
CMD_FW_VERSION = 0x50
|
CMD_FW_VERSION = 0x50
|
||||||
CMD_ROM_READ = 0x51
|
CMD_ROM_READ = 0x51
|
||||||
CMD_RESET = 0x55
|
CMD_RESET = 0x55
|
||||||
CMD_INTERFACES = 0x64
|
CMD_INTERFACES = 0x71
|
||||||
|
|
||||||
CMD_INT0_DATA = 0x00
|
CMD_INT0_DATA = 0x00
|
||||||
CMD_INT1_DATA = 0x10
|
CMD_INT1_DATA = 0x10
|
||||||
|
|
@ -79,19 +89,6 @@ class KISS():
|
||||||
CMD_INT10_DATA = 0xE0
|
CMD_INT10_DATA = 0xE0
|
||||||
CMD_INT11_DATA = 0xF0
|
CMD_INT11_DATA = 0xF0
|
||||||
|
|
||||||
CMD_SEL_INT0 = 0x1E
|
|
||||||
CMD_SEL_INT1 = 0x1F
|
|
||||||
CMD_SEL_INT2 = 0x2F
|
|
||||||
CMD_SEL_INT3 = 0x74
|
|
||||||
CMD_SEL_INT4 = 0x7F
|
|
||||||
CMD_SEL_INT5 = 0x9F
|
|
||||||
CMD_SEL_INT6 = 0xAF
|
|
||||||
CMD_SEL_INT7 = 0xBF
|
|
||||||
CMD_SEL_INT8 = 0xCF
|
|
||||||
CMD_SEL_INT9 = 0xDF
|
|
||||||
CMD_SEL_INT10 = 0xEF
|
|
||||||
CMD_SEL_INT11 = 0xFF
|
|
||||||
|
|
||||||
DETECT_REQ = 0x73
|
DETECT_REQ = 0x73
|
||||||
DETECT_RESP = 0x46
|
DETECT_RESP = 0x46
|
||||||
|
|
||||||
|
|
@ -116,33 +113,7 @@ class KISS():
|
||||||
SX128X = 0x20
|
SX128X = 0x20
|
||||||
SX1280 = 0x21
|
SX1280 = 0x21
|
||||||
|
|
||||||
def int_data_cmd_to_index(int_data_cmd):
|
CMD_SEL_INT = 0x1F
|
||||||
if int_data_cmd == KISS.CMD_INT0_DATA:
|
|
||||||
return 0
|
|
||||||
elif int_data_cmd == KISS.CMD_INT1_DATA:
|
|
||||||
return 1
|
|
||||||
elif int_data_cmd == KISS.CMD_INT2_DATA:
|
|
||||||
return 2
|
|
||||||
elif int_data_cmd == KISS.CMD_INT3_DATA:
|
|
||||||
return 3
|
|
||||||
elif int_data_cmd == KISS.CMD_INT4_DATA:
|
|
||||||
return 4
|
|
||||||
elif int_data_cmd == KISS.CMD_INT5_DATA:
|
|
||||||
return 5
|
|
||||||
elif int_data_cmd == KISS.CMD_INT6_DATA:
|
|
||||||
return 6
|
|
||||||
elif int_data_cmd == KISS.CMD_INT7_DATA:
|
|
||||||
return 7
|
|
||||||
elif int_data_cmd == KISS.CMD_INT8_DATA:
|
|
||||||
return 8
|
|
||||||
elif int_data_cmd == KISS.CMD_INT9_DATA:
|
|
||||||
return 9
|
|
||||||
elif int_data_cmd == KISS.CMD_INT10_DATA:
|
|
||||||
return 10
|
|
||||||
elif int_data_cmd == KISS.CMD_INT11_DATA:
|
|
||||||
return 11
|
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def interface_type_to_str(interface_type):
|
def interface_type_to_str(interface_type):
|
||||||
if interface_type == KISS.SX126X or interface_type == KISS.SX1262:
|
if interface_type == KISS.SX126X or interface_type == KISS.SX1262:
|
||||||
|
|
@ -178,7 +149,7 @@ class RNodeMultiInterface(Interface):
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
||||||
|
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -208,16 +179,17 @@ class RNodeMultiInterface(Interface):
|
||||||
enabled_count += 1
|
enabled_count += 1
|
||||||
|
|
||||||
# Create an array with a row for each subinterface
|
# Create an array with a row for each subinterface
|
||||||
subint_config = [[0 for x in range(11)] for y in range(enabled_count)]
|
subint_config = [[None for x in range(11)] for y in range(enabled_count)]
|
||||||
subint_index = 0
|
subint_index = 0
|
||||||
|
|
||||||
for subinterface in c:
|
for subinterface in c:
|
||||||
if isinstance(c[subinterface], dict):
|
if isinstance(c[subinterface], dict):
|
||||||
subinterface_config = c[subinterface]
|
subinterface_config = c[subinterface]
|
||||||
if (("interface_enabled" in subinterface_config) and subinterface_config.as_bool("interface_enabled") == True) or (("enabled" in c) and c.as_bool("enabled") == True):
|
if (("interface_enabled" in subinterface_config) and subinterface_config.as_bool("interface_enabled") == True) or (("enabled" in c) and c.as_bool("enabled") == True):
|
||||||
|
subint_vport = subinterface_config["vport"] if "vport" in subinterface_config else None
|
||||||
|
|
||||||
subint_config[subint_index][0] = subinterface
|
subint_config[subint_index][0] = subinterface
|
||||||
|
|
||||||
subint_vport = subinterface_config["vport"] if "vport" in subinterface_config else None
|
|
||||||
subint_config[subint_index][1] = subint_vport
|
subint_config[subint_index][1] = subint_vport
|
||||||
|
|
||||||
frequency = int(subinterface_config["frequency"]) if "frequency" in subinterface_config else None
|
frequency = int(subinterface_config["frequency"]) if "frequency" in subinterface_config else None
|
||||||
|
|
@ -241,6 +213,7 @@ class RNodeMultiInterface(Interface):
|
||||||
subint_config[subint_index][10] = False
|
subint_config[subint_index][10] = False
|
||||||
else:
|
else:
|
||||||
subint_config[subint_index][10] = True
|
subint_config[subint_index][10] = True
|
||||||
|
|
||||||
subint_index += 1
|
subint_index += 1
|
||||||
|
|
||||||
# if no subinterfaces are defined
|
# if no subinterfaces are defined
|
||||||
|
|
@ -296,6 +269,7 @@ class RNodeMultiInterface(Interface):
|
||||||
self.r_stat_tx = None
|
self.r_stat_tx = None
|
||||||
self.r_stat_rssi = None
|
self.r_stat_rssi = None
|
||||||
self.r_stat_snr = None
|
self.r_stat_snr = None
|
||||||
|
self.r_stat_q = None
|
||||||
self.r_st_alock = None
|
self.r_st_alock = None
|
||||||
self.r_lt_alock = None
|
self.r_lt_alock = None
|
||||||
self.r_random = None
|
self.r_random = None
|
||||||
|
|
@ -303,6 +277,8 @@ class RNodeMultiInterface(Interface):
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
self.announce_rate_target = None
|
self.announce_rate_target = None
|
||||||
|
self.reports_phy_stats = True
|
||||||
|
self.r_modulation = "LoRa"
|
||||||
|
|
||||||
self.validcfg = True
|
self.validcfg = True
|
||||||
if id_interval != None and id_callsign != None:
|
if id_interval != None and id_callsign != None:
|
||||||
|
|
@ -402,7 +378,7 @@ class RNodeMultiInterface(Interface):
|
||||||
interface.mode = self.mode
|
interface.mode = self.mode
|
||||||
interface.HW_MTU = self.HW_MTU
|
interface.HW_MTU = self.HW_MTU
|
||||||
interface.detected = True
|
interface.detected = True
|
||||||
RNS.Transport.interfaces.append(interface)
|
RNS.Transport.add_interface(interface)
|
||||||
RNS.log("Spawned new RNode subinterface: "+str(interface), RNS.LOG_VERBOSE)
|
RNS.log("Spawned new RNode subinterface: "+str(interface), RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
self.clients += 1
|
self.clients += 1
|
||||||
|
|
@ -474,11 +450,10 @@ class RNodeMultiInterface(Interface):
|
||||||
c4 = frequency & 0xFF
|
c4 = frequency & 0xFF
|
||||||
data = KISS.escape(bytes([c1])+bytes([c2])+bytes([c3])+bytes([c4]))
|
data = KISS.escape(bytes([c1])+bytes([c2])+bytes([c3])+bytes([c4]))
|
||||||
|
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_FREQUENCY])+data+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_FREQUENCY])+data+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring frequency for "+str(self))
|
raise IOError("An IO error occurred while configuring frequency for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setBandwidth(self, bandwidth, interface):
|
def setBandwidth(self, bandwidth, interface):
|
||||||
c1 = bandwidth >> 24
|
c1 = bandwidth >> 24
|
||||||
|
|
@ -487,35 +462,31 @@ class RNodeMultiInterface(Interface):
|
||||||
c4 = bandwidth & 0xFF
|
c4 = bandwidth & 0xFF
|
||||||
data = KISS.escape(bytes([c1])+bytes([c2])+bytes([c3])+bytes([c4]))
|
data = KISS.escape(bytes([c1])+bytes([c2])+bytes([c3])+bytes([c4]))
|
||||||
|
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_BANDWIDTH])+data+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_BANDWIDTH])+data+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring bandwidth for "+str(self))
|
raise IOError("An IO error occurred while configuring bandwidth for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setTXPower(self, txpower, interface):
|
def setTXPower(self, txpower, interface):
|
||||||
txp = txpower.to_bytes(1, byteorder="big", signed=True)
|
txp = txpower.to_bytes(1, byteorder="big", signed=True)
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_TXPOWER])+txp+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_TXPOWER])+txp+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring TX power for "+str(self))
|
raise IOError("An IO error occurred while configuring TX power for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setSpreadingFactor(self, sf, interface):
|
def setSpreadingFactor(self, sf, interface):
|
||||||
sf = bytes([sf])
|
sf = bytes([sf])
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_SF])+sf+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_SF])+sf+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring spreading factor for "+str(self))
|
raise IOError("An IO error occurred while configuring spreading factor for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setCodingRate(self, cr, interface):
|
def setCodingRate(self, cr, interface):
|
||||||
cr = bytes([cr])
|
cr = bytes([cr])
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_CR])+cr+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_CR])+cr+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring coding rate for "+str(self))
|
raise IOError("An IO error occurred while configuring coding rate for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setSTALock(self, st_alock, interface):
|
def setSTALock(self, st_alock, interface):
|
||||||
if st_alock != None:
|
if st_alock != None:
|
||||||
|
|
@ -524,11 +495,10 @@ class RNodeMultiInterface(Interface):
|
||||||
c2 = at & 0xFF
|
c2 = at & 0xFF
|
||||||
data = KISS.escape(bytes([c1])+bytes([c2]))
|
data = KISS.escape(bytes([c1])+bytes([c2]))
|
||||||
|
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_ST_ALOCK])+data+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_ST_ALOCK])+data+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring short-term airtime limit for "+str(self))
|
raise IOError("An IO error occurred while configuring short-term airtime limit for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setLTALock(self, lt_alock, interface):
|
def setLTALock(self, lt_alock, interface):
|
||||||
if lt_alock != None:
|
if lt_alock != None:
|
||||||
|
|
@ -537,19 +507,17 @@ class RNodeMultiInterface(Interface):
|
||||||
c2 = at & 0xFF
|
c2 = at & 0xFF
|
||||||
data = KISS.escape(bytes([c1])+bytes([c2]))
|
data = KISS.escape(bytes([c1])+bytes([c2]))
|
||||||
|
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_LT_ALOCK])+data+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_LT_ALOCK])+data+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring long-term airtime limit for "+str(self))
|
raise IOError("An IO error occurred while configuring long-term airtime limit for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def setRadioState(self, state, interface):
|
def setRadioState(self, state, interface):
|
||||||
#self.state = state
|
#self.state = state
|
||||||
kiss_command = bytes([KISS.FEND])+bytes([interface.sel_cmd])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_RADIO_STATE])+bytes([state])+bytes([KISS.FEND])
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_RADIO_STATE])+bytes([state])+bytes([KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring radio state for "+str(self))
|
raise IOError("An IO error occurred while configuring radio state for "+str(self))
|
||||||
self.selected_index = interface.index
|
|
||||||
|
|
||||||
def validate_firmware(self):
|
def validate_firmware(self):
|
||||||
if (self.maj_version >= RNodeMultiInterface.REQUIRED_FW_VER_MAJ):
|
if (self.maj_version >= RNodeMultiInterface.REQUIRED_FW_VER_MAJ):
|
||||||
|
|
@ -570,7 +538,7 @@ class RNodeMultiInterface(Interface):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
data = KISS.escape(data)
|
data = KISS.escape(data)
|
||||||
frame = bytes([0xc0])+bytes([interface.data_cmd])+data+bytes([0xc0])
|
frame = bytes([KISS.FEND])+bytes([KISS.CMD_SEL_INT])+bytes([interface.index])+bytes([KISS.FEND])+bytes([KISS.FEND])+bytes([KISS.CMD_DATA])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
written = self.serial.write(frame)
|
written = self.serial.write(frame)
|
||||||
self.txb += len(data)
|
self.txb += len(data)
|
||||||
|
|
@ -578,11 +546,25 @@ class RNodeMultiInterface(Interface):
|
||||||
if written != len(frame):
|
if written != len(frame):
|
||||||
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
|
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
|
||||||
|
|
||||||
def received_announce(self, from_spawned=False):
|
def received_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.ia_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.ia_freq_deque.append(time.time())
|
||||||
|
self.arxb += size
|
||||||
|
|
||||||
def sent_announce(self, from_spawned=False):
|
def sent_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.oa_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.oa_freq_deque.append(time.time())
|
||||||
|
self.atxb += size
|
||||||
|
|
||||||
|
def received_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.ip_freq_deque.append(time.time())
|
||||||
|
self.prxb += size
|
||||||
|
|
||||||
|
def sent_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.op_freq_deque.append(time.time())
|
||||||
|
self.ptxb += size
|
||||||
|
|
||||||
def readLoop(self):
|
def readLoop(self):
|
||||||
try:
|
try:
|
||||||
|
|
@ -599,21 +581,9 @@ class RNodeMultiInterface(Interface):
|
||||||
last_read_ms = int(time.time()*1000)
|
last_read_ms = int(time.time()*1000)
|
||||||
|
|
||||||
if (in_frame and byte == KISS.FEND and
|
if (in_frame and byte == KISS.FEND and
|
||||||
(command == KISS.CMD_INT0_DATA or
|
(command == KISS.CMD_DATA)):
|
||||||
command == KISS.CMD_INT1_DATA or
|
|
||||||
command == KISS.CMD_INT2_DATA or
|
|
||||||
command == KISS.CMD_INT3_DATA or
|
|
||||||
command == KISS.CMD_INT4_DATA or
|
|
||||||
command == KISS.CMD_INT5_DATA or
|
|
||||||
command == KISS.CMD_INT6_DATA or
|
|
||||||
command == KISS.CMD_INT7_DATA or
|
|
||||||
command == KISS.CMD_INT8_DATA or
|
|
||||||
command == KISS.CMD_INT9_DATA or
|
|
||||||
command == KISS.CMD_INT10_DATA or
|
|
||||||
command == KISS.CMD_INT11_DATA)):
|
|
||||||
in_frame = False
|
in_frame = False
|
||||||
self.subinterfaces[KISS.int_data_cmd_to_index(command)].process_incoming(data_buffer)
|
self.subinterfaces[self.selected_index].process_incoming(data_buffer)
|
||||||
self.selected_index = KISS.int_data_cmd_to_index(command)
|
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
command_buffer = b""
|
command_buffer = b""
|
||||||
elif (byte == KISS.FEND):
|
elif (byte == KISS.FEND):
|
||||||
|
|
@ -678,6 +648,9 @@ class RNodeMultiInterface(Interface):
|
||||||
RNS.log(str(self.subinterfaces[self.selected_index])+" Radio reporting bandwidth is "+str(self.subinterfaces[self.selected_index].r_bandwidth/1000.0)+" KHz", RNS.LOG_DEBUG)
|
RNS.log(str(self.subinterfaces[self.selected_index])+" Radio reporting bandwidth is "+str(self.subinterfaces[self.selected_index].r_bandwidth/1000.0)+" KHz", RNS.LOG_DEBUG)
|
||||||
self.subinterfaces[self.selected_index].updateBitrate()
|
self.subinterfaces[self.selected_index].updateBitrate()
|
||||||
|
|
||||||
|
elif (command == KISS.CMD_SEL_INT):
|
||||||
|
self.selected_index = byte
|
||||||
|
|
||||||
elif (command == KISS.CMD_TXPOWER):
|
elif (command == KISS.CMD_TXPOWER):
|
||||||
txp = byte - 256 if byte > 127 else byte
|
txp = byte - 256 if byte > 127 else byte
|
||||||
self.subinterfaces[self.selected_index].r_txpower = txp
|
self.subinterfaces[self.selected_index].r_txpower = txp
|
||||||
|
|
@ -943,12 +916,12 @@ class RNodeMultiInterface(Interface):
|
||||||
if interface != 0:
|
if interface != 0:
|
||||||
self.setRadioState(KISS.RADIO_STATE_OFF, interface)
|
self.setRadioState(KISS.RADIO_STATE_OFF, interface)
|
||||||
self.leave()
|
self.leave()
|
||||||
|
self.serial.close()
|
||||||
|
|
||||||
def teardown_subinterfaces(self):
|
def teardown_subinterfaces(self):
|
||||||
for interface in self.subinterfaces:
|
for interface in self.subinterfaces:
|
||||||
if interface != 0:
|
if interface != 0:
|
||||||
if interface in RNS.Transport.interfaces:
|
RNS.Transport.remove_interface(interface)
|
||||||
RNS.Transport.interfaces.remove(interface)
|
|
||||||
self.subinterfaces[interface.index] = 0
|
self.subinterfaces[interface.index] = 0
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self):
|
||||||
|
|
@ -979,7 +952,7 @@ class RNodeSubInterface(Interface):
|
||||||
if RNS.vendor.platformutils.is_android():
|
if RNS.vendor.platformutils.is_android():
|
||||||
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
raise SystemError("Invalid interface type. The Android-specific RNode interface must be used on Android")
|
||||||
|
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -989,51 +962,9 @@ class RNodeSubInterface(Interface):
|
||||||
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
if index == 0:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT0
|
|
||||||
data_cmd= KISS.CMD_INT0_DATA
|
|
||||||
elif index == 1:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT1
|
|
||||||
data_cmd= KISS.CMD_INT1_DATA
|
|
||||||
elif index == 2:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT2
|
|
||||||
data_cmd= KISS.CMD_INT2_DATA
|
|
||||||
elif index == 3:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT3
|
|
||||||
data_cmd= KISS.CMD_INT3_DATA
|
|
||||||
elif index == 4:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT4
|
|
||||||
data_cmd= KISS.CMD_INT4_DATA
|
|
||||||
elif index == 5:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT5
|
|
||||||
data_cmd= KISS.CMD_INT5_DATA
|
|
||||||
elif index == 6:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT6
|
|
||||||
data_cmd= KISS.CMD_INT6_DATA
|
|
||||||
elif index == 7:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT7
|
|
||||||
data_cmd= KISS.CMD_INT7_DATA
|
|
||||||
elif index == 8:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT8
|
|
||||||
data_cmd= KISS.CMD_INT8_DATA
|
|
||||||
elif index == 9:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT9
|
|
||||||
data_cmd= KISS.CMD_INT9_DATA
|
|
||||||
elif index == 10:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT10
|
|
||||||
data_cmd= KISS.CMD_INT10_DATA
|
|
||||||
elif index == 11:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT11
|
|
||||||
data_cmd= KISS.CMD_INT11_DATA
|
|
||||||
else:
|
|
||||||
sel_cmd = KISS.CMD_SEL_INT0
|
|
||||||
data_cmd= KISS.CMD_INT0_DATA
|
|
||||||
|
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
self.name = name
|
self.name = name
|
||||||
self.index = index
|
self.index = index
|
||||||
self.sel_cmd = sel_cmd
|
|
||||||
self.data_cmd = data_cmd
|
|
||||||
self.interface_type= interface_type
|
self.interface_type= interface_type
|
||||||
self.flow_control= flow_control
|
self.flow_control= flow_control
|
||||||
self.online = False
|
self.online = False
|
||||||
|
|
@ -1074,12 +1005,12 @@ class RNodeSubInterface(Interface):
|
||||||
self.r_premable_time_ms = None
|
self.r_premable_time_ms = None
|
||||||
|
|
||||||
self.packet_queue = []
|
self.packet_queue = []
|
||||||
|
self.shared_medium = True
|
||||||
self.interface_ready = False
|
self.interface_ready = False
|
||||||
self.parent_interface = parent_interface
|
self.parent_interface = parent_interface
|
||||||
self.announce_rate_target = None
|
self.announce_rate_target = None
|
||||||
|
|
||||||
self.mode = None
|
self.mode = None
|
||||||
self.announce_cap = None
|
|
||||||
self.bitrate = None
|
self.bitrate = None
|
||||||
self.ifac_size = None
|
self.ifac_size = None
|
||||||
|
|
||||||
|
|
@ -1099,7 +1030,7 @@ class RNodeSubInterface(Interface):
|
||||||
RNS.log("Invalid interface type configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid interface type configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
if (self.txpower < -9 or self.txpower > 27):
|
if (self.txpower < -9 or self.txpower > 37):
|
||||||
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
RNS.log("Invalid TX power configured for "+str(self), RNS.LOG_ERROR)
|
||||||
self.validcfg = False
|
self.validcfg = False
|
||||||
|
|
||||||
|
|
@ -1198,10 +1129,8 @@ class RNodeSubInterface(Interface):
|
||||||
self.bitrate = 0
|
self.bitrate = 0
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
self.r_stat_rssi = None
|
|
||||||
self.r_stat_snr = None
|
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self,data):
|
||||||
if self.online:
|
if self.online:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -53,7 +61,7 @@ class SerialInterface(Interface):
|
||||||
serial = None
|
serial = None
|
||||||
|
|
||||||
def __init__(self, owner, configuration):
|
def __init__(self, owner, configuration):
|
||||||
import importlib
|
import importlib.util
|
||||||
if importlib.util.find_spec('serial') != None:
|
if importlib.util.find_spec('serial') != None:
|
||||||
import serial
|
import serial
|
||||||
else:
|
else:
|
||||||
|
|
@ -75,6 +83,7 @@ class SerialInterface(Interface):
|
||||||
raise ValueError("No port specified for serial interface")
|
raise ValueError("No port specified for serial interface")
|
||||||
|
|
||||||
self.HW_MTU = 564
|
self.HW_MTU = 564
|
||||||
|
self.shared_medium = True
|
||||||
|
|
||||||
self.pyserial = serial
|
self.pyserial = serial
|
||||||
self.serial = None
|
self.serial = None
|
||||||
|
|
@ -88,6 +97,7 @@ class SerialInterface(Interface):
|
||||||
self.timeout = 100
|
self.timeout = 100
|
||||||
self.online = False
|
self.online = False
|
||||||
self.bitrate = self.speed
|
self.bitrate = self.speed
|
||||||
|
self.reconnecting = False
|
||||||
|
|
||||||
if parity.lower() == "e" or parity.lower() == "even":
|
if parity.lower() == "e" or parity.lower() == "even":
|
||||||
self.parity = serial.PARITY_EVEN
|
self.parity = serial.PARITY_EVEN
|
||||||
|
|
@ -97,15 +107,17 @@ class SerialInterface(Interface):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.open_port()
|
self.open_port()
|
||||||
|
if self.serial.is_open: self.configure_device()
|
||||||
|
else: raise IOError("Could not open serial port")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port for interface "+str(self), RNS.LOG_ERROR)
|
RNS.log(f"Could not open serial port for {self}", RNS.LOG_ERROR)
|
||||||
raise e
|
RNS.log(f"The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Reticulum will attempt to bring up this interface periodically", RNS.LOG_ERROR)
|
||||||
if self.serial.is_open:
|
if not self.detached and not self.reconnecting:
|
||||||
self.configure_device()
|
thread = threading.Thread(target=self.reconnect_port)
|
||||||
else:
|
thread.daemon = True
|
||||||
raise IOError("Could not open serial port")
|
thread.start()
|
||||||
|
|
||||||
|
|
||||||
def open_port(self):
|
def open_port(self):
|
||||||
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
RNS.log("Opening serial port "+self.port+"...", RNS.LOG_VERBOSE)
|
||||||
|
|
@ -123,6 +135,11 @@ class SerialInterface(Interface):
|
||||||
dsrdtr = False,
|
dsrdtr = False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
try: self.serial.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close port for {self} at detach: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def configure_device(self):
|
def configure_device(self):
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
|
|
@ -132,12 +149,10 @@ class SerialInterface(Interface):
|
||||||
self.online = True
|
self.online = True
|
||||||
RNS.log("Serial port "+self.port+" is now open", RNS.LOG_VERBOSE)
|
RNS.log("Serial port "+self.port+" is now open", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self,data):
|
||||||
if self.online:
|
if self.online:
|
||||||
data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
|
data = bytes([HDLC.FLAG])+HDLC.escape(data)+bytes([HDLC.FLAG])
|
||||||
|
|
@ -146,7 +161,6 @@ class SerialInterface(Interface):
|
||||||
if written != len(data):
|
if written != len(data):
|
||||||
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
|
raise IOError("Serial interface only wrote "+str(written)+" bytes of "+str(len(data)))
|
||||||
|
|
||||||
|
|
||||||
def readLoop(self):
|
def readLoop(self):
|
||||||
try:
|
try:
|
||||||
in_frame = False
|
in_frame = False
|
||||||
|
|
@ -190,30 +204,28 @@ class SerialInterface(Interface):
|
||||||
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("A serial port error occurred, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
RNS.log("The interface "+str(self)+" experienced an unrecoverable error and is now offline.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
if RNS.Reticulum.panic_on_interface_error:
|
if RNS.Reticulum.panic_on_interface_error: RNS.panic()
|
||||||
RNS.panic()
|
|
||||||
|
|
||||||
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
RNS.log("Reticulum will attempt to reconnect the interface periodically.", RNS.LOG_ERROR)
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
self.serial.close()
|
try: self.serial.close()
|
||||||
self.reconnect_port()
|
except Exception as e: RNS.log(f"Could not close port for {self}", RNS.LOG_ERROR)
|
||||||
|
if not self.detached and not self.reconnecting: self.reconnect_port()
|
||||||
|
|
||||||
def reconnect_port(self):
|
def reconnect_port(self):
|
||||||
while not self.online:
|
self.reconnecting = True
|
||||||
|
while not self.online and not self.detached:
|
||||||
try:
|
try:
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...", RNS.LOG_VERBOSE)
|
||||||
self.open_port()
|
self.open_port()
|
||||||
if self.serial.is_open:
|
if self.serial.is_open: self.configure_device()
|
||||||
self.configure_device()
|
except Exception as e: RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
RNS.log("Reconnected serial port for "+str(self))
|
self.reconnecting = False
|
||||||
|
if self.online: RNS.log("Reconnected serial port for "+str(self))
|
||||||
|
|
||||||
def should_ingress_limit(self):
|
def should_ingress_limit(self): return False
|
||||||
return False
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self): return "SerialInterface["+self.name+"]"
|
||||||
return "SerialInterface["+self.name+"]"
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2024 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -99,8 +107,13 @@ class TCPClientInterface(Interface):
|
||||||
i2p_tunneled = c.as_bool("i2p_tunneled") if "i2p_tunneled" in c else False
|
i2p_tunneled = c.as_bool("i2p_tunneled") if "i2p_tunneled" in c else False
|
||||||
connect_timeout = c.as_int("connect_timeout") if "connect_timeout" in c else None
|
connect_timeout = c.as_int("connect_timeout") if "connect_timeout" in c else None
|
||||||
max_reconnect_tries = c.as_int("max_reconnect_tries") if "max_reconnect_tries" in c else None
|
max_reconnect_tries = c.as_int("max_reconnect_tries") if "max_reconnect_tries" in c else None
|
||||||
|
fixed_mtu = c.as_int("fixed_mtu") if "fixed_mtu" in c else None
|
||||||
|
if fixed_mtu:
|
||||||
|
if fixed_mtu < RNS.Reticulum.MTU: raise ValueError(f"Configured MTU of {fixed_mtu} bytes is too small")
|
||||||
|
self.AUTOCONFIGURE_MTU = False
|
||||||
|
self.FIXED_MTU = True
|
||||||
|
|
||||||
self.HW_MTU = TCPInterface.HW_MTU
|
self.HW_MTU = TCPInterface.HW_MTU if not fixed_mtu else fixed_mtu
|
||||||
self.IN = True
|
self.IN = True
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.socket = None
|
self.socket = None
|
||||||
|
|
@ -113,15 +126,16 @@ class TCPClientInterface(Interface):
|
||||||
self.writing = False
|
self.writing = False
|
||||||
self.online = False
|
self.online = False
|
||||||
self.detached = False
|
self.detached = False
|
||||||
|
self.tunnels_allowed = True
|
||||||
|
self.lossless_medium = True
|
||||||
self.kiss_framing = kiss_framing
|
self.kiss_framing = kiss_framing
|
||||||
self.i2p_tunneled = i2p_tunneled
|
self.i2p_tunneled = i2p_tunneled
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
self.bitrate = TCPClientInterface.BITRATE_GUESS
|
self.bitrate = TCPClientInterface.BITRATE_GUESS
|
||||||
|
|
||||||
if max_reconnect_tries == None:
|
self.supports_discovery = True
|
||||||
self.max_reconnect_tries = TCPClientInterface.RECONNECT_MAX_TRIES
|
if max_reconnect_tries == None: self.max_reconnect_tries = TCPClientInterface.RECONNECT_MAX_TRIES
|
||||||
else:
|
else: self.max_reconnect_tries = max_reconnect_tries
|
||||||
self.max_reconnect_tries = max_reconnect_tries
|
|
||||||
|
|
||||||
if connected_socket != None:
|
if connected_socket != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
|
|
@ -146,13 +160,13 @@ class TCPClientInterface(Interface):
|
||||||
self.connect_timeout = connect_timeout
|
self.connect_timeout = connect_timeout
|
||||||
else:
|
else:
|
||||||
self.connect_timeout = TCPClientInterface.INITIAL_CONNECT_TIMEOUT
|
self.connect_timeout = TCPClientInterface.INITIAL_CONNECT_TIMEOUT
|
||||||
|
|
||||||
if TCPClientInterface.SYNCHRONOUS_START:
|
def final_init(self):
|
||||||
self.initial_connect()
|
if TCPClientInterface.SYNCHRONOUS_START: self.initial_connect()
|
||||||
else:
|
else:
|
||||||
thread = threading.Thread(target=self.initial_connect)
|
thread = threading.Thread(target=self.initial_connect)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
|
|
||||||
def initial_connect(self):
|
def initial_connect(self):
|
||||||
if not self.connect(initial=True):
|
if not self.connect(initial=True):
|
||||||
|
|
@ -163,8 +177,6 @@ class TCPClientInterface(Interface):
|
||||||
thread = threading.Thread(target=self.read_loop)
|
thread = threading.Thread(target=self.read_loop)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
if not self.kiss_framing:
|
|
||||||
self.wants_tunnel = True
|
|
||||||
|
|
||||||
def set_timeouts_linux(self):
|
def set_timeouts_linux(self):
|
||||||
if not self.i2p_tunneled:
|
if not self.i2p_tunneled:
|
||||||
|
|
@ -196,6 +208,7 @@ class TCPClientInterface(Interface):
|
||||||
|
|
||||||
def detach(self):
|
def detach(self):
|
||||||
self.online = False
|
self.online = False
|
||||||
|
self.detached = True
|
||||||
if self.socket != None:
|
if self.socket != None:
|
||||||
if hasattr(self.socket, "close"):
|
if hasattr(self.socket, "close"):
|
||||||
if callable(self.socket.close):
|
if callable(self.socket.close):
|
||||||
|
|
@ -215,6 +228,20 @@ class TCPClientInterface(Interface):
|
||||||
|
|
||||||
self.socket = None
|
self.socket = None
|
||||||
|
|
||||||
|
def early_synthesis(self, packet):
|
||||||
|
if not self.detached:
|
||||||
|
if not packet.packed: packet.pack()
|
||||||
|
if self.ifac_identity: raw = RNS.Transport.handle_outgoing_ifac(self, packet.raw);
|
||||||
|
else: raw = packet.raw
|
||||||
|
|
||||||
|
if self.kiss_framing: frame = bytes([KISS.FEND])+bytes([KISS.CMD_DATA])+KISS.escape(raw)+bytes([KISS.FEND])
|
||||||
|
else: frame = bytes([HDLC.FLAG])+HDLC.escape(raw)+bytes([HDLC.FLAG])
|
||||||
|
|
||||||
|
self.socket.sendall(frame)
|
||||||
|
self.txb += len(frame)
|
||||||
|
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
||||||
|
self.parent_interface.txb += len(frame)
|
||||||
|
|
||||||
def connect(self, initial=False):
|
def connect(self, initial=False):
|
||||||
try:
|
try:
|
||||||
if initial:
|
if initial:
|
||||||
|
|
@ -229,6 +256,13 @@ class TCPClientInterface(Interface):
|
||||||
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
|
||||||
self.socket.connect(target_address)
|
self.socket.connect(target_address)
|
||||||
self.socket.settimeout(None)
|
self.socket.settimeout(None)
|
||||||
|
|
||||||
|
if not self.kiss_framing:
|
||||||
|
self.wants_tunnel = True
|
||||||
|
self.remote_transport = None
|
||||||
|
self.synthesis_received = False
|
||||||
|
RNS.Transport.synthesize_tunnel(self)
|
||||||
|
|
||||||
self.online = True
|
self.online = True
|
||||||
|
|
||||||
if initial:
|
if initial:
|
||||||
|
|
@ -260,7 +294,7 @@ class TCPClientInterface(Interface):
|
||||||
if not self.reconnecting:
|
if not self.reconnecting:
|
||||||
self.reconnecting = True
|
self.reconnecting = True
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while not self.online:
|
while not self.online and not self.detached:
|
||||||
time.sleep(TCPClientInterface.RECONNECT_WAIT)
|
time.sleep(TCPClientInterface.RECONNECT_WAIT)
|
||||||
attempts += 1
|
attempts += 1
|
||||||
|
|
||||||
|
|
@ -269,9 +303,7 @@ class TCPClientInterface(Interface):
|
||||||
self.teardown()
|
self.teardown()
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try: self.connect()
|
||||||
self.connect()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Connection attempt for "+str(self)+" failed: "+str(e), RNS.LOG_DEBUG)
|
RNS.log("Connection attempt for "+str(self)+" failed: "+str(e), RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
|
@ -282,19 +314,14 @@ class TCPClientInterface(Interface):
|
||||||
thread = threading.Thread(target=self.read_loop)
|
thread = threading.Thread(target=self.read_loop)
|
||||||
thread.daemon = True
|
thread.daemon = True
|
||||||
thread.start()
|
thread.start()
|
||||||
if not self.kiss_framing:
|
|
||||||
RNS.Transport.synthesize_tunnel(self)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
RNS.log("Attempt to reconnect on a non-initiator TCP interface. This should not happen.", RNS.LOG_ERROR)
|
RNS.log("Attempt to reconnect on a non-initiator TCP interface. This should not happen.", RNS.LOG_ERROR)
|
||||||
raise IOError("Attempt to reconnect on a non-initiator TCP interface")
|
raise IOError("Attempt to reconnect on a non-initiator TCP interface")
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
|
if not data: return
|
||||||
if self.online and not self.detached:
|
if self.online and not self.detached:
|
||||||
self.rxb += len(data)
|
|
||||||
if hasattr(self, "parent_interface") and self.parent_interface != None:
|
|
||||||
self.parent_interface.rxb += len(data)
|
|
||||||
|
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
|
|
@ -321,6 +348,13 @@ class TCPClientInterface(Interface):
|
||||||
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
self.teardown()
|
self.teardown()
|
||||||
|
|
||||||
|
def check_frame_len(self, frame_len):
|
||||||
|
if frame_len <= RNS.Reticulum.HEADER_MINSIZE: return False
|
||||||
|
elif frame_len > self.HW_MTU + (self.ifac_size or 0): return False
|
||||||
|
else: return True
|
||||||
|
|
||||||
|
def invalid_frame(self, frame_len):
|
||||||
|
RNS.log(f"Invalid HDLC frame of {RNS.prettysize(frame_len)} received on {self}, dropping frame", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
|
||||||
def read_loop(self):
|
def read_loop(self):
|
||||||
try:
|
try:
|
||||||
|
|
@ -331,7 +365,8 @@ class TCPClientInterface(Interface):
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data_in = self.socket.recv(4096)
|
if self.socket: data_in = self.socket.recv(4096)
|
||||||
|
else: data_in = b""
|
||||||
if len(data_in) > 0:
|
if len(data_in) > 0:
|
||||||
if self.kiss_framing:
|
if self.kiss_framing:
|
||||||
# Read loop for KISS framing
|
# Read loop for KISS framing
|
||||||
|
|
@ -376,12 +411,18 @@ class TCPClientInterface(Interface):
|
||||||
frame = frame_buffer[frame_start+1:frame_end]
|
frame = frame_buffer[frame_start+1:frame_end]
|
||||||
frame = frame.replace(bytes([HDLC.ESC, HDLC.FLAG ^ HDLC.ESC_MASK]), bytes([HDLC.FLAG]))
|
frame = frame.replace(bytes([HDLC.ESC, HDLC.FLAG ^ HDLC.ESC_MASK]), bytes([HDLC.FLAG]))
|
||||||
frame = frame.replace(bytes([HDLC.ESC, HDLC.ESC ^ HDLC.ESC_MASK]), bytes([HDLC.ESC]))
|
frame = frame.replace(bytes([HDLC.ESC, HDLC.ESC ^ HDLC.ESC_MASK]), bytes([HDLC.ESC]))
|
||||||
if len(frame) > RNS.Reticulum.HEADER_MINSIZE:
|
frame_len = len(frame)
|
||||||
self.process_incoming(frame)
|
if frame_len != 0:
|
||||||
|
if self.check_frame_len(frame_len): self.process_incoming(frame)
|
||||||
|
else: self.invalid_frame(len(frame))
|
||||||
|
|
||||||
frame_buffer = frame_buffer[frame_end:]
|
frame_buffer = frame_buffer[frame_end:]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
if len(frame_buffer) > self.HW_MTU*2: frame_buffer = b""
|
||||||
flags_remaining = False
|
flags_remaining = False
|
||||||
else:
|
else:
|
||||||
|
frame_buffer = b""
|
||||||
flags_remaining = False
|
flags_remaining = False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
@ -390,7 +431,7 @@ class TCPClientInterface(Interface):
|
||||||
RNS.log("The socket for "+str(self)+" was closed, attempting to reconnect...", RNS.LOG_WARNING)
|
RNS.log("The socket for "+str(self)+" was closed, attempting to reconnect...", RNS.LOG_WARNING)
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
else:
|
else:
|
||||||
RNS.log("The socket for remote client "+str(self)+" was closed.", RNS.LOG_VERBOSE)
|
RNS.log("The socket for remote client "+str(self)+" was closed.", RNS.LOG_DEBUG)
|
||||||
self.teardown()
|
self.teardown()
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
@ -400,7 +441,7 @@ class TCPClientInterface(Interface):
|
||||||
self.online = False
|
self.online = False
|
||||||
RNS.log("An interface error occurred for "+str(self)+", the contained exception was: "+str(e), RNS.LOG_WARNING)
|
RNS.log("An interface error occurred for "+str(self)+", the contained exception was: "+str(e), RNS.LOG_WARNING)
|
||||||
|
|
||||||
if self.initiator:
|
if self.initiator and not self.detached:
|
||||||
RNS.log("Attempting to reconnect...", RNS.LOG_WARNING)
|
RNS.log("Attempting to reconnect...", RNS.LOG_WARNING)
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
else:
|
else:
|
||||||
|
|
@ -423,16 +464,12 @@ class TCPClientInterface(Interface):
|
||||||
while self in self.parent_interface.spawned_interfaces:
|
while self in self.parent_interface.spawned_interfaces:
|
||||||
self.parent_interface.spawned_interfaces.remove(self)
|
self.parent_interface.spawned_interfaces.remove(self)
|
||||||
|
|
||||||
if self in RNS.Transport.interfaces:
|
if not self.initiator:
|
||||||
if not self.initiator:
|
RNS.Transport.remove_interface(self)
|
||||||
RNS.Transport.interfaces.remove(self)
|
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if ":" in self.target_ip:
|
if ":" in self.target_ip: ip_str = f"[{self.target_ip}]"
|
||||||
ip_str = f"[{self.target_ip}]"
|
else: ip_str = f"{self.target_ip}"
|
||||||
else:
|
|
||||||
ip_str = f"{self.target_ip}"
|
|
||||||
|
|
||||||
return "TCPInterface["+str(self.name)+"/"+ip_str+":"+str(self.target_port)+"]"
|
return "TCPInterface["+str(self.name)+"/"+ip_str+":"+str(self.target_port)+"]"
|
||||||
|
|
||||||
|
|
@ -444,7 +481,7 @@ class TCPServerInterface(Interface):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_address_for_if(name, bind_port, prefer_ipv6=False):
|
def get_address_for_if(name, bind_port, prefer_ipv6=False):
|
||||||
import RNS.vendor.ifaddr.niwrapper as netinfo
|
from RNS.Interfaces import netinfo
|
||||||
ifaddr = netinfo.ifaddresses(name)
|
ifaddr = netinfo.ifaddresses(name)
|
||||||
if len(ifaddr) < 1:
|
if len(ifaddr) < 1:
|
||||||
raise SystemError(f"No addresses available on specified kernel interface \"{name}\" for TCPServerInterface to bind to")
|
raise SystemError(f"No addresses available on specified kernel interface \"{name}\" for TCPServerInterface to bind to")
|
||||||
|
|
@ -453,9 +490,9 @@ class TCPServerInterface(Interface):
|
||||||
bind_ip = ifaddr[netinfo.AF_INET6][0]["addr"]
|
bind_ip = ifaddr[netinfo.AF_INET6][0]["addr"]
|
||||||
if bind_ip.lower().startswith("fe80::"):
|
if bind_ip.lower().startswith("fe80::"):
|
||||||
# We'll need to add the interface as scope for link-local addresses
|
# We'll need to add the interface as scope for link-local addresses
|
||||||
return TCPServerInterface.get_address_for_host(f"{bind_ip}%{name}", bind_port)
|
return TCPServerInterface.get_address_for_host(f"{bind_ip}%{name}", bind_port, prefer_ipv6)
|
||||||
else:
|
else:
|
||||||
return TCPServerInterface.get_address_for_host(bind_ip, bind_port)
|
return TCPServerInterface.get_address_for_host(bind_ip, bind_port, prefer_ipv6)
|
||||||
elif netinfo.AF_INET in ifaddr:
|
elif netinfo.AF_INET in ifaddr:
|
||||||
bind_ip = ifaddr[netinfo.AF_INET][0]["addr"]
|
bind_ip = ifaddr[netinfo.AF_INET][0]["addr"]
|
||||||
return (bind_ip, bind_port)
|
return (bind_ip, bind_port)
|
||||||
|
|
@ -463,8 +500,15 @@ class TCPServerInterface(Interface):
|
||||||
raise SystemError(f"No addresses available on specified kernel interface \"{name}\" for TCPServerInterface to bind to")
|
raise SystemError(f"No addresses available on specified kernel interface \"{name}\" for TCPServerInterface to bind to")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_address_for_host(name, bind_port):
|
def get_address_for_host(name, bind_port, prefer_ipv6=False):
|
||||||
address_info = socket.getaddrinfo(name, bind_port, proto=socket.IPPROTO_TCP)[0]
|
address_infos = socket.getaddrinfo(name, bind_port, proto=socket.IPPROTO_TCP)
|
||||||
|
address_info = address_infos[0]
|
||||||
|
for entry in address_infos:
|
||||||
|
if prefer_ipv6 and entry[0] == socket.AF_INET6:
|
||||||
|
address_info = entry; break
|
||||||
|
elif not prefer_ipv6 and entry[0] == socket.AF_INET:
|
||||||
|
address_info = entry; break
|
||||||
|
|
||||||
if address_info[0] == socket.AF_INET6:
|
if address_info[0] == socket.AF_INET6:
|
||||||
return (name, bind_port, address_info[4][2], address_info[4][3])
|
return (name, bind_port, address_info[4][2], address_info[4][3])
|
||||||
elif address_info[0] == socket.AF_INET:
|
elif address_info[0] == socket.AF_INET:
|
||||||
|
|
@ -492,6 +536,7 @@ class TCPServerInterface(Interface):
|
||||||
if port != None:
|
if port != None:
|
||||||
bindport = port
|
bindport = port
|
||||||
|
|
||||||
|
self.supports_discovery = True
|
||||||
self.HW_MTU = TCPInterface.HW_MTU
|
self.HW_MTU = TCPInterface.HW_MTU
|
||||||
|
|
||||||
self.online = False
|
self.online = False
|
||||||
|
|
@ -501,6 +546,8 @@ class TCPServerInterface(Interface):
|
||||||
self.OUT = False
|
self.OUT = False
|
||||||
self.name = name
|
self.name = name
|
||||||
self.detached = False
|
self.detached = False
|
||||||
|
self.tunnels_allowed = True
|
||||||
|
self.lossless_medium = True
|
||||||
|
|
||||||
self.i2p_tunneled = i2p_tunneled
|
self.i2p_tunneled = i2p_tunneled
|
||||||
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
self.mode = RNS.Interfaces.Interface.Interface.MODE_FULL
|
||||||
|
|
@ -516,7 +563,7 @@ class TCPServerInterface(Interface):
|
||||||
else:
|
else:
|
||||||
if bindip == None:
|
if bindip == None:
|
||||||
raise SystemError(f"No TCP bind IP configured for interface \"{name}\"")
|
raise SystemError(f"No TCP bind IP configured for interface \"{name}\"")
|
||||||
bind_address = TCPServerInterface.get_address_for_host(bindip, self.bind_port)
|
bind_address = TCPServerInterface.get_address_for_host(bindip, self.bind_port, prefer_ipv6)
|
||||||
|
|
||||||
if bind_address != None:
|
if bind_address != None:
|
||||||
self.receives = True
|
self.receives = True
|
||||||
|
|
@ -558,6 +605,24 @@ class TCPServerInterface(Interface):
|
||||||
spawned_interface = TCPClientInterface(self.owner, spawned_configuration, connected_socket=handler.request)
|
spawned_interface = TCPClientInterface(self.owner, spawned_configuration, connected_socket=handler.request)
|
||||||
spawned_interface.OUT = self.OUT
|
spawned_interface.OUT = self.OUT
|
||||||
spawned_interface.IN = self.IN
|
spawned_interface.IN = self.IN
|
||||||
|
|
||||||
|
spawned_interface.ingress_control = self.ingress_control
|
||||||
|
spawned_interface.ic_max_held_announces = self.ic_max_held_announces
|
||||||
|
spawned_interface.ic_burst_hold = self.ic_burst_hold
|
||||||
|
spawned_interface.ic_burst_freq = self.ic_burst_freq
|
||||||
|
spawned_interface.ic_burst_freq_new = self.ic_burst_freq_new
|
||||||
|
spawned_interface.ic_new_time = self.ic_new_time
|
||||||
|
spawned_interface.ic_burst_penalty = self.ic_burst_penalty
|
||||||
|
spawned_interface.ic_held_release_interval = self.ic_held_release_interval
|
||||||
|
|
||||||
|
spawned_interface.egress_control = self.egress_control
|
||||||
|
spawned_interface.ec_pr_freq = self.ec_pr_freq
|
||||||
|
spawned_interface.ic_pr_burst_freq_new = self.ic_pr_burst_freq_new
|
||||||
|
spawned_interface.ic_pr_burst_freq = self.ic_pr_burst_freq
|
||||||
|
|
||||||
|
spawned_interface._destination_cost = self._destination_cost
|
||||||
|
spawned_interface._pr_cost = self._pr_cost
|
||||||
|
|
||||||
spawned_interface.target_ip = handler.client_address[0]
|
spawned_interface.target_ip = handler.client_address[0]
|
||||||
spawned_interface.target_port = str(handler.client_address[1])
|
spawned_interface.target_port = str(handler.client_address[1])
|
||||||
spawned_interface.parent_interface = self
|
spawned_interface.parent_interface = self
|
||||||
|
|
@ -588,20 +653,40 @@ class TCPServerInterface(Interface):
|
||||||
spawned_interface.announce_rate_grace = self.announce_rate_grace
|
spawned_interface.announce_rate_grace = self.announce_rate_grace
|
||||||
spawned_interface.announce_rate_penalty = self.announce_rate_penalty
|
spawned_interface.announce_rate_penalty = self.announce_rate_penalty
|
||||||
spawned_interface.mode = self.mode
|
spawned_interface.mode = self.mode
|
||||||
|
spawned_interface.gravity = self.gravity
|
||||||
|
spawned_interface.hop_limiter = self.hop_limiter
|
||||||
|
spawned_interface.recursive_prs = self.recursive_prs
|
||||||
|
spawned_interface.announces_from_internal = self.announces_from_internal
|
||||||
|
spawned_interface.announces_to_internal = self.announces_to_internal
|
||||||
|
spawned_interface.announce_cap = self.announce_cap
|
||||||
spawned_interface.HW_MTU = self.HW_MTU
|
spawned_interface.HW_MTU = self.HW_MTU
|
||||||
spawned_interface.online = True
|
spawned_interface.online = True
|
||||||
RNS.log("Spawned new TCPClient Interface: "+str(spawned_interface), RNS.LOG_VERBOSE)
|
RNS.log("Spawned new TCPClient Interface: "+str(spawned_interface), RNS.LOG_VERBOSE)
|
||||||
RNS.Transport.interfaces.append(spawned_interface)
|
RNS.Transport.add_interface(spawned_interface)
|
||||||
while spawned_interface in self.spawned_interfaces:
|
while spawned_interface in self.spawned_interfaces:
|
||||||
self.spawned_interfaces.remove(spawned_interface)
|
self.spawned_interfaces.remove(spawned_interface)
|
||||||
self.spawned_interfaces.append(spawned_interface)
|
self.spawned_interfaces.append(spawned_interface)
|
||||||
spawned_interface.read_loop()
|
spawned_interface.read_loop()
|
||||||
|
|
||||||
def received_announce(self, from_spawned=False):
|
def received_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.ia_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.ia_freq_deque.append(time.time())
|
||||||
|
self.arxb += size
|
||||||
|
|
||||||
def sent_announce(self, from_spawned=False):
|
def sent_announce(self, size=0, from_spawned=False):
|
||||||
if from_spawned: self.oa_freq_deque.append(time.time())
|
if from_spawned:
|
||||||
|
self.oa_freq_deque.append(time.time())
|
||||||
|
self.atxb += size
|
||||||
|
|
||||||
|
def received_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.ip_freq_deque.append(time.time())
|
||||||
|
self.prxb += size
|
||||||
|
|
||||||
|
def sent_path_request(self, size=0, from_spawned=False):
|
||||||
|
if from_spawned:
|
||||||
|
self.op_freq_deque.append(time.time())
|
||||||
|
self.ptxb += size
|
||||||
|
|
||||||
def process_outgoing(self, data):
|
def process_outgoing(self, data):
|
||||||
pass
|
pass
|
||||||
|
|
@ -623,10 +708,8 @@ class TCPServerInterface(Interface):
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
if ":" in self.bind_ip:
|
if ":" in self.bind_ip: ip_str = f"[{self.bind_ip}]"
|
||||||
ip_str = f"[{self.bind_ip}]"
|
else: ip_str = f"{self.bind_ip}"
|
||||||
else:
|
|
||||||
ip_str = f"{self.bind_ip}"
|
|
||||||
|
|
||||||
return "TCPServerInterface["+self.name+"/"+ip_str+":"+str(self.bind_port)+"]"
|
return "TCPServerInterface["+self.name+"/"+ip_str+":"+str(self.bind_port)+"]"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -35,13 +43,13 @@ class UDPInterface(Interface):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_address_for_if(name):
|
def get_address_for_if(name):
|
||||||
import RNS.vendor.ifaddr.niwrapper as netinfo
|
from RNS.Interfaces import netinfo
|
||||||
ifaddr = netinfo.ifaddresses(name)
|
ifaddr = netinfo.ifaddresses(name)
|
||||||
return ifaddr[netinfo.AF_INET][0]["addr"]
|
return ifaddr[netinfo.AF_INET][0]["addr"]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_broadcast_for_if(name):
|
def get_broadcast_for_if(name):
|
||||||
import RNS.vendor.ifaddr.niwrapper as netinfo
|
from RNS.Interfaces import netinfo
|
||||||
ifaddr = netinfo.ifaddresses(name)
|
ifaddr = netinfo.ifaddresses(name)
|
||||||
return ifaddr[netinfo.AF_INET][0]["broadcast"]
|
return ifaddr[netinfo.AF_INET][0]["broadcast"]
|
||||||
|
|
||||||
|
|
@ -70,6 +78,7 @@ class UDPInterface(Interface):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.online = False
|
self.online = False
|
||||||
self.bitrate = UDPInterface.BITRATE_GUESS
|
self.bitrate = UDPInterface.BITRATE_GUESS
|
||||||
|
self.shared_medium = True
|
||||||
|
|
||||||
if device != None:
|
if device != None:
|
||||||
if bindip == None:
|
if bindip == None:
|
||||||
|
|
@ -104,9 +113,17 @@ class UDPInterface(Interface):
|
||||||
self.forward_ip = forwardip
|
self.forward_ip = forwardip
|
||||||
self.forward_port = forwardport
|
self.forward_port = forwardport
|
||||||
|
|
||||||
|
def detach(self):
|
||||||
|
self.detached = True
|
||||||
|
self.online = False
|
||||||
|
if self.server:
|
||||||
|
try:
|
||||||
|
self.server.server_close()
|
||||||
|
self.server.shutdown()
|
||||||
|
except Exception as e: RNS.log(f"Could not shut down UDP listener for {self}: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
def process_incoming(self, data):
|
def process_incoming(self, data):
|
||||||
self.rxb += len(data)
|
if not data: return
|
||||||
self.owner.inbound(data, self)
|
self.owner.inbound(data, self)
|
||||||
|
|
||||||
def process_outgoing(self,data):
|
def process_outgoing(self,data):
|
||||||
|
|
|
||||||
1096
RNS/Interfaces/WeaveInterface.py
Normal file
1096
RNS/Interfaces/WeaveInterface.py
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2025 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -23,8 +31,12 @@
|
||||||
import os
|
import os
|
||||||
import glob
|
import glob
|
||||||
import RNS.Interfaces.Android
|
import RNS.Interfaces.Android
|
||||||
|
import RNS.Interfaces.util
|
||||||
|
import RNS.Interfaces.util.netinfo as netinfo
|
||||||
|
|
||||||
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
modules = py_modules+pyc_modules
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
__all__ = list(set([os.path.basename(f).replace(".pyc", "").replace(".py", "") for f in modules if not (f.endswith("__init__.py") or f.endswith("__init__.pyc"))]))
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
115
RNS/Interfaces/util/HDLC.py
Normal file
115
RNS/Interfaces/util/HDLC.py
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
class HDLC():
|
||||||
|
FLAG = 0x7E
|
||||||
|
ESC = 0x7D
|
||||||
|
ESC_MASK = 0x20
|
||||||
|
|
||||||
|
FLAG_B = bytes([FLAG])
|
||||||
|
ESC_B = bytes([ESC])
|
||||||
|
ESCPD_FLAG = bytes([ESC, FLAG ^ ESC_MASK])
|
||||||
|
ESCPD_ESC = bytes([ESC, ESC ^ ESC_MASK])
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def escape(data):
|
||||||
|
data = data.replace(HDLC.ESC_B, HDLC.ESCPD_ESC)
|
||||||
|
data = data.replace(HDLC.FLAG_B, HDLC.ESCPD_FLAG)
|
||||||
|
return data
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def unescape(frame):
|
||||||
|
frame = frame.replace(HDLC.ESCPD_FLAG, HDLC.FLAG_B)
|
||||||
|
frame = frame.replace(HDLC.ESCPD_ESC, HDLC.ESC_B)
|
||||||
|
return frame
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def frame(data):
|
||||||
|
esc = HDLC.escape(data)
|
||||||
|
frame = bytearray(len(esc)+2)
|
||||||
|
frame[0] = HDLC.FLAG
|
||||||
|
frame[1:-1] = esc
|
||||||
|
frame[-1] = HDLC.FLAG
|
||||||
|
return frame
|
||||||
|
|
||||||
|
class ReceiveBuffer():
|
||||||
|
def __init__(self, mtu, min_frame_len, max_frame_len=None, on_frame=None, on_invalid=None):
|
||||||
|
self._mtu = mtu
|
||||||
|
self._min_frame_len = min_frame_len
|
||||||
|
self._max_frame_len = max_frame_len
|
||||||
|
self._on_frame = on_frame
|
||||||
|
self._on_invalid = on_invalid
|
||||||
|
self._buf = bytearray()
|
||||||
|
self._off = 0
|
||||||
|
|
||||||
|
def mtu(self):
|
||||||
|
return self._mtu() if callable(self._mtu) else self._mtu
|
||||||
|
|
||||||
|
def max(self):
|
||||||
|
m = self._max_frame_len
|
||||||
|
return m() if callable(m) else m
|
||||||
|
|
||||||
|
def feed(self, data):
|
||||||
|
if not data: return
|
||||||
|
self._buf.extend(data)
|
||||||
|
buf = self._buf
|
||||||
|
|
||||||
|
while True:
|
||||||
|
frame_start = buf.find(HDLC.FLAG, self._off)
|
||||||
|
if frame_start == -1:
|
||||||
|
self.reset()
|
||||||
|
return
|
||||||
|
|
||||||
|
frame_end = buf.find(HDLC.FLAG, frame_start+1)
|
||||||
|
if frame_end == -1:
|
||||||
|
if len(buf)-self._off > self.mtu()*2: self.reset()
|
||||||
|
return
|
||||||
|
|
||||||
|
frame = HDLC.unescape(bytes(buf[frame_start+1:frame_end]))
|
||||||
|
frame_len = len(frame)
|
||||||
|
if frame_len > 0:
|
||||||
|
max_len = self.max()
|
||||||
|
if frame_len > self._min_frame_len and (max_len is None or frame_len <= max_len):
|
||||||
|
if self._on_frame is not None: self._on_frame(frame)
|
||||||
|
elif self._on_invalid is not None: self._on_invalid(frame_len)
|
||||||
|
|
||||||
|
self._off = frame_end
|
||||||
|
if self._off > 0 and self._off >= len(buf) // 2:
|
||||||
|
del buf[:self._off]
|
||||||
|
self._off = 0
|
||||||
|
|
||||||
|
# Drops any partial frames from the buffer and
|
||||||
|
# resets assembly offset
|
||||||
|
def reset(self):
|
||||||
|
self._buf.clear()
|
||||||
|
self._off = 0
|
||||||
|
|
||||||
|
# Buffered bytes currently unconsumed
|
||||||
|
def __len__(self): return len(self._buf)-self._off
|
||||||
171
RNS/Interfaces/util/TransmitBuffer.py
Normal file
171
RNS/Interfaces/util/TransmitBuffer.py
Normal file
|
|
@ -0,0 +1,171 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from collections import deque
|
||||||
|
import threading
|
||||||
|
|
||||||
|
# Zero-copy coalesced transmit buffer for arbitrary
|
||||||
|
# frame sizes. It's even almost 100% lock-free.
|
||||||
|
class TransmitBuffer:
|
||||||
|
COALESCE_TARGET = 65536
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._tail_lock = threading.Lock()
|
||||||
|
|
||||||
|
# Producer-owned state
|
||||||
|
self._cur = None
|
||||||
|
self._cur_frames = 0
|
||||||
|
|
||||||
|
# Consumer-owned state
|
||||||
|
self._head_offset = 0
|
||||||
|
|
||||||
|
# Shared chunk queue. Producer appends right, consumer
|
||||||
|
# pops from left. Appended chunks are immutable.
|
||||||
|
self._chunks = deque()
|
||||||
|
|
||||||
|
# Accounting counters
|
||||||
|
|
||||||
|
# Producer-owned
|
||||||
|
self._tx_total = 0 # Total bytes appended
|
||||||
|
self._tx_visible = 0 # Total bytes pushed to the chunk queue
|
||||||
|
self._tx_frames = 0 # Total frames appended
|
||||||
|
|
||||||
|
# Consumer-owned
|
||||||
|
self._tx_sent = 0 # Total bytes sent to the kernel
|
||||||
|
self._tx_frames_sent = 0 # Total frames fully sent
|
||||||
|
|
||||||
|
# Queue a finalized and complete on-wire frame.
|
||||||
|
# Executed by producer side only.
|
||||||
|
def append(self, frame, limit=None):
|
||||||
|
with self._tail_lock:
|
||||||
|
if limit is not None and (self._tx_total - self._tx_sent) + len(frame) > limit: return False
|
||||||
|
|
||||||
|
# Allocate separate chunks to frames
|
||||||
|
# larger than the coalescing target.
|
||||||
|
if len(frame) >= self.COALESCE_TARGET:
|
||||||
|
self._flush()
|
||||||
|
self._chunks.append((frame, 1))
|
||||||
|
self._tx_visible += len(frame)
|
||||||
|
self._tx_total += len(frame)
|
||||||
|
self._tx_frames += 1
|
||||||
|
|
||||||
|
else:
|
||||||
|
if self._cur is not None and len(self._cur) + len(frame) > self.COALESCE_TARGET: self._flush()
|
||||||
|
|
||||||
|
if self._cur is None:
|
||||||
|
self._cur = bytearray()
|
||||||
|
self._cur_frames = 0
|
||||||
|
|
||||||
|
self._cur.extend(frame)
|
||||||
|
self._cur_frames += 1
|
||||||
|
self._tx_total += len(frame)
|
||||||
|
self._tx_frames += 1
|
||||||
|
|
||||||
|
if not self._chunks:
|
||||||
|
# Queue is idle. Make the frame visible immediately so
|
||||||
|
# sparse traffic does not incur coalescing latency.
|
||||||
|
self._flush()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _flush(self):
|
||||||
|
if self._cur is not None and len(self._cur) > 0:
|
||||||
|
self._chunks.append((self._cur, self._cur_frames))
|
||||||
|
self._tx_visible += len(self._cur)
|
||||||
|
self._cur = None
|
||||||
|
self._cur_frames = 0
|
||||||
|
|
||||||
|
# Make producer-side coalescing tail visible to the consumer.
|
||||||
|
# Called by producer when it has finished producing a burst,
|
||||||
|
# and wants to make the remaining bytes transmittable. Safe to
|
||||||
|
# call after every append.
|
||||||
|
def flush(self):
|
||||||
|
with self._tail_lock: self._flush()
|
||||||
|
|
||||||
|
# Drain buffered frames to a socket. Called by consumer. If the
|
||||||
|
# chunk queue is drained completely, any potential producer-side
|
||||||
|
# coalescing tail is released under the tail lock, and drained in
|
||||||
|
# the same cycle. If you possess wizardry for doing this without
|
||||||
|
# the lock, I am all ears.
|
||||||
|
def drain_to(self, socket):
|
||||||
|
# TODO: At *some* point (no, not now), replace with a
|
||||||
|
# mechanism for send_msg to immediately drain all
|
||||||
|
# chunks directly to the kernel.
|
||||||
|
total = 0
|
||||||
|
whole = True
|
||||||
|
while whole:
|
||||||
|
if not self._chunks:
|
||||||
|
with self._tail_lock: self._flush()
|
||||||
|
if not self._chunks: break
|
||||||
|
|
||||||
|
chunk, _ = self._chunks[0]
|
||||||
|
remaining = len(chunk) - self._head_offset
|
||||||
|
if remaining <= 0:
|
||||||
|
self._pop_head()
|
||||||
|
continue
|
||||||
|
|
||||||
|
try: written = socket.send(memoryview(chunk)[self._head_offset:])
|
||||||
|
except BlockingIOError: break
|
||||||
|
except InterruptedError: continue
|
||||||
|
if written <= 0: break
|
||||||
|
|
||||||
|
total += written
|
||||||
|
self._head_offset += written
|
||||||
|
self._tx_sent += written
|
||||||
|
|
||||||
|
if self._head_offset >= len(chunk): self._pop_head()
|
||||||
|
else: whole = False
|
||||||
|
|
||||||
|
return total
|
||||||
|
|
||||||
|
def _pop_head(self):
|
||||||
|
chunk, frames = self._chunks.popleft()
|
||||||
|
self._tx_frames_sent += frames
|
||||||
|
self._head_offset = 0
|
||||||
|
|
||||||
|
# Total number of bytes currently buffered, including the
|
||||||
|
# producer's in-progress coalescing chunk.
|
||||||
|
def __len__(self): return self._tx_total-self._tx_sent
|
||||||
|
|
||||||
|
# The number of bytes actually ready to send, and will be
|
||||||
|
# visible to the consumer, that is, what can be drained.
|
||||||
|
@property
|
||||||
|
def sendable(self): return self._tx_visible-self._tx_sent
|
||||||
|
|
||||||
|
# The number of complete frames currently buffere, including
|
||||||
|
# ones contained in a potential in-progress coalescing chunk.
|
||||||
|
@property
|
||||||
|
def frames_buffered(self): return self._tx_frames-self._tx_frames_sent
|
||||||
|
|
||||||
|
# Number of chunks currently queued and visible to the consumer
|
||||||
|
@property
|
||||||
|
def chunks_buffered(self): return len(self._chunks)
|
||||||
|
|
||||||
|
def __repr__(self): return f"<TransmitBuffer buffered={len(self)} sendable={self.sendable} frames={self.frames_buffered} chunks={self.chunks_buffered}>"
|
||||||
9
RNS/Interfaces/util/__init__.py
Normal file
9
RNS/Interfaces/util/__init__.py
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
|
||||||
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
325
RNS/Interfaces/util/netinfo.py
Normal file
325
RNS/Interfaces/util/netinfo.py
Normal file
|
|
@ -0,0 +1,325 @@
|
||||||
|
# MIT License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2014 Stefan C. Mueller
|
||||||
|
# Copyright (c) 2025 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
import ipaddress
|
||||||
|
import platform
|
||||||
|
import ctypes.util
|
||||||
|
import collections
|
||||||
|
from typing import List, Iterable, Optional, Tuple, Union
|
||||||
|
|
||||||
|
AF_INET6 = socket.AF_INET6.value
|
||||||
|
AF_INET = socket.AF_INET.value
|
||||||
|
|
||||||
|
def interfaces() -> List[str]:
|
||||||
|
adapters = get_adapters(include_unconfigured=True)
|
||||||
|
return [a.name for a in adapters]
|
||||||
|
|
||||||
|
def interface_names_to_indexes() -> dict:
|
||||||
|
adapters = get_adapters(include_unconfigured=True)
|
||||||
|
results = {}
|
||||||
|
for adapter in adapters:
|
||||||
|
results[adapter.name] = adapter.index
|
||||||
|
return results
|
||||||
|
|
||||||
|
def interface_name_to_nice_name(ifname) -> str:
|
||||||
|
try:
|
||||||
|
adapters = get_adapters(include_unconfigured=True)
|
||||||
|
for adapter in adapters:
|
||||||
|
if adapter.name == ifname:
|
||||||
|
if hasattr(adapter, "nice_name"):
|
||||||
|
return adapter.nice_name
|
||||||
|
|
||||||
|
except: return None
|
||||||
|
return None
|
||||||
|
|
||||||
|
def ifaddresses(ifname) -> dict:
|
||||||
|
adapters = get_adapters(include_unconfigured=True)
|
||||||
|
ifa = {}
|
||||||
|
for a in adapters:
|
||||||
|
if a.name == ifname:
|
||||||
|
ipv4s = []
|
||||||
|
ipv6s = []
|
||||||
|
for ip in a.ips:
|
||||||
|
t = {}
|
||||||
|
if ip.is_IPv4:
|
||||||
|
net = ipaddress.ip_network(str(ip.ip)+"/"+str(ip.network_prefix), strict=False)
|
||||||
|
t["addr"] = ip.ip
|
||||||
|
t["prefix"] = ip.network_prefix
|
||||||
|
t["broadcast"] = str(net.broadcast_address)
|
||||||
|
ipv4s.append(t)
|
||||||
|
if ip.is_IPv6:
|
||||||
|
t["addr"] = ip.ip[0]
|
||||||
|
ipv6s.append(t)
|
||||||
|
|
||||||
|
if len(ipv4s) > 0: ifa[AF_INET] = ipv4s
|
||||||
|
if len(ipv6s) > 0: ifa[AF_INET6] = ipv6s
|
||||||
|
|
||||||
|
return ifa
|
||||||
|
|
||||||
|
def get_adapters(include_unconfigured=False):
|
||||||
|
if os.name == "posix": return _get_adapters_posix(include_unconfigured=include_unconfigured)
|
||||||
|
elif os.name == "nt": return _get_adapters_win(include_unconfigured=include_unconfigured)
|
||||||
|
else: raise RuntimeError(f"Unsupported Operating System: {os.name}")
|
||||||
|
|
||||||
|
class Adapter(object):
|
||||||
|
def __init__(self, name: str, nice_name: str, ips: List["IP"], index: Optional[int] = None) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.nice_name = nice_name
|
||||||
|
self.ips = ips
|
||||||
|
self.index = index
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return "Adapter(name={name}, nice_name={nice_name}, ips={ips}, index={index})".format(
|
||||||
|
name=repr(self.name), nice_name=repr(self.nice_name), ips=repr(self.ips), index=repr(self.index))
|
||||||
|
|
||||||
|
_IPv4Address = str
|
||||||
|
_IPv6Address = Tuple[str, int, int]
|
||||||
|
class IP(object):
|
||||||
|
def __init__(self, ip: Union[_IPv4Address, _IPv6Address], network_prefix: int, nice_name: str) -> None:
|
||||||
|
self.ip = ip
|
||||||
|
self.network_prefix = network_prefix
|
||||||
|
self.nice_name = nice_name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_IPv4(self) -> bool: return not isinstance(self.ip, tuple)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_IPv6(self) -> bool: return isinstance(self.ip, tuple)
|
||||||
|
|
||||||
|
def __repr__(self) -> str:
|
||||||
|
return "IP(ip={ip}, network_prefix={network_prefix}, nice_name={nice_name})".format(ip=repr(self.ip), network_prefix=repr(self.network_prefix), nice_name=repr(self.nice_name))
|
||||||
|
|
||||||
|
if platform.system() == "Darwin" or "BSD" in platform.system():
|
||||||
|
class sockaddr(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("sa_len", ctypes.c_uint8),
|
||||||
|
("sa_familiy", ctypes.c_uint8),
|
||||||
|
("sa_data", ctypes.c_uint8 * 14)]
|
||||||
|
|
||||||
|
class sockaddr_in(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("sa_len", ctypes.c_uint8),
|
||||||
|
("sa_familiy", ctypes.c_uint8),
|
||||||
|
("sin_port", ctypes.c_uint16),
|
||||||
|
("sin_addr", ctypes.c_uint8 * 4),
|
||||||
|
("sin_zero", ctypes.c_uint8 * 8)]
|
||||||
|
|
||||||
|
class sockaddr_in6(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("sa_len", ctypes.c_uint8),
|
||||||
|
("sa_familiy", ctypes.c_uint8),
|
||||||
|
("sin6_port", ctypes.c_uint16),
|
||||||
|
("sin6_flowinfo", ctypes.c_uint32),
|
||||||
|
("sin6_addr", ctypes.c_uint8 * 16),
|
||||||
|
("sin6_scope_id", ctypes.c_uint32)]
|
||||||
|
|
||||||
|
else:
|
||||||
|
class sockaddr(ctypes.Structure): # type: ignore
|
||||||
|
_fields_ = [("sa_familiy", ctypes.c_uint16), ("sa_data", ctypes.c_uint8 * 14)]
|
||||||
|
|
||||||
|
class sockaddr_in(ctypes.Structure): # type: ignore
|
||||||
|
_fields_ = [
|
||||||
|
("sin_familiy", ctypes.c_uint16),
|
||||||
|
("sin_port", ctypes.c_uint16),
|
||||||
|
("sin_addr", ctypes.c_uint8 * 4),
|
||||||
|
("sin_zero", ctypes.c_uint8 * 8)]
|
||||||
|
|
||||||
|
class sockaddr_in6(ctypes.Structure): # type: ignore
|
||||||
|
_fields_ = [
|
||||||
|
("sin6_familiy", ctypes.c_uint16),
|
||||||
|
("sin6_port", ctypes.c_uint16),
|
||||||
|
("sin6_flowinfo", ctypes.c_uint32),
|
||||||
|
("sin6_addr", ctypes.c_uint8 * 16),
|
||||||
|
("sin6_scope_id", ctypes.c_uint32)]
|
||||||
|
|
||||||
|
def sockaddr_to_ip(sockaddr_ptr: "ctypes.pointer[sockaddr]") -> Optional[Union[_IPv4Address, _IPv6Address]]:
|
||||||
|
if sockaddr_ptr:
|
||||||
|
if sockaddr_ptr[0].sa_familiy == socket.AF_INET:
|
||||||
|
ipv4 = ctypes.cast(sockaddr_ptr, ctypes.POINTER(sockaddr_in))
|
||||||
|
ippacked = bytes(bytearray(ipv4[0].sin_addr))
|
||||||
|
ip = str(ipaddress.ip_address(ippacked))
|
||||||
|
return ip
|
||||||
|
elif sockaddr_ptr[0].sa_familiy == socket.AF_INET6:
|
||||||
|
ipv6 = ctypes.cast(sockaddr_ptr, ctypes.POINTER(sockaddr_in6))
|
||||||
|
flowinfo = ipv6[0].sin6_flowinfo
|
||||||
|
ippacked = bytes(bytearray(ipv6[0].sin6_addr))
|
||||||
|
ip = str(ipaddress.ip_address(ippacked))
|
||||||
|
scope_id = ipv6[0].sin6_scope_id
|
||||||
|
return (ip, flowinfo, scope_id)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def ipv6_prefixlength(address: ipaddress.IPv6Address) -> int:
|
||||||
|
prefix_length = 0
|
||||||
|
for i in range(address.max_prefixlen):
|
||||||
|
if int(address) >> i & 1: prefix_length = prefix_length + 1
|
||||||
|
return prefix_length
|
||||||
|
|
||||||
|
if os.name == "posix":
|
||||||
|
class ifaddrs(ctypes.Structure): pass
|
||||||
|
ifaddrs._fields_ = [
|
||||||
|
("ifa_next", ctypes.POINTER(ifaddrs)),
|
||||||
|
("ifa_name", ctypes.c_char_p),
|
||||||
|
("ifa_flags", ctypes.c_uint),
|
||||||
|
("ifa_addr", ctypes.POINTER(sockaddr)),
|
||||||
|
("ifa_netmask", ctypes.POINTER(sockaddr)),]
|
||||||
|
|
||||||
|
libc = ctypes.CDLL(ctypes.util.find_library("socket" if os.uname()[0] == "SunOS" else "c"), use_errno=True) # type: ignore
|
||||||
|
|
||||||
|
def _get_adapters_posix(include_unconfigured: bool = False) -> Iterable[Adapter]:
|
||||||
|
addr0 = addr = ctypes.POINTER(ifaddrs)()
|
||||||
|
retval = libc.getifaddrs(ctypes.byref(addr))
|
||||||
|
if retval != 0:
|
||||||
|
eno = ctypes.get_errno()
|
||||||
|
raise OSError(eno, os.strerror(eno))
|
||||||
|
|
||||||
|
ips = collections.OrderedDict()
|
||||||
|
|
||||||
|
def add_ip(adapter_name: str, ip: Optional[IP]) -> None:
|
||||||
|
if adapter_name not in ips:
|
||||||
|
index = None # type: Optional[int]
|
||||||
|
try:
|
||||||
|
index = socket.if_nametoindex(adapter_name) # type: ignore
|
||||||
|
except (OSError, AttributeError): pass
|
||||||
|
ips[adapter_name] = Adapter(adapter_name, adapter_name, [], index=index)
|
||||||
|
if ip is not None:
|
||||||
|
ips[adapter_name].ips.append(ip)
|
||||||
|
|
||||||
|
while addr:
|
||||||
|
name = addr[0].ifa_name.decode(encoding="UTF-8")
|
||||||
|
ip_addr = sockaddr_to_ip(addr[0].ifa_addr)
|
||||||
|
if ip_addr:
|
||||||
|
if addr[0].ifa_netmask and not addr[0].ifa_netmask[0].sa_familiy:
|
||||||
|
addr[0].ifa_netmask[0].sa_familiy = addr[0].ifa_addr[0].sa_familiy
|
||||||
|
netmask = sockaddr_to_ip(addr[0].ifa_netmask)
|
||||||
|
if isinstance(netmask, tuple):
|
||||||
|
netmaskStr = str(netmask[0])
|
||||||
|
prefixlen = ipv6_prefixlength(ipaddress.IPv6Address(netmaskStr))
|
||||||
|
else:
|
||||||
|
assert netmask is not None, f"sockaddr_to_ip({addr[0].ifa_netmask}) returned None"
|
||||||
|
netmaskStr = str("0.0.0.0/" + netmask)
|
||||||
|
prefixlen = ipaddress.IPv4Network(netmaskStr).prefixlen
|
||||||
|
ip = IP(ip_addr, prefixlen, name)
|
||||||
|
add_ip(name, ip)
|
||||||
|
else:
|
||||||
|
if include_unconfigured:
|
||||||
|
add_ip(name, None)
|
||||||
|
addr = addr[0].ifa_next
|
||||||
|
|
||||||
|
libc.freeifaddrs(addr0)
|
||||||
|
return ips.values()
|
||||||
|
|
||||||
|
elif os.name == "nt":
|
||||||
|
from ctypes import wintypes
|
||||||
|
NO_ERROR = 0
|
||||||
|
ERROR_BUFFER_OVERFLOW = 111
|
||||||
|
MAX_ADAPTER_NAME_LENGTH = 256
|
||||||
|
MAX_ADAPTER_DESCRIPTION_LENGTH = 128
|
||||||
|
MAX_ADAPTER_ADDRESS_LENGTH = 8
|
||||||
|
AF_UNSPEC = 0
|
||||||
|
|
||||||
|
class SOCKET_ADDRESS(ctypes.Structure): _fields_ = [("lpSockaddr", ctypes.POINTER(sockaddr)), ("iSockaddrLength", wintypes.INT)]
|
||||||
|
class IP_ADAPTER_UNICAST_ADDRESS(ctypes.Structure): pass
|
||||||
|
IP_ADAPTER_UNICAST_ADDRESS._fields_ = [
|
||||||
|
("Length", wintypes.ULONG),
|
||||||
|
("Flags", wintypes.DWORD),
|
||||||
|
("Next", ctypes.POINTER(IP_ADAPTER_UNICAST_ADDRESS)),
|
||||||
|
("Address", SOCKET_ADDRESS),
|
||||||
|
("PrefixOrigin", ctypes.c_uint),
|
||||||
|
("SuffixOrigin", ctypes.c_uint),
|
||||||
|
("DadState", ctypes.c_uint),
|
||||||
|
("ValidLifetime", wintypes.ULONG),
|
||||||
|
("PreferredLifetime", wintypes.ULONG),
|
||||||
|
("LeaseLifetime", wintypes.ULONG),
|
||||||
|
("OnLinkPrefixLength", ctypes.c_uint8)]
|
||||||
|
|
||||||
|
class IP_ADAPTER_ADDRESSES(ctypes.Structure): pass
|
||||||
|
IP_ADAPTER_ADDRESSES._fields_ = [
|
||||||
|
("Length", wintypes.ULONG),
|
||||||
|
("IfIndex", wintypes.DWORD),
|
||||||
|
("Next", ctypes.POINTER(IP_ADAPTER_ADDRESSES)),
|
||||||
|
("AdapterName", ctypes.c_char_p),
|
||||||
|
("FirstUnicastAddress", ctypes.POINTER(IP_ADAPTER_UNICAST_ADDRESS)),
|
||||||
|
("FirstAnycastAddress", ctypes.c_void_p),
|
||||||
|
("FirstMulticastAddress", ctypes.c_void_p),
|
||||||
|
("FirstDnsServerAddress", ctypes.c_void_p),
|
||||||
|
("DnsSuffix", ctypes.c_wchar_p),
|
||||||
|
("Description", ctypes.c_wchar_p),
|
||||||
|
("FriendlyName", ctypes.c_wchar_p)]
|
||||||
|
|
||||||
|
iphlpapi = ctypes.windll.LoadLibrary("Iphlpapi") # type: ignore
|
||||||
|
|
||||||
|
def _enumerate_interfaces_of_adapter_win(nice_name: str, address: IP_ADAPTER_UNICAST_ADDRESS) -> Iterable[IP]:
|
||||||
|
# Iterate through linked list and fill list
|
||||||
|
addresses = [] # type: List[IP_ADAPTER_UNICAST_ADDRESS]
|
||||||
|
while True:
|
||||||
|
addresses.append(address)
|
||||||
|
if not address.Next: break
|
||||||
|
address = address.Next[0]
|
||||||
|
|
||||||
|
for address in addresses:
|
||||||
|
ip = sockaddr_to_ip(address.Address.lpSockaddr)
|
||||||
|
assert ip is not None, f"sockaddr_to_ip({address.Address.lpSockaddr}) returned None"
|
||||||
|
network_prefix = address.OnLinkPrefixLength
|
||||||
|
yield IP(ip, network_prefix, nice_name)
|
||||||
|
|
||||||
|
def _get_adapters_win(include_unconfigured: bool = False) -> Iterable[Adapter]:
|
||||||
|
addressbuffersize = wintypes.ULONG(15 * 1024)
|
||||||
|
retval = ERROR_BUFFER_OVERFLOW
|
||||||
|
while retval == ERROR_BUFFER_OVERFLOW:
|
||||||
|
addressbuffer = ctypes.create_string_buffer(addressbuffersize.value)
|
||||||
|
retval = iphlpapi.GetAdaptersAddresses(
|
||||||
|
wintypes.ULONG(AF_UNSPEC),
|
||||||
|
wintypes.ULONG(0),
|
||||||
|
None,
|
||||||
|
ctypes.byref(addressbuffer),
|
||||||
|
ctypes.byref(addressbuffersize))
|
||||||
|
|
||||||
|
if retval != NO_ERROR:
|
||||||
|
raise ctypes.WinError() # type: ignore
|
||||||
|
|
||||||
|
# Iterate through adapters and fill array
|
||||||
|
address_infos = [] # type: List[IP_ADAPTER_ADDRESSES]
|
||||||
|
address_info = IP_ADAPTER_ADDRESSES.from_buffer(addressbuffer)
|
||||||
|
while True:
|
||||||
|
address_infos.append(address_info)
|
||||||
|
if not address_info.Next: break
|
||||||
|
address_info = address_info.Next[0]
|
||||||
|
|
||||||
|
# Iterate through unicast addresses
|
||||||
|
result = [] # type: List[Adapter]
|
||||||
|
for adapter_info in address_infos:
|
||||||
|
name = adapter_info.AdapterName.decode()
|
||||||
|
nice_name = adapter_info.Description
|
||||||
|
index = adapter_info.IfIndex
|
||||||
|
|
||||||
|
if adapter_info.FirstUnicastAddress:
|
||||||
|
ips = _enumerate_interfaces_of_adapter_win(adapter_info.FriendlyName, adapter_info.FirstUnicastAddress[0])
|
||||||
|
ips = list(ips)
|
||||||
|
result.append(Adapter(name, nice_name, ips, index=index))
|
||||||
|
|
||||||
|
elif include_unconfigured: result.append(Adapter(name, nice_name, [], index=index))
|
||||||
|
|
||||||
|
return result
|
||||||
893
RNS/Link.py
893
RNS/Link.py
File diff suppressed because it is too large
Load diff
196
RNS/Packet.py
196
RNS/Packet.py
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2024 Mark Qvist / unsigned.io and contributors.
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -35,10 +43,10 @@ class Packet:
|
||||||
|
|
||||||
For ``RNS.Destination.GROUP`` destinations, Reticulum will use the
|
For ``RNS.Destination.GROUP`` destinations, Reticulum will use the
|
||||||
pre-shared key configured for the destination. All packets to group
|
pre-shared key configured for the destination. All packets to group
|
||||||
destinations are encrypted with the same AES-128 key.
|
destinations are encrypted with the same AES-256 key.
|
||||||
|
|
||||||
For ``RNS.Destination.SINGLE`` destinations, Reticulum will use a newly
|
For ``RNS.Destination.SINGLE`` destinations, Reticulum will use a newly
|
||||||
derived ephemeral AES-128 key for every packet.
|
derived ephemeral AES-256 key for every packet.
|
||||||
|
|
||||||
For :ref:`RNS.Link<api-link>` destinations, Reticulum will use per-link
|
For :ref:`RNS.Link<api-link>` destinations, Reticulum will use per-link
|
||||||
ephemeral keys, and offers **Forward Secrecy**.
|
ephemeral keys, and offers **Forward Secrecy**.
|
||||||
|
|
@ -95,7 +103,7 @@ class Packet:
|
||||||
# With an MTU of 500, the maximum of data we can
|
# With an MTU of 500, the maximum of data we can
|
||||||
# send in a single encrypted packet is given by
|
# send in a single encrypted packet is given by
|
||||||
# the below calculation; 383 bytes.
|
# the below calculation; 383 bytes.
|
||||||
ENCRYPTED_MDU = math.floor((RNS.Reticulum.MDU-RNS.Identity.TOKEN_OVERHEAD-RNS.Identity.KEYSIZE//16)/RNS.Identity.AES128_BLOCKSIZE)*RNS.Identity.AES128_BLOCKSIZE - 1
|
ENCRYPTED_MDU = math.floor((RNS.Reticulum.MDU-RNS.Identity.TOKEN_OVERHEAD-RNS.Identity.KEYSIZE//16)/RNS.Identity.AES256_BLOCKSIZE)*RNS.Identity.AES256_BLOCKSIZE - 1
|
||||||
"""
|
"""
|
||||||
The maximum size of the payload data in a single encrypted packet
|
The maximum size of the payload data in a single encrypted packet
|
||||||
"""
|
"""
|
||||||
|
|
@ -106,8 +114,15 @@ class Packet:
|
||||||
|
|
||||||
TIMEOUT_PER_HOP = RNS.Reticulum.DEFAULT_PER_HOP_TIMEOUT
|
TIMEOUT_PER_HOP = RNS.Reticulum.DEFAULT_PER_HOP_TIMEOUT
|
||||||
|
|
||||||
def __init__(self, destination, data, packet_type = DATA, context = NONE, transport_type = RNS.Transport.BROADCAST,
|
__slots__ = "hops", "header", "header_type", "packet_type", "transport_type", "context", "context_flag", "destination"
|
||||||
header_type = HEADER_1, transport_id = None, attached_interface = None, create_receipt = True, context_flag=FLAG_UNSET):
|
__slots__ += "transport_id", "data", "flags", "raw", "packed", "sent", "create_receipt", "receipt", "fromPacked", "MTU"
|
||||||
|
__slots__ += "sent_at", "packet_hash", "ratchet_id", "receiving_interface", "attached_interface", "blocked_interfaces"
|
||||||
|
__slots__ += "rssi", "snr", "q", "ciphertext", "plaintext", "destination_hash", "destination_type", "link", "map_hash"
|
||||||
|
__slots__ += "is_outbound_pr", "traffic_class", "announce_signature_validated"
|
||||||
|
|
||||||
|
def __init__(self, destination, data, packet_type=DATA, context=NONE, transport_type=RNS.Transport.BROADCAST,
|
||||||
|
header_type=HEADER_1, transport_id=None, attached_interface=None, blocked_interfaces=None,
|
||||||
|
create_receipt=True, context_flag=FLAG_UNSET):
|
||||||
|
|
||||||
if destination != None:
|
if destination != None:
|
||||||
if transport_type == None:
|
if transport_type == None:
|
||||||
|
|
@ -146,8 +161,12 @@ class Packet:
|
||||||
self.packet_hash = None
|
self.packet_hash = None
|
||||||
self.ratchet_id = None
|
self.ratchet_id = None
|
||||||
|
|
||||||
|
self.traffic_class = None
|
||||||
|
self.announce_signature_validated = None
|
||||||
self.attached_interface = attached_interface
|
self.attached_interface = attached_interface
|
||||||
|
self.blocked_interfaces = blocked_interfaces
|
||||||
self.receiving_interface = None
|
self.receiving_interface = None
|
||||||
|
self.is_outbound_pr = False
|
||||||
self.rssi = None
|
self.rssi = None
|
||||||
self.snr = None
|
self.snr = None
|
||||||
self.q = None
|
self.q = None
|
||||||
|
|
@ -218,8 +237,13 @@ class Packet:
|
||||||
self.header += bytes([self.context])
|
self.header += bytes([self.context])
|
||||||
self.raw = self.header + self.ciphertext
|
self.raw = self.header + self.ciphertext
|
||||||
|
|
||||||
|
if self.packet_type == Packet.ANNOUNCE and self.header_type == Packet.HEADER_1:
|
||||||
|
transport_id_length = RNS.Reticulum.TRUNCATED_HASHLENGTH//8
|
||||||
|
if len(self.raw)+transport_id_length > self.MTU:
|
||||||
|
raise IOError(f"Transport-inserted announce packet size of {len(self.raw)+transport_id_length} exceeds MTU of {self.MTU} bytes")
|
||||||
|
|
||||||
if len(self.raw) > self.MTU:
|
if len(self.raw) > self.MTU:
|
||||||
raise IOError("Packet size of "+str(len(self.raw))+" exceeds MTU of "+str(self.MTU)+" bytes")
|
raise IOError(f"Packet size of {len(self.raw)} exceeds MTU of {self.MTU} bytes")
|
||||||
|
|
||||||
self.packed = True
|
self.packed = True
|
||||||
self.update_hash()
|
self.update_hash()
|
||||||
|
|
@ -230,6 +254,9 @@ class Packet:
|
||||||
self.flags = self.raw[0]
|
self.flags = self.raw[0]
|
||||||
self.hops = self.raw[1]
|
self.hops = self.raw[1]
|
||||||
|
|
||||||
|
if self.hops >= RNS.Transport.PATHFINDER_M:
|
||||||
|
raise ValueError(f"Invalid hop count {self.hops}")
|
||||||
|
|
||||||
self.header_type = (self.flags & 0b01000000) >> 6
|
self.header_type = (self.flags & 0b01000000) >> 6
|
||||||
self.context_flag = (self.flags & 0b00100000) >> 5
|
self.context_flag = (self.flags & 0b00100000) >> 5
|
||||||
self.transport_type = (self.flags & 0b00010000) >> 4
|
self.transport_type = (self.flags & 0b00010000) >> 4
|
||||||
|
|
@ -243,18 +270,23 @@ class Packet:
|
||||||
self.destination_hash = self.raw[DST_LEN+2:2*DST_LEN+2]
|
self.destination_hash = self.raw[DST_LEN+2:2*DST_LEN+2]
|
||||||
self.context = ord(self.raw[2*DST_LEN+2:2*DST_LEN+3])
|
self.context = ord(self.raw[2*DST_LEN+2:2*DST_LEN+3])
|
||||||
self.data = self.raw[2*DST_LEN+3:]
|
self.data = self.raw[2*DST_LEN+3:]
|
||||||
|
if len(self.transport_id) != DST_LEN: raise ValueError("Malformed Transport ID field")
|
||||||
|
if len(self.destination_hash) != DST_LEN: raise ValueError("Malformed destination hash field")
|
||||||
else:
|
else:
|
||||||
self.transport_id = None
|
self.transport_id = None
|
||||||
self.destination_hash = self.raw[2:DST_LEN+2]
|
self.destination_hash = self.raw[2:DST_LEN+2]
|
||||||
self.context = ord(self.raw[DST_LEN+2:DST_LEN+3])
|
self.context = ord(self.raw[DST_LEN+2:DST_LEN+3])
|
||||||
self.data = self.raw[DST_LEN+3:]
|
self.data = self.raw[DST_LEN+3:]
|
||||||
|
if len(self.destination_hash) != DST_LEN: raise ValueError("Malformed destination hash field")
|
||||||
|
|
||||||
|
if len(self.data) == 0: raise ValueError("Zero-length data field")
|
||||||
|
|
||||||
self.packed = False
|
self.packed = False
|
||||||
self.update_hash()
|
self.update_hash()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Received malformed packet, dropping it. The contained exception was: "+str(e), RNS.LOG_EXTREME)
|
RNS.log(f"Dropping malformed packet. The contained exception was: {e}", RNS.LOG_EXTREME) if RNS.sl(RNS.LOG_EXTREME) else None
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def send(self):
|
def send(self):
|
||||||
|
|
@ -264,21 +296,26 @@ class Packet:
|
||||||
:returns: A :ref:`RNS.PacketReceipt<api-packetreceipt>` instance if *create_receipt* was set to *True* when the packet was instantiated, if not returns *None*. If the packet could not be sent *False* is returned.
|
:returns: A :ref:`RNS.PacketReceipt<api-packetreceipt>` instance if *create_receipt* was set to *True* when the packet was instantiated, if not returns *None*. If the packet could not be sent *False* is returned.
|
||||||
"""
|
"""
|
||||||
if not self.sent:
|
if not self.sent:
|
||||||
|
if self.hops >= RNS.Transport.PATHFINDER_M: return False
|
||||||
|
if not self.packed: self.pack()
|
||||||
if self.destination.type == RNS.Destination.LINK:
|
if self.destination.type == RNS.Destination.LINK:
|
||||||
if self.destination.status == RNS.Link.CLOSED:
|
if self.destination.status == RNS.Link.CLOSED:
|
||||||
raise IOError("Attempt to transmit over a closed link")
|
RNS.log("Attempt to transmit over a closed link, dropping packet", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
self.sent = False
|
||||||
|
self.receipt = None
|
||||||
|
return False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.destination.last_outbound = time.time()
|
self.destination.last_outbound = time.time()
|
||||||
self.destination.tx += 1
|
self.destination.tx += 1
|
||||||
self.destination.txbytes += len(self.data)
|
self.destination.txbytes += len(self.ciphertext)
|
||||||
|
RNS.Transport.link_txb += len(self.raw)
|
||||||
|
|
||||||
if not self.packed:
|
if RNS.Transport.outbound(self): return self.receipt
|
||||||
self.pack()
|
|
||||||
|
|
||||||
if RNS.Transport.outbound(self):
|
|
||||||
return self.receipt
|
|
||||||
else:
|
else:
|
||||||
RNS.log("No interfaces could process the outbound packet", RNS.LOG_ERROR)
|
if not self.attached_interface:
|
||||||
|
RNS.log(f"No interfaces could process the outbound {self.hops} hop, type {self.packet_type} packet for {self.destination}", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None
|
||||||
|
|
||||||
self.sent = False
|
self.sent = False
|
||||||
self.receipt = None
|
self.receipt = None
|
||||||
return False
|
return False
|
||||||
|
|
@ -297,51 +334,44 @@ class Packet:
|
||||||
# encrypted destinations
|
# encrypted destinations
|
||||||
self.pack()
|
self.pack()
|
||||||
|
|
||||||
if RNS.Transport.outbound(self):
|
if RNS.Transport.outbound(self): return self.receipt
|
||||||
return self.receipt
|
|
||||||
else:
|
else:
|
||||||
RNS.log("No interfaces could process the outbound packet", RNS.LOG_ERROR)
|
RNS.log("Re-send failed. No interfaces could process the outbound packet", RNS.LOG_WARNING)
|
||||||
self.sent = False
|
self.sent = False
|
||||||
self.receipt = None
|
self.receipt = None
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
raise IOError("Packet was not sent yet")
|
else: raise IOError("Packet was not sent yet")
|
||||||
|
|
||||||
def prove(self, destination=None):
|
def prove(self, destination=None):
|
||||||
if self.fromPacked and hasattr(self, "destination") and self.destination:
|
if self.fromPacked and hasattr(self, "destination") and self.destination:
|
||||||
if self.destination.identity and self.destination.identity.prv:
|
if self.destination.identity and self.destination.identity.prv:
|
||||||
self.destination.identity.prove(self, destination)
|
self.destination.identity.prove(self, destination)
|
||||||
elif self.fromPacked and hasattr(self, "link") and self.link:
|
elif self.fromPacked and hasattr(self, "link") and self.link: self.link.prove_packet(self)
|
||||||
self.link.prove_packet(self)
|
else: RNS.log("Could not prove packet associated with neither a destination nor a link", RNS.LOG_ERROR)
|
||||||
else:
|
|
||||||
RNS.log("Could not prove packet associated with neither a destination nor a link", RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
# Generates a special destination that allows Reticulum
|
# Generates a special destination that allows Reticulum
|
||||||
# to direct the proof back to the proved packet's sender
|
# to direct the proof back to the proved packet's sender
|
||||||
def generate_proof_destination(self):
|
def generate_proof_destination(self): return ProofDestination(self)
|
||||||
return ProofDestination(self)
|
|
||||||
|
|
||||||
def validate_proof_packet(self, proof_packet):
|
def validate_proof_packet(self, proof_packet): return self.receipt.validate_proof_packet(proof_packet)
|
||||||
return self.receipt.validate_proof_packet(proof_packet)
|
|
||||||
|
|
||||||
def validate_proof(self, proof):
|
def validate_proof(self, proof): return self.receipt.validate_proof(proof)
|
||||||
return self.receipt.validate_proof(proof)
|
|
||||||
|
|
||||||
def update_hash(self):
|
def update_hash(self): self.packet_hash = self.get_hash()
|
||||||
self.packet_hash = self.get_hash()
|
|
||||||
|
|
||||||
def get_hash(self):
|
def get_hash(self): return RNS.Identity.full_hash(self.get_hashable_part())
|
||||||
return RNS.Identity.full_hash(self.get_hashable_part())
|
|
||||||
|
|
||||||
def getTruncatedHash(self):
|
def getTruncatedHash(self): return RNS.Identity.truncated_hash(self.get_hashable_part())
|
||||||
return RNS.Identity.truncated_hash(self.get_hashable_part())
|
|
||||||
|
@property
|
||||||
|
def truncated_packet_hash(self):
|
||||||
|
return self.packet_hash[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8]
|
||||||
|
|
||||||
def get_hashable_part(self):
|
def get_hashable_part(self):
|
||||||
hashable_part = bytes([self.raw[0] & 0b00001111])
|
hashable_part = bytes([self.raw[0] & 0b00001111])
|
||||||
if self.header_type == Packet.HEADER_2:
|
if self.header_type == Packet.HEADER_2: hashable_part += self.raw[(RNS.Identity.TRUNCATED_HASHLENGTH//8)+2:]
|
||||||
hashable_part += self.raw[(RNS.Identity.TRUNCATED_HASHLENGTH//8)+2:]
|
else: hashable_part += self.raw[2:]
|
||||||
else:
|
|
||||||
hashable_part += self.raw[2:]
|
|
||||||
|
|
||||||
return hashable_part
|
return hashable_part
|
||||||
|
|
||||||
|
|
@ -349,36 +379,29 @@ class Packet:
|
||||||
"""
|
"""
|
||||||
:returns: The physical layer *Received Signal Strength Indication* if available, otherwise ``None``.
|
:returns: The physical layer *Received Signal Strength Indication* if available, otherwise ``None``.
|
||||||
"""
|
"""
|
||||||
if self.rssi != None:
|
if self.rssi != None: return self.rssi
|
||||||
return self.rssi
|
else: return RNS.Reticulum.get_instance().get_packet_rssi(self.packet_hash)
|
||||||
else:
|
|
||||||
return reticulum.get_packet_rssi(self.packet_hash)
|
|
||||||
|
|
||||||
def get_snr(self):
|
def get_snr(self):
|
||||||
"""
|
"""
|
||||||
:returns: The physical layer *Signal-to-Noise Ratio* if available, otherwise ``None``.
|
:returns: The physical layer *Signal-to-Noise Ratio* if available, otherwise ``None``.
|
||||||
"""
|
"""
|
||||||
if self.snr != None:
|
if self.snr != None: return self.snr
|
||||||
return self.snr
|
else: return RNS.Reticulum.get_instance().get_packet_snr(self.packet_hash)
|
||||||
else:
|
|
||||||
return reticulum.get_packet_snr(self.packet_hash)
|
|
||||||
|
|
||||||
def get_q(self):
|
def get_q(self):
|
||||||
"""
|
"""
|
||||||
:returns: The physical layer *Link Quality* if available, otherwise ``None``.
|
:returns: The physical layer *Link Quality* if available, otherwise ``None``.
|
||||||
"""
|
"""
|
||||||
if self.q != None:
|
if self.q != None: return self.q
|
||||||
return self.q
|
else: return RNS.Reticulum.get_instance().get_packet_q(self.packet_hash)
|
||||||
else:
|
|
||||||
return reticulum.get_packet_q(self.packet_hash)
|
|
||||||
|
|
||||||
class ProofDestination:
|
class ProofDestination:
|
||||||
def __init__(self, packet):
|
def __init__(self, packet):
|
||||||
self.hash = packet.get_hash()[:RNS.Reticulum.TRUNCATED_HASHLENGTH//8];
|
self.hash = packet.truncated_packet_hash;
|
||||||
self.type = RNS.Destination.SINGLE
|
self.type = RNS.Destination.SINGLE
|
||||||
|
|
||||||
def encrypt(self, plaintext):
|
def encrypt(self, plaintext): return plaintext
|
||||||
return plaintext
|
|
||||||
|
|
||||||
|
|
||||||
class PacketReceipt:
|
class PacketReceipt:
|
||||||
|
|
@ -400,8 +423,8 @@ class PacketReceipt:
|
||||||
|
|
||||||
# Creates a new packet receipt from a sent packet
|
# Creates a new packet receipt from a sent packet
|
||||||
def __init__(self, packet):
|
def __init__(self, packet):
|
||||||
self.hash = packet.get_hash()
|
self.hash = packet.packet_hash
|
||||||
self.truncated_hash = packet.getTruncatedHash()
|
self.truncated_hash = packet.truncated_packet_hash
|
||||||
self.sent = True
|
self.sent = True
|
||||||
self.sent_at = time.time()
|
self.sent_at = time.time()
|
||||||
self.proved = False
|
self.proved = False
|
||||||
|
|
@ -447,18 +470,17 @@ class PacketReceipt:
|
||||||
link.last_proof = self.concluded_at
|
link.last_proof = self.concluded_at
|
||||||
|
|
||||||
if self.callbacks.delivery != None:
|
if self.callbacks.delivery != None:
|
||||||
try:
|
try: self.callbacks.delivery(self)
|
||||||
self.callbacks.delivery(self)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("An error occurred while evaluating external delivery callback for "+str(link), RNS.LOG_ERROR)
|
RNS.log("An error occurred while evaluating external delivery callback for "+str(link), RNS.LOG_ERROR)
|
||||||
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
RNS.trace_exception(e)
|
RNS.trace_exception(e)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
else: return False
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
elif len(proof) == PacketReceipt.IMPL_LENGTH:
|
elif len(proof) == PacketReceipt.IMPL_LENGTH:
|
||||||
pass
|
pass
|
||||||
# TODO: Why is this disabled?
|
# TODO: Why is this disabled?
|
||||||
|
|
@ -475,8 +497,7 @@ class PacketReceipt:
|
||||||
# else:
|
# else:
|
||||||
# RNS.log("invalid")
|
# RNS.log("invalid")
|
||||||
# return False
|
# return False
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
|
|
||||||
# Validate a raw proof
|
# Validate a raw proof
|
||||||
def validate_proof(self, proof, proof_packet=None):
|
def validate_proof(self, proof, proof_packet=None):
|
||||||
|
|
@ -493,24 +514,20 @@ class PacketReceipt:
|
||||||
self.proof_packet = proof_packet
|
self.proof_packet = proof_packet
|
||||||
|
|
||||||
if self.callbacks.delivery != None:
|
if self.callbacks.delivery != None:
|
||||||
try:
|
try: self.callbacks.delivery(self)
|
||||||
self.callbacks.delivery(self)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
else: return False
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
elif len(proof) == PacketReceipt.IMPL_LENGTH:
|
elif len(proof) == PacketReceipt.IMPL_LENGTH:
|
||||||
# This is an implicit proof
|
# This is an implicit proof
|
||||||
|
|
||||||
if not hasattr(self.destination, "identity"):
|
if not hasattr(self.destination, "identity"): return False
|
||||||
return False
|
if self.destination.identity == None: return False
|
||||||
|
|
||||||
if self.destination.identity == None:
|
|
||||||
return False
|
|
||||||
|
|
||||||
signature = proof[:RNS.Identity.SIGLENGTH//8]
|
signature = proof[:RNS.Identity.SIGLENGTH//8]
|
||||||
proof_valid = self.destination.identity.validate(signature, self.hash)
|
proof_valid = self.destination.identity.validate(signature, self.hash)
|
||||||
|
|
@ -521,16 +538,13 @@ class PacketReceipt:
|
||||||
self.proof_packet = proof_packet
|
self.proof_packet = proof_packet
|
||||||
|
|
||||||
if self.callbacks.delivery != None:
|
if self.callbacks.delivery != None:
|
||||||
try:
|
try: self.callbacks.delivery(self)
|
||||||
self.callbacks.delivery(self)
|
except Exception as e: RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||||
except Exception as e:
|
|
||||||
RNS.log("Error while executing proof validated callback. The contained exception was: "+str(e), RNS.LOG_ERROR)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
else: return False
|
||||||
else:
|
else: return False
|
||||||
return False
|
|
||||||
|
|
||||||
def get_rtt(self):
|
def get_rtt(self):
|
||||||
"""
|
"""
|
||||||
|
|
@ -543,10 +557,8 @@ class PacketReceipt:
|
||||||
|
|
||||||
def check_timeout(self):
|
def check_timeout(self):
|
||||||
if self.status == PacketReceipt.SENT and self.is_timed_out():
|
if self.status == PacketReceipt.SENT and self.is_timed_out():
|
||||||
if self.timeout == -1:
|
if self.timeout == -1: self.status = PacketReceipt.CULLED
|
||||||
self.status = PacketReceipt.CULLED
|
else: self.status = PacketReceipt.FAILED
|
||||||
else:
|
|
||||||
self.status = PacketReceipt.FAILED
|
|
||||||
|
|
||||||
self.concluded_at = time.time()
|
self.concluded_at = time.time()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2023 Mark Qvist / unsigned.io and contributors.
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -23,5 +31,4 @@
|
||||||
class Resolver:
|
class Resolver:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def resolve_identity(full_name):
|
def resolve_identity(full_name): pass
|
||||||
pass
|
|
||||||
683
RNS/Resource.py
683
RNS/Resource.py
File diff suppressed because it is too large
Load diff
2526
RNS/Reticulum.py
2526
RNS/Reticulum.py
File diff suppressed because it is too large
Load diff
1093
RNS/Stamps.py
Normal file
1093
RNS/Stamps.py
Normal file
File diff suppressed because it is too large
Load diff
851
RNS/Storage.py
Normal file
851
RNS/Storage.py
Normal file
|
|
@ -0,0 +1,851 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
import hashlib
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from array import array
|
||||||
|
|
||||||
|
class AnnounceRateTable:
|
||||||
|
# Columnar store for announce rate tracking. Layout:
|
||||||
|
# _index : dict[destination_hash -> slot]
|
||||||
|
# _keys : list[slot] -> destination_hash (for swap-remove bookkeeping)
|
||||||
|
# _last : array('d') last non-violating announce time
|
||||||
|
# _blocked: array('d') blocked_until
|
||||||
|
# _viol : array('i') rate_violations
|
||||||
|
# _ts : array('d') flat ring of max_timestamps doubles per slot
|
||||||
|
# _ts_len : array('B') valid timestamp count per slot
|
||||||
|
# _ts_head: array('B') ring write cursor per slot
|
||||||
|
|
||||||
|
def __init__(self, max_timestamps=16):
|
||||||
|
assert max_timestamps <= 255, "Timestamp ring size must fit in a single byte"
|
||||||
|
self.max_ts = max_timestamps
|
||||||
|
self._index = {}
|
||||||
|
self._keys = []
|
||||||
|
self._last = array('d')
|
||||||
|
self._blocked = array('d')
|
||||||
|
self._viol = array('i')
|
||||||
|
self._ts = array('d')
|
||||||
|
self._ts_len = array('B')
|
||||||
|
self._ts_head = array('B')
|
||||||
|
self._cursor = 0
|
||||||
|
|
||||||
|
def __len__(self): return len(self._keys)
|
||||||
|
def __contains__(self, destination_hash): return destination_hash in self._index
|
||||||
|
|
||||||
|
# Perform the announce-rate check-and-update for one announce.
|
||||||
|
# Caller must hold the announce_rate_table_lock.
|
||||||
|
def check_and_update(self, destination_hash, now, target, grace, penalty):
|
||||||
|
slot = self._index.get(destination_hash)
|
||||||
|
|
||||||
|
if slot is None:
|
||||||
|
slot = len(self._keys)
|
||||||
|
self._index[destination_hash] = slot
|
||||||
|
self._keys.append(destination_hash)
|
||||||
|
self._last.append(now)
|
||||||
|
self._blocked.append(0.0)
|
||||||
|
self._viol.append(0)
|
||||||
|
self._ts.extend([0.0]*self.max_ts)
|
||||||
|
self._ts[slot*self.max_ts] = now
|
||||||
|
self._ts_len.append(1)
|
||||||
|
self._ts_head.append(1 % self.max_ts)
|
||||||
|
return False
|
||||||
|
|
||||||
|
base = slot*self.max_ts
|
||||||
|
head = self._ts_head[slot]
|
||||||
|
self._ts[base+head] = now
|
||||||
|
self._ts_head[slot] = (head+1) % self.max_ts
|
||||||
|
if self._ts_len[slot] < self.max_ts: self._ts_len[slot] += 1
|
||||||
|
|
||||||
|
current_rate = now - self._last[slot]
|
||||||
|
rate_blocked = False
|
||||||
|
|
||||||
|
if now > self._blocked[slot]:
|
||||||
|
if current_rate < target: self._viol[slot] += 1
|
||||||
|
else:
|
||||||
|
v = self._viol[slot] - 1
|
||||||
|
self._viol[slot] = v if v > 0 else 0
|
||||||
|
|
||||||
|
if self._viol[slot] > grace:
|
||||||
|
self._blocked[slot] = self._last[slot] + target + penalty
|
||||||
|
rate_blocked = True
|
||||||
|
else: self._last[slot] = now
|
||||||
|
|
||||||
|
else: rate_blocked = True
|
||||||
|
|
||||||
|
return rate_blocked
|
||||||
|
|
||||||
|
# Caller must hold the table lock
|
||||||
|
def cull(self, now, ttl, max_scan=1024, max_cull=None):
|
||||||
|
if max_cull is None: max_cull = max_scan
|
||||||
|
horizon = now - ttl
|
||||||
|
culled = 0
|
||||||
|
scanned = 0
|
||||||
|
n = len(self._keys)
|
||||||
|
|
||||||
|
while scanned < max_scan and culled < max_cull and n > 0:
|
||||||
|
if self._cursor >= n: self._cursor = 0
|
||||||
|
slot = self._cursor
|
||||||
|
|
||||||
|
if self._last[slot] < horizon and self._blocked[slot] <= now:
|
||||||
|
last_i = n - 1
|
||||||
|
stale_key = self._keys[slot]
|
||||||
|
|
||||||
|
if slot != last_i:
|
||||||
|
moved_key = self._keys[last_i]
|
||||||
|
self._keys[slot] = moved_key
|
||||||
|
self._index[moved_key] = slot
|
||||||
|
self._last[slot] = self._last[last_i]
|
||||||
|
self._blocked[slot] = self._blocked[last_i]
|
||||||
|
self._viol[slot] = self._viol[last_i]
|
||||||
|
self._ts_len[slot] = self._ts_len[last_i]
|
||||||
|
self._ts_head[slot] = self._ts_head[last_i]
|
||||||
|
|
||||||
|
f = last_i*self.max_ts; t = slot*self.max_ts
|
||||||
|
self._ts[t:t+self.max_ts] = self._ts[f:f+self.max_ts]
|
||||||
|
|
||||||
|
self._keys.pop()
|
||||||
|
self._last.pop(); self._blocked.pop(); self._viol.pop()
|
||||||
|
self._ts_len.pop(); self._ts_head.pop()
|
||||||
|
del self._ts[len(self._ts)-self.max_ts:] # drop tail block of removed slot
|
||||||
|
del self._index[stale_key]
|
||||||
|
|
||||||
|
n -= 1
|
||||||
|
culled += 1
|
||||||
|
# Do not advance cursor, re-examine the slot a moved entry landed in
|
||||||
|
|
||||||
|
else:
|
||||||
|
self._cursor = slot + 1
|
||||||
|
scanned += 1
|
||||||
|
|
||||||
|
if self._cursor >= n: self._cursor = 0
|
||||||
|
|
||||||
|
return culled
|
||||||
|
|
||||||
|
# Hold table lock, or accept a possibly inconsistent snapshot
|
||||||
|
def items(self):
|
||||||
|
for slot, key in enumerate(self._keys):
|
||||||
|
n = self._ts_len[slot]
|
||||||
|
head = self._ts_head[slot]
|
||||||
|
base = slot*self.max_ts
|
||||||
|
if n < self.max_ts: ts = list(self._ts[base:base+n])
|
||||||
|
else: ts = list(self._ts[base+head:base+self.max_ts]) + list(self._ts[base:base+head])
|
||||||
|
yield key, { "last": self._last[slot], "rate_violations": self._viol[slot],
|
||||||
|
"blocked_until": self._blocked[slot], "timestamps": ts }
|
||||||
|
|
||||||
|
# Persists the announce rate table to a file, in
|
||||||
|
# compacted, little-endian binary format:
|
||||||
|
#
|
||||||
|
# Header (16 bytes)
|
||||||
|
# 0x00 6 Magic bytes
|
||||||
|
# 0x06 1 Format version
|
||||||
|
# 0x07 1 Flags, reserved, must be zero
|
||||||
|
# 0x08 1 Timestamp ring size (max_ts)
|
||||||
|
# 0x09 1 Destination hash length in bytes (keylen)
|
||||||
|
# 0x0A 2 Reserved, zero
|
||||||
|
# 0x0C 4 Entry count, uint32, little-endian
|
||||||
|
#
|
||||||
|
# Entries (columnar, in native slot order)
|
||||||
|
# Destination hash keys, followed by the _last, _blocked,
|
||||||
|
# _viol, _ts_len, _ts_head and _ts columns, all encoded as
|
||||||
|
# little-endian fixed-width values.
|
||||||
|
#
|
||||||
|
# Caller must hold the announce rate table lock
|
||||||
|
|
||||||
|
PERSIST_MAGIC = b"RNSART"
|
||||||
|
PERSIST_VERSION = 0x01
|
||||||
|
def persist(self, path, retain=None):
|
||||||
|
try:
|
||||||
|
count = len(self._keys)
|
||||||
|
|
||||||
|
if count > 0:
|
||||||
|
keylen = len(self._keys[0])
|
||||||
|
for key in self._keys:
|
||||||
|
if len(key) != keylen: raise ValueError("Inconsistent destination hash lengths in table")
|
||||||
|
else:
|
||||||
|
keylen = RNS.Identity.TRUNCATED_HASHLENGTH//8
|
||||||
|
|
||||||
|
if retain != None and count > 0:
|
||||||
|
now = time.time()
|
||||||
|
horizon = now - retain
|
||||||
|
keep = [slot for slot in range(count) if not (self._last[slot] < horizon and self._blocked[slot] <= now)]
|
||||||
|
else:
|
||||||
|
keep = None
|
||||||
|
|
||||||
|
if keep != None and len(keep) != count:
|
||||||
|
count = len(keep)
|
||||||
|
keys = [self._keys[slot] for slot in keep]
|
||||||
|
last = array("d", [self._last[slot] for slot in keep])
|
||||||
|
blocked = array("d", [self._blocked[slot] for slot in keep])
|
||||||
|
viol = array("i", [self._viol[slot] for slot in keep])
|
||||||
|
ts_len = array("B", [self._ts_len[slot] for slot in keep])
|
||||||
|
ts_head = array("B", [self._ts_head[slot] for slot in keep])
|
||||||
|
ts = array("d")
|
||||||
|
for slot in keep: ts.extend(self._ts[slot*self.max_ts:(slot+1)*self.max_ts])
|
||||||
|
else:
|
||||||
|
keys = self._keys
|
||||||
|
last = self._last
|
||||||
|
blocked = self._blocked
|
||||||
|
viol = self._viol
|
||||||
|
ts_len = self._ts_len
|
||||||
|
ts_head = self._ts_head
|
||||||
|
ts = self._ts
|
||||||
|
|
||||||
|
if last.itemsize != 8 or viol.itemsize != 4:
|
||||||
|
raise ValueError(f"Unsupported array itemsize at persist: d={last.itemsize}, i={viol.itemsize}")
|
||||||
|
|
||||||
|
header = self.PERSIST_MAGIC
|
||||||
|
header += bytes([self.PERSIST_VERSION, 0x00, self.max_ts & 0xFF, keylen & 0xFF])
|
||||||
|
header += struct.pack("<H", 0)
|
||||||
|
header += struct.pack("<I", count)
|
||||||
|
|
||||||
|
blob = header
|
||||||
|
blob += b"".join(keys)
|
||||||
|
blob += self._column_le(last)
|
||||||
|
blob += self._column_le(blocked)
|
||||||
|
blob += self._column_le(viol)
|
||||||
|
blob += ts_len.tobytes()
|
||||||
|
blob += ts_head.tobytes()
|
||||||
|
blob += self._column_le(ts)
|
||||||
|
|
||||||
|
tmp_path = path+".tmp"
|
||||||
|
with open(tmp_path, "wb") as file: file.write(blob)
|
||||||
|
os.replace(tmp_path, path)
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not persist announce rate table to {path}: {e}", RNS.LOG_ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Caller must hold the announce rate table lock
|
||||||
|
def load(self, path):
|
||||||
|
try:
|
||||||
|
with open(path, "rb") as file: blob = file.read()
|
||||||
|
except Exception: return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
if len(blob) < 16: return False
|
||||||
|
if blob[0:6] != self.PERSIST_MAGIC: return False
|
||||||
|
if blob[6] != self.PERSIST_VERSION: return False
|
||||||
|
if blob[7] != 0x00: return False
|
||||||
|
|
||||||
|
file_max_ts = blob[8]
|
||||||
|
keylen = blob[9]
|
||||||
|
count = struct.unpack_from("<I", blob, 12)[0]
|
||||||
|
|
||||||
|
if file_max_ts < 1 or file_max_ts > 255: return False
|
||||||
|
if keylen < 1: return False
|
||||||
|
|
||||||
|
entry_size = keylen+22+file_max_ts*8
|
||||||
|
if len(blob) != 16+count*entry_size: return False
|
||||||
|
|
||||||
|
if file_max_ts == self.max_ts:
|
||||||
|
# Ring size matches, load directly
|
||||||
|
keys = [blob[16+slot*keylen : 16+(slot+1)*keylen] for slot in range(count)]
|
||||||
|
index = {}
|
||||||
|
for slot in range(count):
|
||||||
|
if keys[slot] in index: return False
|
||||||
|
index[keys[slot]] = slot
|
||||||
|
|
||||||
|
offset = 16+count*keylen
|
||||||
|
last = self._column_from_le("d", blob[offset:offset+count*8]); offset += count*8
|
||||||
|
blocked = self._column_from_le("d", blob[offset:offset+count*8]); offset += count*8
|
||||||
|
viol = self._column_from_le("i", blob[offset:offset+count*4]); offset += count*4
|
||||||
|
ts_len = array("B", blob[offset:offset+count]); offset += count
|
||||||
|
ts_head = array("B", blob[offset:offset+count]); offset += count
|
||||||
|
ts = self._column_from_le("d", blob[offset:offset+count*self.max_ts*8]); offset += count*self.max_ts*8
|
||||||
|
|
||||||
|
for slot in range(count):
|
||||||
|
if ts_len[slot] > self.max_ts or ts_head[slot] >= self.max_ts: return False
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Different ring sizes from current running config,
|
||||||
|
# rebuild entries best effort.
|
||||||
|
keys = [blob[16+slot*keylen : 16+(slot+1)*keylen] for slot in range(count)]
|
||||||
|
index = {}
|
||||||
|
for slot in range(count):
|
||||||
|
if keys[slot] in index: return False
|
||||||
|
index[keys[slot]] = slot
|
||||||
|
|
||||||
|
offset = 16+count*keylen
|
||||||
|
last = self._column_from_le("d", blob[offset:offset+count*8]); offset += count*8
|
||||||
|
blocked = self._column_from_le("d", blob[offset:offset+count*8]); offset += count*8
|
||||||
|
viol = self._column_from_le("i", blob[offset:offset+count*4]); offset += count*4
|
||||||
|
f_ts_len = array("B", blob[offset:offset+count]); offset += count
|
||||||
|
f_ts_head = array("B", blob[offset:offset+count]); offset += count
|
||||||
|
f_ts = self._column_from_le("d", blob[offset:offset+count*file_max_ts*8]); offset += count*file_max_ts*8
|
||||||
|
|
||||||
|
for slot in range(count):
|
||||||
|
if f_ts_len[slot] > file_max_ts or f_ts_head[slot] >= file_max_ts: return False
|
||||||
|
|
||||||
|
ts_len = array("B"); ts_head = array("B"); ts = array("d")
|
||||||
|
for slot in range(count):
|
||||||
|
base = slot*file_max_ts
|
||||||
|
if f_ts_len[slot] < file_max_ts: chronological = list(f_ts[base:base+f_ts_len[slot]])
|
||||||
|
else: chronological = list(f_ts[base+f_ts_head[slot]:base+file_max_ts])+list(f_ts[base:base+f_ts_head[slot]])
|
||||||
|
chronological = chronological[-self.max_ts:]
|
||||||
|
ts_count = len(chronological)
|
||||||
|
|
||||||
|
ts_len.append(ts_count)
|
||||||
|
ts.extend(chronological)
|
||||||
|
if ts_count < self.max_ts: ts.extend([0.0]*(self.max_ts-ts_count))
|
||||||
|
ts_head.append(ts_count % self.max_ts)
|
||||||
|
|
||||||
|
self._index = index
|
||||||
|
self._keys = keys
|
||||||
|
self._last = last
|
||||||
|
self._blocked = blocked
|
||||||
|
self._viol = viol
|
||||||
|
self._ts = ts
|
||||||
|
self._ts_len = ts_len
|
||||||
|
self._ts_head = ts_head
|
||||||
|
self._cursor = 0
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not load announce rate table from {path}: {e}", RNS.LOG_ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _column_le(self, column):
|
||||||
|
if sys.byteorder == "big": swapped = array(column.typecode, column); swapped.byteswap(); return swapped.tobytes()
|
||||||
|
else: return column.tobytes()
|
||||||
|
|
||||||
|
def _column_from_le(self, typecode, data):
|
||||||
|
column = array(typecode); column.frombytes(data)
|
||||||
|
if sys.byteorder == "big": column.byteswap()
|
||||||
|
return column
|
||||||
|
|
||||||
|
class PacketHashlist:
|
||||||
|
# Bucketed byte-blob packet filter hashlist, with the primary
|
||||||
|
# design goal of memory coherence and density. All operations
|
||||||
|
# are safe to call without holding a lock.
|
||||||
|
|
||||||
|
def __init__(self, hash_width=32, bucket_count=16384):
|
||||||
|
assert bucket_count > 0 and bucket_count & (bucket_count-1) == 0, "Bucket count must be a power of two"
|
||||||
|
self.width = hash_width
|
||||||
|
self._nb = bucket_count
|
||||||
|
self._mask = bucket_count - 1
|
||||||
|
self._cur = [bytearray() for _ in range(bucket_count)]
|
||||||
|
self._prv = [bytearray() for _ in range(bucket_count)]
|
||||||
|
self._count = 0
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return self._count
|
||||||
|
|
||||||
|
def __contains__(self, packet_hash):
|
||||||
|
bucket = (packet_hash[0] | (packet_hash[1] << 8)) & self._mask
|
||||||
|
return packet_hash in self._cur[bucket] or packet_hash in self._prv[bucket]
|
||||||
|
|
||||||
|
def add(self, packet_hash):
|
||||||
|
assert len(packet_hash) == self.width, "Packet hash width mismatch"
|
||||||
|
bucket = (packet_hash[0] | (packet_hash[1] << 8)) & self._mask
|
||||||
|
if packet_hash not in self._cur[bucket]:
|
||||||
|
self._cur[bucket].extend(packet_hash)
|
||||||
|
self._count += 1
|
||||||
|
|
||||||
|
def discard(self, packet_hash):
|
||||||
|
w = self.width
|
||||||
|
bucket = (packet_hash[0] | (packet_hash[1] << 8)) & self._mask
|
||||||
|
for generation in (self._cur, self._prv):
|
||||||
|
entries = generation[bucket]
|
||||||
|
pos = entries.find(packet_hash)
|
||||||
|
while pos != -1:
|
||||||
|
if pos % w == 0:
|
||||||
|
del entries[pos:pos+w]
|
||||||
|
if generation is self._cur: self._count -= 1
|
||||||
|
pos = entries.find(packet_hash, pos)
|
||||||
|
else:
|
||||||
|
pos = entries.find(packet_hash, pos+1)
|
||||||
|
|
||||||
|
def swap(self):
|
||||||
|
self._prv = self._cur
|
||||||
|
self._cur = [bytearray() for _ in range(self._nb)]
|
||||||
|
self._count = 0
|
||||||
|
|
||||||
|
# Current-generation buckets for persist operations
|
||||||
|
def buckets(self): return tuple(self._cur)
|
||||||
|
|
||||||
|
class KnownDestinationsStore:
|
||||||
|
# Write-behind SQLite storage backend for known destinations.
|
||||||
|
# The actual in-memory table is used directly for all transport
|
||||||
|
# operations; this serves purely as the store, and no read operations
|
||||||
|
# utilize the database directly.
|
||||||
|
|
||||||
|
def __init__(self, db_path, hash_width=16, read_only=False):
|
||||||
|
import sqlite3
|
||||||
|
self.sqlite3 = sqlite3
|
||||||
|
self.db_path = db_path
|
||||||
|
self.hash_width = hash_width
|
||||||
|
self.read_only = read_only
|
||||||
|
self._dirty_upsert = set()
|
||||||
|
self._dirty_delete = set()
|
||||||
|
self._ratchet_upserts = {}
|
||||||
|
self._ratchet_deletes = set()
|
||||||
|
self._kd_quarantine = {}
|
||||||
|
self._ratchet_quarantine = {}
|
||||||
|
self._flush_lock = threading.Lock()
|
||||||
|
|
||||||
|
if read_only: self._conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, check_same_thread=False, isolation_level=None, cached_statements=0)
|
||||||
|
else:
|
||||||
|
self._conn = sqlite3.connect(db_path, check_same_thread=False, isolation_level=None, cached_statements=0)
|
||||||
|
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||||
|
self._conn.execute("PRAGMA synchronous=NORMAL")
|
||||||
|
self._conn.execute("PRAGMA journal_size_limit=8388608")
|
||||||
|
self.__create_schema()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self._conn != None:
|
||||||
|
self._conn.close()
|
||||||
|
self._conn = None
|
||||||
|
|
||||||
|
def __create_schema(self):
|
||||||
|
db_version = self._conn.execute("PRAGMA user_version").fetchone()[0]
|
||||||
|
|
||||||
|
# Pre-release database cleanup
|
||||||
|
# TODO: Remove this in 1.6.1
|
||||||
|
if db_version > 0:
|
||||||
|
RNS.log(f"Pre-release database detected, rebuilding known destinations DB to release format", RNS.LOG_WARNING)
|
||||||
|
self._conn.execute("DROP TABLE IF EXISTS known_destinations")
|
||||||
|
|
||||||
|
self._conn.execute("""CREATE TABLE IF NOT EXISTS known_destinations (
|
||||||
|
destination_hash BLOB PRIMARY KEY,
|
||||||
|
last_heard REAL NOT NULL,
|
||||||
|
packet_hash BLOB,
|
||||||
|
public_key BLOB NOT NULL,
|
||||||
|
app_data BLOB,
|
||||||
|
used REAL NOT NULL DEFAULT 0,
|
||||||
|
stamp BLOB,
|
||||||
|
stamp_value REAL,
|
||||||
|
lr_cost REAL,
|
||||||
|
identity_hash BLOB NOT NULL)""")
|
||||||
|
|
||||||
|
self._conn.execute("""CREATE TABLE IF NOT EXISTS ratchets (
|
||||||
|
destination_hash BLOB PRIMARY KEY,
|
||||||
|
ratchet BLOB NOT NULL,
|
||||||
|
received REAL NOT NULL)""")
|
||||||
|
|
||||||
|
# Schema migrations
|
||||||
|
try:
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
# No migrations yet
|
||||||
|
self._conn.execute("PRAGMA user_version = 0")
|
||||||
|
self._conn.execute("COMMIT")
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Known destinations database version migration failed: {e}", RNS.LOG_CRITICAL)
|
||||||
|
RNS.log(f"You will need to remove the known_destinations.db* files from your storage directory so it can be rebuilt", RNS.LOG_CRITICAL)
|
||||||
|
|
||||||
|
def __entry_row(self, destination_hash, entry):
|
||||||
|
stamp = entry[5] if len(entry) > 5 else None
|
||||||
|
stamp_value = entry[6] if len(entry) > 6 else 0
|
||||||
|
lr_cost = entry[7] if len(entry) > 7 else None
|
||||||
|
identity_hash = hashlib.sha256(entry[2]).digest()[:self.hash_width]
|
||||||
|
return (destination_hash, entry[0], entry[1], entry[2], entry[3], entry[4], stamp, stamp_value, lr_cost, identity_hash)
|
||||||
|
|
||||||
|
# Caller must hold Identity.known_destinations_lock
|
||||||
|
def mark_dirty(self, destination_hash):
|
||||||
|
if self.read_only: return
|
||||||
|
self._dirty_delete.discard(destination_hash)
|
||||||
|
self._kd_quarantine.pop(destination_hash, None)
|
||||||
|
self._dirty_upsert.add(destination_hash)
|
||||||
|
|
||||||
|
# Caller must hold Identity.known_destinations_lock
|
||||||
|
def mark_deleted(self, destination_hash):
|
||||||
|
if self.read_only: return
|
||||||
|
self._dirty_upsert.discard(destination_hash)
|
||||||
|
self._dirty_delete.add(destination_hash)
|
||||||
|
self._kd_quarantine.pop(destination_hash, None)
|
||||||
|
self._ratchet_quarantine.pop(destination_hash, None)
|
||||||
|
|
||||||
|
# Caller must hold Identity.known_destinations_lock
|
||||||
|
def take_pending(self):
|
||||||
|
if self.read_only: return set(), set(), {}, set()
|
||||||
|
upserts = self._dirty_upsert
|
||||||
|
deletes = self._dirty_delete
|
||||||
|
ratchet_upserts = self._ratchet_upserts
|
||||||
|
ratchet_deletes = self._ratchet_deletes
|
||||||
|
self._dirty_upsert = set()
|
||||||
|
self._dirty_delete = set()
|
||||||
|
self._ratchet_upserts = {}
|
||||||
|
self._ratchet_deletes = set()
|
||||||
|
return upserts, deletes, ratchet_upserts, ratchet_deletes
|
||||||
|
|
||||||
|
# Caller must hold the flush lock and have an already open transaction
|
||||||
|
def __wrap_exec(self, sql, rows):
|
||||||
|
rejects = []
|
||||||
|
|
||||||
|
# By default, run batched queries in chunks.
|
||||||
|
for i in range(0, len(rows), 4096):
|
||||||
|
chunk = rows[i:i+4096]
|
||||||
|
try:
|
||||||
|
self._conn.execute("SAVEPOINT chunk")
|
||||||
|
self._conn.executemany(sql, chunk)
|
||||||
|
self._conn.execute("RELEASE chunk")
|
||||||
|
|
||||||
|
# But on integrity and data errors, roll back the chunk
|
||||||
|
# and iterate per-row, so we can catch any violating
|
||||||
|
# entries and quarantine them. A later, sane update
|
||||||
|
# will then unquarantine and heal upon arrival.
|
||||||
|
#
|
||||||
|
# Other exceptions are intentionally not caught here,
|
||||||
|
# so they can propagate directly.
|
||||||
|
except (self.sqlite3.IntegrityError, self.sqlite3.DataError):
|
||||||
|
self._conn.execute("ROLLBACK TO chunk")
|
||||||
|
for row in chunk:
|
||||||
|
try:
|
||||||
|
self._conn.execute("SAVEPOINT row")
|
||||||
|
self._conn.execute(sql, row)
|
||||||
|
self._conn.execute("RELEASE row")
|
||||||
|
|
||||||
|
except (self.sqlite3.IntegrityError, self.sqlite3.DataError) as e:
|
||||||
|
self._conn.execute("ROLLBACK TO row")
|
||||||
|
rejects.append((row[0], str(e)))
|
||||||
|
|
||||||
|
self._conn.execute("RELEASE chunk")
|
||||||
|
|
||||||
|
return rejects
|
||||||
|
|
||||||
|
# May be called without any locks held
|
||||||
|
def apply(self, upserts, deletes, ratchet_upserts, ratchet_deletes, known_destinations):
|
||||||
|
if self.read_only: return 0
|
||||||
|
if len(upserts) == 0 and len(deletes) == 0 and len(ratchet_upserts) == 0 and len(ratchet_deletes) == 0: return 0
|
||||||
|
|
||||||
|
with self._flush_lock:
|
||||||
|
rows = []
|
||||||
|
removables = []
|
||||||
|
ratchet_rows = []
|
||||||
|
ratchet_removables = []
|
||||||
|
|
||||||
|
for destination_hash in upserts:
|
||||||
|
if destination_hash in self._kd_quarantine: continue
|
||||||
|
entry = known_destinations.get(destination_hash)
|
||||||
|
if entry != None: rows.append(self.__entry_row(destination_hash, entry))
|
||||||
|
|
||||||
|
for destination_hash in deletes:
|
||||||
|
if destination_hash not in known_destinations: removables.append((destination_hash,))
|
||||||
|
|
||||||
|
for destination_hash, ratchet_entry in ratchet_upserts.items():
|
||||||
|
# Ratchets of purged destinations must be removed as well
|
||||||
|
if destination_hash not in known_destinations: ratchet_removables.append((destination_hash,))
|
||||||
|
elif destination_hash not in self._ratchet_quarantine: ratchet_rows.append((destination_hash, *ratchet_entry))
|
||||||
|
|
||||||
|
for destination_hash in ratchet_deletes:
|
||||||
|
if destination_hash not in known_destinations: ratchet_removables.append((destination_hash,))
|
||||||
|
|
||||||
|
kd_rejects = []
|
||||||
|
ratchet_rejects = []
|
||||||
|
try:
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
kd_rejects += self.__wrap_exec("INSERT INTO known_destinations (destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost, identity_hash) VALUES (?,?,?,?,?,?,?,?,?,?) ON CONFLICT(destination_hash) DO UPDATE SET last_heard=excluded.last_heard, packet_hash=excluded.packet_hash, public_key=excluded.public_key, app_data=excluded.app_data, used=excluded.used, stamp=excluded.stamp, stamp_value=excluded.stamp_value, lr_cost=excluded.lr_cost, identity_hash=excluded.identity_hash", rows)
|
||||||
|
kd_rejects += self.__wrap_exec("DELETE FROM known_destinations WHERE destination_hash=?", removables)
|
||||||
|
ratchet_rejects += self.__wrap_exec("INSERT INTO ratchets VALUES (?,?,?) ON CONFLICT(destination_hash) DO UPDATE SET ratchet=excluded.ratchet, received=excluded.received", ratchet_rows)
|
||||||
|
ratchet_rejects += self.__wrap_exec("DELETE FROM ratchets WHERE destination_hash=?", ratchet_removables)
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
# If an exception is not handled in the batch query wrapper,
|
||||||
|
# we will have to roll back the transaction, and mark all
|
||||||
|
# attempted entries as dirty. A failure state like such
|
||||||
|
# should not happen, and this is a last resort; at this
|
||||||
|
# point, writes may stall until an updated entry for
|
||||||
|
# the violating row(s) arrive and heal the invalid
|
||||||
|
# dirty entries, so this is raised for logging.
|
||||||
|
self._dirty_upsert |= {row[0] for row in rows}
|
||||||
|
self._dirty_delete |= {row[0] for row in removables}
|
||||||
|
self._ratchet_upserts |= {row[0]: row[1:] for row in ratchet_rows}
|
||||||
|
self._ratchet_deletes |= {row[0] for row in ratchet_removables}
|
||||||
|
self._conn.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
# Track failed persists and one-time log per failure. A later
|
||||||
|
# update of the destination clears the quarantine state and
|
||||||
|
# allows persist to retry with the (hopefully) valid data.
|
||||||
|
if len(kd_rejects) > 0 or len(ratchet_rejects) > 0:
|
||||||
|
for destination_hash, error in kd_rejects:
|
||||||
|
self._kd_quarantine[destination_hash] = (error, time.time())
|
||||||
|
RNS.log(f"Invalid known destination data for {RNS.prettyhexrep(destination_hash)} at persist. The contained exception was: {error}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
for destination_hash, error in ratchet_rejects:
|
||||||
|
self._ratchet_quarantine[destination_hash] = (error, time.time())
|
||||||
|
RNS.log(f"Invalid ratchet data for {RNS.prettyhexrep(destination_hash)} at persist. The contained exception was: {error}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
return len(rows)+len(removables)+len(ratchet_rows)+len(ratchet_removables)-len(kd_rejects)-len(ratchet_rejects)
|
||||||
|
|
||||||
|
def load_all(self):
|
||||||
|
cursor = self._conn.execute("SELECT destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost FROM known_destinations")
|
||||||
|
while True:
|
||||||
|
rows = cursor.fetchmany(4096)
|
||||||
|
if not rows: break
|
||||||
|
for row in rows: yield row[0], [row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8]]
|
||||||
|
|
||||||
|
def remember_ratchet(self, destination_hash, ratchet, now):
|
||||||
|
if self.read_only: return False
|
||||||
|
self._ratchet_quarantine.pop(destination_hash, None)
|
||||||
|
|
||||||
|
pending = self._ratchet_upserts.get(destination_hash)
|
||||||
|
if pending != None:
|
||||||
|
if pending[0] == ratchet:
|
||||||
|
self._ratchet_deletes.discard(destination_hash)
|
||||||
|
return False
|
||||||
|
|
||||||
|
else:
|
||||||
|
with self._flush_lock:
|
||||||
|
row = self._conn.execute("SELECT ratchet FROM ratchets WHERE destination_hash=?", (destination_hash,)).fetchone()
|
||||||
|
|
||||||
|
if row != None and row[0] == ratchet:
|
||||||
|
# Discard any pending delete mark queued
|
||||||
|
# before this ratchet arrived
|
||||||
|
self._ratchet_deletes.discard(destination_hash)
|
||||||
|
return False
|
||||||
|
|
||||||
|
self._ratchet_upserts[destination_hash] = (ratchet, now)
|
||||||
|
self._ratchet_deletes.discard(destination_hash)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def fetch_ratchet(self, destination_hash):
|
||||||
|
pending = self._ratchet_upserts.get(destination_hash)
|
||||||
|
if pending != None: return pending[0], pending[1]
|
||||||
|
if destination_hash in self._ratchet_deletes: return None
|
||||||
|
|
||||||
|
with self._flush_lock:
|
||||||
|
row = self._conn.execute("SELECT ratchet, received FROM ratchets WHERE destination_hash=?", (destination_hash,)).fetchone()
|
||||||
|
|
||||||
|
if row != None: return row[0], row[1]
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Caller must hold Identity.known_destinations_lock
|
||||||
|
def mark_ratchet_deleted(self, destination_hash):
|
||||||
|
if self.read_only: return
|
||||||
|
self._ratchet_upserts.pop(destination_hash, None)
|
||||||
|
self._ratchet_deletes.add(destination_hash)
|
||||||
|
self._ratchet_quarantine.pop(destination_hash, None)
|
||||||
|
|
||||||
|
def expire_ratchets(self, cutoff):
|
||||||
|
if self.read_only: return []
|
||||||
|
|
||||||
|
with self._flush_lock:
|
||||||
|
# Drop expired entries in pending as well
|
||||||
|
expired = set(row[0] for row in self._conn.execute("SELECT destination_hash FROM ratchets WHERE received<?", (cutoff,)))
|
||||||
|
for destination_hash, entry in list(self._ratchet_upserts.items()):
|
||||||
|
if entry[1] < cutoff:
|
||||||
|
expired.add(destination_hash)
|
||||||
|
self._ratchet_upserts.pop(destination_hash, None)
|
||||||
|
|
||||||
|
expired.difference_update(self._ratchet_deletes)
|
||||||
|
if len(expired) > 0:
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
for i in range(0, len(expired), 4096):
|
||||||
|
self._conn.executemany("DELETE FROM ratchets WHERE destination_hash=?", [(h,) for h in list(expired)[i:i+4096]])
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
return list(expired)
|
||||||
|
|
||||||
|
def load_ratchets(self):
|
||||||
|
cursor = self._conn.execute("SELECT destination_hash, ratchet, received FROM ratchets")
|
||||||
|
while True:
|
||||||
|
rows = cursor.fetchmany(4096)
|
||||||
|
if not rows: break
|
||||||
|
for row in rows: yield row[0], row[1], row[2]
|
||||||
|
|
||||||
|
def import_legacy_ratchets(self, dir_path, expiry_cutoff, known_destinations, ratchet_width=32):
|
||||||
|
from .vendor import umsgpack
|
||||||
|
imported = 0
|
||||||
|
for filename in os.listdir(dir_path):
|
||||||
|
file_path = os.path.join(dir_path, filename)
|
||||||
|
try:
|
||||||
|
if not os.path.isfile(file_path): continue
|
||||||
|
with open(file_path, "rb") as rf:
|
||||||
|
ratchet_data = umsgpack.unpackb(rf.read())
|
||||||
|
|
||||||
|
destination_hash = bytes.fromhex(filename)
|
||||||
|
ratchet = ratchet_data["ratchet"]; received = ratchet_data["received"]
|
||||||
|
if len(ratchet) != ratchet_width: raise ValueError("Invalid ratchet width")
|
||||||
|
if received < expiry_cutoff: raise ValueError("Ratchet expired")
|
||||||
|
if destination_hash not in known_destinations: raise ValueError("Destination unknown")
|
||||||
|
|
||||||
|
self._ratchet_upserts[destination_hash] = (ratchet, received)
|
||||||
|
imported += 1
|
||||||
|
os.unlink(file_path)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not import ratchet for {filename}: {e}", RNS.LOG_WARNING)
|
||||||
|
try: os.unlink(file_path)
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
pending = self._ratchet_upserts
|
||||||
|
self._ratchet_upserts = {}
|
||||||
|
self.apply(set(), set(), pending, set(), known_destinations)
|
||||||
|
|
||||||
|
return imported
|
||||||
|
|
||||||
|
def import_legacy(self, path):
|
||||||
|
from .vendor import umsgpack
|
||||||
|
with open(path, "rb") as file: legacy = umsgpack.load(file)
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
now = time.time()
|
||||||
|
for destination_hash, entry in legacy.items():
|
||||||
|
if len(entry) < 5: continue
|
||||||
|
entry = list(entry)
|
||||||
|
while len(entry) < 8: entry.append(None if len(entry) in (5, 7) else 0)
|
||||||
|
if entry[0] == None or entry[0] <= 0: entry[0] = now
|
||||||
|
rows.append(self.__entry_row(destination_hash, entry))
|
||||||
|
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
for i in range(0, len(rows), 4096): self._conn.executemany("INSERT OR REPLACE INTO known_destinations (destination_hash, last_heard, packet_hash, public_key, app_data, used, stamp, stamp_value, lr_cost, identity_hash) VALUES (?,?,?,?,?,?,?,?,?,?)", rows[i:i+4096])
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
return len(rows)
|
||||||
|
|
||||||
|
class AnnounceCache:
|
||||||
|
|
||||||
|
def __init__(self, db_path):
|
||||||
|
import sqlite3
|
||||||
|
self.db_path = db_path
|
||||||
|
self._pending = {}
|
||||||
|
self._flush_lock = threading.Lock()
|
||||||
|
self._conn = sqlite3.connect(db_path, check_same_thread=False, isolation_level=None, cached_statements=0)
|
||||||
|
|
||||||
|
self._conn.execute("PRAGMA auto_vacuum=INCREMENTAL")
|
||||||
|
self._conn.execute("PRAGMA journal_mode=WAL")
|
||||||
|
self._conn.execute("PRAGMA synchronous=NORMAL")
|
||||||
|
self._conn.execute("PRAGMA journal_size_limit=8388608")
|
||||||
|
self.__create_schema()
|
||||||
|
|
||||||
|
def __create_schema(self):
|
||||||
|
self._conn.execute("""CREATE TABLE IF NOT EXISTS cached_announces (
|
||||||
|
packet_hash BLOB PRIMARY KEY,
|
||||||
|
received REAL NOT NULL,
|
||||||
|
raw BLOB NOT NULL,
|
||||||
|
interface_reference TEXT)""")
|
||||||
|
|
||||||
|
self._conn.execute("PRAGMA user_version = 1")
|
||||||
|
|
||||||
|
def store(self, packet_hash, raw, interface_reference):
|
||||||
|
self._pending[packet_hash] = (time.time(), raw, interface_reference)
|
||||||
|
|
||||||
|
def pending_count(self):
|
||||||
|
return len(self._pending)
|
||||||
|
|
||||||
|
def fetch(self, packet_hash):
|
||||||
|
pending = self._pending.get(packet_hash)
|
||||||
|
if pending != None: return pending[1], pending[2]
|
||||||
|
|
||||||
|
with self._flush_lock:
|
||||||
|
row = self._conn.execute("SELECT raw, interface_reference FROM cached_announces WHERE packet_hash=?", (packet_hash,)).fetchone()
|
||||||
|
|
||||||
|
if row != None: return row[0], row[1]
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def flush(self):
|
||||||
|
with self._flush_lock: return self.__apply_pending()
|
||||||
|
|
||||||
|
def __apply_pending(self):
|
||||||
|
pending = self._pending
|
||||||
|
self._pending = {}
|
||||||
|
if len(pending) == 0: return 0
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
items = list(pending.items())
|
||||||
|
for i in range(0, len(items), 4096):
|
||||||
|
self._conn.executemany("INSERT INTO cached_announces VALUES (?,?,?,?) ON CONFLICT(packet_hash) DO UPDATE SET received=excluded.received, raw=excluded.raw, interface_reference=excluded.interface_reference",
|
||||||
|
[(packet_hash, *entry) for packet_hash, entry in items[i:i+4096]])
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
# Re-mark everything as pending, so the changes
|
||||||
|
# are retried on the next flush, then propagate.
|
||||||
|
self._pending.update(pending)
|
||||||
|
self._conn.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
return len(pending)
|
||||||
|
|
||||||
|
def clean(self, active_hashes):
|
||||||
|
with self._flush_lock:
|
||||||
|
self.__apply_pending()
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._conn.execute("BEGIN")
|
||||||
|
self._conn.execute("DROP TABLE IF EXISTS temp.active_announces")
|
||||||
|
self._conn.execute("CREATE TEMP TABLE active_announces (packet_hash BLOB PRIMARY KEY)")
|
||||||
|
active_list = list(active_hashes)
|
||||||
|
for i in range(0, len(active_list), 4096):
|
||||||
|
self._conn.executemany("INSERT OR IGNORE INTO active_announces VALUES (?)", [(h,) for h in active_list[i:i+4096]])
|
||||||
|
|
||||||
|
changes_before = self._conn.total_changes
|
||||||
|
self._conn.execute("DELETE FROM cached_announces WHERE packet_hash NOT IN (SELECT packet_hash FROM active_announces)")
|
||||||
|
removed = self._conn.total_changes - changes_before
|
||||||
|
self._conn.commit()
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
self._conn.rollback()
|
||||||
|
raise
|
||||||
|
|
||||||
|
if removed > 0: self._conn.execute("PRAGMA incremental_vacuum")
|
||||||
|
|
||||||
|
return removed
|
||||||
|
|
||||||
|
def import_legacy(self, dir_path):
|
||||||
|
from .vendor import umsgpack
|
||||||
|
imported = 0
|
||||||
|
for filename in os.listdir(dir_path):
|
||||||
|
file_path = os.path.join(dir_path, filename)
|
||||||
|
try:
|
||||||
|
if not os.path.isfile(file_path): continue
|
||||||
|
with open(file_path, "rb") as file:
|
||||||
|
cached_data = umsgpack.unpackb(file.read())
|
||||||
|
|
||||||
|
packet_hash = bytes.fromhex(filename)
|
||||||
|
interface_reference = cached_data[1] if len(cached_data) > 1 else None
|
||||||
|
self.store(packet_hash, cached_data[0], interface_reference)
|
||||||
|
imported += 1
|
||||||
|
os.unlink(file_path)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
try: os.unlink(file_path)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
self.flush()
|
||||||
|
return imported
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self._conn != None:
|
||||||
|
self._conn.close()
|
||||||
|
self._conn = None
|
||||||
6111
RNS/Transport.py
6111
RNS/Transport.py
File diff suppressed because it is too large
Load diff
|
|
@ -1,6 +1,6 @@
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -9,8 +9,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -25,5 +33,7 @@ import glob
|
||||||
|
|
||||||
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
modules = py_modules+pyc_modules
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
__all__ = list(set([os.path.basename(f).replace(".pyc", "").replace(".py", "") for f in modules if not (f.endswith("__init__.py") or f.endswith("__init__.pyc"))]))
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -25,6 +33,7 @@
|
||||||
import RNS
|
import RNS
|
||||||
import argparse
|
import argparse
|
||||||
import threading
|
import threading
|
||||||
|
import shutil
|
||||||
import time
|
import time
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
@ -34,22 +43,45 @@ from RNS._version import __version__
|
||||||
APP_NAME = "rncp"
|
APP_NAME = "rncp"
|
||||||
allow_all = False
|
allow_all = False
|
||||||
allow_fetch = False
|
allow_fetch = False
|
||||||
|
allow_overwrite_on_receive = False
|
||||||
|
fetch_auto_compress = True
|
||||||
fetch_jail = None
|
fetch_jail = None
|
||||||
save_path = None
|
save_path = None
|
||||||
show_phy_rates = False
|
show_phy_rates = False
|
||||||
allowed_identity_hashes = []
|
allowed_identity_hashes = []
|
||||||
|
identity = None
|
||||||
|
|
||||||
|
def prepare_identity(identity_path):
|
||||||
|
global identity
|
||||||
|
if identity_path == None:
|
||||||
|
identity_path = RNS.Reticulum.identitypath+"/"+APP_NAME
|
||||||
|
|
||||||
|
if os.path.isfile(identity_path):
|
||||||
|
identity = RNS.Identity.from_file(identity_path)
|
||||||
|
if identity == None:
|
||||||
|
RNS.log(f"Could not load identity for rncp. The identity file at \"{identity_path}\" may be corrupt or unreadable.", RNS.LOG_ERROR)
|
||||||
|
RNS.exit(2)
|
||||||
|
|
||||||
|
if identity == None:
|
||||||
|
RNS.log("No valid saved identity found, creating new...", RNS.LOG_INFO)
|
||||||
|
identity = RNS.Identity()
|
||||||
|
identity.to_file(identity_path)
|
||||||
|
|
||||||
REQ_FETCH_NOT_ALLOWED = 0xF0
|
REQ_FETCH_NOT_ALLOWED = 0xF0
|
||||||
|
|
||||||
es = " "
|
es = " "
|
||||||
erase_str = "\33[2K\r"
|
erase_str = "\33[2K\r"
|
||||||
|
|
||||||
def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identity = False,
|
def listen(configdir, identitypath = None, verbosity = 0, quietness = 0, allowed = [], display_identity = False,
|
||||||
limit = None, disable_auth = None, fetch_allowed = False, jail = None, save = None, announce = False):
|
limit = None, disable_auth = None, fetch_allowed = False, no_compress=False,
|
||||||
global allow_all, allow_fetch, allowed_identity_hashes, fetch_jail, save_path
|
jail = None, save = None, announce = False, allow_overwrite=False):
|
||||||
from tempfile import TemporaryFile
|
|
||||||
|
global allow_all, allow_fetch, allowed_identity_hashes, fetch_jail, save_path, identity
|
||||||
|
global fetch_auto_compress, allow_overwrite_on_receive
|
||||||
|
|
||||||
allow_fetch = fetch_allowed
|
allow_fetch = fetch_allowed
|
||||||
|
fetch_auto_compress = not no_compress
|
||||||
|
allow_overwrite_on_receive = allow_overwrite
|
||||||
identity = None
|
identity = None
|
||||||
if announce < 0:
|
if announce < 0:
|
||||||
announce = False
|
announce = False
|
||||||
|
|
@ -68,28 +100,21 @@ def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identi
|
||||||
save_path = sp
|
save_path = sp
|
||||||
else:
|
else:
|
||||||
RNS.log("Output directory not writable", RNS.LOG_ERROR)
|
RNS.log("Output directory not writable", RNS.LOG_ERROR)
|
||||||
exit(4)
|
RNS.exit(4)
|
||||||
else:
|
else:
|
||||||
RNS.log("Output directory not found", RNS.LOG_ERROR)
|
RNS.log("Output directory not found", RNS.LOG_ERROR)
|
||||||
exit(3)
|
RNS.exit(3)
|
||||||
|
|
||||||
RNS.log("Saving received files in \""+save_path+"\"", RNS.LOG_VERBOSE)
|
RNS.log("Saving received files in \""+save_path+"\"", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
identity_path = RNS.Reticulum.identitypath+"/"+APP_NAME
|
prepare_identity(identitypath)
|
||||||
if os.path.isfile(identity_path):
|
|
||||||
identity = RNS.Identity.from_file(identity_path)
|
|
||||||
|
|
||||||
if identity == None:
|
|
||||||
RNS.log("No valid saved identity found, creating new...", RNS.LOG_INFO)
|
|
||||||
identity = RNS.Identity()
|
|
||||||
identity.to_file(identity_path)
|
|
||||||
|
|
||||||
destination = RNS.Destination(identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, "receive")
|
destination = RNS.Destination(identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, "receive")
|
||||||
|
|
||||||
if display_identity:
|
if display_identity:
|
||||||
print("Identity : "+str(identity))
|
print("Identity : "+str(identity))
|
||||||
print("Listening on : "+RNS.prettyhexrep(destination.hash))
|
print("Listening on : "+RNS.prettyhexrep(destination.hash))
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
|
|
||||||
if disable_auth:
|
if disable_auth:
|
||||||
allow_all = True
|
allow_all = True
|
||||||
|
|
@ -138,14 +163,14 @@ def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identi
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
raise ValueError("Invalid destination entered. Check your input.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
RNS.log(f"Could not apply allowed identity: {e}", RNS.LOG_ERROR)
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
|
|
||||||
if len(allowed_identity_hashes) < 1 and not disable_auth:
|
if len(allowed_identity_hashes) < 1 and not disable_auth:
|
||||||
print("Warning: No allowed identities configured, rncp will not accept any files!")
|
RNS.log("No allowed identities configured, rncp will not accept any files!", RNS.LOG_WARNING)
|
||||||
|
|
||||||
def fetch_request(path, data, request_id, link_id, remote_identity, requested_at):
|
def fetch_request(path, data, request_id, link_id, remote_identity, requested_at):
|
||||||
global allow_fetch, fetch_jail
|
global allow_fetch, fetch_jail, fetch_auto_compress
|
||||||
if not allow_fetch:
|
if not allow_fetch:
|
||||||
return REQ_FETCH_NOT_ALLOWED
|
return REQ_FETCH_NOT_ALLOWED
|
||||||
|
|
||||||
|
|
@ -171,22 +196,15 @@ def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identi
|
||||||
if target_link != None:
|
if target_link != None:
|
||||||
RNS.log("Sending file "+str(file_path)+" to client", RNS.LOG_VERBOSE)
|
RNS.log("Sending file "+str(file_path)+" to client", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
temp_file = TemporaryFile()
|
try:
|
||||||
real_file = open(file_path, "rb")
|
metadata = {"name": os.path.basename(file_path).encode("utf-8") }
|
||||||
filename_bytes = os.path.basename(file_path).encode("utf-8")
|
fetch_resource = RNS.Resource(open(file_path, "rb"), target_link, metadata=metadata, auto_compress=fetch_auto_compress)
|
||||||
filename_len = len(filename_bytes)
|
return True
|
||||||
|
|
||||||
if filename_len > 0xFFFF:
|
except Exception as e:
|
||||||
print("Filename exceeds max size, cannot send")
|
RNS.log(f"Could not send file to client. The contained exception was: {e}", RNS.LOG_ERROR)
|
||||||
exit(1)
|
return False
|
||||||
|
|
||||||
temp_file.write(filename_len.to_bytes(2, "big"))
|
|
||||||
temp_file.write(filename_bytes)
|
|
||||||
temp_file.write(real_file.read())
|
|
||||||
temp_file.seek(0)
|
|
||||||
|
|
||||||
fetch_resource = RNS.Resource(temp_file, target_link)
|
|
||||||
return True
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
@ -199,7 +217,7 @@ def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identi
|
||||||
else:
|
else:
|
||||||
destination.register_request_handler("fetch_file", response_generator=fetch_request, allow=RNS.Destination.ALLOW_LIST, allowed_list=allowed_identity_hashes)
|
destination.register_request_handler("fetch_file", response_generator=fetch_request, allow=RNS.Destination.ALLOW_LIST, allowed_list=allowed_identity_hashes)
|
||||||
|
|
||||||
print("rncp listening on "+RNS.prettyhexrep(destination.hash))
|
RNS.log("rncp listening on "+RNS.prettyhexrep(destination.hash), RNS.LOG_INFO)
|
||||||
|
|
||||||
if announce >= 0:
|
if announce >= 0:
|
||||||
def job():
|
def job():
|
||||||
|
|
@ -211,8 +229,7 @@ def listen(configdir, verbosity = 0, quietness = 0, allowed = [], display_identi
|
||||||
|
|
||||||
threading.Thread(target=job, daemon=True).start()
|
threading.Thread(target=job, daemon=True).start()
|
||||||
|
|
||||||
while True:
|
while True: time.sleep(1)
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def client_link_established(link):
|
def client_link_established(link):
|
||||||
RNS.log("Incoming link established", RNS.LOG_VERBOSE)
|
RNS.log("Incoming link established", RNS.LOG_VERBOSE)
|
||||||
|
|
@ -254,40 +271,49 @@ def receive_resource_started(resource):
|
||||||
else:
|
else:
|
||||||
id_str = ""
|
id_str = ""
|
||||||
|
|
||||||
print("Starting resource transfer "+RNS.prettyhexrep(resource.hash)+id_str)
|
RNS.log("Starting resource transfer "+RNS.prettyhexrep(resource.hash)+id_str, RNS.LOG_INFO)
|
||||||
|
|
||||||
def receive_resource_concluded(resource):
|
def receive_resource_concluded(resource):
|
||||||
global save_path
|
global save_path, allow_overwrite_on_receive
|
||||||
if resource.status == RNS.Resource.COMPLETE:
|
if resource.status == RNS.Resource.COMPLETE:
|
||||||
print(str(resource)+" completed")
|
RNS.log(f"Incoming resource {resource} completed", RNS.LOG_INFO)
|
||||||
|
|
||||||
if resource.total_size > 4:
|
if resource.metadata == None:
|
||||||
filename_len = int.from_bytes(resource.data.read(2), "big")
|
RNS.log("Invalid data received, ignoring resource", RNS.LOG_WARNING)
|
||||||
filename = resource.data.read(filename_len).decode("utf-8")
|
return
|
||||||
|
|
||||||
counter = 0
|
|
||||||
if save_path:
|
|
||||||
saved_filename = os.path.abspath(os.path.expanduser(save_path+"/"+filename))
|
|
||||||
if not saved_filename.startswith(save_path+"/"):
|
|
||||||
RNS.log(f"Invalid save path {saved_filename}, ignoring", RNS.LOG_ERROR)
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
saved_filename = filename
|
|
||||||
|
|
||||||
full_save_path = saved_filename
|
|
||||||
while os.path.isfile(full_save_path):
|
|
||||||
counter += 1
|
|
||||||
full_save_path = saved_filename+"."+str(counter)
|
|
||||||
|
|
||||||
file = open(full_save_path, "wb")
|
|
||||||
file.write(resource.data.read())
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Invalid data received, ignoring resource")
|
try:
|
||||||
|
filename = os.path.basename(resource.metadata["name"].decode("utf-8"))
|
||||||
|
counter = 0
|
||||||
|
if save_path:
|
||||||
|
saved_filename = os.path.abspath(os.path.expanduser(save_path+"/"+filename))
|
||||||
|
if not saved_filename.startswith(save_path+"/"):
|
||||||
|
RNS.log(f"Invalid save path {saved_filename}, ignoring", RNS.LOG_ERROR)
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
saved_filename = filename
|
||||||
|
|
||||||
|
full_save_path = saved_filename
|
||||||
|
if allow_overwrite_on_receive:
|
||||||
|
if os.path.isfile(full_save_path):
|
||||||
|
try: os.unlink(full_save_path)
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not overwrite existing file {full_save_path}, renaming instead", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
while os.path.isfile(full_save_path):
|
||||||
|
counter += 1
|
||||||
|
full_save_path = saved_filename+"."+str(counter)
|
||||||
|
|
||||||
|
shutil.move(resource.data.name, full_save_path)
|
||||||
|
RNS.log("Saved received file to "+full_save_path, RNS.LOG_NOTICE)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"An error occurred while saving received resource: {e}", RNS.LOG_ERROR)
|
||||||
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Resource failed")
|
RNS.log("Resource failed", RNS.LOG_INFO)
|
||||||
|
|
||||||
resource_done = False
|
resource_done = False
|
||||||
current_resource = None
|
current_resource = None
|
||||||
|
|
@ -330,10 +356,11 @@ def sender_progress(resource):
|
||||||
resource_done = True
|
resource_done = True
|
||||||
|
|
||||||
link = None
|
link = None
|
||||||
def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = None, timeout = RNS.Transport.PATH_REQUEST_TIMEOUT, silent=False, phy_rates=False, save=None):
|
def fetch(configdir, identitypath = None, verbosity = 0, quietness = 0, destination = None, file = None, timeout = RNS.Transport.PATH_REQUEST_TIMEOUT, silent=False, phy_rates=False, save=None, allow_overwrite=False):
|
||||||
global current_resource, resource_done, link, speed, show_phy_rates, save_path
|
global current_resource, resource_done, link, speed, show_phy_rates, save_path, allow_overwrite_on_receive, identity
|
||||||
targetloglevel = 3+verbosity-quietness
|
targetloglevel = 3+verbosity-quietness
|
||||||
show_phy_rates = phy_rates
|
show_phy_rates = phy_rates
|
||||||
|
allow_overwrite_on_receive = allow_overwrite
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
sp = os.path.abspath(os.path.expanduser(save))
|
sp = os.path.abspath(os.path.expanduser(save))
|
||||||
|
|
@ -342,10 +369,10 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
save_path = sp
|
save_path = sp
|
||||||
else:
|
else:
|
||||||
RNS.log("Output directory not writable", RNS.LOG_ERROR)
|
RNS.log("Output directory not writable", RNS.LOG_ERROR)
|
||||||
exit(4)
|
RNS.exit(4)
|
||||||
else:
|
else:
|
||||||
RNS.log("Output directory not found", RNS.LOG_ERROR)
|
RNS.log("Output directory not found", RNS.LOG_ERROR)
|
||||||
exit(3)
|
RNS.exit(3)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
|
|
@ -357,23 +384,13 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
raise ValueError("Invalid destination entered. Check your input.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
|
|
||||||
reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
|
reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
|
||||||
|
reticulum.wants_destination_type(f"{APP_NAME}.receive")
|
||||||
identity_path = RNS.Reticulum.identitypath+"/"+APP_NAME
|
|
||||||
if os.path.isfile(identity_path):
|
|
||||||
identity = RNS.Identity.from_file(identity_path)
|
|
||||||
if identity == None:
|
|
||||||
RNS.log("Could not load identity for rncp. The identity file at \""+str(identity_path)+"\" may be corrupt or unreadable.", RNS.LOG_ERROR)
|
|
||||||
exit(2)
|
|
||||||
else:
|
|
||||||
identity = None
|
|
||||||
|
|
||||||
if identity == None:
|
if identity == None:
|
||||||
RNS.log("No valid saved identity found, creating new...", RNS.LOG_INFO)
|
prepare_identity(identitypath)
|
||||||
identity = RNS.Identity()
|
|
||||||
identity.to_file(identity_path)
|
|
||||||
|
|
||||||
if not RNS.Transport.has_path(destination_hash):
|
if not RNS.Transport.has_path(destination_hash):
|
||||||
RNS.Transport.request_path(destination_hash)
|
RNS.Transport.request_path(destination_hash)
|
||||||
|
|
@ -385,7 +402,7 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
||||||
estab_timeout = time.time()+timeout
|
estab_timeout = time.time()+max(timeout, reticulum.get_medium_path_timeout())
|
||||||
while not RNS.Transport.has_path(destination_hash) and time.time() < estab_timeout:
|
while not RNS.Transport.has_path(destination_hash) and time.time() < estab_timeout:
|
||||||
if not silent:
|
if not silent:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
@ -398,7 +415,7 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
print("Path not found")
|
print("Path not found")
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}Path not found")
|
print(f"{erase_str}Path not found")
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print("Establishing link with "+RNS.prettyhexrep(destination_hash))
|
print("Establishing link with "+RNS.prettyhexrep(destination_hash))
|
||||||
|
|
@ -415,6 +432,7 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
)
|
)
|
||||||
|
|
||||||
link = RNS.Link(listener_destination)
|
link = RNS.Link(listener_destination)
|
||||||
|
estab_timeout = max(estab_timeout, time.time()+link.establishment_timeout)
|
||||||
while link.status != RNS.Link.ACTIVE and time.time() < estab_timeout:
|
while link.status != RNS.Link.ACTIVE and time.time() < estab_timeout:
|
||||||
if not silent:
|
if not silent:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
@ -427,7 +445,7 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
print("Could not establish link with "+RNS.prettyhexrep(destination_hash))
|
print("Could not establish link with "+RNS.prettyhexrep(destination_hash))
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}Could not establish link with "+RNS.prettyhexrep(destination_hash))
|
print(f"{erase_str}Could not establish link with "+RNS.prettyhexrep(destination_hash))
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print("Requesting file from remote...")
|
print("Requesting file from remote...")
|
||||||
|
|
@ -441,6 +459,7 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
resource_resolved = False
|
resource_resolved = False
|
||||||
resource_status = "unrequested"
|
resource_status = "unrequested"
|
||||||
current_resource = None
|
current_resource = None
|
||||||
|
current_transfer_started = None
|
||||||
def request_response(request_receipt):
|
def request_response(request_receipt):
|
||||||
nonlocal request_resolved, request_status
|
nonlocal request_resolved, request_status
|
||||||
if request_receipt.response == False:
|
if request_receipt.response == False:
|
||||||
|
|
@ -460,39 +479,48 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
request_resolved = True
|
request_resolved = True
|
||||||
|
|
||||||
def fetch_resource_started(resource):
|
def fetch_resource_started(resource):
|
||||||
nonlocal resource_status
|
nonlocal resource_status, current_transfer_started
|
||||||
current_resource = resource
|
current_resource = resource
|
||||||
current_resource.progress_callback(sender_progress)
|
current_resource.progress_callback(sender_progress)
|
||||||
resource_status = "started"
|
resource_status = "started"
|
||||||
|
if not current_transfer_started: current_transfer_started = time.time()
|
||||||
|
|
||||||
def fetch_resource_concluded(resource):
|
def fetch_resource_concluded(resource):
|
||||||
nonlocal resource_resolved, resource_status
|
nonlocal resource_resolved, resource_status
|
||||||
global save_path
|
global save_path, allow_overwrite_on_receive
|
||||||
if resource.status == RNS.Resource.COMPLETE:
|
if resource.status == RNS.Resource.COMPLETE:
|
||||||
if resource.total_size > 4:
|
if resource.metadata == None:
|
||||||
filename_len = int.from_bytes(resource.data.read(2), "big")
|
print("Invalid data received, ignoring resource")
|
||||||
filename = resource.data.read(filename_len).decode("utf-8")
|
return
|
||||||
|
|
||||||
counter = 0
|
|
||||||
if save_path:
|
|
||||||
saved_filename = os.path.abspath(os.path.expanduser(save_path+"/"+filename))
|
|
||||||
|
|
||||||
else:
|
|
||||||
saved_filename = filename
|
|
||||||
|
|
||||||
full_save_path = saved_filename
|
|
||||||
while os.path.isfile(full_save_path):
|
|
||||||
counter += 1
|
|
||||||
full_save_path = saved_filename+"."+str(counter)
|
|
||||||
|
|
||||||
file = open(full_save_path, "wb")
|
|
||||||
file.write(resource.data.read())
|
|
||||||
file.close()
|
|
||||||
resource_status = "completed"
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Invalid data received, ignoring resource")
|
try:
|
||||||
resource_status = "invalid_data"
|
filename = os.path.basename(resource.metadata["name"].decode("utf-8"))
|
||||||
|
counter = 0
|
||||||
|
if save_path:
|
||||||
|
saved_filename = os.path.abspath(os.path.expanduser(save_path+"/"+filename))
|
||||||
|
if not saved_filename.startswith(save_path+"/"):
|
||||||
|
print(f"Invalid save path {saved_filename}, ignoring")
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
saved_filename = filename
|
||||||
|
|
||||||
|
full_save_path = saved_filename
|
||||||
|
if allow_overwrite_on_receive:
|
||||||
|
if os.path.isfile(full_save_path):
|
||||||
|
try: os.unlink(full_save_path)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not overwrite existing file {full_save_path}, renaming instead")
|
||||||
|
|
||||||
|
while os.path.isfile(full_save_path):
|
||||||
|
counter += 1
|
||||||
|
full_save_path = saved_filename+"."+str(counter)
|
||||||
|
|
||||||
|
shutil.move(resource.data.name, full_save_path)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred while saving received resource: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Resource failed")
|
print("Resource failed")
|
||||||
|
|
@ -518,25 +546,25 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
print("Fetch request failed, fetching the file "+str(file)+" was not allowed by the remote")
|
print("Fetch request failed, fetching the file "+str(file)+" was not allowed by the remote")
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.15)
|
time.sleep(0.15)
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
elif request_status == "not_found":
|
elif request_status == "not_found":
|
||||||
if not silent: print(f"{erase_str}", end="")
|
if not silent: print(f"{erase_str}", end="")
|
||||||
print("Fetch request failed, the file "+str(file)+" was not found on the remote")
|
print("Fetch request failed, the file "+str(file)+" was not found on the remote")
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.15)
|
time.sleep(0.15)
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
elif request_status == "remote_error":
|
elif request_status == "remote_error":
|
||||||
if not silent: print(f"{erase_str}", end="")
|
if not silent: print(f"{erase_str}", end="")
|
||||||
print("Fetch request failed due to an error on the remote system")
|
print("Fetch request failed due to an error on the remote system")
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.15)
|
time.sleep(0.15)
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
elif request_status == "unknown":
|
elif request_status == "unknown":
|
||||||
if not silent: print(f"{erase_str}", end="")
|
if not silent: print(f"{erase_str}", end="")
|
||||||
print("Fetch request failed due to an unknown error (probably not authorised)")
|
print("Fetch request failed due to an unknown error (probably not authorised)")
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.15)
|
time.sleep(0.15)
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
elif request_status == "found":
|
elif request_status == "found":
|
||||||
if not silent: print(f"{erase_str}", end="")
|
if not silent: print(f"{erase_str}", end="")
|
||||||
|
|
||||||
|
|
@ -558,34 +586,38 @@ def fetch(configdir, verbosity = 0, quietness = 0, destination = None, file = No
|
||||||
if prg != 1.0:
|
if prg != 1.0:
|
||||||
print(f"{erase_str}Transferring file {syms[i]} {stat_str}", end=es)
|
print(f"{erase_str}Transferring file {syms[i]} {stat_str}", end=es)
|
||||||
else:
|
else:
|
||||||
|
end_time = time.time(); delta_time = end_time - current_transfer_started
|
||||||
|
speed = current_resource.total_size/delta_time; dt_str = RNS.prettytime(delta_time)
|
||||||
|
ss = size_str(speed, "b")
|
||||||
|
stat_str = f"{percent}% - {ps} of {ts} in {dt_str} - {ss}ps{phy_str}"
|
||||||
print(f"{erase_str}Transfer complete {stat_str}", end=es)
|
print(f"{erase_str}Transfer complete {stat_str}", end=es)
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}Waiting for transfer to start {syms[i]} ", end=es)
|
print(f"{erase_str}Waiting for transfer to start {syms[i]} ", end=es)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
i = (i+1)%len(syms)
|
i = (i+1)%len(syms)
|
||||||
|
|
||||||
if current_resource.status != RNS.Resource.COMPLETE:
|
if not current_resource or current_resource.status != RNS.Resource.COMPLETE:
|
||||||
if silent:
|
if silent:
|
||||||
print("The transfer failed")
|
print("The transfer failed")
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}The transfer failed")
|
print(f"{erase_str}The transfer failed")
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print(str(file)+" fetched from "+RNS.prettyhexrep(destination_hash))
|
print(str(file)+" fetched from "+RNS.prettyhexrep(destination_hash))
|
||||||
else:
|
else:
|
||||||
print("\n"+str(file)+" fetched from "+RNS.prettyhexrep(destination_hash))
|
print("\n"+str(file)+" fetched from "+RNS.prettyhexrep(destination_hash))
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.15)
|
time.sleep(0.1)
|
||||||
exit(0)
|
RNS.exit(0)
|
||||||
|
|
||||||
link.teardown()
|
link.teardown()
|
||||||
exit(0)
|
time.sleep(0.1)
|
||||||
|
RNS.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def send(configdir, verbosity = 0, quietness = 0, destination = None, file = None, timeout = RNS.Transport.PATH_REQUEST_TIMEOUT, silent=False, phy_rates=False, no_compress=False):
|
def send(configdir, identitypath = None, verbosity = 0, quietness = 0, destination = None, file = None, timeout = RNS.Transport.PATH_REQUEST_TIMEOUT, silent=False, phy_rates=False, no_compress=False):
|
||||||
global current_resource, resource_done, link, speed, show_phy_rates, phy_got_total, phy_speed
|
global current_resource, resource_done, link, speed, show_phy_rates, phy_got_total, phy_speed, identity
|
||||||
from tempfile import TemporaryFile
|
|
||||||
targetloglevel = 3+verbosity-quietness
|
targetloglevel = 3+verbosity-quietness
|
||||||
show_phy_rates = phy_rates
|
show_phy_rates = phy_rates
|
||||||
|
|
||||||
|
|
@ -599,47 +631,23 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
raise ValueError("Invalid destination entered. Check your input.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
|
|
||||||
|
|
||||||
file_path = os.path.expanduser(file)
|
file_path = os.path.expanduser(file)
|
||||||
if not os.path.isfile(file_path):
|
if not os.path.isfile(file_path):
|
||||||
print("File not found")
|
print("File not found")
|
||||||
exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
temp_file = TemporaryFile()
|
metadata = {"name": os.path.basename(file_path).encode("utf-8") }
|
||||||
real_file = open(file_path, "rb")
|
|
||||||
filename_bytes = os.path.basename(file_path).encode("utf-8")
|
|
||||||
filename_len = len(filename_bytes)
|
|
||||||
|
|
||||||
if filename_len > 0xFFFF:
|
|
||||||
print("Filename exceeds max size, cannot send")
|
|
||||||
exit(1)
|
|
||||||
else:
|
|
||||||
print("Preparing file...", end=es)
|
|
||||||
|
|
||||||
temp_file.write(filename_len.to_bytes(2, "big"))
|
|
||||||
temp_file.write(filename_bytes)
|
|
||||||
temp_file.write(real_file.read())
|
|
||||||
temp_file.seek(0)
|
|
||||||
|
|
||||||
print(f"{erase_str}", end="")
|
print(f"{erase_str}", end="")
|
||||||
|
|
||||||
reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
|
reticulum = RNS.Reticulum(configdir=configdir, loglevel=targetloglevel)
|
||||||
|
reticulum.wants_destination_type(f"{APP_NAME}.receive")
|
||||||
identity_path = RNS.Reticulum.identitypath+"/"+APP_NAME
|
|
||||||
if os.path.isfile(identity_path):
|
|
||||||
identity = RNS.Identity.from_file(identity_path)
|
|
||||||
if identity == None:
|
|
||||||
RNS.log("Could not load identity for rncp. The identity file at \""+str(identity_path)+"\" may be corrupt or unreadable.", RNS.LOG_ERROR)
|
|
||||||
exit(2)
|
|
||||||
else:
|
|
||||||
identity = None
|
|
||||||
|
|
||||||
if identity == None:
|
if identity == None:
|
||||||
RNS.log("No valid saved identity found, creating new...", RNS.LOG_INFO)
|
prepare_identity(identitypath)
|
||||||
identity = RNS.Identity()
|
|
||||||
identity.to_file(identity_path)
|
|
||||||
|
|
||||||
if not RNS.Transport.has_path(destination_hash):
|
if not RNS.Transport.has_path(destination_hash):
|
||||||
RNS.Transport.request_path(destination_hash)
|
RNS.Transport.request_path(destination_hash)
|
||||||
|
|
@ -651,7 +659,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
||||||
estab_timeout = time.time()+timeout
|
estab_timeout = time.time()+max(timeout, reticulum.get_medium_path_timeout())
|
||||||
while not RNS.Transport.has_path(destination_hash) and time.time() < estab_timeout:
|
while not RNS.Transport.has_path(destination_hash) and time.time() < estab_timeout:
|
||||||
if not silent:
|
if not silent:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
@ -664,7 +672,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
print("Path not found")
|
print("Path not found")
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}Path not found")
|
print(f"{erase_str}Path not found")
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print("Establishing link with "+RNS.prettyhexrep(destination_hash))
|
print("Establishing link with "+RNS.prettyhexrep(destination_hash))
|
||||||
|
|
@ -681,6 +689,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
)
|
)
|
||||||
|
|
||||||
link = RNS.Link(receiver_destination)
|
link = RNS.Link(receiver_destination)
|
||||||
|
estab_timeout = max(estab_timeout, time.time()+link.establishment_timeout)
|
||||||
while link.status != RNS.Link.ACTIVE and time.time() < estab_timeout:
|
while link.status != RNS.Link.ACTIVE and time.time() < estab_timeout:
|
||||||
if not silent:
|
if not silent:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
@ -693,13 +702,13 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
print("Link establishment with "+RNS.prettyhexrep(destination_hash)+" timed out")
|
print("Link establishment with "+RNS.prettyhexrep(destination_hash)+" timed out")
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}Link establishment with "+RNS.prettyhexrep(destination_hash)+" timed out")
|
print(f"{erase_str}Link establishment with "+RNS.prettyhexrep(destination_hash)+" timed out")
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
elif not RNS.Transport.has_path(destination_hash):
|
elif not RNS.Transport.has_path(destination_hash):
|
||||||
if silent:
|
if silent:
|
||||||
print("No path found to "+RNS.prettyhexrep(destination_hash))
|
print("No path found to "+RNS.prettyhexrep(destination_hash))
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}No path found to "+RNS.prettyhexrep(destination_hash))
|
print(f"{erase_str}No path found to "+RNS.prettyhexrep(destination_hash))
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print("Advertising file resource...")
|
print("Advertising file resource...")
|
||||||
|
|
@ -708,9 +717,12 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
|
|
||||||
link.identify(identity)
|
link.identify(identity)
|
||||||
auto_compress = True
|
auto_compress = True
|
||||||
if no_compress:
|
if no_compress: auto_compress = False
|
||||||
auto_compress = False
|
try: resource = RNS.Resource(open(file_path, "rb"), link, metadata=metadata, callback = sender_progress, progress_callback = sender_progress, auto_compress = auto_compress)
|
||||||
resource = RNS.Resource(temp_file, link, callback = sender_progress, progress_callback = sender_progress, auto_compress = auto_compress)
|
except Exception as e:
|
||||||
|
print(f"Could not start transfer: {e}")
|
||||||
|
RNS.exit(1)
|
||||||
|
|
||||||
current_resource = resource
|
current_resource = resource
|
||||||
|
|
||||||
while resource.status < RNS.Resource.TRANSFERRING:
|
while resource.status < RNS.Resource.TRANSFERRING:
|
||||||
|
|
@ -727,7 +739,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
print("File was not accepted by "+RNS.prettyhexrep(destination_hash))
|
print("File was not accepted by "+RNS.prettyhexrep(destination_hash))
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}File was not accepted by "+RNS.prettyhexrep(destination_hash))
|
print(f"{erase_str}File was not accepted by "+RNS.prettyhexrep(destination_hash))
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print("Transferring file...")
|
print("Transferring file...")
|
||||||
|
|
@ -773,7 +785,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
print("The transfer failed")
|
print("The transfer failed")
|
||||||
else:
|
else:
|
||||||
print(f"{erase_str}The transfer failed")
|
print(f"{erase_str}The transfer failed")
|
||||||
exit(1)
|
RNS.exit(1)
|
||||||
else:
|
else:
|
||||||
if silent:
|
if silent:
|
||||||
print(str(file_path)+" copied to "+RNS.prettyhexrep(destination_hash))
|
print(str(file_path)+" copied to "+RNS.prettyhexrep(destination_hash))
|
||||||
|
|
@ -781,9 +793,7 @@ def send(configdir, verbosity = 0, quietness = 0, destination = None, file = Non
|
||||||
print("\n"+str(file_path)+" copied to "+RNS.prettyhexrep(destination_hash))
|
print("\n"+str(file_path)+" copied to "+RNS.prettyhexrep(destination_hash))
|
||||||
link.teardown()
|
link.teardown()
|
||||||
time.sleep(0.25)
|
time.sleep(0.25)
|
||||||
real_file.close()
|
RNS.exit(0)
|
||||||
temp_file.close()
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
@ -800,10 +810,12 @@ def main():
|
||||||
parser.add_argument("-f", '--fetch', action='store_true', default=False, help="fetch file from remote listener instead of sending")
|
parser.add_argument("-f", '--fetch', action='store_true', default=False, help="fetch file from remote listener instead of sending")
|
||||||
parser.add_argument("-j", "--jail", metavar="path", action="store", default=None, help="restrict fetch requests to specified path", type=str)
|
parser.add_argument("-j", "--jail", metavar="path", action="store", default=None, help="restrict fetch requests to specified path", type=str)
|
||||||
parser.add_argument("-s", "--save", metavar="path", action="store", default=None, help="save received files in specified path", type=str)
|
parser.add_argument("-s", "--save", metavar="path", action="store", default=None, help="save received files in specified path", type=str)
|
||||||
|
parser.add_argument('-O', '--overwrite', action='store_true', default=False, help="Allow overwriting received files, instead of adding postfix")
|
||||||
parser.add_argument("-b", action='store', metavar="seconds", default=-1, help="announce interval, 0 to only announce at startup", type=int)
|
parser.add_argument("-b", action='store', metavar="seconds", default=-1, help="announce interval, 0 to only announce at startup", type=int)
|
||||||
parser.add_argument('-a', metavar="allowed_hash", dest="allowed", action='append', help="allow this identity (or add in ~/.rncp/allowed_identities)", type=str)
|
parser.add_argument('-a', metavar="allowed_hash", dest="allowed", action='append', help="allow this identity (or add in ~/.rncp/allowed_identities)", type=str)
|
||||||
parser.add_argument('-n', '--no-auth', action='store_true', default=False, help="accept requests from anyone")
|
parser.add_argument('-n', '--no-auth', action='store_true', default=False, help="accept requests from anyone")
|
||||||
parser.add_argument('-p', '--print-identity', action='store_true', default=False, help="print identity and destination info and exit")
|
parser.add_argument('-p', '--print-identity', action='store_true', default=False, help="print identity and destination info and exit")
|
||||||
|
parser.add_argument('-i', metavar="identity", action='store', dest="identity", default=None, help="path to identity to use", type=str)
|
||||||
parser.add_argument("-w", action="store", metavar="seconds", type=float, help="sender timeout before giving up", default=RNS.Transport.PATH_REQUEST_TIMEOUT)
|
parser.add_argument("-w", action="store", metavar="seconds", type=float, help="sender timeout before giving up", default=RNS.Transport.PATH_REQUEST_TIMEOUT)
|
||||||
parser.add_argument('-P', '--phy-rates', action='store_true', default=False, help="display physical layer transfer rates")
|
parser.add_argument('-P', '--phy-rates', action='store_true', default=False, help="display physical layer transfer rates")
|
||||||
# parser.add_argument("--limit", action="store", metavar="files", type=float, help="maximum number of files to accept", default=None)
|
# parser.add_argument("--limit", action="store", metavar="files", type=float, help="maximum number of files to accept", default=None)
|
||||||
|
|
@ -814,22 +826,26 @@ def main():
|
||||||
if args.listen or args.print_identity:
|
if args.listen or args.print_identity:
|
||||||
listen(
|
listen(
|
||||||
configdir = args.config,
|
configdir = args.config,
|
||||||
|
identitypath = args.identity,
|
||||||
verbosity=args.verbose,
|
verbosity=args.verbose,
|
||||||
quietness=args.quiet,
|
quietness=args.quiet,
|
||||||
allowed = args.allowed,
|
allowed = args.allowed,
|
||||||
fetch_allowed = args.allow_fetch,
|
fetch_allowed = args.allow_fetch,
|
||||||
|
no_compress = args.no_compress,
|
||||||
jail = args.jail,
|
jail = args.jail,
|
||||||
save = args.save,
|
save = args.save,
|
||||||
display_identity=args.print_identity,
|
display_identity=args.print_identity,
|
||||||
# limit=args.limit,
|
# limit=args.limit,
|
||||||
disable_auth=args.no_auth,
|
disable_auth=args.no_auth,
|
||||||
announce=args.b,
|
announce=args.b,
|
||||||
|
allow_overwrite=args.overwrite,
|
||||||
)
|
)
|
||||||
|
|
||||||
elif args.fetch:
|
elif args.fetch:
|
||||||
if args.destination != None and args.file != None:
|
if args.destination != None and args.file != None:
|
||||||
fetch(
|
fetch(
|
||||||
configdir = args.config,
|
configdir = args.config,
|
||||||
|
identitypath = args.identity,
|
||||||
verbosity = args.verbose,
|
verbosity = args.verbose,
|
||||||
quietness = args.quiet,
|
quietness = args.quiet,
|
||||||
destination = args.destination,
|
destination = args.destination,
|
||||||
|
|
@ -838,6 +854,7 @@ def main():
|
||||||
silent = args.silent,
|
silent = args.silent,
|
||||||
phy_rates = args.phy_rates,
|
phy_rates = args.phy_rates,
|
||||||
save = args.save,
|
save = args.save,
|
||||||
|
allow_overwrite=args.overwrite,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -847,6 +864,7 @@ def main():
|
||||||
elif args.destination != None and args.file != None:
|
elif args.destination != None and args.file != None:
|
||||||
send(
|
send(
|
||||||
configdir = args.config,
|
configdir = args.config,
|
||||||
|
identitypath = args.identity,
|
||||||
verbosity = args.verbose,
|
verbosity = args.verbose,
|
||||||
quietness = args.quiet,
|
quietness = args.quiet,
|
||||||
destination = args.destination,
|
destination = args.destination,
|
||||||
|
|
@ -864,11 +882,11 @@ def main():
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("")
|
print("")
|
||||||
if resource != None:
|
if current_resource != None:
|
||||||
resource.cancel()
|
current_resource.cancel()
|
||||||
if link != None:
|
if link != None:
|
||||||
link.teardown()
|
link.teardown()
|
||||||
exit()
|
RNS.exit()
|
||||||
|
|
||||||
def size_str(num, suffix='B'):
|
def size_str(num, suffix='B'):
|
||||||
units = ['','K','M','G','T','P','E','Z']
|
units = ['','K','M','G','T','P','E','Z']
|
||||||
|
|
|
||||||
41
RNS/Utilities/rngit/__init__.py
Normal file
41
RNS/Utilities/rngit/__init__.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
APP_NAME = "git"
|
||||||
|
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
|
||||||
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
720
RNS/Utilities/rngit/client.py
Normal file
720
RNS/Utilities/rngit/client.py
Normal file
|
|
@ -0,0 +1,720 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import shutil
|
||||||
|
import threading
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from RNS._version import __version__
|
||||||
|
from RNS.Utilities.rngit import APP_NAME
|
||||||
|
from RNS.Utilities.rngit.util import medium_path_timeout
|
||||||
|
|
||||||
|
from RNS.vendor.configobj import ConfigObj
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
def program_setup(configdir, rnsconfigdir, destination_hexhash, group_name, repo_name):
|
||||||
|
git_client = ReticulumGitClient(configdir=configdir, rnsconfigdir=rnsconfigdir, destination_hexhash=destination_hexhash,
|
||||||
|
group_name=group_name, repo_name=repo_name)
|
||||||
|
|
||||||
|
if not git_client.ready: sys.exit(1)
|
||||||
|
else: git_client.run()
|
||||||
|
|
||||||
|
def main():
|
||||||
|
if len(sys.argv) < 3:
|
||||||
|
print("Usage: git-remote-rns <remote-name> <url>", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
url = sys.argv[2]
|
||||||
|
if not url.startswith("rns://"):
|
||||||
|
print("Invalid URL scheme. Must be rns://", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
parts = url[6:].split("/", 2)
|
||||||
|
destination_hexhash = parts[0]
|
||||||
|
group_name = parts[1]
|
||||||
|
repo_name = parts[2]
|
||||||
|
|
||||||
|
except IndexError: print("Invalid URL format. Use rns://<hash>/<group>/<repo>", file=sys.stderr); sys.exit(1)
|
||||||
|
|
||||||
|
configdir = os.environ.get("RNGIT_CONFIG", None)
|
||||||
|
rnsconfigdir = os.environ.get("RNS_CONFIG", None)
|
||||||
|
|
||||||
|
program_setup(configdir, rnsconfigdir, destination_hexhash, group_name, repo_name)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
class ReticulumGitClient():
|
||||||
|
PATH_LIST = "/git/list"
|
||||||
|
PATH_FETCH = "/git/fetch"
|
||||||
|
PATH_PUSH = "/git/push"
|
||||||
|
PATH_DELETE = "/git/delete"
|
||||||
|
|
||||||
|
RES_DISALLOWED = 0x01
|
||||||
|
RES_INVALID_REQ = 0x02
|
||||||
|
RES_NOT_FOUND = 0x03
|
||||||
|
RES_REMOTE_FAIL = 0xFF
|
||||||
|
|
||||||
|
IDX_REPOSITORY = 0x00
|
||||||
|
IDX_RESULT_CODE = 0x01
|
||||||
|
|
||||||
|
REF_BATCH_SIZE = 25
|
||||||
|
PATH_TIMEOUT = 15
|
||||||
|
LINK_TIMEOUT = 15
|
||||||
|
|
||||||
|
def __init__(self, configdir, rnsconfigdir, destination_hexhash, group_name, repo_name):
|
||||||
|
# Client state and configuration
|
||||||
|
self.identity = None
|
||||||
|
self.userdir = os.path.expanduser("~")
|
||||||
|
self.config = None
|
||||||
|
self.ready = False
|
||||||
|
|
||||||
|
self.destination_aliases = {}
|
||||||
|
self.remote_identity = None
|
||||||
|
self.destination = None
|
||||||
|
self.link = None
|
||||||
|
self.link_ready = False
|
||||||
|
self.link_failed = False
|
||||||
|
self.link_timeout = self.LINK_TIMEOUT
|
||||||
|
self.path_timeout = self.PATH_TIMEOUT
|
||||||
|
|
||||||
|
self.destination_hexhash = destination_hexhash
|
||||||
|
self.group_name = group_name
|
||||||
|
self.repo_name = repo_name
|
||||||
|
self.repo_path = f"{group_name}/{repo_name}"
|
||||||
|
|
||||||
|
self.tmp_dir = TemporaryDirectory()
|
||||||
|
self.request_event = threading.Event()
|
||||||
|
self.request_response = None
|
||||||
|
self.response_metadata = None
|
||||||
|
|
||||||
|
self.ref_batch_size = self.REF_BATCH_SIZE
|
||||||
|
self.remote_refs = {}
|
||||||
|
|
||||||
|
self.response_progress = 0
|
||||||
|
self.previous_progress = 0
|
||||||
|
self.response_size = None
|
||||||
|
self.response_transfer_size = None
|
||||||
|
self.progress_updated_at = None
|
||||||
|
self.progress_enabled = False
|
||||||
|
|
||||||
|
if configdir != None: self.configdir = configdir
|
||||||
|
else:
|
||||||
|
if os.path.isdir(self.userdir+"/.config/rngit") and os.path.isfile(self.userdir+"/.config/rngit/config"): self.configdir = self.userdir+"/.rngit/reticulum"
|
||||||
|
else: self.configdir = self.userdir+"/.rngit"
|
||||||
|
|
||||||
|
self.logfile = self.configdir+"/client_log"
|
||||||
|
self.configpath = self.configdir+"/client_config"
|
||||||
|
self.identitypath = self.configdir+"/client_identity"
|
||||||
|
|
||||||
|
if os.path.isfile(self.configpath):
|
||||||
|
try: self.config = ConfigObj(self.configpath)
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log("Could not parse the configuration at "+self.configpath, RNS.LOG_ERROR)
|
||||||
|
return
|
||||||
|
|
||||||
|
else: self.__create_default_config()
|
||||||
|
|
||||||
|
RNS.logfile = self.logfile
|
||||||
|
try: self.reticulum = RNS.Reticulum(configdir=rnsconfigdir, logdest=RNS.LOG_FILE)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed to initialize Reticulum: {e}", file=sys.stderr)
|
||||||
|
return
|
||||||
|
|
||||||
|
self.reticulum.wants_destination_type(f"{APP_NAME}.repositories")
|
||||||
|
|
||||||
|
self.__apply_config()
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
def __create_default_config(self):
|
||||||
|
self.config = ConfigObj(__default_rngit_config__)
|
||||||
|
self.config.filename = self.configpath
|
||||||
|
if not os.path.isdir(self.configdir): os.makedirs(self.configdir)
|
||||||
|
self.config.write()
|
||||||
|
|
||||||
|
def __apply_config(self):
|
||||||
|
if "logging" in self.config:
|
||||||
|
section = self.config["logging"]
|
||||||
|
if "loglevel" in section: RNS.loglevel = max(RNS.LOG_NONE, min(RNS.LOG_EXTREME, section.as_int("loglevel")))
|
||||||
|
|
||||||
|
if "client" in self.config:
|
||||||
|
section = self.config["client"]
|
||||||
|
if "ref_batch_size" in section: self.ref_batch_size = max(0, min(1024, section.as_int("ref_batch_size")))
|
||||||
|
|
||||||
|
if "aliases" in self.config:
|
||||||
|
section = self.config["aliases"]
|
||||||
|
for alias in section:
|
||||||
|
alias_hexhash = section[alias]
|
||||||
|
len_ok = len(alias_hexhash) == RNS.Identity.TRUNCATED_HASHLENGTH//8*2
|
||||||
|
try: alias_hash = bytes.fromhex(alias_hexhash)
|
||||||
|
except: alias_hash = None
|
||||||
|
alias_exists = alias in self.destination_aliases
|
||||||
|
if not len_ok or not alias_hash: continue
|
||||||
|
if alias_exists: continue
|
||||||
|
self.destination_aliases[alias] = RNS.hexrep(alias_hash, delimit=False)
|
||||||
|
|
||||||
|
if not os.path.isfile(self.identitypath):
|
||||||
|
identity = RNS.Identity()
|
||||||
|
identity.to_file(self.identitypath)
|
||||||
|
RNS.log(f"Client identity generated and persisted to {self.identitypath}", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
|
else:
|
||||||
|
identity = RNS.Identity.from_file(self.identitypath)
|
||||||
|
RNS.log(f"Client identity loaded from {self.identitypath}", RNS.LOG_VERBOSE)
|
||||||
|
|
||||||
|
if not identity:
|
||||||
|
RNS.log("Could not initialize client identity.", RNS.LOG_ERROR)
|
||||||
|
self.ready = False
|
||||||
|
|
||||||
|
else: self.identity = identity
|
||||||
|
|
||||||
|
self.destination_hexhash = self.__resolve_destination_alias(self.destination_hexhash)
|
||||||
|
|
||||||
|
def __resolve_destination_alias(self, alias):
|
||||||
|
def resolve(alias):
|
||||||
|
len_match = len(alias) == RNS.Identity.TRUNCATED_HASHLENGTH//8*2
|
||||||
|
try: hash_bytes = bytes.fromhex(alias)
|
||||||
|
except: hash_bytes = None
|
||||||
|
if len_match and hash_bytes: return alias
|
||||||
|
else: return self.destination_aliases[alias] if alias in self.destination_aliases else alias
|
||||||
|
|
||||||
|
resolved = resolve(alias)
|
||||||
|
return resolved
|
||||||
|
|
||||||
|
def abort(self, reason=None, code=255):
|
||||||
|
if not reason: reason = "Unknown reason"
|
||||||
|
print(f"git-remote-rns failed: {reason}", file=sys.stderr)
|
||||||
|
if self.link: self.link.teardown()
|
||||||
|
sys.exit(code)
|
||||||
|
|
||||||
|
def connect_server(self):
|
||||||
|
try: destination_hash = bytes.fromhex(self.destination_hexhash)
|
||||||
|
except Exception as e: self.abort(f"Invalid destination hash: {e}")
|
||||||
|
|
||||||
|
RNS.log(f"Requesting path to {RNS.prettyhexrep(destination_hash)}", RNS.LOG_DEBUG)
|
||||||
|
sys.stderr.write(f"Requesting path..."); sys.stderr.flush()
|
||||||
|
if not RNS.Transport.await_path(destination_hash, timeout=medium_path_timeout(self.path_timeout)):
|
||||||
|
sys.stderr.write(f"\n"); sys.stderr.flush()
|
||||||
|
self.abort(f"Could not resolve path to {RNS.prettyhexrep(destination_hash)}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
RNS.log(f"Path to {RNS.prettyhexrep(destination_hash)} resolved", RNS.LOG_DEBUG);
|
||||||
|
sys.stderr.write(f"\rPath resolved "); sys.stderr.flush()
|
||||||
|
|
||||||
|
self.remote_identity = RNS.Identity.recall(destination_hash)
|
||||||
|
if not self.remote_identity: self.abort("Could not recall remote identity. Is the server announcing?")
|
||||||
|
|
||||||
|
sys.stderr.write(f"\rEstablishing link..."); sys.stderr.flush()
|
||||||
|
self.destination = RNS.Destination(self.remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, APP_NAME, "repositories")
|
||||||
|
self.link = RNS.Link(self.destination)
|
||||||
|
self.link.set_link_established_callback(self.link_established)
|
||||||
|
self.link.set_link_closed_callback(self.link_closed)
|
||||||
|
|
||||||
|
def link_established(self, link):
|
||||||
|
RNS.log(f"Link established, identifying...", RNS.LOG_DEBUG)
|
||||||
|
sys.stderr.write(f"\rLink established with remote\n"); sys.stderr.flush()
|
||||||
|
link.identify(self.identity)
|
||||||
|
self.link_ready = True
|
||||||
|
|
||||||
|
def link_closed(self, link):
|
||||||
|
RNS.log(f"Link was closed", RNS.LOG_DEBUG)
|
||||||
|
if not self.link_ready: self.link_failed = True
|
||||||
|
|
||||||
|
def _on_progress(self, transfer_instance):
|
||||||
|
if hasattr(transfer_instance, "progress"):
|
||||||
|
self.response_progress = transfer_instance.progress
|
||||||
|
self.response_size = transfer_instance.response_size
|
||||||
|
self.response_transfer_size = transfer_instance.response_transfer_size
|
||||||
|
|
||||||
|
elif hasattr(transfer_instance, "get_progress") and callable(transfer_instance.get_progress):
|
||||||
|
self.response_progress = transfer_instance.get_progress()
|
||||||
|
self.response_size = transfer_instance.total_size
|
||||||
|
self.response_transfer_size = transfer_instance.size
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
|
if self.progress_updated_at == None: self.progress_updated_at = now
|
||||||
|
|
||||||
|
if now > self.progress_updated_at+1:
|
||||||
|
td = now - self.progress_updated_at
|
||||||
|
pd = self.response_progress - self.previous_progress
|
||||||
|
bd = pd*self.response_size if self.response_size else 0
|
||||||
|
self.response_speed = (bd/td)*8 if td > 0 else 0
|
||||||
|
self.previous_progress = self.response_progress
|
||||||
|
self.progress_updated_at = now
|
||||||
|
|
||||||
|
# Report progress to git via stderr
|
||||||
|
if self.progress_enabled and self.response_size:
|
||||||
|
percent = round(self.response_progress * 100, 1)
|
||||||
|
size = self.response_size
|
||||||
|
rxd = size*self.response_progress
|
||||||
|
speed_kbps = (self.response_speed / 1000) if hasattr(self, 'response_speed') else 0
|
||||||
|
sys.stderr.write(f"Transferring: {percent}% ({RNS.prettysize(rxd)}/{RNS.prettysize(size)}) {RNS.prettyspeed(self.response_speed)} \r")
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
|
################################
|
||||||
|
# Synchronous Request Wrappers #
|
||||||
|
################################
|
||||||
|
|
||||||
|
def _response_ready(self, request_receipt):
|
||||||
|
self.request_response = request_receipt.response
|
||||||
|
self.response_metadata = request_receipt.metadata
|
||||||
|
|
||||||
|
if hasattr(self.request_response, "read") and callable(self.request_response.read):
|
||||||
|
response_path = self.request_response.name
|
||||||
|
try: self.request_response.close()
|
||||||
|
except Exception as e: RNS.log(f"Could not close resource file descriptor before retain: {e}", RNS.LOG_ERROR)
|
||||||
|
base_name = os.path.basename(response_path)
|
||||||
|
retained_path = os.path.join(self.tmp_dir.name, base_name)
|
||||||
|
shutil.move(response_path, retained_path)
|
||||||
|
self.request_response = open(retained_path, "rb")
|
||||||
|
|
||||||
|
self.request_event.set()
|
||||||
|
|
||||||
|
def _response_failed(self, request_receipt=None):
|
||||||
|
self.request_response = None
|
||||||
|
self.request_event.set()
|
||||||
|
|
||||||
|
def send_request(self, path, data, timeout=7200):
|
||||||
|
if not self.link_ready: self.abort("Link not ready for request")
|
||||||
|
|
||||||
|
self.request_event.clear()
|
||||||
|
self.request_response = None
|
||||||
|
self.response_metadata = None
|
||||||
|
self.previous_progress = 0
|
||||||
|
self.progress_updated_at = None
|
||||||
|
|
||||||
|
RNS.log(f"Sending request: {path}", RNS.LOG_DEBUG)
|
||||||
|
request_receipt = self.link.request(path, data, progress_callback=self._on_progress, response_callback=self._response_ready, failed_callback=self._response_failed, timeout=timeout)
|
||||||
|
if request_receipt.resource: request_receipt.resource.progress_callback(self._on_progress)
|
||||||
|
self.request_event.wait(timeout=timeout)
|
||||||
|
|
||||||
|
if self.request_response is None: self.abort("Request failed or timed out")
|
||||||
|
RNS.log(f"Got response for: {path}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
return self.request_response, self.response_metadata
|
||||||
|
|
||||||
|
#############################
|
||||||
|
# Git Helper Protocol Logic #
|
||||||
|
#############################
|
||||||
|
|
||||||
|
def _detach_stdout(self):
|
||||||
|
sys.stdout = open(os.devnull, "w")
|
||||||
|
sys.stderr = open(os.devnull, "w")
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
try: self.connect_server()
|
||||||
|
except Exception as e: self.abort(str(e))
|
||||||
|
|
||||||
|
timeout = max(self.link_timeout, self.link.establishment_timeout)
|
||||||
|
while not self.link_ready and not self.link_failed and timeout > 0:
|
||||||
|
time.sleep(0.5)
|
||||||
|
timeout -= 0.5
|
||||||
|
|
||||||
|
if not self.link_ready: self.abort("Failed to establish link")
|
||||||
|
|
||||||
|
self.progress_enabled = False
|
||||||
|
|
||||||
|
git_stdin = sys.stdin
|
||||||
|
git_stdout = sys.stdout
|
||||||
|
git_stderr = sys.stderr
|
||||||
|
|
||||||
|
fetch_queue = []
|
||||||
|
push_queue = []
|
||||||
|
|
||||||
|
while True:
|
||||||
|
line = git_stdin.readline()
|
||||||
|
if not line: break
|
||||||
|
|
||||||
|
line = line.strip()
|
||||||
|
if line == "capabilities":
|
||||||
|
git_stdout.write("list\n")
|
||||||
|
git_stdout.write("fetch\n")
|
||||||
|
git_stdout.write("push\n")
|
||||||
|
git_stdout.write("option\n")
|
||||||
|
git_stdout.write("\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
|
||||||
|
elif line == "list": self.handle_git_list(git_stdout)
|
||||||
|
|
||||||
|
elif line.startswith("list "): self.handle_git_list(git_stdout, for_push=True) # List for push
|
||||||
|
|
||||||
|
elif line.startswith("option"):
|
||||||
|
# Line format: option <name> <value>
|
||||||
|
parts = line.split(maxsplit=2)
|
||||||
|
opt_name = parts[1] if len(parts) > 1 else ""
|
||||||
|
opt_value = parts[2] if len(parts) > 2 else ""
|
||||||
|
|
||||||
|
if opt_name == "progress": self.progress_enabled = opt_value.lower() in ("true", "1", "yes"); git_stdout.write("ok\n")
|
||||||
|
else: git_stdout.write("unsupported\n")
|
||||||
|
|
||||||
|
git_stdout.flush()
|
||||||
|
|
||||||
|
elif line.startswith("fetch"):
|
||||||
|
# Line format: fetch <sha> <ref>
|
||||||
|
parts = line.split()
|
||||||
|
sha = parts[1]
|
||||||
|
ref = parts[2]
|
||||||
|
# Avoid duplicates in the same batch - TODO: Re-evaluate this
|
||||||
|
if (sha, ref) not in fetch_queue: fetch_queue.append((sha, ref))
|
||||||
|
push_queue = []
|
||||||
|
|
||||||
|
elif line.startswith("push"):
|
||||||
|
# Line format: push <local_ref>:<remote_ref>
|
||||||
|
parts = line.split()
|
||||||
|
refspec = parts[1]
|
||||||
|
local_ref, remote_ref = refspec.split(":", 1)
|
||||||
|
push_queue.append((local_ref, remote_ref))
|
||||||
|
fetch_queue = []
|
||||||
|
|
||||||
|
elif line == "": # End of batch
|
||||||
|
try:
|
||||||
|
self.process_fetch_queue(fetch_queue, git_stdout, self.progress_enabled, self.ref_batch_size)
|
||||||
|
self.process_push_queue(push_queue, git_stdout, git_stderr, self.progress_enabled)
|
||||||
|
fetch_queue = []
|
||||||
|
push_queue = []
|
||||||
|
git_stdout.write("\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
|
||||||
|
except BrokenPipeError:
|
||||||
|
self._detach_stdout()
|
||||||
|
RNS.log("Git closed connection, exiting", RNS.LOG_DEBUG)
|
||||||
|
break
|
||||||
|
|
||||||
|
else: self.abort(f"Unknown Git command: {line}")
|
||||||
|
|
||||||
|
try: sys.stdout.flush()
|
||||||
|
except BrokenPipeError: pass
|
||||||
|
|
||||||
|
if self.link: self.link.teardown()
|
||||||
|
|
||||||
|
def handle_git_list(self, git_stdout, for_push=False):
|
||||||
|
RNS.log("Handle git list" + (" for-push" if for_push else ""), RNS.LOG_DEBUG)
|
||||||
|
request_data = {self.IDX_REPOSITORY: self.repo_path, "for_push": for_push}
|
||||||
|
response, metadata = self.send_request(self.PATH_LIST, request_data)
|
||||||
|
|
||||||
|
if not response or not isinstance(response, bytes): self.abort("Invalid list response from server")
|
||||||
|
|
||||||
|
status_byte = response[0]
|
||||||
|
payload = response[1:]
|
||||||
|
|
||||||
|
if status_byte != 0: self.abort(f"Server refused list: {payload.decode('utf-8', errors='ignore')}")
|
||||||
|
|
||||||
|
response_text = payload.decode("utf-8")
|
||||||
|
|
||||||
|
self.remote_refs = {}
|
||||||
|
for line in response_text.split("\n"):
|
||||||
|
line = line.strip()
|
||||||
|
if not line: continue
|
||||||
|
parts = line.split(" ", 1)
|
||||||
|
if len(parts) == 2:
|
||||||
|
sha, ref_name = parts
|
||||||
|
if ref_name == "HEAD": continue
|
||||||
|
self.remote_refs[ref_name] = sha
|
||||||
|
|
||||||
|
git_stdout.write(response_text)
|
||||||
|
git_stdout.write("\n") # Required to terminate list
|
||||||
|
git_stdout.flush()
|
||||||
|
|
||||||
|
def escape_for_stdout(self, value):
|
||||||
|
if isinstance(value, bytes): value = value.decode('utf-8', errors='replace')
|
||||||
|
|
||||||
|
escaped = '"'
|
||||||
|
for char in value:
|
||||||
|
if char == '\\': escaped += '\\\\'
|
||||||
|
elif char == '"': escaped += '\\"'
|
||||||
|
elif char == '\n': escaped += '\\n'
|
||||||
|
elif char == '\t': escaped += '\\t'
|
||||||
|
elif char == '\r': escaped += '\\r'
|
||||||
|
elif ord(char) < 32 or ord(char) > 126: escaped += f'\\x{ord(char):02x}'
|
||||||
|
else: escaped += char
|
||||||
|
|
||||||
|
return escaped + '"'
|
||||||
|
|
||||||
|
def process_fetch_queue(self, fetch_queue, git_stdout, progress_enabled=False, ref_batch_size=REF_BATCH_SIZE):
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
if not fetch_queue: return
|
||||||
|
|
||||||
|
# Build a global have list from all remote refs that the client already has objects for
|
||||||
|
have_shas = []
|
||||||
|
for sha in self.remote_refs.values():
|
||||||
|
try:
|
||||||
|
result = subprocess.run(["git", "cat-file", "-t", sha], capture_output=True, check=False)
|
||||||
|
if result.returncode == 0: have_shas.append(sha)
|
||||||
|
|
||||||
|
except Exception as e: RNS.log(f"Could not verify remote SHA {sha} locally: {e}", RNS.LOG_WARNING)
|
||||||
|
|
||||||
|
while fetch_queue:
|
||||||
|
batch = fetch_queue[:ref_batch_size]
|
||||||
|
fetch_queue = fetch_queue[ref_batch_size:]
|
||||||
|
|
||||||
|
refs_list = []
|
||||||
|
for sha, ref in batch:
|
||||||
|
ref_entry = {"sha": sha, "ref": ref}
|
||||||
|
try:
|
||||||
|
# Attempt to get local ref SHA for incremental bundle generation on remote
|
||||||
|
result = subprocess.run(["git", "rev-parse", ref], capture_output=True, text=True, check=False)
|
||||||
|
if result.returncode == 0:
|
||||||
|
local_sha = result.stdout.strip()
|
||||||
|
if local_sha != sha: ref_entry["have"] = local_sha
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not resolve local SHA for {ref} during fetch enumeration, getting full history for this ref: {e}", RNS.LOG_WARNING)
|
||||||
|
|
||||||
|
refs_list.append(ref_entry)
|
||||||
|
|
||||||
|
ref_names = [ref for _, ref in batch]
|
||||||
|
RNS.log(f"Fetching batch of {len(refs_list)} refs: {ref_names} (have {len(have_shas)} common objects)", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
request_data = { self.IDX_REPOSITORY: self.repo_path, "refs": refs_list }
|
||||||
|
if have_shas: request_data["have"] = have_shas
|
||||||
|
|
||||||
|
response, metadata = self.send_request(self.PATH_FETCH, request_data)
|
||||||
|
|
||||||
|
if not response: self.abort(f"No data in fetch response for batch")
|
||||||
|
if not metadata:
|
||||||
|
if not isinstance(response, bytes): self.abort(f"Invalid fetch response for batch")
|
||||||
|
status_byte = response[0]
|
||||||
|
|
||||||
|
if status_byte == 0:
|
||||||
|
RNS.log(f"Server returned empty bundle, all objects already exist locally", RNS.LOG_DEBUG)
|
||||||
|
continue
|
||||||
|
|
||||||
|
else:
|
||||||
|
error_msg = response[1:].decode('utf-8', errors='ignore')
|
||||||
|
self.abort(f"Fetch failed for batch: {error_msg}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
if not self.IDX_RESULT_CODE in metadata: self.abort(f"No result metadata on bundle response")
|
||||||
|
status_byte = metadata[self.IDX_RESULT_CODE]
|
||||||
|
if status_byte == 0: bundle_path = response.name
|
||||||
|
else: self.abort(f"Unknown remote state for batch ref fetch")
|
||||||
|
|
||||||
|
if progress_enabled:
|
||||||
|
size = os.stat(bundle_path).st_size
|
||||||
|
sys.stderr.write(f"Transferring: 100% ({RNS.prettysize(size)}). \n")
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
|
stderr_arg = sys.stderr if progress_enabled else subprocess.DEVNULL
|
||||||
|
|
||||||
|
verify_cmd = ["git", "bundle", "verify", "-q", bundle_path]
|
||||||
|
verify_result = subprocess.run(verify_cmd, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
if verify_result.returncode != 0: self.abort(f"Bundle verification failed for batch")
|
||||||
|
|
||||||
|
unbundle_cmd = ["git", "bundle", "unbundle"]
|
||||||
|
if progress_enabled: unbundle_cmd.append("--progress")
|
||||||
|
unbundle_cmd.append(bundle_path)
|
||||||
|
|
||||||
|
unbundle_result = subprocess.run(unbundle_cmd, stderr=stderr_arg, stdout=subprocess.DEVNULL)
|
||||||
|
|
||||||
|
if unbundle_result.returncode != 0: self.abort(f"Bundle unbundle failed for batch: Non-zero return code")
|
||||||
|
|
||||||
|
def process_push_queue(self, push_queue, git_stdout, git_stderr, progress_enabled=False):
|
||||||
|
import tempfile
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
for local_ref, remote_ref in push_queue:
|
||||||
|
RNS.log(f"Pushing {local_ref} to {remote_ref}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
# Handle potential deletions
|
||||||
|
if not local_ref or local_ref == "":
|
||||||
|
request_data = { self.IDX_REPOSITORY: self.repo_path, "ref": remote_ref }
|
||||||
|
response, metadata = self.send_request(self.PATH_DELETE, request_data)
|
||||||
|
|
||||||
|
if not response or not isinstance(response, bytes):
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout('No response from server')}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
status_byte = response[0]
|
||||||
|
if status_byte != 0:
|
||||||
|
error_msg = response[1:].decode("utf-8", errors="ignore")
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout(error_msg)}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
git_stdout.write(f"ok {remote_ref}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
force = local_ref.startswith("+")
|
||||||
|
if force: local_ref = local_ref[1:]
|
||||||
|
|
||||||
|
stderr_arg = sys.stderr if progress_enabled else subprocess.DEVNULL
|
||||||
|
|
||||||
|
# Resolve the SHA that local_ref points to
|
||||||
|
sha_result = subprocess.run(["git", "rev-parse", local_ref], capture_output=True, text=True, check=False)
|
||||||
|
if sha_result.returncode != 0:
|
||||||
|
error_msg = f"Could not resolve local ref {local_ref}"
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout(error_msg)}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
local_sha = sha_result.stdout.strip()
|
||||||
|
|
||||||
|
bundle_empty = False
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
bundle_path = tmpdir + "/push.bundle"
|
||||||
|
|
||||||
|
create_cmd = ["git", "bundle", "create", bundle_path, local_ref]
|
||||||
|
|
||||||
|
# Exclude all remote ref SHAs that exist locally, so the
|
||||||
|
# bundle only contains objects the remote doesn't already have
|
||||||
|
exclude_count = 0
|
||||||
|
for sha in self.remote_refs.values():
|
||||||
|
try:
|
||||||
|
# We need to verify each SHA actually exists locally, since git
|
||||||
|
# bundle create will fail if a ^<sha> argument references an object
|
||||||
|
# not present in the local repository.
|
||||||
|
result = subprocess.run(["git", "cat-file", "-t", sha], capture_output=True, check=False)
|
||||||
|
if result.returncode == 0:
|
||||||
|
create_cmd.append(f"^{sha}")
|
||||||
|
exclude_count += 1
|
||||||
|
|
||||||
|
except Exception as e: RNS.log(f"Could not verify remote SHA {sha} locally: {e}", RNS.LOG_WARNING)
|
||||||
|
|
||||||
|
RNS.log(f"Excluding {exclude_count}/{len(self.remote_refs)} remote refs for {local_ref}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
if progress_enabled: create_cmd.insert(3, "--progress")
|
||||||
|
|
||||||
|
create_result = subprocess.run(create_cmd, capture_output=True, text=True, check=False)
|
||||||
|
|
||||||
|
if create_result.returncode == 0:
|
||||||
|
if create_result.stderr:
|
||||||
|
# git_stderr.write(create_result.stderr)
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
if "empty bundle" in create_result.stderr.lower():
|
||||||
|
# All objects reachable from local_ref already exist on
|
||||||
|
# the remote. In this case, no bundle is needed and we can
|
||||||
|
# update the ref directly via the operations path instead.
|
||||||
|
bundle_empty = True
|
||||||
|
RNS.log(f"Empty bundle for {local_ref}, all objects already on remote", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if progress_enabled and create_result.stderr: git_stderr.write(create_result.stderr)
|
||||||
|
error_msg = "Bundle creation failed"
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout(error_msg)}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not bundle_empty:
|
||||||
|
with open(bundle_path, "rb") as f: bundle_data = f.read()
|
||||||
|
|
||||||
|
request_data = { self.IDX_REPOSITORY: self.repo_path, "local_ref": local_ref, "remote_ref": remote_ref,
|
||||||
|
"force": force, "bundle": bundle_data }
|
||||||
|
|
||||||
|
response, metadata = self.send_request(self.PATH_PUSH, request_data)
|
||||||
|
|
||||||
|
if not response or not isinstance(response, bytes):
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout('No response from server')}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
status_byte = response[0]
|
||||||
|
if status_byte != 0:
|
||||||
|
error_msg = response[1:].decode('utf-8', errors='ignore')
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout(error_msg)}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
# When all reachable objects already exist on the remote, send a
|
||||||
|
# direct ref update operation instead of a bundle.
|
||||||
|
if bundle_empty:
|
||||||
|
operation = {"action": "update_ref", "ref": remote_ref, "sha": local_sha, "force": force}
|
||||||
|
request_data = { self.IDX_REPOSITORY: self.repo_path,
|
||||||
|
"operations": [operation] }
|
||||||
|
|
||||||
|
response, metadata = self.send_request(self.PATH_PUSH, request_data)
|
||||||
|
|
||||||
|
if not response or not isinstance(response, bytes):
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout('No response from server')}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
status_byte = response[0]
|
||||||
|
if status_byte != 0:
|
||||||
|
error_msg = response[1:].decode('utf-8', errors='ignore')
|
||||||
|
git_stdout.write(f"error {remote_ref} {self.escape_for_stdout(error_msg)}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
continue
|
||||||
|
|
||||||
|
git_stdout.write(f"ok {remote_ref}\n")
|
||||||
|
git_stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
|
__default_rngit_config__ = '''# This is the default rngit client config file.
|
||||||
|
|
||||||
|
[client]
|
||||||
|
|
||||||
|
# You can control the batch size of ref transfers
|
||||||
|
# using the ref_batch_size directive:
|
||||||
|
|
||||||
|
ref_batch_size = 25
|
||||||
|
|
||||||
|
|
||||||
|
[aliases]
|
||||||
|
|
||||||
|
# You can define aliases for commonly used destination
|
||||||
|
# hashes in this section. Each line must be in the format
|
||||||
|
# aliased_name = DESTINATION_HASH
|
||||||
|
#
|
||||||
|
# These hashes are used for resolving remote destinations.
|
||||||
|
# For rngit node permissions and identity resolution,
|
||||||
|
# aliases must be defined in ~/.rngit/config.
|
||||||
|
|
||||||
|
# my_node = 063d38912bffc850af4a1b8a270a9d85
|
||||||
|
# bobs_node = 714981d03e41deda0e4468cb274414cc
|
||||||
|
|
||||||
|
|
||||||
|
[logging]
|
||||||
|
# Valid log levels are 0 through 7:
|
||||||
|
# 0: Log only critical information
|
||||||
|
# 1: Log errors and lower log levels
|
||||||
|
# 2: Log warnings and lower log levels
|
||||||
|
# 3: Log notices and lower log levels
|
||||||
|
# 4: Log info and lower (this is the default)
|
||||||
|
# 5: Verbose logging
|
||||||
|
# 6: Debug logging
|
||||||
|
# 7: Extreme logging
|
||||||
|
|
||||||
|
loglevel = 4
|
||||||
|
|
||||||
|
'''.splitlines()
|
||||||
|
|
||||||
|
if __name__ == "__main__": main()
|
||||||
324
RNS/Utilities/rngit/commitsigs.py
Executable file
324
RNS/Utilities/rngit/commitsigs.py
Executable file
|
|
@ -0,0 +1,324 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import RNS
|
||||||
|
import struct
|
||||||
|
import base64
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
from RNS.Utilities.rnid import validate_rsg, create_rsg, extract_signed_rsg_data
|
||||||
|
|
||||||
|
SSHSIG_MAGIC = b"SSHSIG"
|
||||||
|
SSHSIG_VERSION = 1
|
||||||
|
NAMESPACE_GIT = b"git"
|
||||||
|
RESERVED_EMPTY = b""
|
||||||
|
HASH_ALGORITHM = b"sha256"
|
||||||
|
|
||||||
|
def ssh_string(data): return struct.pack(">I", len(data)) + data
|
||||||
|
|
||||||
|
def read_ssh_string(data, offset):
|
||||||
|
if offset + 4 > len(data): raise ValueError("Not enough data for string length")
|
||||||
|
length = struct.unpack(">I", data[offset:offset+4])[0]
|
||||||
|
if offset + 4 + length > len(data): raise ValueError("Not enough data for string content")
|
||||||
|
return data[offset+4:offset+4+length], offset + 4 + length
|
||||||
|
|
||||||
|
def create_ssh_signature(public_key_wire, namespace, reserved, hash_algorithm, signature_data):
|
||||||
|
# SSHSIG (6 bytes) || version (uint32) || pubkey (ssh-string) || namespace (ssh-string) ||
|
||||||
|
# reserved (ssh-string) || hash_algorithm (ssh-string) || signature (ssh-string)
|
||||||
|
sig_blob = SSHSIG_MAGIC
|
||||||
|
sig_blob += struct.pack(">I", SSHSIG_VERSION)
|
||||||
|
sig_blob += ssh_string(public_key_wire)
|
||||||
|
sig_blob += ssh_string(namespace)
|
||||||
|
sig_blob += ssh_string(reserved)
|
||||||
|
sig_blob += ssh_string(hash_algorithm)
|
||||||
|
sig_blob += ssh_string(signature_data)
|
||||||
|
return sig_blob
|
||||||
|
|
||||||
|
def parse_ssh_signature(sig_data):
|
||||||
|
offset = 0
|
||||||
|
|
||||||
|
if not sig_data.startswith(SSHSIG_MAGIC): raise ValueError("Invalid SSH signature: missing SSHSIG magic")
|
||||||
|
offset += len(SSHSIG_MAGIC)
|
||||||
|
|
||||||
|
if offset + 4 > len(sig_data): raise ValueError("Invalid SSH signature: truncated")
|
||||||
|
version = struct.unpack(">I", sig_data[offset:offset+4])[0]
|
||||||
|
if version != SSHSIG_VERSION: raise ValueError(f"Unsupported SSH signature version: {version}")
|
||||||
|
offset += 4
|
||||||
|
|
||||||
|
public_key, offset = read_ssh_string(sig_data, offset)
|
||||||
|
namespace, offset = read_ssh_string(sig_data, offset)
|
||||||
|
reserved, offset = read_ssh_string(sig_data, offset)
|
||||||
|
hash_algorithm, offset = read_ssh_string(sig_data, offset)
|
||||||
|
signature_data, offset = read_ssh_string(sig_data, offset)
|
||||||
|
|
||||||
|
return { "version": version,
|
||||||
|
"public_key": public_key,
|
||||||
|
"namespace": namespace,
|
||||||
|
"reserved": reserved,
|
||||||
|
"hash_algorithm": hash_algorithm,
|
||||||
|
"signature_data": signature_data }
|
||||||
|
|
||||||
|
def armor_ssh_signature(sig_blob):
|
||||||
|
b64_data = base64.b64encode(sig_blob).decode('ascii')
|
||||||
|
lines = [b64_data[i:i+70] for i in range(0, len(b64_data), 70)]
|
||||||
|
|
||||||
|
result = "-----BEGIN SSH SIGNATURE-----\n"
|
||||||
|
result += "\n".join(lines) + "\n"
|
||||||
|
result += "-----END SSH SIGNATURE-----\n"
|
||||||
|
return result
|
||||||
|
|
||||||
|
def unarmor_ssh_signature(armored_data):
|
||||||
|
lines = armored_data.strip().split('\n')
|
||||||
|
b64_data = ""
|
||||||
|
in_sig = False
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if 'BEGIN SSH SIGNATURE' in line: in_sig = True; continue
|
||||||
|
if 'END SSH SIGNATURE' in line: break
|
||||||
|
if in_sig: b64_data += line.strip()
|
||||||
|
|
||||||
|
if not b64_data: raise ValueError("No signature data found in armored input")
|
||||||
|
|
||||||
|
return base64.b64decode(b64_data)
|
||||||
|
|
||||||
|
def get_pubkey_wire_format(identity):
|
||||||
|
return ssh_string(b"ssh-ed25519")+ssh_string(identity.sig_pub_bytes)
|
||||||
|
|
||||||
|
def sign(args):
|
||||||
|
keyfile = args.keyfile
|
||||||
|
if not keyfile or not os.path.isfile(keyfile):
|
||||||
|
print(f"Identity file not found: {keyfile}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
identity = RNS.Identity.from_file(keyfile)
|
||||||
|
if not identity or not identity.get_private_key():
|
||||||
|
print("Error: Could not load identity or identity has no private key", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error loading identity: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if args.file and os.path.isfile(args.file):
|
||||||
|
with open(args.file, 'rb') as f: message = f.read()
|
||||||
|
sig_file = args.file + ".sig"
|
||||||
|
else:
|
||||||
|
message = sys.stdin.buffer.read()
|
||||||
|
sig_file = None
|
||||||
|
|
||||||
|
try: rsg = create_rsg(identity, message)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating signature: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try: ssh_pubkey = get_pubkey_wire_format(identity)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error converting public key: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
ssh_sig = create_ssh_signature(public_key_wire=ssh_pubkey, namespace=NAMESPACE_GIT, reserved=RESERVED_EMPTY,
|
||||||
|
hash_algorithm=HASH_ALGORITHM, signature_data=rsg)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating SSH signature: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
try: armored = armor_ssh_signature(ssh_sig)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error armoring signature: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if sig_file:
|
||||||
|
try:
|
||||||
|
with open(sig_file, 'w') as f: f.write(armored)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error writing signature file: {e}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
else: print(armored, end="")
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def find_principals(args):
|
||||||
|
sigfile = args.sigfile
|
||||||
|
if not sigfile or not os.path.isfile(sigfile): print("Error: Signature file not found", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sigfile, 'r') as f: armored_sig = f.read()
|
||||||
|
except Exception as e: print(f"Error reading signature file: {e}", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
try: ssh_sig = parse_ssh_signature(unarmor_ssh_signature(armored_sig))
|
||||||
|
except Exception as e: print(f"Error parsing SSH signature: {e}", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
if ssh_sig["namespace"] != NAMESPACE_GIT:
|
||||||
|
print(f"Error: Namespace mismatch: {ssh_sig['namespace']}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
rsg = ssh_sig["signature_data"]
|
||||||
|
try: identity_hash = extract_signed_rsg_data(rsg)["meta"]["signer"]
|
||||||
|
except Exception as e: print(f"Could not determine signer identity: {e}", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
print(RNS.hexrep(identity_hash, delimit=False))
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def check_novalidate(args):
|
||||||
|
sigfile = args.sigfile
|
||||||
|
if not sigfile or not os.path.isfile(sigfile): return 1
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sigfile, 'r') as f: armored_sig = f.read()
|
||||||
|
ssh_sig = parse_ssh_signature(unarmor_ssh_signature(armored_sig))
|
||||||
|
|
||||||
|
if ssh_sig["namespace"] != NAMESPACE_GIT: return 1
|
||||||
|
|
||||||
|
rsg = ssh_sig["signature_data"]
|
||||||
|
signed_data = extract_signed_rsg_data(rsg)
|
||||||
|
if not signed_data: return 1
|
||||||
|
else: return 0
|
||||||
|
|
||||||
|
except Exception: return 1
|
||||||
|
|
||||||
|
def extract_commit_author(message):
|
||||||
|
message_lines = message.splitlines()
|
||||||
|
author = ""
|
||||||
|
AUTHOR_TARGET = b"author "
|
||||||
|
for line in message_lines:
|
||||||
|
if not line.strip(b""): break
|
||||||
|
elif line.startswith(AUTHOR_TARGET):
|
||||||
|
try:
|
||||||
|
spos = line.find(b"<"); epos = line.find(b">")
|
||||||
|
if spos > len(AUTHOR_TARGET) and epos > spos and epos < len(line)-1:
|
||||||
|
author = line[spos+1:epos].decode("utf-8")
|
||||||
|
break
|
||||||
|
except Exception as e: print(f"Error while determining author from signed commit"); return 1
|
||||||
|
|
||||||
|
return author
|
||||||
|
|
||||||
|
def extract_commit_committer(message):
|
||||||
|
message_lines = message.splitlines()
|
||||||
|
committer = ""
|
||||||
|
COMMITTER_TARGET = b"committer "
|
||||||
|
for line in message_lines:
|
||||||
|
if not line.strip(b""): break
|
||||||
|
elif line.startswith(COMMITTER_TARGET):
|
||||||
|
try:
|
||||||
|
spos = line.find(b"<"); epos = line.find(b">")
|
||||||
|
if spos > len(COMMITTER_TARGET) and epos > spos and epos < len(line)-1:
|
||||||
|
committer = line[spos+1:epos].decode("utf-8")
|
||||||
|
break
|
||||||
|
except Exception as e: print(f"Error while determining committer from signed commit"); return 1
|
||||||
|
|
||||||
|
return committer
|
||||||
|
|
||||||
|
def extract_commit_tagger(message):
|
||||||
|
message_lines = message.splitlines()
|
||||||
|
tagger = ""
|
||||||
|
is_tag = False
|
||||||
|
for line in message_lines:
|
||||||
|
TAG_TARGET = b"tag "
|
||||||
|
TAGGER_TARGET = b"tagger "
|
||||||
|
if not line.strip(b""): break
|
||||||
|
elif line.startswith(TAG_TARGET): is_tag = True
|
||||||
|
elif line.startswith(TAGGER_TARGET) and is_tag:
|
||||||
|
try:
|
||||||
|
spos = line.find(b"<"); epos = line.find(b">")
|
||||||
|
if spos > len(TAGGER_TARGET) and epos > spos and epos < len(line)-1:
|
||||||
|
tagger = line[spos+1:epos].decode("utf-8")
|
||||||
|
break
|
||||||
|
except Exception as e: print(f"Error while determining tagger from signed commit"); return 1
|
||||||
|
|
||||||
|
return tagger, is_tag
|
||||||
|
|
||||||
|
def verify(args):
|
||||||
|
sigfile = args.sigfile
|
||||||
|
principal = args.principal
|
||||||
|
if not sigfile or not os.path.isfile(sigfile): print("Error: Signature file not found", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
message = sys.stdin.buffer.read()
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(sigfile, 'r') as f: armored_sig = f.read()
|
||||||
|
raw_sig = unarmor_ssh_signature(armored_sig)
|
||||||
|
ssh_sig = parse_ssh_signature(raw_sig)
|
||||||
|
|
||||||
|
except Exception as e: print(f"Error parsing signature: {e}", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
author = extract_commit_author(message)
|
||||||
|
committer = extract_commit_committer(message)
|
||||||
|
tagger, is_tag = extract_commit_tagger(message)
|
||||||
|
|
||||||
|
if ssh_sig["namespace"] != NAMESPACE_GIT: print(f"Invalid commit signature namespace", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
rsg = ssh_sig["signature_data"]
|
||||||
|
valid, signed_data, signing_identity = validate_rsg(rsg, message)
|
||||||
|
|
||||||
|
if not valid: print(f"Invalid signature", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
if is_tag: author = tagger
|
||||||
|
|
||||||
|
signer_hash = RNS.hexrep(signing_identity.hash, delimit=False)
|
||||||
|
if not author == signer_hash: print(f"Commit not signed by author <{author}>"); return 1
|
||||||
|
|
||||||
|
if principal:
|
||||||
|
if principal != signer_hash: print(f"Principal mismatch", file=sys.stderr); return 1
|
||||||
|
|
||||||
|
print(f"Good \"git\" signature for commit, signed with Reticulum Identity key <{signer_hash}>")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description="Git commit signer and validator")
|
||||||
|
parser.add_argument("-Y", dest="op", required=True, choices=["sign", "find-principals", "check-novalidate", "verify"], help="Operation to perform")
|
||||||
|
parser.add_argument("-n", dest="namespace", default="git", help="Namespace")
|
||||||
|
parser.add_argument("-f", dest="keyfile", help="Key file (for signing) or allowed signers file (for verification)")
|
||||||
|
parser.add_argument("-I", dest="principal", help="Principal identity (for verification)")
|
||||||
|
parser.add_argument("-s", dest="sigfile", help="Signature file")
|
||||||
|
parser.add_argument("file", nargs="?", help="File to sign (for signing)")
|
||||||
|
parser.add_argument("-O", dest="ssh_options", action="append", default=[], help="SSH options (for git compatibility, ignored)")
|
||||||
|
|
||||||
|
args, unknown = parser.parse_known_args()
|
||||||
|
for arg in unknown:
|
||||||
|
if arg.startswith('-O'): continue # TODO: Add options for time validation
|
||||||
|
else:
|
||||||
|
print(f"Error: Unknown argument: {arg}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if args.op == "sign": return sign(args)
|
||||||
|
elif args.op == "find-principals": return find_principals(args)
|
||||||
|
elif args.op == "check-novalidate": return check_novalidate(args)
|
||||||
|
elif args.op == "verify": return verify(args)
|
||||||
|
else:
|
||||||
|
print(f"Error: Unknown operation: {args.op}", file=sys.stderr)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
if __name__ == "__main__": sys.exit(main())
|
||||||
412
RNS/Utilities/rngit/highlight.py
Normal file
412
RNS/Utilities/rngit/highlight.py
Normal file
|
|
@ -0,0 +1,412 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import io
|
||||||
|
import RNS
|
||||||
|
|
||||||
|
class SyntaxHighlighter:
|
||||||
|
|
||||||
|
def __init__(self, theme=None):
|
||||||
|
self.pygments_available = False
|
||||||
|
self.pygments = None
|
||||||
|
self._lexer_cache = {}
|
||||||
|
self._check_pygments()
|
||||||
|
self.theme = theme or self._get_default_theme()
|
||||||
|
|
||||||
|
def _get_default_theme(self):
|
||||||
|
return {
|
||||||
|
# Control flow - warm coral-red
|
||||||
|
"keyword": "ff7b72",
|
||||||
|
"keyword_constant": "ff7b72",
|
||||||
|
"keyword_control": "ff7b72",
|
||||||
|
"keyword_declaration": "ff7b72",
|
||||||
|
|
||||||
|
# Function definitions - bright sky blue
|
||||||
|
"function_def": "79c0ff",
|
||||||
|
"function_magic": "ff7b72",
|
||||||
|
|
||||||
|
# Function calls - soft lavender
|
||||||
|
"function_call": "d2a8ff",
|
||||||
|
"function_builtin": "ffa657", # amber
|
||||||
|
|
||||||
|
# Class definitions - fresh mint green
|
||||||
|
"class_def": "7ee787",
|
||||||
|
"class_ref": "56d364", # muted when referenced
|
||||||
|
|
||||||
|
# Instance context - soft pink
|
||||||
|
"self": "ff9bce",
|
||||||
|
"cls": "ff9bce",
|
||||||
|
|
||||||
|
# Data literals - cool, calm ice blue
|
||||||
|
"string": "a5d6ff",
|
||||||
|
"string_quoted": "a5d6ff",
|
||||||
|
"string_doc": "8b949e", # docstrings - like comments
|
||||||
|
"string_interpol": "ffd700", # f-string braces - gold
|
||||||
|
"string_escape": "ffea00", # escape sequences - bright yellow
|
||||||
|
|
||||||
|
# Numbers - same as function def
|
||||||
|
"number": "79c0ff",
|
||||||
|
"number_float": "79c0ff",
|
||||||
|
"number_integer": "79c0ff",
|
||||||
|
"number_hex": "79c0ff",
|
||||||
|
|
||||||
|
# Comments - muted gray
|
||||||
|
"comment": "8b949e",
|
||||||
|
"comment_doc": "8b949e",
|
||||||
|
"comment_preproc": "ff7b72", # preprocessor directives
|
||||||
|
|
||||||
|
# Operators - distinct pink/red for visibility
|
||||||
|
"operator": "ff7b72", # General operators - coral
|
||||||
|
"operator_arithmetic": "ff7b72", # +, -, *, /, etc.
|
||||||
|
"operator_comparison": "ff7b72", # ==, !=, <, >, etc.
|
||||||
|
"operator_assignment": "ff7b72", # =, +=, -=, etc.
|
||||||
|
"operator_word": "ff7b72", # and, or, not, in, is
|
||||||
|
"operator_dot": "c9d1d9", # . - subtle for attribute access
|
||||||
|
|
||||||
|
# Punctuation - neutral
|
||||||
|
"punctuation": "b4b4b4",
|
||||||
|
"punctuation_brace": "b4b4b4", # [, ], {, }
|
||||||
|
"punctuation_paren": "b4b4b4", # (, )
|
||||||
|
"punctuation_colon": "b4b4b4", # :, ;
|
||||||
|
"punctuation_comma": "8b949e", # , - slightly dimmed
|
||||||
|
|
||||||
|
# Decorators - burnt orange
|
||||||
|
"decorator": "f0883e",
|
||||||
|
|
||||||
|
# Constants - same as keywords
|
||||||
|
"constant": "ff7b72",
|
||||||
|
"constant_builtin": "ff7b72", # True, False, None
|
||||||
|
|
||||||
|
# Type hints and annotations - amber
|
||||||
|
"type_hint": "ffa657",
|
||||||
|
"type_builtin": "ffa657",
|
||||||
|
|
||||||
|
# Exception handling - alert red
|
||||||
|
"exception": "f85149",
|
||||||
|
"exception_builtin": "f85149",
|
||||||
|
|
||||||
|
# Names and attributes - near-white for readability
|
||||||
|
"name": "e6edf3",
|
||||||
|
"attribute": "e6edf3",
|
||||||
|
"attribute_call": "d2a8ff", # Function/method calls after dot - lavender
|
||||||
|
"variable": "e6edf3",
|
||||||
|
"parameter": "e6edf3",
|
||||||
|
|
||||||
|
# Namespaces and modules
|
||||||
|
"namespace": "7ee787",
|
||||||
|
"module": "a5d6ff",
|
||||||
|
|
||||||
|
# Generic tokens
|
||||||
|
"generic_heading": "c9d1d9",
|
||||||
|
"generic_subheading": "c9d1d9",
|
||||||
|
"generic_prompt": "8b949e",
|
||||||
|
"generic_error": "f85149",
|
||||||
|
"generic_deleted": "f85149",
|
||||||
|
"generic_inserted": "7ee787",
|
||||||
|
"generic_output": "e6edf3",
|
||||||
|
|
||||||
|
# Text and whitespace - no color (None means no color tag)
|
||||||
|
"text": None,
|
||||||
|
"whitespace": None,
|
||||||
|
}
|
||||||
|
|
||||||
|
def _check_pygments(self):
|
||||||
|
try:
|
||||||
|
import pygments
|
||||||
|
from pygments.lexers import get_lexer_for_filename, guess_lexer, get_lexer_by_name
|
||||||
|
from pygments.formatter import Formatter
|
||||||
|
from pygments.token import Token
|
||||||
|
|
||||||
|
self.pygments = pygments
|
||||||
|
self.pygments_available = True
|
||||||
|
RNS.log("Pygments syntax highlighting available", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
self.pygments_available = False
|
||||||
|
RNS.log("Pygments not available, using plain text rendering", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
def highlight(self, content, filename=None, language=None):
|
||||||
|
if not content: return self._plain_text(content)
|
||||||
|
|
||||||
|
if self.pygments_available:
|
||||||
|
try:
|
||||||
|
highlighted = self._highlight_pygments(content, filename, language)
|
||||||
|
# Fix pygments insisting on trailing newlines
|
||||||
|
if highlighted.endswith("\n") and not content.endswith("\n"): highlighted = highlighted[:-1]
|
||||||
|
return highlighted
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Pygments highlighting failed, falling back: {e}", RNS.LOG_WARNING)
|
||||||
|
return self._plain_text(content).replace("\\", "\\\\")
|
||||||
|
|
||||||
|
# TODO: Implement Python tokenize fallback for .py files.
|
||||||
|
# For now, route to plain text
|
||||||
|
if filename and filename.endswith(".py"):
|
||||||
|
return self._plain_text(content).replace("\\", "\\\\")
|
||||||
|
|
||||||
|
# Universal fallback
|
||||||
|
return self._plain_text(content).replace("\\", "\\\\")
|
||||||
|
|
||||||
|
def _highlight_pygments(self, content, filename=None, language=None):
|
||||||
|
from pygments.lexers import get_lexer_for_filename, guess_lexer, get_lexer_by_name
|
||||||
|
from pygments.util import ClassNotFound
|
||||||
|
|
||||||
|
lexer = None
|
||||||
|
if language:
|
||||||
|
if language == "env": language = "bash"
|
||||||
|
if language == "environment": language = "bash"
|
||||||
|
try: lexer = get_lexer_by_name(language)
|
||||||
|
except ClassNotFound: pass
|
||||||
|
|
||||||
|
if lexer is None and filename:
|
||||||
|
try: lexer = get_lexer_for_filename(filename)
|
||||||
|
except ClassNotFound: pass
|
||||||
|
|
||||||
|
if lexer is None:
|
||||||
|
try:
|
||||||
|
if len(content) > 20: lexer = guess_lexer(content)
|
||||||
|
except ClassNotFound: pass
|
||||||
|
|
||||||
|
if lexer is None: return self._plain_text(content)
|
||||||
|
|
||||||
|
formatter = MicronFormatter(theme=self.theme)
|
||||||
|
result = self.pygments.highlight(content, lexer, formatter)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _plain_text(self, content):
|
||||||
|
escaped = self._escape_micron(content)
|
||||||
|
return f"`=\n{escaped}\n`="
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _escape_micron(text): return text.replace("`", "\\`")
|
||||||
|
|
||||||
|
|
||||||
|
class MicronFormatter:
|
||||||
|
def __init__(self, theme, **options):
|
||||||
|
self.theme = theme
|
||||||
|
self.options = options
|
||||||
|
|
||||||
|
def format(self, tokensource, outfile):
|
||||||
|
output_parts = []
|
||||||
|
prev_was_dot = False
|
||||||
|
|
||||||
|
last_ended_with_break = True
|
||||||
|
for ttype, value in tokensource:
|
||||||
|
is_dot = (str(ttype) == "Token.Operator" and value == ".")
|
||||||
|
ends_with_break = value.endswith("\n")
|
||||||
|
|
||||||
|
# If previous token was a dot and this is a Name, treat as attribute/function call
|
||||||
|
# TODO: Improve this if we can check next token as parantheses or something.
|
||||||
|
if prev_was_dot and str(ttype).startswith("Token.Name") and value:
|
||||||
|
color = self._get_color_from_key("attribute_call")
|
||||||
|
if color:
|
||||||
|
escaped = self._escape_value(value)
|
||||||
|
output_parts.append(f"`FT{color}{escaped}`f")
|
||||||
|
else:
|
||||||
|
output_parts.append(self._escape_value(value))
|
||||||
|
|
||||||
|
else:
|
||||||
|
color_key = self._get_color_key_for_token(ttype)
|
||||||
|
color = self._get_color_from_key(color_key)
|
||||||
|
|
||||||
|
if color and value:
|
||||||
|
escaped = self._escape_value(value)
|
||||||
|
if escaped.startswith("\n"): ilb = "\n"; escaped = escaped[1:]
|
||||||
|
else: ilb = ""
|
||||||
|
if escaped.endswith("\n"): tlb = "\n"; escaped = escaped[:-1]
|
||||||
|
else: tlb = ""
|
||||||
|
|
||||||
|
if len(escaped): output = f"{ilb}`FT{color}{escaped}`f{tlb}"
|
||||||
|
else: output = f"{ilb}{tlb}"
|
||||||
|
|
||||||
|
output_parts.append(output)
|
||||||
|
|
||||||
|
else:
|
||||||
|
escaped = self._escape_value(value)
|
||||||
|
if "\n" in escaped:
|
||||||
|
parts = []
|
||||||
|
splitl = escaped.splitlines()
|
||||||
|
if len(splitl) > 1:
|
||||||
|
for line in splitl:
|
||||||
|
if line.startswith("-"): l = f"\\{line}"
|
||||||
|
elif line.startswith(">"): l = f"\\{line}"
|
||||||
|
elif line.startswith("<"): l = f"\\{line}"
|
||||||
|
else: l = line
|
||||||
|
parts.append(l)
|
||||||
|
trmpart = "\n" if escaped.endswith("\n") else ""
|
||||||
|
escaped = "\n".join(parts)+trmpart
|
||||||
|
|
||||||
|
elif last_ended_with_break:
|
||||||
|
if escaped.startswith("-"): escaped = f"\\{escaped}"
|
||||||
|
elif escaped.startswith(">"): escaped = f"\\{escaped}"
|
||||||
|
elif escaped.startswith("<"): escaped = f"\\{escaped}"
|
||||||
|
|
||||||
|
output_parts.append(escaped)
|
||||||
|
|
||||||
|
prev_was_dot = is_dot
|
||||||
|
last_ended_with_break = ends_with_break
|
||||||
|
|
||||||
|
output = "".join(output_parts)
|
||||||
|
outfile.write(output)
|
||||||
|
|
||||||
|
def _get_color_key_for_token(self, ttype):
|
||||||
|
token_parts = []
|
||||||
|
current = ttype
|
||||||
|
while current:
|
||||||
|
token_parts.insert(0, current[0] if isinstance(current, tuple) else str(current).split(".")[-1])
|
||||||
|
current = current.parent if hasattr(current, "parent") else None
|
||||||
|
|
||||||
|
token_str = ".".join(["Token"] + token_parts[1:] if len(token_parts) > 1 else token_parts)
|
||||||
|
|
||||||
|
current_type = ttype
|
||||||
|
while current_type:
|
||||||
|
token_key = str(current_type)
|
||||||
|
if token_key in granular_token_map: return granular_token_map[token_key]
|
||||||
|
|
||||||
|
# Move to parent
|
||||||
|
current_type = current_type.parent if hasattr(current_type, "parent") else None
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _get_color_from_key(self, color_key):
|
||||||
|
if color_key and color_key in self.theme: return self.theme[color_key]
|
||||||
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _escape_value(value):
|
||||||
|
return value.replace("\\", "\\\\").replace("`", "\\`")
|
||||||
|
|
||||||
|
# Required by Pygments formatter API, returns None for Micron
|
||||||
|
def get_style_defs(self, arg=None): return None
|
||||||
|
|
||||||
|
|
||||||
|
# Convenience function for direct use
|
||||||
|
def highlight_code(content: str, filename: str = None, language: str = None, theme=None) -> str:
|
||||||
|
highlighter = SyntaxHighlighter(theme=theme)
|
||||||
|
return highlighter.highlight(content, filename, language)
|
||||||
|
|
||||||
|
granular_token_map = {
|
||||||
|
# Keywords with semantic distinction
|
||||||
|
"Token.Keyword": "keyword",
|
||||||
|
"Token.Keyword.Constant": "keyword_constant",
|
||||||
|
"Token.Keyword.Declaration": "keyword_declaration",
|
||||||
|
"Token.Keyword.Namespace": "keyword_control",
|
||||||
|
"Token.Keyword.Pseudo": "keyword_control",
|
||||||
|
"Token.Keyword.Reserved": "keyword_control",
|
||||||
|
"Token.Keyword.Type": "type_builtin",
|
||||||
|
|
||||||
|
# Names - functions with definition vs call distinction
|
||||||
|
"Token.Name.Function": "function_call",
|
||||||
|
"Token.Name.Function.Magic": "function_magic",
|
||||||
|
"Token.Name.Class": "class_ref",
|
||||||
|
"Token.Name.Builtin": "function_builtin",
|
||||||
|
"Token.Name.Builtin.Pseudo": "constant_builtin",
|
||||||
|
"Token.Name.Exception": "exception_builtin",
|
||||||
|
"Token.Name.Decorator": "decorator",
|
||||||
|
"Token.Name.Namespace": "namespace",
|
||||||
|
"Token.Name.Attribute": "attribute",
|
||||||
|
"Token.Name.Variable": "variable",
|
||||||
|
"Token.Name.Variable.Magic": "function_magic",
|
||||||
|
"Token.Name.Other": "name",
|
||||||
|
"Token.Name": "name",
|
||||||
|
"Token.Name.Tag": "keyword", # HTML/XML tags
|
||||||
|
"Token.Name.Constant": "constant",
|
||||||
|
"Token.Name.Label": "name",
|
||||||
|
"Token.Name.Entity": "name",
|
||||||
|
|
||||||
|
# Literals - strings with detailed handling
|
||||||
|
"Token.Literal.String": "string",
|
||||||
|
"Token.Literal.String.Affix": "string", # f, r, b prefixes
|
||||||
|
"Token.Literal.String.Backtick": "string",
|
||||||
|
"Token.Literal.String.Char": "string",
|
||||||
|
"Token.Literal.String.Delimiter": "string",
|
||||||
|
"Token.Literal.String.Doc": "string_doc",
|
||||||
|
"Token.Literal.String.Double": "string_quoted",
|
||||||
|
"Token.Literal.String.Escape": "string_escape",
|
||||||
|
"Token.Literal.String.Heredoc": "string",
|
||||||
|
"Token.Literal.String.Interpol": "string_interpol",
|
||||||
|
"Token.Literal.String.Other": "string",
|
||||||
|
"Token.Literal.String.Regex": "string",
|
||||||
|
"Token.Literal.String.Single": "string_quoted",
|
||||||
|
"Token.Literal.String.Symbol": "string",
|
||||||
|
|
||||||
|
# Numbers
|
||||||
|
"Token.Literal.Number": "number",
|
||||||
|
"Token.Literal.Number.Bin": "number",
|
||||||
|
"Token.Literal.Number.Float": "number_float",
|
||||||
|
"Token.Literal.Number.Hex": "number_hex",
|
||||||
|
"Token.Literal.Number.Integer": "number_integer",
|
||||||
|
"Token.Literal.Number.Integer.Long": "number_integer",
|
||||||
|
"Token.Literal.Number.Oct": "number",
|
||||||
|
"Token.Literal": "string",
|
||||||
|
"Token.Literal.Date": "string",
|
||||||
|
|
||||||
|
# Operators - all operators get distinct coloring
|
||||||
|
"Token.Operator": "operator",
|
||||||
|
"Token.Operator.Word": "operator_word",
|
||||||
|
"Token.Operator.Comparison": "operator_comparison",
|
||||||
|
"Token.Operator.Assignment": "operator_assignment",
|
||||||
|
"Token.Operator.Arithmetic": "operator_arithmetic",
|
||||||
|
|
||||||
|
# Punctuation - braces, parens, colons, commas
|
||||||
|
"Token.Punctuation": "punctuation",
|
||||||
|
"Token.Punctuation.Marker": "punctuation",
|
||||||
|
"Token.Punctuation.Brace": "punctuation_brace",
|
||||||
|
"Token.Punctuation.Bracket": "punctuation_brace",
|
||||||
|
"Token.Punctuation.Parenthesis": "punctuation_paren",
|
||||||
|
"Token.Punctuation.Colon": "punctuation_colon",
|
||||||
|
"Token.Punctuation.Comma": "punctuation_comma",
|
||||||
|
|
||||||
|
# Comments
|
||||||
|
"Token.Comment": "comment",
|
||||||
|
"Token.Comment.Hashbang": "comment",
|
||||||
|
"Token.Comment.Multiline": "comment_doc",
|
||||||
|
"Token.Comment.Preproc": "comment_preproc",
|
||||||
|
"Token.Comment.Single": "comment",
|
||||||
|
"Token.Comment.Special": "comment",
|
||||||
|
|
||||||
|
# Generic tokens
|
||||||
|
"Token.Generic.Deleted": "generic_deleted",
|
||||||
|
"Token.Generic.Emph": "text",
|
||||||
|
"Token.Generic.Error": "generic_error",
|
||||||
|
"Token.Generic.Heading": "generic_heading",
|
||||||
|
"Token.Generic.Inserted": "generic_inserted",
|
||||||
|
"Token.Generic.Output": "generic_output",
|
||||||
|
"Token.Generic.Prompt": "generic_prompt",
|
||||||
|
"Token.Generic.Strong": "text",
|
||||||
|
"Token.Generic.Subheading": "generic_subheading",
|
||||||
|
"Token.Generic.Traceback": "generic_error",
|
||||||
|
"Token.Generic": "text",
|
||||||
|
|
||||||
|
# Text and whitespace
|
||||||
|
"Token.Text": "text",
|
||||||
|
"Token.Text.Whitespace": "whitespace",
|
||||||
|
}
|
||||||
40
RNS/Utilities/rngit/main.py
Normal file
40
RNS/Utilities/rngit/main.py
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from RNS.Utilities.rngit import client, server
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cmd = sys.argv[0]
|
||||||
|
if cmd == "rngit": ec = server.main()
|
||||||
|
elif cmd == "git-remote-rns": ec = client.main()
|
||||||
|
else: raise NotImplementedError(f"The {cmd} executable entrypoint is not yet implemented in rngit")
|
||||||
|
|
||||||
|
sys.exit(ec)
|
||||||
239
RNS/Utilities/rngit/media.py
Normal file
239
RNS/Utilities/rngit/media.py
Normal file
|
|
@ -0,0 +1,239 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import tempfile
|
||||||
|
import RNS
|
||||||
|
|
||||||
|
CONVERSION_TIMEOUT = 8
|
||||||
|
|
||||||
|
# WebP encoding backends, in preference order. All supported
|
||||||
|
# encoder families handle the common still formats (PNG, JPEG,
|
||||||
|
# TIFF, BMP, GIF, WebP).
|
||||||
|
#
|
||||||
|
# Environment overrides:
|
||||||
|
# RNGIT_MEDIA_BACKEND force a specific backend
|
||||||
|
|
||||||
|
BACKENDS = ( ("magick", ["magick", "-", "webp:-"]),
|
||||||
|
("convert", ["convert", "-", "webp:-"]),
|
||||||
|
("gm", ["gm", "convert", "-", "webp:-"]),
|
||||||
|
("ffmpeg", ["ffmpeg", "-y", "-loglevel", "error", "-i", "-", "-f", "webp", "pipe:1"]),
|
||||||
|
("avconv", ["avconv", "-y", "-loglevel", "error", "-i", "-", "-f", "webp", "pipe:1"]) )
|
||||||
|
|
||||||
|
_ENV_BACKEND = os.environ.get("RNGIT_MEDIA_BACKEND")
|
||||||
|
_winner = None
|
||||||
|
_no_backend_logged = False
|
||||||
|
|
||||||
|
def available_backends():
|
||||||
|
out = []
|
||||||
|
for name, argv in BACKENDS: out.append((name, shutil.which(argv[0]) is not None))
|
||||||
|
return out
|
||||||
|
|
||||||
|
def _selected_backend():
|
||||||
|
global _winner
|
||||||
|
if _ENV_BACKEND:
|
||||||
|
for name, argv in BACKENDS:
|
||||||
|
if name == _ENV_BACKEND and shutil.which(argv[0]) is not None:
|
||||||
|
return (name, argv)
|
||||||
|
return None
|
||||||
|
|
||||||
|
order = list(BACKENDS)
|
||||||
|
if _winner is not None: order.sort(key=lambda backend: backend[0] != _winner)
|
||||||
|
|
||||||
|
for name, argv in order:
|
||||||
|
if shutil.which(argv[0]) is not None:
|
||||||
|
_winner = name
|
||||||
|
return (name, argv)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _terminate(proc):
|
||||||
|
try: proc.kill()
|
||||||
|
except Exception: pass
|
||||||
|
try: proc.wait()
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def _stderr_tail(proc, limit=1024):
|
||||||
|
try:
|
||||||
|
data = proc.stderr.read(limit)
|
||||||
|
return data.decode("utf-8", "replace").strip()
|
||||||
|
except Exception: return ""
|
||||||
|
|
||||||
|
def _webp_info(data):
|
||||||
|
if len(data) < 30 or data[:4] != b"RIFF" or data[8:12] != b"WEBP": return None
|
||||||
|
fourcc = data[12:16]
|
||||||
|
if fourcc == b"VP8X":
|
||||||
|
width = int.from_bytes(data[24:27], "little") + 1
|
||||||
|
height = int.from_bytes(data[27:30], "little") + 1
|
||||||
|
elif fourcc == b"VP8 ":
|
||||||
|
width = int.from_bytes(data[26:28], "little") & 0x3FFF
|
||||||
|
height = int.from_bytes(data[28:30], "little") & 0x3FFF
|
||||||
|
elif fourcc == b"VP8L":
|
||||||
|
bits = int.from_bytes(data[21:25], "little")
|
||||||
|
width = (bits & 0x3FFF) + 1
|
||||||
|
height = ((bits >> 14) & 0x3FFF) + 1
|
||||||
|
else: return None
|
||||||
|
if width > 0 and height > 0: return (width, height)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _valid_webp(path):
|
||||||
|
try:
|
||||||
|
with open(path, "rb") as fh: return _webp_info(fh.read(30)) is not None
|
||||||
|
except Exception: return False
|
||||||
|
|
||||||
|
def _unlink_output(path):
|
||||||
|
try: os.unlink(path)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def _configured_backend(quality=None, max_dimension=None):
|
||||||
|
global _no_backend_logged
|
||||||
|
backend = _selected_backend()
|
||||||
|
if backend is None:
|
||||||
|
if not _no_backend_logged:
|
||||||
|
_no_backend_logged = True
|
||||||
|
RNS.log("No WebP encoding backend available for media conversion. You can Install ImageMagick or ffmpeg to enable image conversion.", RNS.LOG_WARNING)
|
||||||
|
return None
|
||||||
|
|
||||||
|
backend_name, encoder_argv = backend
|
||||||
|
_no_backend_logged = False
|
||||||
|
|
||||||
|
quality_arg = None
|
||||||
|
dimension_arg = None
|
||||||
|
if quality is not None:
|
||||||
|
try: quality_arg = max(1, min(100, int(quality)))
|
||||||
|
except (ValueError, TypeError): pass
|
||||||
|
if max_dimension is not None:
|
||||||
|
try: dimension_arg = int(max_dimension)
|
||||||
|
except (ValueError, TypeError): pass
|
||||||
|
if dimension_arg is not None and dimension_arg < 1: dimension_arg = None
|
||||||
|
|
||||||
|
encoder_argv = list(encoder_argv)
|
||||||
|
if backend_name in ("magick", "convert", "gm"):
|
||||||
|
options = []
|
||||||
|
if quality_arg is not None: options += ["-quality", str(quality_arg)]
|
||||||
|
if dimension_arg is not None: options += ["-resize", f"{dimension_arg}x{dimension_arg}>"]
|
||||||
|
encoder_argv = encoder_argv[:-1] + options + encoder_argv[-1:]
|
||||||
|
elif backend_name in ("ffmpeg", "avconv"):
|
||||||
|
options = []
|
||||||
|
if quality_arg is not None: options += ["-quality", str(quality_arg)]
|
||||||
|
if dimension_arg is not None: options += ["-vf", f"scale='min(iw,{dimension_arg})':'min(ih,{dimension_arg})':force_original_aspect_ratio=decrease"]
|
||||||
|
try: format_index = encoder_argv.index("-f")
|
||||||
|
except ValueError: format_index = len(encoder_argv)
|
||||||
|
encoder_argv = encoder_argv[:format_index] + options + encoder_argv[format_index:]
|
||||||
|
|
||||||
|
return backend_name, encoder_argv
|
||||||
|
|
||||||
|
def _await(backend_name, encoder_proc, input_proc=None, timeout=None):
|
||||||
|
if timeout is None: timeout = CONVERSION_TIMEOUT
|
||||||
|
|
||||||
|
deadline = time.time() + timeout
|
||||||
|
try:
|
||||||
|
encoder_proc.wait(timeout=max(0.0, deadline - time.time()))
|
||||||
|
if input_proc is not None: input_proc.wait(timeout=max(0.0, deadline - time.time()))
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
_terminate(input_proc)
|
||||||
|
_terminate(encoder_proc)
|
||||||
|
RNS.log(f"Media conversion via {backend_name} timed out after {timeout} seconds", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if encoder_proc.returncode != 0:
|
||||||
|
detail = ""
|
||||||
|
encoder_tail = _stderr_tail(encoder_proc)
|
||||||
|
input_tail = _stderr_tail(input_proc) if input_proc is not None else ""
|
||||||
|
if encoder_tail: detail += f" Encoder: {encoder_tail}"
|
||||||
|
if input_tail: detail += f" Input: {input_tail}"
|
||||||
|
RNS.log(f"Media conversion via {backend_name} failed.{detail}", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def convert_to_webp(input_argv, output_path, cwd=None, timeout=None, quality=None, max_dimension=None):
|
||||||
|
configured = _configured_backend(quality=quality, max_dimension=max_dimension)
|
||||||
|
if configured is None: return False
|
||||||
|
|
||||||
|
backend_name, encoder_argv = configured
|
||||||
|
|
||||||
|
try:
|
||||||
|
with open(output_path, "wb") as output_fh:
|
||||||
|
input_proc = subprocess.Popen(input_argv, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
encoder_proc = subprocess.Popen(encoder_argv, stdin=input_proc.stdout, stdout=output_fh, stderr=subprocess.PIPE)
|
||||||
|
|
||||||
|
# The parent must not retain a copy of the input pipe's write
|
||||||
|
# end, or the input process would never see EOF.
|
||||||
|
input_proc.stdout.close()
|
||||||
|
if not _await(backend_name, encoder_proc, input_proc=input_proc, timeout=timeout): return False
|
||||||
|
|
||||||
|
if not _valid_webp(output_path):
|
||||||
|
RNS.log(f"Media conversion via {backend_name} produced invalid WebP output", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
RNS.log(f"Media converted to WebP with {backend_name}", RNS.LOG_DEBUG)
|
||||||
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Error during media conversion: {e}", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def convert_file_to_webp(source_path, quality=85, max_dimension=None, timeout=None):
|
||||||
|
configured = _configured_backend(quality=quality, max_dimension=max_dimension)
|
||||||
|
if configured is None: return False
|
||||||
|
backend_name, encoder_argv = configured
|
||||||
|
|
||||||
|
tmp_path = None
|
||||||
|
success = False
|
||||||
|
try:
|
||||||
|
if not os.path.isfile(source_path):
|
||||||
|
RNS.log(f"Cannot convert media: source file does not exist: {source_path}", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
fd, tmp_path = tempfile.mkstemp(prefix="rns_media_", suffix=".webp")
|
||||||
|
os.close(fd)
|
||||||
|
|
||||||
|
with open(source_path, "rb") as input_fh, open(tmp_path, "wb") as output_fh:
|
||||||
|
encoder_proc = subprocess.Popen(encoder_argv, stdin=input_fh, stdout=output_fh, stderr=subprocess.PIPE)
|
||||||
|
if not _await(backend_name, encoder_proc, timeout=timeout): return False
|
||||||
|
|
||||||
|
if not _valid_webp(tmp_path):
|
||||||
|
RNS.log(f"Invalid media conversion output from {backend_name}", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
success = True
|
||||||
|
RNS.log(f"Media converted to WebP with {backend_name}", RNS.LOG_DEBUG)
|
||||||
|
return tmp_path
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Error during media conversion: {e}", RNS.LOG_WARNING)
|
||||||
|
return False
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if not success and tmp_path is not None: _unlink_output(tmp_path)
|
||||||
3073
RNS/Utilities/rngit/pages.py
Normal file
3073
RNS/Utilities/rngit/pages.py
Normal file
File diff suppressed because it is too large
Load diff
5120
RNS/Utilities/rngit/server.py
Normal file
5120
RNS/Utilities/rngit/server.py
Normal file
File diff suppressed because it is too large
Load diff
819
RNS/Utilities/rngit/util.py
Normal file
819
RNS/Utilities/rngit/util.py
Normal file
|
|
@ -0,0 +1,819 @@
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import re
|
||||||
|
import RNS
|
||||||
|
|
||||||
|
# Validate ref names according to https://git-scm.com/docs/git-check-ref-format
|
||||||
|
# This may be a bit overkill, since git validates names as well, but why not.
|
||||||
|
def san_ref(ref):
|
||||||
|
if ref.startswith("-"): return None
|
||||||
|
if ref.startswith("/"): return None
|
||||||
|
if ref.endswith("/"): return None
|
||||||
|
if ref.endswith("."): return None
|
||||||
|
|
||||||
|
if " " in ref: return None
|
||||||
|
if not "/" in ref: return None
|
||||||
|
if ".." in ref: return None
|
||||||
|
if "/." in ref: return None
|
||||||
|
if "//" in ref: return None
|
||||||
|
if "\\" in ref: return None
|
||||||
|
|
||||||
|
for comp in ref.split("/"):
|
||||||
|
if comp.endswith(".lock"): return None
|
||||||
|
|
||||||
|
if not all(ord(c) >= 40 for c in ref): return None # Any control character
|
||||||
|
if "\x7f" in ref: return None # ASCII DEL (177)
|
||||||
|
if "~" in ref: return None
|
||||||
|
if "^" in ref: return None
|
||||||
|
if ":" in ref: return None
|
||||||
|
if "?" in ref: return None
|
||||||
|
if "*" in ref: return None
|
||||||
|
if "[" in ref: return None
|
||||||
|
if "@{" in ref: return None
|
||||||
|
if "@" == ref: return None
|
||||||
|
|
||||||
|
return ref
|
||||||
|
|
||||||
|
def san_refs(refs):
|
||||||
|
if not type(refs) == list: return None
|
||||||
|
for ref in refs:
|
||||||
|
if not san_ref(ref): return None
|
||||||
|
|
||||||
|
return refs
|
||||||
|
|
||||||
|
# Git SHA format validation
|
||||||
|
def san_sha(sha):
|
||||||
|
if len(sha) < 40: return None
|
||||||
|
try: bytes.fromhex(sha)
|
||||||
|
except: return None
|
||||||
|
return sha
|
||||||
|
|
||||||
|
class MarkdownToMicron:
|
||||||
|
BOLD = "`!"
|
||||||
|
BOLD_END = "`!"
|
||||||
|
ITALIC = "`*"
|
||||||
|
ITALIC_END = "`*"
|
||||||
|
UNDERLINE = "`_"
|
||||||
|
UNDERLINE_END = "`_"
|
||||||
|
|
||||||
|
CODE_BG = "`BT282828"
|
||||||
|
CODE_BG_INLINE = "`BT383838"
|
||||||
|
CODE_FG = "`Fddd"
|
||||||
|
CODE_RESET = "`f`b"
|
||||||
|
|
||||||
|
LITERAL_START = "`="
|
||||||
|
LITERAL_END = "`="
|
||||||
|
|
||||||
|
BULLET = "•"
|
||||||
|
|
||||||
|
# Regex patterns for markdown elements
|
||||||
|
HEADER_RE = re.compile(r'^(#{1,6})\s+(.+)$')
|
||||||
|
CODE_FENCE_RE = re.compile(r'^(\s*)```(.*)$')
|
||||||
|
HORIZONTAL_RULE_RE = re.compile(r'^(\s*)(---+|===+|\*\*\*+|___+)\s*$')
|
||||||
|
UNORDERED_LIST_RE = re.compile(r'^(\s*)([-*+])\s+(.+)$')
|
||||||
|
|
||||||
|
# Table patterns
|
||||||
|
TABLE_ROW_RE = re.compile(r'^\s*\|?(.+?)\|?\s*$')
|
||||||
|
TABLE_SEP_RE = re.compile(r'^\s*\|?(?:\s*:?-+:?\s*\|)+\s*$')
|
||||||
|
|
||||||
|
# Quote pattern
|
||||||
|
QUOTE_RE = re.compile(r'^>\s?(.*)$')
|
||||||
|
|
||||||
|
# Inline patterns (processed in order of specificity)
|
||||||
|
LINK_RE = re.compile(r'\[([^\]]+)\]\(([^)]+)\)')
|
||||||
|
INLINE_CODE_RE = re.compile(r'`([^`]+)`')
|
||||||
|
BOLD_RE = re.compile(r'\*\*(.+?)\*\*|__(.+?)__')
|
||||||
|
ITALIC_RE = re.compile(r'\*(.+?)\*|_(.+?)_')
|
||||||
|
|
||||||
|
TABLE_H = "─"
|
||||||
|
TABLE_V = "│"
|
||||||
|
TABLE_TL = "┌"
|
||||||
|
TABLE_TR = "┐"
|
||||||
|
TABLE_BL = "└"
|
||||||
|
TABLE_BR = "┘"
|
||||||
|
TABLE_ML = "├"
|
||||||
|
TABLE_MR = "┤"
|
||||||
|
TABLE_TM = "┬"
|
||||||
|
TABLE_BM = "┴"
|
||||||
|
TABLE_MM = "┼"
|
||||||
|
|
||||||
|
TABLE_MIN_COL_WIDTH = 3
|
||||||
|
|
||||||
|
def __init__(self, max_width=100, syntax_highlighter=None, url_scope=None):
|
||||||
|
self.max_width = max_width
|
||||||
|
self.local_url_scope = url_scope or ":/page/"
|
||||||
|
self.__local_url_scope = self.local_url_scope
|
||||||
|
self.syntax_highlighter = syntax_highlighter
|
||||||
|
self.wcwidth = None
|
||||||
|
|
||||||
|
self.bold_links = True
|
||||||
|
self.underline_links = True
|
||||||
|
self.link_color = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import wcwidth
|
||||||
|
self.wcwidth = wcwidth
|
||||||
|
|
||||||
|
except: RNS.log(f"The wcwidth module is unavailable, display width calculations for some glyphs will be incorrect", RNS.LOG_WARNING)
|
||||||
|
|
||||||
|
def set_url_scope(self, url_scope): self.local_url_scope = url_scope
|
||||||
|
def restore_url_scope(self, url_scope): self.local_url_scope = self.__local_url_scope
|
||||||
|
|
||||||
|
def display_width(self, text):
|
||||||
|
if not self.wcwidth: return len(text)
|
||||||
|
else:
|
||||||
|
# wcswidth returns -1 for non-printable strings,
|
||||||
|
# fallback to len in this case
|
||||||
|
w = self.wcwidth.wcswidth(text)
|
||||||
|
return w if w is not None and w >= 0 else len(text)
|
||||||
|
|
||||||
|
def format_block(self, text, url_scope=None):
|
||||||
|
# text = text.replace("\\", "\\\\") # Now handled in format_line instead
|
||||||
|
lines = text.split('\n')
|
||||||
|
result_lines = []
|
||||||
|
in_code_block = False
|
||||||
|
code_block_lang = None
|
||||||
|
code_buffer = []
|
||||||
|
in_table = False
|
||||||
|
table_buffer = []
|
||||||
|
in_quote = False
|
||||||
|
quote_buffer = []
|
||||||
|
|
||||||
|
def flush_quote_buffer():
|
||||||
|
nonlocal result_lines, quote_buffer, in_quote
|
||||||
|
if not quote_buffer:
|
||||||
|
in_quote = False
|
||||||
|
return
|
||||||
|
|
||||||
|
para = " ".join(quote_buffer)
|
||||||
|
formatted = self._format_inline(para)
|
||||||
|
|
||||||
|
effective_width = self.max_width - 3
|
||||||
|
if effective_width < 1: effective_width = 1
|
||||||
|
wrapped_lines = self._wrap_text(formatted, effective_width)
|
||||||
|
for wrapped_line in wrapped_lines: result_lines.append(f" │ {wrapped_line}")
|
||||||
|
|
||||||
|
quote_buffer = []
|
||||||
|
in_quote = False
|
||||||
|
|
||||||
|
def flush_table_buffer():
|
||||||
|
nonlocal result_lines, table_buffer, in_table
|
||||||
|
if not table_buffer:
|
||||||
|
in_table = False
|
||||||
|
return
|
||||||
|
|
||||||
|
if len(table_buffer) >= 2 and self._is_table_separator(table_buffer[1]):
|
||||||
|
formatted_lines = self.format_table(table_buffer)
|
||||||
|
result_lines.extend(formatted_lines)
|
||||||
|
|
||||||
|
else:
|
||||||
|
for line in table_buffer: result_lines.append(self.format_line(line))
|
||||||
|
|
||||||
|
table_buffer = []
|
||||||
|
in_table = False
|
||||||
|
|
||||||
|
def flush_code_block():
|
||||||
|
nonlocal result_lines, code_buffer, code_block_lang
|
||||||
|
if not code_buffer:
|
||||||
|
return
|
||||||
|
|
||||||
|
code_content = '\n'.join(code_buffer)
|
||||||
|
|
||||||
|
if self.syntax_highlighter and code_block_lang:
|
||||||
|
if code_block_lang.lower() == "rawmu": result_lines.append(code_content)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
highlighted = self.syntax_highlighter.highlight(code_content, language=code_block_lang)
|
||||||
|
result_lines.append(f"{self.CODE_BG}{self.CODE_FG}")
|
||||||
|
result_lines.append(highlighted)
|
||||||
|
result_lines.append(self.CODE_RESET)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
# Fallback to plain literal block on any error
|
||||||
|
result_lines.append(f"{self.CODE_BG}{self.CODE_FG}")
|
||||||
|
result_lines.append(self.LITERAL_START)
|
||||||
|
result_lines.append(self._escape_literals(code_content))
|
||||||
|
result_lines.append(self.LITERAL_END)
|
||||||
|
result_lines.append(self.CODE_RESET)
|
||||||
|
else:
|
||||||
|
result_lines.append(f"{self.CODE_BG}{self.CODE_FG}")
|
||||||
|
result_lines.append(self.LITERAL_START)
|
||||||
|
result_lines.append(self._escape_literals(code_content))
|
||||||
|
result_lines.append(self.LITERAL_END)
|
||||||
|
result_lines.append(self.CODE_RESET)
|
||||||
|
|
||||||
|
code_buffer = []
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
is_fence, lang_hint = self._detect_code_fence(line)
|
||||||
|
|
||||||
|
if is_fence:
|
||||||
|
# Flush any pending structures before code fence
|
||||||
|
flush_quote_buffer()
|
||||||
|
flush_table_buffer()
|
||||||
|
|
||||||
|
if not in_code_block:
|
||||||
|
# Opening fence, start buffering
|
||||||
|
in_code_block = True
|
||||||
|
code_block_lang = lang_hint.strip() if lang_hint else None
|
||||||
|
code_buffer = []
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Closing fence, flush highlighted code
|
||||||
|
flush_code_block()
|
||||||
|
in_code_block = False
|
||||||
|
code_block_lang = None
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Buffer code lines for later highlighting
|
||||||
|
if in_code_block: code_buffer.append(line)
|
||||||
|
else:
|
||||||
|
quote_match = self.QUOTE_RE.match(line)
|
||||||
|
if quote_match:
|
||||||
|
if not in_quote:
|
||||||
|
flush_table_buffer()
|
||||||
|
in_quote = True
|
||||||
|
quote_buffer = []
|
||||||
|
|
||||||
|
quote_buffer.append(quote_match.group(1))
|
||||||
|
|
||||||
|
else:
|
||||||
|
if in_quote:
|
||||||
|
flush_quote_buffer()
|
||||||
|
if line.strip() != "":
|
||||||
|
if self._is_table_row(line):
|
||||||
|
in_table = True
|
||||||
|
table_buffer = [line]
|
||||||
|
|
||||||
|
else:
|
||||||
|
formatted = self.format_line(line)
|
||||||
|
result_lines.append(formatted)
|
||||||
|
|
||||||
|
# Pass through blank line as separator
|
||||||
|
else: result_lines.append("")
|
||||||
|
|
||||||
|
else:
|
||||||
|
if self._is_table_row(line):
|
||||||
|
if not in_table:
|
||||||
|
in_table = True
|
||||||
|
table_buffer = [line]
|
||||||
|
|
||||||
|
else: table_buffer.append(line)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# Line breaks table, flush buffer
|
||||||
|
if in_table: flush_table_buffer()
|
||||||
|
formatted = self.format_line(line)
|
||||||
|
result_lines.append(formatted)
|
||||||
|
|
||||||
|
# Handle unclosed structures
|
||||||
|
if in_quote: flush_quote_buffer()
|
||||||
|
if in_table: flush_table_buffer()
|
||||||
|
if in_code_block: flush_code_block()
|
||||||
|
|
||||||
|
return '\n'.join(result_lines)
|
||||||
|
|
||||||
|
def format_line(self, line, mode="normal"):
|
||||||
|
if mode == "codeblock": return self._escape_literals(line)
|
||||||
|
line = line.replace("\\", "\\\\")
|
||||||
|
|
||||||
|
if line.startswith("-") and not line.startswith("---") and not line.startswith("- "): line = f"\\{line}"
|
||||||
|
if line.startswith("<"): line = f"\\{line}"
|
||||||
|
# if line.startswith(">"): line = f"\\{line}" # Now handled by blockquotes
|
||||||
|
|
||||||
|
if self.HORIZONTAL_RULE_RE.match(line): return self._format_horizontal_rule()
|
||||||
|
|
||||||
|
header_match = self.HEADER_RE.match(line)
|
||||||
|
if header_match: return self._format_header(header_match)
|
||||||
|
|
||||||
|
list_match = self.UNORDERED_LIST_RE.match(line)
|
||||||
|
if list_match: return self._format_list_item(list_match)
|
||||||
|
|
||||||
|
line = self._format_inline(line)
|
||||||
|
|
||||||
|
return line
|
||||||
|
|
||||||
|
def _format_inline(self, text):
|
||||||
|
code_blocks = []
|
||||||
|
def extract_code(match):
|
||||||
|
code_blocks.append(match.group(1))
|
||||||
|
return f"\x00CODE{len(code_blocks)-1}\x00"
|
||||||
|
|
||||||
|
links = []
|
||||||
|
def extract_link(match):
|
||||||
|
links.append((match.group(1), match.group(2)))
|
||||||
|
return f"\x00LINK{len(links)-1}\x00"
|
||||||
|
|
||||||
|
text = self.LINK_RE.sub(extract_link, text)
|
||||||
|
text = self.INLINE_CODE_RE.sub(extract_code, text)
|
||||||
|
text = self.BOLD_RE.sub(self._bold_sub, text)
|
||||||
|
text = self.ITALIC_RE.sub(self._italic_sub, text)
|
||||||
|
|
||||||
|
def restore_link(match):
|
||||||
|
idx = int(match.group(1))
|
||||||
|
text, url = links[idx]
|
||||||
|
|
||||||
|
anchor_components = url.split("#")
|
||||||
|
url = anchor_components[0]
|
||||||
|
anchor = anchor_components[1] if len(anchor_components) > 1 else ""
|
||||||
|
|
||||||
|
if not ":/" in url:
|
||||||
|
url = f"{self.local_url_scope}{url}"
|
||||||
|
if anchor: url = f"{url}|anchor={anchor}"
|
||||||
|
|
||||||
|
undl = "`_" if self.underline_links else ""
|
||||||
|
bold = "`!" if self.bold_links else ""
|
||||||
|
text = text.replace('`', '')
|
||||||
|
link = f"{undl}{bold}`[{text}`{url}]{bold}{undl}"
|
||||||
|
|
||||||
|
if self.link_color and len(self.link_color) == 3: link = f"`F{self.link_color}{link}`f"
|
||||||
|
if self.link_color and len(self.link_color) == 6: link = f"`FT{self.link_color}{link}`f"
|
||||||
|
|
||||||
|
return link
|
||||||
|
|
||||||
|
text = re.sub(r'\x00LINK(\d+)\x00', restore_link, text)
|
||||||
|
|
||||||
|
def restore_code(match):
|
||||||
|
idx = int(match.group(1))
|
||||||
|
content = code_blocks[idx]
|
||||||
|
content = content.replace('`', '\\`')
|
||||||
|
return f"{self.CODE_BG_INLINE}{self.CODE_FG}{content}{self.CODE_RESET}"
|
||||||
|
|
||||||
|
text = re.sub(r'\x00CODE(\d+)\x00', restore_code, text)
|
||||||
|
return text
|
||||||
|
|
||||||
|
def _highlight_inline_code(self, content):
|
||||||
|
if not self.syntax_highlighter: return None
|
||||||
|
return self.syntax_highlighter.highlight(content, language=None)
|
||||||
|
|
||||||
|
def _bold_sub(self, match):
|
||||||
|
content = match.group(1) or match.group(2)
|
||||||
|
return f"{self.BOLD}{content}{self.BOLD_END}"
|
||||||
|
|
||||||
|
def _italic_sub(self, match):
|
||||||
|
content = match.group(1) or match.group(2)
|
||||||
|
return f"{self.ITALIC}{content}{self.ITALIC_END}"
|
||||||
|
|
||||||
|
def _format_header(self, match):
|
||||||
|
hashes = match.group(1)
|
||||||
|
content = match.group(2)
|
||||||
|
level = len(hashes)
|
||||||
|
prefix = ">" * min(level, 6)
|
||||||
|
return f"{prefix}{self._format_inline(content)}"
|
||||||
|
|
||||||
|
def _format_list_item(self, match):
|
||||||
|
indent = match.group(1)
|
||||||
|
content = match.group(3)
|
||||||
|
content = self._format_inline(content)
|
||||||
|
return f"{indent} {self.BULLET} {content}"
|
||||||
|
|
||||||
|
def _format_horizontal_rule(self):
|
||||||
|
return "-"
|
||||||
|
|
||||||
|
def _detect_code_fence(self, line):
|
||||||
|
match = self.CODE_FENCE_RE.match(line)
|
||||||
|
if match:
|
||||||
|
# match.group(2) contains everything after the backticks (language hint)
|
||||||
|
return True, match.group(2)
|
||||||
|
return False, ""
|
||||||
|
|
||||||
|
def _is_table_row(self, line):
|
||||||
|
if '|' not in line: return False
|
||||||
|
match = self.TABLE_ROW_RE.match(line)
|
||||||
|
if match is None: return False
|
||||||
|
content = match.group(1)
|
||||||
|
return '|' in content or line.strip().startswith('|')
|
||||||
|
|
||||||
|
def _is_table_separator(self, line):
|
||||||
|
if '|' not in line: return False
|
||||||
|
match = self.TABLE_SEP_RE.match(line)
|
||||||
|
return match is not None
|
||||||
|
|
||||||
|
def _escape_literals(self, text):
|
||||||
|
return text.replace('`', '\\`')
|
||||||
|
|
||||||
|
def format_table(self, rows, align="c"):
|
||||||
|
if len(rows) < 2: return rows
|
||||||
|
|
||||||
|
# Parse header and separator
|
||||||
|
header_cells = self._parse_table_row(rows[0])
|
||||||
|
alignments = self._parse_table_alignments(rows[1])
|
||||||
|
|
||||||
|
# Ensure alignment count matches header cells
|
||||||
|
while len(alignments) < len(header_cells): alignments.append('left')
|
||||||
|
alignments = alignments[:len(header_cells)]
|
||||||
|
|
||||||
|
# Parse data rows
|
||||||
|
data_rows = []
|
||||||
|
for i in range(2, len(rows)):
|
||||||
|
cells = self._parse_table_row(rows[i])
|
||||||
|
while len(cells) < len(header_cells): cells.append("")
|
||||||
|
cells = cells[:len(header_cells)]
|
||||||
|
data_rows.append(cells)
|
||||||
|
|
||||||
|
# Calculate column widths based on content
|
||||||
|
num_cols = len(header_cells)
|
||||||
|
col_widths = [0] * num_cols
|
||||||
|
|
||||||
|
all_rows = [header_cells] + data_rows
|
||||||
|
for row in all_rows:
|
||||||
|
for i, cell in enumerate(row):
|
||||||
|
formatted = self._format_inline(cell)
|
||||||
|
width = self._visible_width(formatted)
|
||||||
|
col_widths[i] = max(col_widths[i], width)
|
||||||
|
|
||||||
|
# Apply minimum width and calculate total
|
||||||
|
col_widths = [max(w, self.TABLE_MIN_COL_WIDTH) for w in col_widths]
|
||||||
|
|
||||||
|
# Check max_width constraint
|
||||||
|
# Total = sum of columns + 3 chars per column (space + 2 borders) + 1 for final border
|
||||||
|
total_width = sum(col_widths) + (num_cols * 3) + 1
|
||||||
|
|
||||||
|
if total_width > self.max_width:
|
||||||
|
# Reduce widest columns proportionally
|
||||||
|
excess = total_width - self.max_width
|
||||||
|
indexed_widths = [(i, w) for i, w in enumerate(col_widths)]
|
||||||
|
indexed_widths.sort(key=lambda x: -x[1])
|
||||||
|
|
||||||
|
for i, w in indexed_widths:
|
||||||
|
if excess <= 0: break
|
||||||
|
reduction = min(excess, w - self.TABLE_MIN_COL_WIDTH)
|
||||||
|
col_widths[i] -= reduction
|
||||||
|
excess -= reduction
|
||||||
|
|
||||||
|
# Build formatted table
|
||||||
|
result = []
|
||||||
|
|
||||||
|
# Alignment start
|
||||||
|
if align: result.append(f"`{align}")
|
||||||
|
|
||||||
|
# Top border
|
||||||
|
border = self.TABLE_TL
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
border += self.TABLE_H * (w + 2)
|
||||||
|
if i < len(col_widths) - 1: border += self.TABLE_TM
|
||||||
|
else: border += self.TABLE_TR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(border))
|
||||||
|
|
||||||
|
# Header row
|
||||||
|
header_line = self.TABLE_V
|
||||||
|
for i, cell in enumerate(header_cells):
|
||||||
|
formatted = self._format_inline(cell)
|
||||||
|
padded = self._pad_cell(formatted, col_widths[i], 'left')
|
||||||
|
header_line += f" {padded} {self.TABLE_V}"
|
||||||
|
result.append(self._escape_literals(header_line))
|
||||||
|
|
||||||
|
# Separator row
|
||||||
|
sep_line = self.TABLE_ML
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
cell_width = w + 2
|
||||||
|
sep_line += self.TABLE_H * cell_width
|
||||||
|
|
||||||
|
if i < len(col_widths) - 1: sep_line += self.TABLE_MM
|
||||||
|
else: sep_line += self.TABLE_MR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(sep_line))
|
||||||
|
|
||||||
|
# Data rows
|
||||||
|
for row in data_rows:
|
||||||
|
row_line = self.TABLE_V
|
||||||
|
for i, cell in enumerate(row):
|
||||||
|
formatted = self._format_inline(cell)
|
||||||
|
padded = self._pad_cell(formatted, col_widths[i], alignments[i])
|
||||||
|
row_line += f" {padded} {self.TABLE_V}"
|
||||||
|
|
||||||
|
result.append(row_line)
|
||||||
|
|
||||||
|
# Bottom border
|
||||||
|
border = self.TABLE_BL
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
border += self.TABLE_H * (w + 2)
|
||||||
|
if i < len(col_widths) - 1: border += self.TABLE_BM
|
||||||
|
else: border += self.TABLE_BR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(border))
|
||||||
|
|
||||||
|
# End alignment
|
||||||
|
if align: result.append("`a")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def format_table_raw(self, rows, align="c"):
|
||||||
|
if len(rows) < 2: return rows
|
||||||
|
|
||||||
|
# Parse header and separator
|
||||||
|
header_cells = self._parse_table_row(rows[0])
|
||||||
|
alignments = self._parse_table_alignments(rows[1])
|
||||||
|
|
||||||
|
# Ensure alignment count matches header cells
|
||||||
|
while len(alignments) < len(header_cells): alignments.append('left')
|
||||||
|
alignments = alignments[:len(header_cells)]
|
||||||
|
|
||||||
|
# Parse data rows
|
||||||
|
data_rows = []
|
||||||
|
for i in range(2, len(rows)):
|
||||||
|
cells = self._parse_table_row(rows[i])
|
||||||
|
while len(cells) < len(header_cells): cells.append("")
|
||||||
|
cells = cells[:len(header_cells)]
|
||||||
|
data_rows.append(cells)
|
||||||
|
|
||||||
|
# Calculate column widths based on raw content
|
||||||
|
num_cols = len(header_cells)
|
||||||
|
col_widths = [0] * num_cols
|
||||||
|
|
||||||
|
all_rows = [header_cells] + data_rows
|
||||||
|
for row in all_rows:
|
||||||
|
for i, cell in enumerate(row):
|
||||||
|
width = self._visible_width(cell)
|
||||||
|
col_widths[i] = max(col_widths[i], width)
|
||||||
|
|
||||||
|
# Apply minimum width and calculate total
|
||||||
|
col_widths = [max(w, self.TABLE_MIN_COL_WIDTH) for w in col_widths]
|
||||||
|
|
||||||
|
# Check max_width constraint
|
||||||
|
total_width = sum(col_widths) + (num_cols * 3) + 1
|
||||||
|
|
||||||
|
if total_width > self.max_width:
|
||||||
|
# Reduce widest columns proportionally
|
||||||
|
excess = total_width - self.max_width
|
||||||
|
indexed_widths = [(i, w) for i, w in enumerate(col_widths)]
|
||||||
|
indexed_widths.sort(key=lambda x: -x[1])
|
||||||
|
|
||||||
|
for i, w in indexed_widths:
|
||||||
|
if excess <= 0: break
|
||||||
|
reduction = min(excess, w - self.TABLE_MIN_COL_WIDTH)
|
||||||
|
col_widths[i] -= reduction
|
||||||
|
excess -= reduction
|
||||||
|
|
||||||
|
# Build formatted table
|
||||||
|
result = []
|
||||||
|
|
||||||
|
# Alignment start
|
||||||
|
if align: result.append(f"`{align}")
|
||||||
|
|
||||||
|
# Top border
|
||||||
|
border = self.TABLE_TL
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
border += self.TABLE_H * (w + 2)
|
||||||
|
if i < len(col_widths) - 1: border += self.TABLE_TM
|
||||||
|
else: border += self.TABLE_TR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(border))
|
||||||
|
|
||||||
|
# Header row
|
||||||
|
header_line = self.TABLE_V
|
||||||
|
for i, cell in enumerate(header_cells):
|
||||||
|
padded = self._pad_cell(cell, col_widths[i], 'left')
|
||||||
|
header_line += f" {padded} {self.TABLE_V}"
|
||||||
|
result.append(header_line)
|
||||||
|
|
||||||
|
# Separator row - clean horizontal lines without alignment markers
|
||||||
|
sep_line = self.TABLE_ML
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
cell_width = w + 2
|
||||||
|
sep_line += self.TABLE_H * cell_width
|
||||||
|
|
||||||
|
if i < len(col_widths) - 1: sep_line += self.TABLE_MM
|
||||||
|
else: sep_line += self.TABLE_MR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(sep_line))
|
||||||
|
|
||||||
|
# Data rows (with alignment)
|
||||||
|
for row in data_rows:
|
||||||
|
row_line = self.TABLE_V
|
||||||
|
for i, cell in enumerate(row):
|
||||||
|
padded = self._pad_cell(cell, col_widths[i], alignments[i])
|
||||||
|
row_line += f" {padded} {self.TABLE_V}"
|
||||||
|
|
||||||
|
result.append(row_line)
|
||||||
|
|
||||||
|
# Bottom border
|
||||||
|
border = self.TABLE_BL
|
||||||
|
for i, w in enumerate(col_widths):
|
||||||
|
border += self.TABLE_H * (w + 2)
|
||||||
|
if i < len(col_widths) - 1: border += self.TABLE_BM
|
||||||
|
else: border += self.TABLE_BR
|
||||||
|
|
||||||
|
result.append(self._escape_literals(border))
|
||||||
|
|
||||||
|
# End alignment
|
||||||
|
if align: result.append("`a")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _parse_table_row(self, line):
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('|'): line = line[1:]
|
||||||
|
if line.endswith('|'): line = line[:-1]
|
||||||
|
|
||||||
|
cells = []
|
||||||
|
current = ""
|
||||||
|
escaped = False
|
||||||
|
for char in line:
|
||||||
|
if escaped:
|
||||||
|
current += char
|
||||||
|
escaped = False
|
||||||
|
elif char == '\\':
|
||||||
|
escaped = True
|
||||||
|
elif char == '|':
|
||||||
|
cells.append(current.strip())
|
||||||
|
current = ""
|
||||||
|
else:
|
||||||
|
current += char
|
||||||
|
|
||||||
|
cells.append(current.strip())
|
||||||
|
return cells
|
||||||
|
|
||||||
|
def _parse_table_alignments(self, line):
|
||||||
|
cells = self._parse_table_row(line)
|
||||||
|
alignments = []
|
||||||
|
for cell in cells:
|
||||||
|
cell = cell.strip()
|
||||||
|
if cell.startswith(':') and cell.endswith(':'): alignments.append('center')
|
||||||
|
elif cell.endswith(':'): alignments.append('right')
|
||||||
|
else: alignments.append('left')
|
||||||
|
|
||||||
|
return alignments
|
||||||
|
|
||||||
|
def _visible_width(self, text):
|
||||||
|
text = re.sub(r'`[FB][0-9a-fA-F]{3}', '', text)
|
||||||
|
text = re.sub(r'`[FB]T[0-9a-fA-F]{6}', '', text)
|
||||||
|
text = re.sub(r'`[!*_=]', '', text)
|
||||||
|
text = re.sub(r'`f`b', '', text)
|
||||||
|
text = re.sub(r'`f', '', text)
|
||||||
|
text = re.sub(r'`b', '', text)
|
||||||
|
return self.display_width(text)
|
||||||
|
|
||||||
|
def _pad_cell(self, text, width, align):
|
||||||
|
text = self._truncate_cell(text, width)
|
||||||
|
text_width = self._visible_width(text)
|
||||||
|
padding = width - text_width
|
||||||
|
|
||||||
|
if align == 'right':
|
||||||
|
return " " * padding + text
|
||||||
|
elif align == 'center':
|
||||||
|
left = padding // 2
|
||||||
|
right = padding - left
|
||||||
|
return " " * left + text + " " * right
|
||||||
|
else:
|
||||||
|
return text + " " * padding
|
||||||
|
|
||||||
|
def _truncate_cell(self, text, width):
|
||||||
|
if self._visible_width(text) <= width: return text
|
||||||
|
|
||||||
|
truncation_point = len(text)
|
||||||
|
while truncation_point > 0 and self._visible_width(text[0:truncation_point]) >= width:
|
||||||
|
truncation_point -= 1
|
||||||
|
|
||||||
|
truncated = text[:truncation_point]
|
||||||
|
|
||||||
|
# Yes, this is convoluted, but if someone else has
|
||||||
|
# a better idea on how to handle unclosed micron
|
||||||
|
# tags in the truncated cells, I'm all ears.
|
||||||
|
active_tags = set()
|
||||||
|
fg_active = False
|
||||||
|
bg_active = False
|
||||||
|
|
||||||
|
i = 0
|
||||||
|
while i < len(truncated):
|
||||||
|
if truncated[i] == '`':
|
||||||
|
if i + 1 < len(truncated):
|
||||||
|
tag_char = truncated[i + 1]
|
||||||
|
|
||||||
|
if tag_char in '!*_=':
|
||||||
|
if tag_char in active_tags: active_tags.remove(tag_char)
|
||||||
|
else: active_tags.add(tag_char)
|
||||||
|
i += 2
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif tag_char == 'f':
|
||||||
|
fg_active = False
|
||||||
|
i += 2
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif tag_char == 'b':
|
||||||
|
bg_active = False
|
||||||
|
i += 2
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif tag_char == 'F':
|
||||||
|
fg_active = True
|
||||||
|
if i + 2 < len(truncated) and truncated[i + 2] == 'T': i += 8
|
||||||
|
else: i += 5
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif tag_char == 'B':
|
||||||
|
bg_active = True
|
||||||
|
if i + 2 < len(truncated) and truncated[i + 2] == 'T': i += 8
|
||||||
|
else: i += 5
|
||||||
|
continue
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
closers = []
|
||||||
|
if fg_active: closers.append('`f')
|
||||||
|
if bg_active: closers.append('`b')
|
||||||
|
for fmt in active_tags: closers.append(f'`{fmt}')
|
||||||
|
|
||||||
|
return truncated + ''.join(closers) + "…"
|
||||||
|
|
||||||
|
def _wrap_text(self, text, width):
|
||||||
|
if not text: return [""]
|
||||||
|
|
||||||
|
words = text.split(' ')
|
||||||
|
lines = []
|
||||||
|
current_line = ""
|
||||||
|
current_width = 0
|
||||||
|
|
||||||
|
for word in words:
|
||||||
|
if not word: continue
|
||||||
|
|
||||||
|
word_width = self._visible_width(word)
|
||||||
|
|
||||||
|
# Check if word alone exceeds width to force break it
|
||||||
|
if word_width > width:
|
||||||
|
if current_line:
|
||||||
|
lines.append(current_line)
|
||||||
|
current_line = ""
|
||||||
|
current_width = 0
|
||||||
|
|
||||||
|
# Force break the long word character by character
|
||||||
|
remaining = word
|
||||||
|
while remaining:
|
||||||
|
# Binary search for how many characters fit
|
||||||
|
low, high = 1, len(remaining)
|
||||||
|
fit_chars = 0
|
||||||
|
|
||||||
|
while low <= high:
|
||||||
|
mid = (low + high) // 2
|
||||||
|
test_substr = remaining[:mid]
|
||||||
|
test_width = self._visible_width(test_substr)
|
||||||
|
|
||||||
|
if test_width <= width:
|
||||||
|
fit_chars = mid
|
||||||
|
low = mid + 1
|
||||||
|
else:
|
||||||
|
high = mid - 1
|
||||||
|
|
||||||
|
if fit_chars == 0: fit_chars = 1 # Need to force progress
|
||||||
|
|
||||||
|
lines.append(remaining[:fit_chars])
|
||||||
|
remaining = remaining[fit_chars:]
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Check if word fits on current line
|
||||||
|
space_width = 1 if current_line else 0
|
||||||
|
if current_width + space_width + word_width <= width:
|
||||||
|
if current_line:
|
||||||
|
current_line += " " + word
|
||||||
|
current_width += space_width + word_width
|
||||||
|
else:
|
||||||
|
current_line = word
|
||||||
|
current_width = word_width
|
||||||
|
else:
|
||||||
|
# Flush current line and start new one
|
||||||
|
lines.append(current_line)
|
||||||
|
current_line = word
|
||||||
|
current_width = word_width
|
||||||
|
|
||||||
|
# Don't forget the last line
|
||||||
|
if current_line: lines.append(current_line)
|
||||||
|
|
||||||
|
return lines if lines else [""]
|
||||||
|
|
||||||
|
|
||||||
|
def convert_markdown_to_micron(text):
|
||||||
|
converter = MarkdownToMicron()
|
||||||
|
return converter.format_block(text)
|
||||||
|
|
||||||
|
def medium_path_timeout(default_timeout):
|
||||||
|
return max(default_timeout, RNS.Reticulum.get_instance().get_medium_path_timeout())
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2023 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -47,19 +55,12 @@ def main():
|
||||||
parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
|
parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
|
||||||
parser.add_argument('-v', '--verbose', action='count', default=0)
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
||||||
parser.add_argument('-q', '--quiet', action='count', default=0)
|
parser.add_argument('-q', '--quiet', action='count', default=0)
|
||||||
parser.add_argument("--exampleconfig", action='store_true', default=False, help="print verbose configuration example to stdout and exit")
|
parser.add_argument("--version", action="version", version="rnir {version}".format(version=__version__))
|
||||||
parser.add_argument("--version", action="version", version="ir {version}".format(version=__version__))
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.exampleconfig:
|
if args.config: configarg = args.config
|
||||||
print(__example_rns_config__)
|
else: configarg = None
|
||||||
exit()
|
|
||||||
|
|
||||||
if args.config:
|
|
||||||
configarg = args.config
|
|
||||||
else:
|
|
||||||
configarg = None
|
|
||||||
|
|
||||||
program_setup(configdir = configarg, verbosity=args.verbose, quietness=args.quiet)
|
program_setup(configdir = configarg, verbosity=args.verbose, quietness=args.quiet)
|
||||||
|
|
||||||
|
|
@ -67,8 +68,5 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
__example_rns_config__ = '''# This is an example Identity Resolver file.
|
|
||||||
'''
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2018-2025 Mark Qvist
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -41,9 +49,9 @@ import RNS
|
||||||
RNS.logtimefmt = "%H:%M:%S"
|
RNS.logtimefmt = "%H:%M:%S"
|
||||||
RNS.compact_log_fmt = True
|
RNS.compact_log_fmt = True
|
||||||
|
|
||||||
program_version = "2.4.0"
|
program_version = "2.5.0"
|
||||||
eth_addr = "0xFDabC71AC4c0C78C95aDDDe3B4FA19d6273c5E73"
|
eth_addr = "0x91C421DdfB8a30a49A71d63447ddb54cEBe3465E"
|
||||||
btc_addr = "35G9uWVzrpJJibzUwpNUQGQNFzLirhrYAH"
|
btc_addr = "bc1pgqgu8h8xvj4jtafslq396v7ju7hkgymyrzyqft4llfslz5vp99psqfk3a6"
|
||||||
xmr_addr = "87HcDx6jRSkMQ9nPRd5K9hGGpZLn2s7vWETjMaVM5KfV4TD36NcYa8J8WSxhTSvBzzFpqDwp2fg5GX2moZ7VAP9QMZCZGET"
|
xmr_addr = "87HcDx6jRSkMQ9nPRd5K9hGGpZLn2s7vWETjMaVM5KfV4TD36NcYa8J8WSxhTSvBzzFpqDwp2fg5GX2moZ7VAP9QMZCZGET"
|
||||||
|
|
||||||
rnode = None
|
rnode = None
|
||||||
|
|
@ -89,10 +97,17 @@ class KISS():
|
||||||
CMD_BT_CTRL = 0x46
|
CMD_BT_CTRL = 0x46
|
||||||
CMD_BT_PIN = 0x62
|
CMD_BT_PIN = 0x62
|
||||||
CMD_DIS_IA = 0x69
|
CMD_DIS_IA = 0x69
|
||||||
|
CMD_WIFI_MODE = 0x6A
|
||||||
|
CMD_WIFI_SSID = 0x6B
|
||||||
|
CMD_WIFI_PSK = 0x6C
|
||||||
|
CMD_WIFI_CHN = 0x6E
|
||||||
|
CMD_WIFI_IP = 0x84
|
||||||
|
CMD_WIFI_NM = 0x85
|
||||||
CMD_BOARD = 0x47
|
CMD_BOARD = 0x47
|
||||||
CMD_PLATFORM = 0x48
|
CMD_PLATFORM = 0x48
|
||||||
CMD_MCU = 0x49
|
CMD_MCU = 0x49
|
||||||
CMD_FW_VERSION = 0x50
|
CMD_FW_VERSION = 0x50
|
||||||
|
CMD_CFG_READ = 0x6D
|
||||||
CMD_ROM_READ = 0x51
|
CMD_ROM_READ = 0x51
|
||||||
CMD_ROM_WRITE = 0x52
|
CMD_ROM_WRITE = 0x52
|
||||||
CMD_ROM_WIPE = 0x59
|
CMD_ROM_WIPE = 0x59
|
||||||
|
|
@ -159,6 +174,7 @@ class ROM():
|
||||||
MODEL_B9 = 0xB9
|
MODEL_B9 = 0xB9
|
||||||
MODEL_B4_TCXO = 0x04 # The TCXO model codes are only used here to select the correct firmware,
|
MODEL_B4_TCXO = 0x04 # The TCXO model codes are only used here to select the correct firmware,
|
||||||
MODEL_B9_TCXO = 0x09 # actual model codes in firmware is still 0xB4 and 0xB9.
|
MODEL_B9_TCXO = 0x09 # actual model codes in firmware is still 0xB4 and 0xB9.
|
||||||
|
|
||||||
PRODUCT_H32_V2 = 0xC0
|
PRODUCT_H32_V2 = 0xC0
|
||||||
MODEL_C4 = 0xC4
|
MODEL_C4 = 0xC4
|
||||||
MODEL_C9 = 0xC9
|
MODEL_C9 = 0xC9
|
||||||
|
|
@ -167,6 +183,9 @@ class ROM():
|
||||||
MODEL_C5 = 0xC5
|
MODEL_C5 = 0xC5
|
||||||
MODEL_CA = 0xCA
|
MODEL_CA = 0xCA
|
||||||
|
|
||||||
|
PRODUCT_H32_V4 = 0xC3
|
||||||
|
MODEL_C8 = 0xC8 # 868/915/923 MHz with PA
|
||||||
|
|
||||||
PRODUCT_TBEAM = 0xE0
|
PRODUCT_TBEAM = 0xE0
|
||||||
MODEL_E4 = 0xE4
|
MODEL_E4 = 0xE4
|
||||||
MODEL_E9 = 0xE9
|
MODEL_E9 = 0xE9
|
||||||
|
|
@ -199,6 +218,11 @@ class ROM():
|
||||||
MODEL_C6 = 0xC6 # Heltec Mesh Node T114, 470-510 MHz (HT-n5262-LF)
|
MODEL_C6 = 0xC6 # Heltec Mesh Node T114, 470-510 MHz (HT-n5262-LF)
|
||||||
MODEL_C7 = 0xC7 # Heltec Mesh Node T114, 863-928 MHz (HT-n5262-HF)
|
MODEL_C7 = 0xC7 # Heltec Mesh Node T114, 863-928 MHz (HT-n5262-HF)
|
||||||
|
|
||||||
|
PRODUCT_XIAO_S3 = 0xEB
|
||||||
|
BOARD_XIAO_S3 = 0x3E
|
||||||
|
MODEL_DE = 0xDE # Xiao ESP32S3 with Wio-SX1262 module, 433 MHz
|
||||||
|
MODEL_DD = 0xDD # Xiao ESP32S3 with Wio-SX1262 module, 868 MHz
|
||||||
|
|
||||||
PRODUCT_HMBRW = 0xF0
|
PRODUCT_HMBRW = 0xF0
|
||||||
MODEL_FF = 0xFF
|
MODEL_FF = 0xFF
|
||||||
MODEL_FE = 0xFE
|
MODEL_FE = 0xFE
|
||||||
|
|
@ -228,6 +252,12 @@ class ROM():
|
||||||
ADDR_CONF_PINT = 0xB6
|
ADDR_CONF_PINT = 0xB6
|
||||||
ADDR_CONF_BSET = 0xB7
|
ADDR_CONF_BSET = 0xB7
|
||||||
ADDR_CONF_DIA = 0xB9
|
ADDR_CONF_DIA = 0xB9
|
||||||
|
ADDR_CONF_WIFI = 0xBA
|
||||||
|
ADDR_CONF_WCHN = 0xBB
|
||||||
|
ADDR_CONF_SSID = 0x00
|
||||||
|
ADDR_CONF_PSK = 0x21
|
||||||
|
ADDR_CONF_IP = 0x42
|
||||||
|
ADDR_CONF_NM = 0x46
|
||||||
|
|
||||||
INFO_LOCK_BYTE = 0x73
|
INFO_LOCK_BYTE = 0x73
|
||||||
CONF_OK_BYTE = 0x73
|
CONF_OK_BYTE = 0x73
|
||||||
|
|
@ -257,10 +287,12 @@ products = {
|
||||||
ROM.PRODUCT_T32_21: "LilyGO LoRa32 v2.1",
|
ROM.PRODUCT_T32_21: "LilyGO LoRa32 v2.1",
|
||||||
ROM.PRODUCT_H32_V2: "Heltec LoRa32 v2",
|
ROM.PRODUCT_H32_V2: "Heltec LoRa32 v2",
|
||||||
ROM.PRODUCT_H32_V3: "Heltec LoRa32 v3",
|
ROM.PRODUCT_H32_V3: "Heltec LoRa32 v3",
|
||||||
|
ROM.PRODUCT_H32_V4: "Heltec LoRa32 v4",
|
||||||
ROM.PRODUCT_TECHO: "LilyGO T-Echo",
|
ROM.PRODUCT_TECHO: "LilyGO T-Echo",
|
||||||
ROM.PRODUCT_RAK4631: "RAK4631",
|
ROM.PRODUCT_RAK4631: "RAK4631",
|
||||||
ROM.PRODUCT_OPENCOM_XL: "openCom XL",
|
ROM.PRODUCT_OPENCOM_XL: "openCom XL",
|
||||||
ROM.PRODUCT_HELTEC_T114: "Heltec Mesh Node T114",
|
ROM.PRODUCT_HELTEC_T114: "Heltec Mesh Node T114",
|
||||||
|
ROM.PRODUCT_XIAO_S3: "Seeed XIAO ESP32S3 Wio-SX1262",
|
||||||
}
|
}
|
||||||
|
|
||||||
platforms = {
|
platforms = {
|
||||||
|
|
@ -300,6 +332,7 @@ models = {
|
||||||
0xC9: [850000000, 950000000, 17, "850 - 950 MHz", "rnode_firmware_heltec32v2.zip", "SX1276"],
|
0xC9: [850000000, 950000000, 17, "850 - 950 MHz", "rnode_firmware_heltec32v2.zip", "SX1276"],
|
||||||
0xC5: [420000000, 520000000, 22, "420 - 520 MHz", "rnode_firmware_heltec32v3.zip", "SX1268"],
|
0xC5: [420000000, 520000000, 22, "420 - 520 MHz", "rnode_firmware_heltec32v3.zip", "SX1268"],
|
||||||
0xCA: [850000000, 950000000, 22, "850 - 950 MHz", "rnode_firmware_heltec32v3.zip", "SX1262"],
|
0xCA: [850000000, 950000000, 22, "850 - 950 MHz", "rnode_firmware_heltec32v3.zip", "SX1262"],
|
||||||
|
0xC8: [860000000, 930000000, 28, "850 - 950 MHz", "rnode_firmware_heltec32v4pa.zip", "SX1262"],
|
||||||
0xC6: [420000000, 520000000, 22, "420 - 520 MHz", "rnode_firmware_heltec_t114.zip", "SX1268"],
|
0xC6: [420000000, 520000000, 22, "420 - 520 MHz", "rnode_firmware_heltec_t114.zip", "SX1268"],
|
||||||
0xC7: [850000000, 950000000, 22, "850 - 950 MHz", "rnode_firmware_heltec_t114.zip", "SX1262"],
|
0xC7: [850000000, 950000000, 22, "850 - 950 MHz", "rnode_firmware_heltec_t114.zip", "SX1262"],
|
||||||
0xE4: [420000000, 520000000, 17, "420 - 520 MHz", "rnode_firmware_tbeam.zip", "SX1278"],
|
0xE4: [420000000, 520000000, 17, "420 - 520 MHz", "rnode_firmware_tbeam.zip", "SX1278"],
|
||||||
|
|
@ -317,6 +350,8 @@ models = {
|
||||||
0x16: [779000000, 928000000, 22, "430 - 510 Mhz", "rnode_firmware_techo.zip", "SX1262"],
|
0x16: [779000000, 928000000, 22, "430 - 510 Mhz", "rnode_firmware_techo.zip", "SX1262"],
|
||||||
0x17: [779000000, 928000000, 22, "779 - 928 Mhz", "rnode_firmware_techo.zip", "SX1262"],
|
0x17: [779000000, 928000000, 22, "779 - 928 Mhz", "rnode_firmware_techo.zip", "SX1262"],
|
||||||
0x21: [820000000, 960000000, 22, "820 - 960 MHz", "rnode_firmware_opencom_xl.zip", "SX1262 + SX1280"],
|
0x21: [820000000, 960000000, 22, "820 - 960 MHz", "rnode_firmware_opencom_xl.zip", "SX1262 + SX1280"],
|
||||||
|
0xDE: [420000000, 520000000, 22, "420 - 520 MHz", "rnode_firmware_xiao_esp32s3.zip", "SX1262"],
|
||||||
|
0xDD: [850000000, 950000000, 22, "850 - 950 MHz", "rnode_firmware_xiao_esp32s3.zip", "SX1262"],
|
||||||
0xFE: [100000000, 1100000000, 17, "(Band capabilities unknown)", None, "Unknown"],
|
0xFE: [100000000, 1100000000, 17, "(Band capabilities unknown)", None, "Unknown"],
|
||||||
0xFF: [100000000, 1100000000, 14, "(Band capabilities unknown)", None, "Unknown"],
|
0xFF: [100000000, 1100000000, 14, "(Band capabilities unknown)", None, "Unknown"],
|
||||||
}
|
}
|
||||||
|
|
@ -380,6 +415,7 @@ class RNode():
|
||||||
self.platform = None
|
self.platform = None
|
||||||
self.mcu = None
|
self.mcu = None
|
||||||
self.eeprom = None
|
self.eeprom = None
|
||||||
|
self.cfg_sector = None
|
||||||
self.major_version = None
|
self.major_version = None
|
||||||
self.minor_version = None
|
self.minor_version = None
|
||||||
self.version = None
|
self.version = None
|
||||||
|
|
@ -439,12 +475,17 @@ class RNode():
|
||||||
in_frame = False
|
in_frame = False
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
command_buffer = b""
|
command_buffer = b""
|
||||||
|
elif (in_frame and byte == KISS.FEND and command == KISS.CMD_CFG_READ):
|
||||||
|
self.cfg_sector = data_buffer
|
||||||
|
in_frame = False
|
||||||
|
data_buffer = b""
|
||||||
|
command_buffer = b""
|
||||||
elif (byte == KISS.FEND):
|
elif (byte == KISS.FEND):
|
||||||
in_frame = True
|
in_frame = True
|
||||||
command = KISS.CMD_UNKNOWN
|
command = KISS.CMD_UNKNOWN
|
||||||
data_buffer = b""
|
data_buffer = b""
|
||||||
command_buffer = b""
|
command_buffer = b""
|
||||||
elif (in_frame and len(data_buffer) < 512):
|
elif (in_frame and len(data_buffer) < 1024):
|
||||||
if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN):
|
if (len(data_buffer) == 0 and command == KISS.CMD_UNKNOWN):
|
||||||
command = byte
|
command = byte
|
||||||
elif (command == KISS.CMD_ROM_READ):
|
elif (command == KISS.CMD_ROM_READ):
|
||||||
|
|
@ -458,6 +499,17 @@ class RNode():
|
||||||
byte = KISS.FESC
|
byte = KISS.FESC
|
||||||
escape = False
|
escape = False
|
||||||
data_buffer = data_buffer+bytes([byte])
|
data_buffer = data_buffer+bytes([byte])
|
||||||
|
elif (command == KISS.CMD_CFG_READ):
|
||||||
|
if (byte == KISS.FESC):
|
||||||
|
escape = True
|
||||||
|
else:
|
||||||
|
if (escape):
|
||||||
|
if (byte == KISS.TFEND):
|
||||||
|
byte = KISS.FEND
|
||||||
|
if (byte == KISS.TFESC):
|
||||||
|
byte = KISS.FESC
|
||||||
|
escape = False
|
||||||
|
data_buffer = data_buffer+bytes([byte])
|
||||||
elif (command == KISS.CMD_DATA):
|
elif (command == KISS.CMD_DATA):
|
||||||
if (byte == KISS.FESC):
|
if (byte == KISS.FESC):
|
||||||
escape = True
|
escape = True
|
||||||
|
|
@ -766,6 +818,92 @@ class RNode():
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while sending firmware update command to device")
|
raise IOError("An IO error occurred while sending firmware update command to device")
|
||||||
|
|
||||||
|
def set_wifi_mode(self, mode):
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_MODE, mode])+bytes([KISS.FEND])
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command):
|
||||||
|
raise IOError("An IO error occurred while sending wifi mode command to device")
|
||||||
|
|
||||||
|
def set_wifi_channel(self, channel):
|
||||||
|
try: ch = int(channel)
|
||||||
|
except: raise ValueError("Invalid WiFi channel")
|
||||||
|
if ch < 1 or ch > 14: raise ValueError("Invalid WiFi channel")
|
||||||
|
ch_data = bytes([ch])
|
||||||
|
data = KISS.escape(ch_data)
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_CHN])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command):
|
||||||
|
raise IOError("An IO error occurred while sending wifi channel to device")
|
||||||
|
|
||||||
|
def set_wifi_ip(self, ip):
|
||||||
|
if ip == None: ip_data = bytes([0x00, 0x00, 0x00, 0x00])
|
||||||
|
else:
|
||||||
|
ip_data = b""
|
||||||
|
if not type(ip) == str: raise TypeError("Invalid IP address")
|
||||||
|
octets = ip.split(".")
|
||||||
|
if not len(octets) == 4: raise ValueError("Invalid IP address length")
|
||||||
|
try:
|
||||||
|
for i in range(0, 4):
|
||||||
|
octet = int(octets[i])
|
||||||
|
if octet < 0 or octet > 255: raise ValueError("Invalid IP octet value")
|
||||||
|
else: ip_data += bytes([octet])
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"Could not decode IP address octet: {e}")
|
||||||
|
|
||||||
|
data = KISS.escape(ip_data)
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_IP])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command): raise IOError("An IO error occurred while sending wifi IP address to device")
|
||||||
|
|
||||||
|
def set_wifi_nm(self, nm):
|
||||||
|
if nm == None: nm_data = bytes([0x00, 0x00, 0x00, 0x00])
|
||||||
|
else:
|
||||||
|
nm_data = b""
|
||||||
|
if not type(nm) == str: raise TypeError("Invalid IP address")
|
||||||
|
octets = nm.split(".")
|
||||||
|
if not len(octets) == 4: raise ValueError("Invalid IP address length")
|
||||||
|
try:
|
||||||
|
for i in range(0, 4):
|
||||||
|
octet = int(octets[i])
|
||||||
|
if octet < 0 or octet > 255: raise ValueError("Invalid IP octet value")
|
||||||
|
else: nm_data += bytes([octet])
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"Could not decode IP address octet: {e}")
|
||||||
|
|
||||||
|
data = KISS.escape(nm_data)
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_NM])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command): raise IOError("An IO error occurred while sending wifi netmask to device")
|
||||||
|
|
||||||
|
def set_wifi_ssid(self, ssid):
|
||||||
|
if ssid == None: data = bytes([0x00])
|
||||||
|
else:
|
||||||
|
ssid_data = ssid.encode("utf-8")+bytes([0x00])
|
||||||
|
if len(ssid_data) < 0 or len(ssid_data) > 33: raise ValueError("Invalid SSID length")
|
||||||
|
data = KISS.escape(ssid_data)
|
||||||
|
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_SSID])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command):
|
||||||
|
raise IOError("An IO error occurred while sending wifi SSID to device")
|
||||||
|
|
||||||
|
def set_wifi_psk(self, psk):
|
||||||
|
if psk == None: data = bytes([0x00])
|
||||||
|
else:
|
||||||
|
psk_data = psk.encode("utf-8")+bytes([0x00])
|
||||||
|
if len(psk_data) < 8 or len(psk_data) > 33: raise ValueError("Invalid psk length")
|
||||||
|
data = KISS.escape(psk_data)
|
||||||
|
|
||||||
|
kiss_command = bytes([KISS.FEND])+bytes([KISS.CMD_WIFI_PSK])+data+bytes([KISS.FEND])
|
||||||
|
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command):
|
||||||
|
raise IOError("An IO error occurred while sending wifi SSID to device")
|
||||||
|
|
||||||
def initRadio(self):
|
def initRadio(self):
|
||||||
self.setFrequency()
|
self.setFrequency()
|
||||||
self.setBandwidth()
|
self.setBandwidth()
|
||||||
|
|
@ -872,7 +1010,7 @@ class RNode():
|
||||||
kiss_command = bytes([KISS.FEND, KISS.CMD_ROM_READ, 0x00, KISS.FEND])
|
kiss_command = bytes([KISS.FEND, KISS.CMD_ROM_READ, 0x00, KISS.FEND])
|
||||||
written = self.serial.write(kiss_command)
|
written = self.serial.write(kiss_command)
|
||||||
if written != len(kiss_command):
|
if written != len(kiss_command):
|
||||||
raise IOError("An IO error occurred while configuring radio state")
|
raise IOError("An IO error occurred while downloading EEPROM")
|
||||||
|
|
||||||
sleep(0.6)
|
sleep(0.6)
|
||||||
if self.eeprom == None:
|
if self.eeprom == None:
|
||||||
|
|
@ -881,6 +1019,15 @@ class RNode():
|
||||||
else:
|
else:
|
||||||
self.parse_eeprom()
|
self.parse_eeprom()
|
||||||
|
|
||||||
|
def download_cfg_sector(self):
|
||||||
|
self.cfg_sector = None
|
||||||
|
kiss_command = bytes([KISS.FEND, KISS.CMD_CFG_READ, 0x00, KISS.FEND])
|
||||||
|
written = self.serial.write(kiss_command)
|
||||||
|
if written != len(kiss_command):
|
||||||
|
raise IOError("An IO error occurred while downloading config sector")
|
||||||
|
|
||||||
|
sleep(0.6)
|
||||||
|
|
||||||
def parse_eeprom(self):
|
def parse_eeprom(self):
|
||||||
global squashvw;
|
global squashvw;
|
||||||
try:
|
try:
|
||||||
|
|
@ -1038,8 +1185,8 @@ class RNode():
|
||||||
print(" Always use a firmware downloaded as binaries or compiled from source")
|
print(" Always use a firmware downloaded as binaries or compiled from source")
|
||||||
print(" from one of the following locations:")
|
print(" from one of the following locations:")
|
||||||
print(" ")
|
print(" ")
|
||||||
print(" https://unsigned.io/rnode")
|
|
||||||
print(" https://github.com/markqvist/rnode_firmware")
|
print(" https://github.com/markqvist/rnode_firmware")
|
||||||
|
print(" https://github.com/liberatedsystems/RNode_Firmware_CE")
|
||||||
print(" ")
|
print(" ")
|
||||||
print(" You can reflash and bootstrap this device to a verifiable state")
|
print(" You can reflash and bootstrap this device to a verifiable state")
|
||||||
print(" by using this utility. It is recommended to do so NOW!")
|
print(" by using this utility. It is recommended to do so NOW!")
|
||||||
|
|
@ -1081,7 +1228,7 @@ class RNode():
|
||||||
|
|
||||||
selected_version = None
|
selected_version = None
|
||||||
selected_hash = None
|
selected_hash = None
|
||||||
firmware_version_url = "https://unsigned.io/firmware/latest/?v="+program_version+"&variant="
|
firmware_version_url = "https://github.com/markqvist/rnode_firmware/releases/latest/download/release.json"
|
||||||
fallback_firmware_version_url = "https://github.com/markqvist/rnode_firmware/releases/latest/download/release.json"
|
fallback_firmware_version_url = "https://github.com/markqvist/rnode_firmware/releases/latest/download/release.json"
|
||||||
def ensure_firmware_file(fw_filename):
|
def ensure_firmware_file(fw_filename):
|
||||||
global selected_version, selected_hash, upd_nocheck
|
global selected_version, selected_hash, upd_nocheck
|
||||||
|
|
@ -1122,9 +1269,15 @@ def ensure_firmware_file(fw_filename):
|
||||||
try:
|
try:
|
||||||
# if custom firmware url, download latest release
|
# if custom firmware url, download latest release
|
||||||
if selected_version == None and fw_url == None:
|
if selected_version == None and fw_url == None:
|
||||||
version_url = firmware_version_url+fw_filename
|
urlretrieve(firmware_version_url, UPD_DIR+"/release_info.json")
|
||||||
RNS.log("Retrieving latest version info from "+version_url)
|
import json
|
||||||
urlretrieve(firmware_version_url+fw_filename, UPD_DIR+"/"+fw_filename+".version.latest")
|
with open(UPD_DIR+"/release_info.json", "rb") as rif:
|
||||||
|
rdat = json.loads(rif.read())
|
||||||
|
variant = rdat[fw_filename]
|
||||||
|
with open(UPD_DIR+"/"+fw_filename+".version.latest", "wb") as verf:
|
||||||
|
inf_str = str(variant["version"])+" "+str(variant["hash"])
|
||||||
|
verf.write(inf_str.encode("utf-8"))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if fw_url != None:
|
if fw_url != None:
|
||||||
if selected_version == None:
|
if selected_version == None:
|
||||||
|
|
@ -1343,6 +1496,14 @@ def main():
|
||||||
parser.add_argument("-B", "--bluetooth-off", action="store_true", help="Turn device bluetooth off")
|
parser.add_argument("-B", "--bluetooth-off", action="store_true", help="Turn device bluetooth off")
|
||||||
parser.add_argument("-p", "--bluetooth-pair", action="store_true", help="Put device into bluetooth pairing mode")
|
parser.add_argument("-p", "--bluetooth-pair", action="store_true", help="Put device into bluetooth pairing mode")
|
||||||
|
|
||||||
|
parser.add_argument("-w", "--wifi", action="store", metavar="mode", default=None, help="Set WiFi mode (OFF, AP or STATION)")
|
||||||
|
parser.add_argument("--channel", action="store", metavar="channel", default=None, help="Set WiFi channel")
|
||||||
|
parser.add_argument("--ssid", action="store", metavar="ssid", default=None, help="Set WiFi SSID (NONE to delete)")
|
||||||
|
parser.add_argument("--psk", action="store", metavar="psk", default=None, help="Set WiFi PSK (NONE to delete)")
|
||||||
|
parser.add_argument("--show-psk", action="store_true", default=False, help="Display stored WiFi PSK")
|
||||||
|
parser.add_argument("--ip", action="store", metavar="ip", default=None, help="Set static WiFi IP address (NONE for DHCP)")
|
||||||
|
parser.add_argument("--nm", action="store", metavar="nm", default=None, help="Set static WiFi network mask (NONE for DHCP)")
|
||||||
|
|
||||||
parser.add_argument("-D", "--display", action="store", metavar="i", type=int, default=None, help="Set display intensity (0-255)")
|
parser.add_argument("-D", "--display", action="store", metavar="i", type=int, default=None, help="Set display intensity (0-255)")
|
||||||
parser.add_argument("-t", "--timeout", action="store", metavar="s", type=int, default=None, help="Set display timeout in seconds, 0 to disable")
|
parser.add_argument("-t", "--timeout", action="store", metavar="s", type=int, default=None, help="Set display timeout in seconds, 0 to disable")
|
||||||
parser.add_argument("-R", "--rotation", action="store", metavar="rotation", type=int, default=None, help="Set display rotation, valid values are 0 through 3")
|
parser.add_argument("-R", "--rotation", action="store", metavar="rotation", type=int, default=None, help="Set display rotation, valid values are 0 through 3")
|
||||||
|
|
@ -1387,13 +1548,14 @@ def main():
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
def print_donation_block():
|
def print_donation_block():
|
||||||
print(" Ethereum : "+eth_addr)
|
print(" Ethereum : "+eth_addr)
|
||||||
print(" Bitcoin : "+btc_addr)
|
print(" Bitcoin : "+btc_addr)
|
||||||
print(" Monero : "+xmr_addr)
|
print(" Monero : "+xmr_addr)
|
||||||
print(" Ko-Fi : https://ko-fi.com/markqvist")
|
print(" Ko-Fi : https://ko-fi.com/markqvist")
|
||||||
|
print(" LiberaPay : https://liberapay.com/reticulum")
|
||||||
print("")
|
print("")
|
||||||
print(" Info : https://unsigned.io/")
|
print(" Info : https://reticulum.network")
|
||||||
print(" Code : https://github.com/markqvist")
|
print(" Code : https://github.com/markqvist")
|
||||||
|
|
||||||
if args.version:
|
if args.version:
|
||||||
print("rnodeconf "+program_version)
|
print("rnodeconf "+program_version)
|
||||||
|
|
@ -1700,7 +1862,7 @@ def main():
|
||||||
print(" '")
|
print(" '")
|
||||||
print("[1] A specific kind of RNode")
|
print("[1] A specific kind of RNode")
|
||||||
print("")
|
print("")
|
||||||
print(" | Select this option if you have put toghether an RNode")
|
print(" | Select this option if you have put together an RNode")
|
||||||
print(" \\ / of your own design, or if you are prototyping one.")
|
print(" \\ / of your own design, or if you are prototyping one.")
|
||||||
print(" '")
|
print(" '")
|
||||||
print("[2] Homebrew RNode")
|
print("[2] Homebrew RNode")
|
||||||
|
|
@ -1714,12 +1876,14 @@ def main():
|
||||||
print("[6] LilyGO T-Beam")
|
print("[6] LilyGO T-Beam")
|
||||||
print("[7] Heltec LoRa32 v2")
|
print("[7] Heltec LoRa32 v2")
|
||||||
print("[8] Heltec LoRa32 v3")
|
print("[8] Heltec LoRa32 v3")
|
||||||
print("[9] LilyGO LoRa T3S3")
|
print("[9] Heltec LoRa32 v4")
|
||||||
print("[10] RAK4631")
|
print("[10] LilyGO LoRa T3S3")
|
||||||
print("[11] LilyGo T-Echo")
|
print("[11] RAK4631")
|
||||||
print("[12] LilyGO T-Beam Supreme")
|
print("[12] LilyGo T-Echo")
|
||||||
print("[13] LilyGO T-Deck")
|
print("[13] LilyGO T-Beam Supreme")
|
||||||
print("[14] Heltec T114")
|
print("[14] LilyGO T-Deck")
|
||||||
|
print("[15] Heltec T114")
|
||||||
|
print("[16] Seeed XIAO ESP32S3 Wio-SX1262")
|
||||||
print("")
|
print("")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
print("\nEnter the number that matches your device type:\n? ", end="")
|
print("\nEnter the number that matches your device type:\n? ", end="")
|
||||||
|
|
@ -1728,7 +1892,7 @@ def main():
|
||||||
try:
|
try:
|
||||||
c_dev = int(input())
|
c_dev = int(input())
|
||||||
c_mod = False
|
c_mod = False
|
||||||
if c_dev < 1 or c_dev > 14:
|
if c_dev < 1 or c_dev > 16:
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
elif c_dev == 1:
|
elif c_dev == 1:
|
||||||
selected_product = ROM.PRODUCT_RNODE
|
selected_product = ROM.PRODUCT_RNODE
|
||||||
|
|
@ -1745,7 +1909,7 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on homebrew devices should currently be")
|
print("Important! Using RNode firmware on homebrew devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -1761,11 +1925,11 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 12:
|
elif c_dev == 13:
|
||||||
selected_product = ROM.PRODUCT_TBEAM_S_V1
|
selected_product = ROM.PRODUCT_TBEAM_S_V1
|
||||||
clear()
|
clear()
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -1777,11 +1941,11 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 13:
|
elif c_dev == 14:
|
||||||
selected_product = ROM.PRODUCT_TDECK
|
selected_product = ROM.PRODUCT_TDECK
|
||||||
clear()
|
clear()
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -1793,7 +1957,7 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
print("Important! Using RNode firmware on T-Beam devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -1806,7 +1970,7 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -1819,7 +1983,7 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it.")
|
print("who would like to experiment with it.")
|
||||||
print("")
|
print("")
|
||||||
print("Please Note! This device is known to have a faulty battery charging circuit,")
|
print("Please Note! This device is known to have a faulty battery charging circuit,")
|
||||||
|
|
@ -1838,7 +2002,7 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
print("Important! Using RNode firmware on LoRa32 devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -1852,11 +2016,11 @@ def main():
|
||||||
print("Important! Using RNode firmware on Heltec devices should currently be")
|
print("Important! Using RNode firmware on Heltec devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("")
|
print("")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 9:
|
elif c_dev == 10:
|
||||||
selected_product = ROM.PRODUCT_RNODE
|
selected_product = ROM.PRODUCT_RNODE
|
||||||
c_mod = True
|
c_mod = True
|
||||||
clear()
|
clear()
|
||||||
|
|
@ -1867,7 +2031,7 @@ def main():
|
||||||
print("Important! Using RNode firmware on T3S3 devices should currently be")
|
print("Important! Using RNode firmware on T3S3 devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("")
|
print("")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -1881,11 +2045,25 @@ def main():
|
||||||
print("Important! Using RNode firmware on Heltec devices should currently be")
|
print("Important! Using RNode firmware on Heltec devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("")
|
print("")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 10:
|
elif c_dev == 9:
|
||||||
|
selected_product = ROM.PRODUCT_H32_V4
|
||||||
|
clear()
|
||||||
|
print("")
|
||||||
|
print("---------------------------------------------------------------------------")
|
||||||
|
print(" Heltec LoRa32 v4 RNode Installer")
|
||||||
|
print("")
|
||||||
|
print("Important! Using RNode firmware on Heltec devices should currently be")
|
||||||
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
|
print("")
|
||||||
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
|
print("---------------------------------------------------------------------------")
|
||||||
|
input()
|
||||||
|
elif c_dev == 11:
|
||||||
selected_product = ROM.PRODUCT_RAK4631
|
selected_product = ROM.PRODUCT_RAK4631
|
||||||
clear()
|
clear()
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -1894,11 +2072,11 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on RAKwireless devices should currently be")
|
print("Important! Using RNode firmware on RAKwireless devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 11:
|
elif c_dev == 12:
|
||||||
selected_product = ROM.PRODUCT_TECHO
|
selected_product = ROM.PRODUCT_TECHO
|
||||||
clear()
|
clear()
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -1907,11 +2085,11 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on LilyGo T-Echo devices should currently be")
|
print("Important! Using RNode firmware on LilyGo T-Echo devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
elif c_dev == 14:
|
elif c_dev == 15:
|
||||||
selected_product = ROM.PRODUCT_HELTEC_T114
|
selected_product = ROM.PRODUCT_HELTEC_T114
|
||||||
clear()
|
clear()
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -1920,7 +2098,20 @@ def main():
|
||||||
print("")
|
print("")
|
||||||
print("Important! Using RNode firmware on Heltec T114 devices should currently be")
|
print("Important! Using RNode firmware on Heltec T114 devices should currently be")
|
||||||
print("considered experimental. It is not intended for production or critical use.")
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
print("The currently supplied firmware is provided AS-IS as a courtesey to those")
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
|
print("---------------------------------------------------------------------------")
|
||||||
|
input()
|
||||||
|
elif c_dev == 16:
|
||||||
|
selected_product = ROM.PRODUCT_XIAO_S3
|
||||||
|
clear()
|
||||||
|
print("")
|
||||||
|
print("---------------------------------------------------------------------------")
|
||||||
|
print(" SeeedStudio XIAO esp32s3 wio RNode Installer")
|
||||||
|
print("")
|
||||||
|
print("Important! Using RNode firmware on SeeedStudio XIAO/wio devices should currently be")
|
||||||
|
print("considered experimental. It is not intended for production or critical use.")
|
||||||
|
print("The currently supplied firmware is provided AS-IS as a courtesy to those")
|
||||||
print("who would like to experiment with it. Hit enter to continue.")
|
print("who would like to experiment with it. Hit enter to continue.")
|
||||||
print("---------------------------------------------------------------------------")
|
print("---------------------------------------------------------------------------")
|
||||||
input()
|
input()
|
||||||
|
|
@ -2234,6 +2425,7 @@ def main():
|
||||||
print("[2] 868 MHz")
|
print("[2] 868 MHz")
|
||||||
print("[3] 915 MHz")
|
print("[3] 915 MHz")
|
||||||
print("[4] 923 MHz")
|
print("[4] 923 MHz")
|
||||||
|
print("\n? ", end="")
|
||||||
try:
|
try:
|
||||||
c_model = int(input())
|
c_model = int(input())
|
||||||
if c_model < 1 or c_model > 4:
|
if c_model < 1 or c_model > 4:
|
||||||
|
|
@ -2248,6 +2440,24 @@ def main():
|
||||||
print("That band does not exist, exiting now.")
|
print("That band does not exist, exiting now.")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
elif selected_product == ROM.PRODUCT_H32_V4:
|
||||||
|
selected_mcu = ROM.MCU_ESP32
|
||||||
|
print("\nWhat band is this Heltec LoRa32 V4 for?\n")
|
||||||
|
print("[1] 868 MHz (28 dBm output)")
|
||||||
|
print("[2] 915 MHz (28 dBm output)")
|
||||||
|
print("[3] 923 MHz (28 dBm output)")
|
||||||
|
print("\n? ", end="")
|
||||||
|
try:
|
||||||
|
c_model = int(input())
|
||||||
|
if c_model < 1 or c_model > 3:
|
||||||
|
raise ValueError()
|
||||||
|
else:
|
||||||
|
selected_model = ROM.MODEL_C8
|
||||||
|
selected_platform = ROM.PLATFORM_ESP32
|
||||||
|
except Exception as e:
|
||||||
|
print("That band does not exist, exiting now.")
|
||||||
|
exit()
|
||||||
|
|
||||||
elif selected_product == ROM.PRODUCT_HELTEC_T114:
|
elif selected_product == ROM.PRODUCT_HELTEC_T114:
|
||||||
selected_mcu = ROM.MCU_NRF52
|
selected_mcu = ROM.MCU_NRF52
|
||||||
print("\nWhat band is this Heltec T114 for?\n")
|
print("\nWhat band is this Heltec T114 for?\n")
|
||||||
|
|
@ -2255,6 +2465,7 @@ def main():
|
||||||
print("[2] 868 MHz")
|
print("[2] 868 MHz")
|
||||||
print("[3] 915 MHz")
|
print("[3] 915 MHz")
|
||||||
print("[4] 923 MHz")
|
print("[4] 923 MHz")
|
||||||
|
print("\n? ", end="")
|
||||||
try:
|
try:
|
||||||
c_model = int(input())
|
c_model = int(input())
|
||||||
if c_model < 1 or c_model > 4:
|
if c_model < 1 or c_model > 4:
|
||||||
|
|
@ -2268,7 +2479,27 @@ def main():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("That band does not exist, exiting now.")
|
print("That band does not exist, exiting now.")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
elif selected_product == ROM.PRODUCT_XIAO_S3:
|
||||||
|
selected_mcu = ROM.MCU_ESP32
|
||||||
|
print("\nWhat band is this XIAO esp32s3 wio module for?\n")
|
||||||
|
print("[1] 433 MHz")
|
||||||
|
print("[2] 868 MHz")
|
||||||
|
print("\n? ", end="")
|
||||||
|
try:
|
||||||
|
c_model = int(input())
|
||||||
|
if c_model < 1 or c_model > 2:
|
||||||
|
raise ValueError()
|
||||||
|
elif c_model == 1:
|
||||||
|
selected_model = ROM.MODEL_DE
|
||||||
|
selected_platform = ROM.PLATFORM_ESP32
|
||||||
|
elif c_model == 2:
|
||||||
|
selected_model = ROM.MODEL_DD
|
||||||
|
selected_platform = ROM.PLATFORM_ESP32
|
||||||
|
except Exception as e:
|
||||||
|
print("That band does not exist, exiting now.")
|
||||||
|
exit()
|
||||||
|
|
||||||
elif selected_product == ROM.PRODUCT_RAK4631:
|
elif selected_product == ROM.PRODUCT_RAK4631:
|
||||||
selected_mcu = ROM.MCU_NRF52
|
selected_mcu = ROM.MCU_NRF52
|
||||||
print("\nWhat band is this RAK4631 for?\n")
|
print("\nWhat band is this RAK4631 for?\n")
|
||||||
|
|
@ -2826,6 +3057,24 @@ def main():
|
||||||
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
||||||
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v3.partitions",
|
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v3.partitions",
|
||||||
]
|
]
|
||||||
|
elif fw_filename == "rnode_firmware_heltec32v4pa.zip":
|
||||||
|
return [
|
||||||
|
sys.executable, flasher,
|
||||||
|
"--chip", "esp32-s3",
|
||||||
|
"--port", args.port,
|
||||||
|
"--baud", args.baud_flash,
|
||||||
|
"--before", "default_reset",
|
||||||
|
"--after", "hard_reset",
|
||||||
|
"write_flash", "-z",
|
||||||
|
"--flash_mode", "dio",
|
||||||
|
"--flash_freq", "80m",
|
||||||
|
"--flash_size", "16MB",
|
||||||
|
"0xe000", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v4pa.boot_app0",
|
||||||
|
"0x0", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v4pa.bootloader",
|
||||||
|
"0x10000", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v4pa.bin",
|
||||||
|
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
||||||
|
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_heltec32v4pa.partitions",
|
||||||
|
]
|
||||||
elif fw_filename == "rnode_firmware_featheresp32.zip":
|
elif fw_filename == "rnode_firmware_featheresp32.zip":
|
||||||
if numeric_version >= 1.55:
|
if numeric_version >= 1.55:
|
||||||
return [
|
return [
|
||||||
|
|
@ -3060,6 +3309,24 @@ def main():
|
||||||
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
||||||
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_tdeck.partitions",
|
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_tdeck.partitions",
|
||||||
]
|
]
|
||||||
|
elif fw_filename == "rnode_firmware_xiao_esp32s3.zip":
|
||||||
|
return [
|
||||||
|
sys.executable, flasher,
|
||||||
|
"--chip", "esp32s3",
|
||||||
|
"--port", args.port,
|
||||||
|
"--baud", args.baud_flash,
|
||||||
|
"--before", "default_reset",
|
||||||
|
"--after", "hard_reset",
|
||||||
|
"write_flash", "-z",
|
||||||
|
"--flash_mode", "dio",
|
||||||
|
"--flash_freq", "80m",
|
||||||
|
"--flash_size", "8MB",
|
||||||
|
"0xe000", UPD_DIR+"/"+selected_version+"/rnode_firmware_xiao_esp32s3.boot_app0",
|
||||||
|
"0x0", UPD_DIR+"/"+selected_version+"/rnode_firmware_xiao_esp32s3.bootloader",
|
||||||
|
"0x10000", UPD_DIR+"/"+selected_version+"/rnode_firmware_xiao_esp32s3.bin",
|
||||||
|
"0x210000",UPD_DIR+"/"+selected_version+"/console_image.bin",
|
||||||
|
"0x8000", UPD_DIR+"/"+selected_version+"/rnode_firmware_xiao_esp32s3.partitions",
|
||||||
|
]
|
||||||
elif fw_filename == "extracted_rnode_firmware.zip":
|
elif fw_filename == "extracted_rnode_firmware.zip":
|
||||||
return [
|
return [
|
||||||
sys.executable, flasher,
|
sys.executable, flasher,
|
||||||
|
|
@ -3431,17 +3698,14 @@ def main():
|
||||||
graceful_exit()
|
graceful_exit()
|
||||||
|
|
||||||
if args.config:
|
if args.config:
|
||||||
|
rnode.download_cfg_sector()
|
||||||
eeprom_reserved = 200
|
eeprom_reserved = 200
|
||||||
if rnode.platform == ROM.PLATFORM_ESP32:
|
if rnode.platform == ROM.PLATFORM_ESP32: eeprom_size = 296
|
||||||
eeprom_size = 296
|
elif rnode.platform == ROM.PLATFORM_NRF52: eeprom_size = 296
|
||||||
elif rnode.platform == ROM.PLATFORM_NRF52:
|
else: eeprom_size = 4096
|
||||||
eeprom_size = 296
|
|
||||||
else:
|
|
||||||
eeprom_size = 4096
|
|
||||||
|
|
||||||
eeprom_offset = eeprom_size-eeprom_reserved
|
eeprom_offset = eeprom_size-eeprom_reserved
|
||||||
def ea(a):
|
def ea(a): return a+eeprom_offset
|
||||||
return a+eeprom_offset
|
|
||||||
ec_bt = rnode.eeprom[ROM.ADDR_CONF_BT]
|
ec_bt = rnode.eeprom[ROM.ADDR_CONF_BT]
|
||||||
ec_dint = rnode.eeprom[ROM.ADDR_CONF_DINT]
|
ec_dint = rnode.eeprom[ROM.ADDR_CONF_DINT]
|
||||||
ec_dadr = rnode.eeprom[ROM.ADDR_CONF_DADR]
|
ec_dadr = rnode.eeprom[ROM.ADDR_CONF_DADR]
|
||||||
|
|
@ -3451,40 +3715,89 @@ def main():
|
||||||
ec_pint = rnode.eeprom[ROM.ADDR_CONF_PINT]
|
ec_pint = rnode.eeprom[ROM.ADDR_CONF_PINT]
|
||||||
ec_bset = rnode.eeprom[ROM.ADDR_CONF_BSET]
|
ec_bset = rnode.eeprom[ROM.ADDR_CONF_BSET]
|
||||||
ec_dia = rnode.eeprom[ROM.ADDR_CONF_DIA]
|
ec_dia = rnode.eeprom[ROM.ADDR_CONF_DIA]
|
||||||
|
ec_wifi = rnode.eeprom[ROM.ADDR_CONF_WIFI]
|
||||||
|
ec_wchn = rnode.eeprom[ROM.ADDR_CONF_WCHN]
|
||||||
|
ec_ssid = None
|
||||||
|
ec_psk = None
|
||||||
|
ec_ip = None
|
||||||
|
ec_nm = None
|
||||||
|
|
||||||
|
if ec_wchn < 1 or ec_wchn > 14: ec_wchn = 1
|
||||||
|
if rnode.cfg_sector:
|
||||||
|
ssid_bytes = b""
|
||||||
|
for i in range(0, 32):
|
||||||
|
byte = rnode.cfg_sector[ROM.ADDR_CONF_SSID+i]
|
||||||
|
if byte == 0xFF: byte = 0x00
|
||||||
|
if byte == 0x00: break
|
||||||
|
else: ssid_bytes += bytes([byte])
|
||||||
|
|
||||||
|
try: ec_ssid = ssid_bytes.decode("utf-8")
|
||||||
|
except Exception as e: print(f"Error: Could not decode WiFi SSID read from device")
|
||||||
|
|
||||||
|
psk_bytes = b""
|
||||||
|
for i in range(0, 32):
|
||||||
|
byte = rnode.cfg_sector[ROM.ADDR_CONF_PSK+i]
|
||||||
|
if byte == 0xFF: byte = 0x00
|
||||||
|
if byte == 0x00: break
|
||||||
|
else: psk_bytes += bytes([byte])
|
||||||
|
|
||||||
|
ip_bytes = b""
|
||||||
|
for i in range(0, 4):
|
||||||
|
byte = rnode.cfg_sector[ROM.ADDR_CONF_IP+i]
|
||||||
|
ip_bytes += bytes([byte])
|
||||||
|
if len(ip_bytes) == 4: ec_ip = f"{int(ip_bytes[0])}.{int(ip_bytes[1])}.{int(ip_bytes[2])}.{int(ip_bytes[3])}"
|
||||||
|
if ec_ip == "255.255.255.255" or ec_ip == "0.0.0.0": ec_ip = None
|
||||||
|
|
||||||
|
nm_bytes = b""
|
||||||
|
for i in range(0, 4):
|
||||||
|
byte = rnode.cfg_sector[ROM.ADDR_CONF_NM+i]
|
||||||
|
nm_bytes += bytes([byte])
|
||||||
|
if len(nm_bytes) == 4: ec_nm = f"{int(nm_bytes[0])}.{int(nm_bytes[1])}.{int(nm_bytes[2])}.{int(nm_bytes[3])}"
|
||||||
|
if ec_nm == "255.255.255.255" or ec_nm == "0.0.0.0": ec_nm = None
|
||||||
|
|
||||||
|
if ec_wifi == 0x02:
|
||||||
|
ec_ip = "10.0.0.1"
|
||||||
|
ec_nm = "255.255.255.0"
|
||||||
|
|
||||||
|
try: ec_psk = psk_bytes.decode("utf-8")
|
||||||
|
except Exception as e: print(f"Error: Could not decode WiFi PSK read from device")
|
||||||
|
if not args.show_psk and ec_psk: ec_psk = "*"*len(ec_psk)
|
||||||
|
|
||||||
print("\nDevice configuration:")
|
print("\nDevice configuration:")
|
||||||
if ec_bt == 0x73:
|
if ec_bt == 0x73: print(f" Bluetooth : Enabled")
|
||||||
print(f" Bluetooth : Enabled")
|
else: print(f" Bluetooth : Disabled")
|
||||||
else:
|
if ec_wifi == 0x01: print(f" WiFi : Enabled (Station)")
|
||||||
print(f" Bluetooth : Disabled")
|
elif ec_wifi == 0x02: print(f" WiFi : Enabled (AP)")
|
||||||
if ec_dia == 0x00:
|
else: print(f" WiFi : Disabled")
|
||||||
print(f" Interference avoidance : Enabled")
|
if ec_wifi == 0x01 or ec_wifi == 0x02:
|
||||||
else:
|
if not ec_wchn: print(f" Channel : Unknown")
|
||||||
print(f" Interference avoidance : Disabled")
|
else: print(f" Channel : {ec_wchn}")
|
||||||
|
if not ec_ssid: print(f" SSID : Not set")
|
||||||
|
else: print(f" SSID : {ec_ssid}")
|
||||||
|
if not ec_psk: print(f" PSK : Not set")
|
||||||
|
else: print(f" PSK : {ec_psk}")
|
||||||
|
if not ec_ip: print(f" IP Address : DHCP")
|
||||||
|
else: print(f" IP Address : {ec_ip}")
|
||||||
|
if ec_ip and ec_nm: print(f" Network Mask : {ec_nm}")
|
||||||
|
if ec_dia == 0x00: print(f" Interference avoidance : Enabled")
|
||||||
|
else: print(f" Interference avoidance : Disabled")
|
||||||
print( f" Display brightness : {ec_dint}")
|
print( f" Display brightness : {ec_dint}")
|
||||||
if ec_dadr == 0xFF:
|
if ec_dadr == 0xFF: print(f" Display address : Default")
|
||||||
print(f" Display address : Default")
|
else: print(f" Display address : {RNS.hexrep(ec_dadr, delimit=False)}")
|
||||||
else:
|
if ec_bset == 0x73 and ec_dblk != 0x00: print(f" Display blanking : {ec_dblk}s")
|
||||||
print(f" Display address : {RNS.hexrep(ec_dadr, delimit=False)}")
|
else: print(f" Display blanking : Disabled")
|
||||||
if ec_bset == 0x73 and ec_dblk != 0x00:
|
|
||||||
print(f" Display blanking : {ec_dblk}s")
|
|
||||||
else:
|
|
||||||
print(f" Display blanking : Disabled")
|
|
||||||
if ec_drot != 0xFF:
|
if ec_drot != 0xFF:
|
||||||
if ec_drot == 0x00:
|
if ec_drot == 0x00: rstr = "Landscape"
|
||||||
rstr = "Landscape"
|
if ec_drot == 0x01: rstr = "Portrait"
|
||||||
if ec_drot == 0x01:
|
if ec_drot == 0x02: rstr = "Landscape 180"
|
||||||
rstr = "Portrait"
|
if ec_drot == 0x03: rstr = "Portrait 180"
|
||||||
if ec_drot == 0x02:
|
|
||||||
rstr = "Landscape 180"
|
|
||||||
if ec_drot == 0x03:
|
|
||||||
rstr = "Portrait 180"
|
|
||||||
print(f" Display rotation : {rstr}")
|
print(f" Display rotation : {rstr}")
|
||||||
else:
|
else:
|
||||||
print(f" Display rotation : Default")
|
print(f" Display rotation : Default")
|
||||||
if ec_pset == 0x73:
|
if ec_pset == 0x73: print(f" Neopixel Intensity : {ec_pint}")
|
||||||
print(f" Neopixel Intensity : {ec_pint}")
|
|
||||||
print("")
|
print("")
|
||||||
|
|
||||||
|
rnode.leave()
|
||||||
graceful_exit()
|
graceful_exit()
|
||||||
|
|
||||||
if args.eeprom_dump:
|
if args.eeprom_dump:
|
||||||
|
|
@ -3595,6 +3908,77 @@ def main():
|
||||||
input()
|
input()
|
||||||
rnode.leave()
|
rnode.leave()
|
||||||
|
|
||||||
|
if args.channel:
|
||||||
|
try:
|
||||||
|
RNS.log(f"Setting WiFi channel to {args.channel}")
|
||||||
|
rnode.set_wifi_channel(args.channel)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi channel: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
|
if args.ssid:
|
||||||
|
try:
|
||||||
|
if args.ssid.lower() == "none":
|
||||||
|
ssid_str = None
|
||||||
|
RNS.log(f"Deleting WiFi SSID")
|
||||||
|
else:
|
||||||
|
ssid_str = str(args.ssid)
|
||||||
|
RNS.log(f"Setting WiFi SSID to: {ssid_str}")
|
||||||
|
rnode.set_wifi_ssid(ssid_str)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi SSID: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
|
if args.psk:
|
||||||
|
try:
|
||||||
|
if args.psk.lower() == "none":
|
||||||
|
psk_str = None
|
||||||
|
RNS.log(f"Deleting WiFi PSK")
|
||||||
|
else:
|
||||||
|
psk_str = str(args.psk)
|
||||||
|
RNS.log(f"Setting WiFi PSK")
|
||||||
|
rnode.set_wifi_psk(psk_str)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi PSK: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
|
if args.ip:
|
||||||
|
try:
|
||||||
|
if args.ip.lower() == "none":
|
||||||
|
RNS.log(f"Setting WiFi IP to DHCP...")
|
||||||
|
rnode.set_wifi_ip(None)
|
||||||
|
else:
|
||||||
|
RNS.log(f"Setting WiFi static IP to: {args.ip}")
|
||||||
|
rnode.set_wifi_ip(args.ip)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi IP: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
|
if args.nm:
|
||||||
|
try:
|
||||||
|
if args.nm.lower() == "none":
|
||||||
|
RNS.log(f"Deleting WiFi static netmask configuration...")
|
||||||
|
rnode.set_wifi_nm(None)
|
||||||
|
else:
|
||||||
|
RNS.log(f"Setting WiFi static netmask to: {args.nm}")
|
||||||
|
rnode.set_wifi_nm(args.nm)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi netmask: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
|
if args.wifi:
|
||||||
|
try:
|
||||||
|
mode = 0x00
|
||||||
|
if str(args.wifi).lower().startswith("sta"): mode = 0x01
|
||||||
|
elif str(args.wifi).lower().startswith("ap"): mode = 0x02
|
||||||
|
if mode == 0x00: RNS.log(f"Disabling WiFi...")
|
||||||
|
elif mode == 0x01: RNS.log(f"Setting WiFi to station mode")
|
||||||
|
elif mode == 0x02: RNS.log(f"Setting WiFi to AP mode")
|
||||||
|
rnode.set_wifi_mode(mode)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not set WiFi mode: {e}")
|
||||||
|
graceful_exit()
|
||||||
|
|
||||||
if args.info:
|
if args.info:
|
||||||
if rnode.provisioned:
|
if rnode.provisioned:
|
||||||
timestamp = struct.unpack(">I", rnode.made)[0]
|
timestamp = struct.unpack(">I", rnode.made)[0]
|
||||||
|
|
@ -3969,8 +4353,8 @@ def main():
|
||||||
print("cases, and copies of the source code for both the RNode Firmware,")
|
print("cases, and copies of the source code for both the RNode Firmware,")
|
||||||
print("Reticulum and other utilities.")
|
print("Reticulum and other utilities.")
|
||||||
print("")
|
print("")
|
||||||
print("To activate the RNode Bootstrap Console, power up your RNode and press")
|
print("To activate the RNode Bootstrap Console, power up your RNode and hold")
|
||||||
print("the reset button twice with a one second interval. The RNode will now")
|
print("down the user button for 10+ seconds, then release. The RNode will now")
|
||||||
print("reboot into console mode, and activate a WiFi access point for you to")
|
print("reboot into console mode, and activate a WiFi access point for you to")
|
||||||
print("connect to. The console is then reachable at: http://10.0.0.1")
|
print("connect to. The console is then reachable at: http://10.0.0.1")
|
||||||
print("")
|
print("")
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -31,8 +39,10 @@ import argparse
|
||||||
from RNS._version import __version__
|
from RNS._version import __version__
|
||||||
|
|
||||||
remote_link = None
|
remote_link = None
|
||||||
def connect_remote(destination_hash, auth_identity, timeout, no_output = False):
|
output_rst_str = "\r \r"
|
||||||
|
def connect_remote(destination_hash, auth_identity, timeout, no_output = False, purpose="management"):
|
||||||
global remote_link, reticulum
|
global remote_link, reticulum
|
||||||
|
timeout = max(timeout, reticulum.get_medium_path_timeout())
|
||||||
if not RNS.Transport.has_path(destination_hash):
|
if not RNS.Transport.has_path(destination_hash):
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("Path to "+RNS.prettyhexrep(destination_hash)+" requested", end=" ")
|
print("Path to "+RNS.prettyhexrep(destination_hash)+" requested", end=" ")
|
||||||
|
|
@ -43,107 +53,226 @@ def connect_remote(destination_hash, auth_identity, timeout, no_output = False):
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
if time.time() - pr_time > timeout:
|
if time.time() - pr_time > timeout:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Path request timed out")
|
print("Path request timed out")
|
||||||
exit(12)
|
exit(12)
|
||||||
|
|
||||||
remote_identity = RNS.Identity.recall(destination_hash)
|
remote_identity = RNS.Identity.recall(destination_hash)
|
||||||
|
|
||||||
def remote_link_closed(link):
|
def remote_link_closed(link):
|
||||||
if link.teardown_reason == RNS.Link.TIMEOUT:
|
if link.teardown_reason == RNS.Link.INITIATOR_CLOSED: return
|
||||||
|
elif link.teardown_reason == RNS.Link.TIMEOUT:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("The link timed out, exiting now")
|
print("The link timed out, exiting now")
|
||||||
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
|
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("The link was closed by the server, exiting now")
|
print("The link was closed by the server, exiting now")
|
||||||
else:
|
else:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Link closed unexpectedly, exiting now")
|
print("Link closed unexpectedly, exiting now")
|
||||||
exit(10)
|
exit(10)
|
||||||
|
|
||||||
def remote_link_established(link):
|
def remote_link_established(link):
|
||||||
global remote_link
|
global remote_link
|
||||||
link.identify(auth_identity)
|
if purpose == "management": link.identify(auth_identity)
|
||||||
remote_link = link
|
remote_link = link
|
||||||
|
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Establishing link with remote transport instance...", end=" ")
|
print("Establishing link with remote transport instance...", end=" ")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "remote", "management")
|
if purpose == "management": remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "remote", "management")
|
||||||
|
elif purpose == "blackhole": remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "info", "blackhole")
|
||||||
link = RNS.Link(remote_destination)
|
link = RNS.Link(remote_destination)
|
||||||
link.set_link_established_callback(remote_link_established)
|
link.set_link_established_callback(remote_link_established)
|
||||||
link.set_link_closed_callback(remote_link_closed)
|
link.set_link_closed_callback(remote_link_closed)
|
||||||
|
|
||||||
|
def parse_hash(input_str):
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
|
if len(input_str) != dest_len: raise ValueError("Hash length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
|
try:
|
||||||
|
hash_bytes = bytes.fromhex(input_str)
|
||||||
|
return hash_bytes
|
||||||
|
except Exception as e: raise ValueError("Invalid hash entered. Check your input.")
|
||||||
|
|
||||||
def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues,
|
def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues,
|
||||||
drop_via, max_hops, remote=None, management_identity=None, remote_timeout=RNS.Transport.PATH_REQUEST_TIMEOUT,
|
drop_via, max_hops, remote=None, management_identity=None, remote_timeout=RNS.Transport.PATH_REQUEST_TIMEOUT,
|
||||||
no_output=False, json=False):
|
blackholed=False, blackhole=False, unblackhole=False, blackhole_duration=None, blackhole_reason=None, remote_blackhole_list=False, remote_blackhole_list_filter=None,
|
||||||
|
active=False, usage=False, vias=False, expinfo=False, min_value=None, no_output=False, analyze=False, capture=None, json=False):
|
||||||
|
|
||||||
global remote_link, reticulum
|
global remote_link, reticulum
|
||||||
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
|
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
|
||||||
|
reticulum.wants_destination_type("rnstransport.remote.management")
|
||||||
|
reticulum.wants_destination_type("rnstransport.info.blackhole")
|
||||||
if remote:
|
if remote:
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(remote) != dest_len:
|
if len(remote) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
|
||||||
try:
|
try:
|
||||||
identity_hash = bytes.fromhex(remote)
|
identity_hash = bytes.fromhex(remote)
|
||||||
remote_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.remote.management", identity_hash)
|
remote_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.remote.management", identity_hash)
|
||||||
except Exception as e:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
|
|
||||||
identity = RNS.Identity.from_file(os.path.expanduser(management_identity))
|
identity = RNS.Identity.from_file(os.path.expanduser(management_identity))
|
||||||
if identity == None:
|
if identity == None: raise ValueError("Could not load management identity from "+str(management_identity))
|
||||||
raise ValueError("Could not load management identity from "+str(management_identity))
|
|
||||||
|
|
||||||
try:
|
try: connect_remote(remote_hash, identity, remote_timeout, no_output)
|
||||||
connect_remote(remote_hash, identity, remote_timeout, no_output)
|
except Exception as e: raise e
|
||||||
except Exception as e:
|
|
||||||
raise e
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
exit(20)
|
exit(20)
|
||||||
|
|
||||||
while remote_link == None:
|
while remote_link == None: time.sleep(0.1)
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
|
if blackholed or remote_blackhole_list:
|
||||||
|
blackholed_list = None
|
||||||
|
if blackholed:
|
||||||
|
if remote_link:
|
||||||
|
if not no_output:
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
print("Listing blackholed identities on remote instances not yet implemented")
|
||||||
|
exit(255)
|
||||||
|
|
||||||
if table:
|
try: blackholed_list = reticulum.get_blackholed_identities()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not get blackholed identities from RNS instance: {e}")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
elif remote_blackhole_list:
|
||||||
|
try: identity_hash = parse_hash(destination_hexhash)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{e}")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
remote_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.info.blackhole", identity_hash)
|
||||||
|
connect_remote(remote_hash, None, remote_timeout, no_output, purpose="blackhole")
|
||||||
|
while remote_link == None: time.sleep(0.1)
|
||||||
|
|
||||||
|
if not no_output:
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
print("Sending request...", end=" ")
|
||||||
|
sys.stdout.flush()
|
||||||
|
receipt = remote_link.request("/list")
|
||||||
|
while not receipt.concluded(): time.sleep(0.1)
|
||||||
|
response = receipt.get_response()
|
||||||
|
if type(response) == dict:
|
||||||
|
blackholed_list = response
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
else:
|
||||||
|
if not no_output:
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
print("The remote request failed.")
|
||||||
|
exit(10)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"Nowhere to fetch blackhole list from")
|
||||||
|
exit(255)
|
||||||
|
|
||||||
|
if not blackholed_list:
|
||||||
|
print("No blackholed identity data available")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
else:
|
||||||
|
rmlen = 64
|
||||||
|
def trunc(input_str):
|
||||||
|
if len(input_str) <= rmlen: return input_str
|
||||||
|
else: return f"{input_str[:rmlen-1]}…"
|
||||||
|
|
||||||
|
try:
|
||||||
|
now = time.time()
|
||||||
|
for identity_hash in blackholed_list:
|
||||||
|
until = blackholed_list[identity_hash]["until"]
|
||||||
|
reason = blackholed_list[identity_hash]["reason"]
|
||||||
|
source = blackholed_list[identity_hash]["source"]
|
||||||
|
until_str = f"for {RNS.prettytime(max(0, until-now))}" if until else "indefinitely"
|
||||||
|
reason_str = f" ({trunc(reason)})" if reason else ""
|
||||||
|
by_str = f" by {RNS.prettyhexrep(source)}" if source != RNS.Transport.identity.hash else ""
|
||||||
|
filter_str = f"{RNS.prettyhexrep(identity_hash)} {until_str} {reason_str} {by_str}"
|
||||||
|
|
||||||
|
if not remote_blackhole_list:
|
||||||
|
if destination_hexhash and not destination_hexhash in filter_str: continue
|
||||||
|
else:
|
||||||
|
if remote_blackhole_list_filter and not remote_blackhole_list_filter in filter_str: continue
|
||||||
|
|
||||||
|
print(f"{RNS.prettyhexrep(identity_hash)} blackholed {until_str}{reason_str}{by_str}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error while displaying collected blackhole data: {e}")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
elif blackhole:
|
||||||
|
if remote_link:
|
||||||
|
if not no_output:
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
print("Blackholing identity on remote instances not yet implemented")
|
||||||
|
exit(255)
|
||||||
|
|
||||||
|
try:
|
||||||
|
identity_hash = parse_hash(destination_hexhash)
|
||||||
|
until = time.time()+blackhole_duration*60*60 if blackhole_duration else None
|
||||||
|
result = reticulum.blackhole_identity(identity_hash, until=until, reason=blackhole_reason)
|
||||||
|
if result == True: print(f"Blackholed identity {destination_hexhash}")
|
||||||
|
elif result == None: print(f"Identity {destination_hexhash} already blackholed")
|
||||||
|
else: print(f"Could not blackhole identity {destination_hexhash}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not blackhole identity: {e}")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
elif unblackhole:
|
||||||
|
if remote_link:
|
||||||
|
if not no_output:
|
||||||
|
print(output_rst_str, end="")
|
||||||
|
print("Blackholing identity on remote instances not yet implemented")
|
||||||
|
exit(255)
|
||||||
|
|
||||||
|
try:
|
||||||
|
identity_hash = parse_hash(destination_hexhash)
|
||||||
|
result = reticulum.unblackhole_identity(identity_hash)
|
||||||
|
if result == True: print(f"Lifted blackhole for identity {destination_hexhash}")
|
||||||
|
elif result == None: print(f"Identity {destination_hexhash} not blackholed")
|
||||||
|
else: print(f"Could not unblackhole identity {destination_hexhash}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not unblackhole identity: {e}")
|
||||||
|
exit(20)
|
||||||
|
|
||||||
|
elif analyze:
|
||||||
|
analyze_tables(path_table=reticulum.get_path_table(), rate_table=reticulum.get_rate_table(),
|
||||||
|
link_distribution=reticulum.get_link_distribution(), capture=capture)
|
||||||
|
|
||||||
|
elif table:
|
||||||
destination_hash = None
|
destination_hash = None
|
||||||
if destination_hexhash != None:
|
if destination_hexhash != None:
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(destination_hexhash) != dest_len:
|
if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
try: destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
try:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
destination_hash = bytes.fromhex(destination_hexhash)
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not remote_link:
|
if not remote_link: table = sorted(reticulum.get_path_table(max_hops=max_hops), key=lambda e: (e["interface"], e["hops"]) )
|
||||||
table = sorted(reticulum.get_path_table(max_hops=max_hops), key=lambda e: (e["interface"], e["hops"]) )
|
|
||||||
else:
|
else:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Sending request...", end=" ")
|
print("Sending request...", end=" ")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
receipt = remote_link.request("/path", data = ["table", destination_hash, max_hops])
|
receipt = remote_link.request("/path", data = ["table", destination_hash, max_hops])
|
||||||
while not receipt.concluded():
|
while not receipt.concluded(): time.sleep(0.1)
|
||||||
time.sleep(0.1)
|
|
||||||
response = receipt.get_response()
|
response = receipt.get_response()
|
||||||
if response:
|
if response:
|
||||||
table = response
|
table = response
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
else:
|
else:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("The remote request failed. Likely authentication failure.")
|
print("The remote request failed. Likely authentication failure.")
|
||||||
exit(10)
|
exit(10)
|
||||||
|
|
||||||
|
|
@ -152,21 +281,41 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
import json
|
import json
|
||||||
for p in table:
|
for p in table:
|
||||||
for k in p:
|
for k in p:
|
||||||
if isinstance(p[k], bytes):
|
if isinstance(p[k], bytes): p[k] = RNS.hexrep(p[k], delimit=False)
|
||||||
p[k] = RNS.hexrep(p[k], delimit=False)
|
|
||||||
|
|
||||||
print(json.dumps(table))
|
print(json.dumps(table))
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
now = time.time()
|
||||||
|
if usage:
|
||||||
|
try: table = sorted(table, key=lambda e: e["used"], reverse=True)
|
||||||
|
except Exception as e: pass
|
||||||
|
|
||||||
|
now = time.time()
|
||||||
for path in table:
|
for path in table:
|
||||||
if destination_hash == None or destination_hash == path["hash"]:
|
if destination_hash == None or destination_hash == path["hash"]:
|
||||||
displayed += 1
|
displayed += 1
|
||||||
|
if min_value:
|
||||||
|
if not path["value"]: continue
|
||||||
|
if path["value"] < min_value: continue
|
||||||
|
|
||||||
|
if path["state"] == RNS.Transport.STATE_RESPONSIVE: state_str = " (R)"
|
||||||
|
elif path["state"] == RNS.Transport.STATE_UNRESPONSIVE: state_str = " (u)"
|
||||||
|
elif path["state"] == RNS.Transport.STATE_UNKNOWN: state_str = " (?)"
|
||||||
|
else: state_str = " (?)"
|
||||||
|
|
||||||
exp_str = RNS.timestamp_str(path["expires"])
|
exp_str = RNS.timestamp_str(path["expires"])
|
||||||
if path["hops"] == 1:
|
if path["hops"] == 1: m_str = " "
|
||||||
m_str = " "
|
else: m_str = "s"
|
||||||
|
if not "used" in path: u_str = ", no usage info"
|
||||||
else:
|
else:
|
||||||
m_str = "s"
|
if active and not path["used"]: continue
|
||||||
print(RNS.prettyhexrep(path["hash"])+" is "+str(path["hops"])+" hop"+m_str+" away via "+RNS.prettyhexrep(path["via"])+" on "+path["interface"]+" expires "+RNS.timestamp_str(path["expires"]))
|
if usage: u_str = f", last used {RNS.prettytime(now-path['used'])} ago" if path["used"] else ", never used"
|
||||||
|
else: u_str = ""
|
||||||
|
via_str = f" via {RNS.prettyhexrep(path['via'])}" if vias else ""
|
||||||
|
exp_str = f" expires in {RNS.prettytime(path['expires']-now, compact=True)}" if expinfo else ""
|
||||||
|
print(f"{RNS.prettyhexrep(path['hash'])} {path['hops']:>3}h{state_str}{via_str} on {path['interface']}{exp_str} (dv:{RNS.prettycost(path['value'])}/lc:{RNS.prettycost(path['lr_cost'])}){u_str}")
|
||||||
|
|
||||||
if destination_hash != None and displayed == 0:
|
if destination_hash != None and displayed == 0:
|
||||||
print("No path known")
|
print("No path known")
|
||||||
|
|
@ -177,21 +326,17 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
if destination_hexhash != None:
|
if destination_hexhash != None:
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(destination_hexhash) != dest_len:
|
if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
try: destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
try:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
destination_hash = bytes.fromhex(destination_hexhash)
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if not remote_link:
|
if not remote_link: table = reticulum.get_rate_table()
|
||||||
table = reticulum.get_rate_table()
|
|
||||||
else:
|
else:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Sending request...", end=" ")
|
print("Sending request...", end=" ")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
receipt = remote_link.request("/path", data = ["rates", destination_hash])
|
receipt = remote_link.request("/path", data = ["rates", destination_hash])
|
||||||
|
|
@ -200,10 +345,10 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
response = receipt.get_response()
|
response = receipt.get_response()
|
||||||
if response:
|
if response:
|
||||||
table = response
|
table = response
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
else:
|
else:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("The remote request failed. Likely authentication failure.")
|
print("The remote request failed. Likely authentication failure.")
|
||||||
exit(10)
|
exit(10)
|
||||||
|
|
||||||
|
|
@ -212,15 +357,12 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
import json
|
import json
|
||||||
for p in table:
|
for p in table:
|
||||||
for k in p:
|
for k in p:
|
||||||
if isinstance(p[k], bytes):
|
if isinstance(p[k], bytes): p[k] = RNS.hexrep(p[k], delimit=False)
|
||||||
p[k] = RNS.hexrep(p[k], delimit=False)
|
|
||||||
|
|
||||||
print(json.dumps(table))
|
print(json.dumps(table))
|
||||||
exit()
|
exit()
|
||||||
else:
|
else:
|
||||||
if len(table) == 0:
|
if len(table) == 0: print("No information available")
|
||||||
print("No information available")
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
displayed = 0
|
displayed = 0
|
||||||
for entry in table:
|
for entry in table:
|
||||||
|
|
@ -266,7 +408,7 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
elif drop_queues:
|
elif drop_queues:
|
||||||
if remote_link:
|
if remote_link:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Dropping announce queues on remote instances not yet implemented")
|
print("Dropping announce queues on remote instances not yet implemented")
|
||||||
exit(255)
|
exit(255)
|
||||||
|
|
||||||
|
|
@ -276,24 +418,20 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
elif drop:
|
elif drop:
|
||||||
if remote_link:
|
if remote_link:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Dropping path on remote instances not yet implemented")
|
print("Dropping path on remote instances not yet implemented")
|
||||||
exit(255)
|
exit(255)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(destination_hexhash) != dest_len:
|
if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
try: destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
try:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
destination_hash = bytes.fromhex(destination_hexhash)
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if reticulum.drop_path(destination_hash):
|
if reticulum.drop_path(destination_hash): print("Dropped path to "+RNS.prettyhexrep(destination_hash))
|
||||||
print("Dropped path to "+RNS.prettyhexrep(destination_hash))
|
|
||||||
else:
|
else:
|
||||||
print("Unable to drop path to "+RNS.prettyhexrep(destination_hash)+". Does it exist?")
|
print("Unable to drop path to "+RNS.prettyhexrep(destination_hash)+". Does it exist?")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
@ -301,24 +439,20 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
elif drop_via:
|
elif drop_via:
|
||||||
if remote_link:
|
if remote_link:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Dropping all paths via specific transport instance on remote instances yet not implemented")
|
print("Dropping all paths via specific transport instance on remote instances yet not implemented")
|
||||||
exit(255)
|
exit(255)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(destination_hexhash) != dest_len:
|
if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
try: destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
try:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
destination_hash = bytes.fromhex(destination_hexhash)
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if reticulum.drop_all_via(destination_hash):
|
if reticulum.drop_all_via(destination_hash): print("Dropped all paths via "+RNS.prettyhexrep(destination_hash))
|
||||||
print("Dropped all paths via "+RNS.prettyhexrep(destination_hash))
|
|
||||||
else:
|
else:
|
||||||
print("Unable to drop paths via "+RNS.prettyhexrep(destination_hash)+". Does the transport instance exist?")
|
print("Unable to drop paths via "+RNS.prettyhexrep(destination_hash)+". Does the transport instance exist?")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
@ -326,18 +460,15 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
else:
|
else:
|
||||||
if remote_link:
|
if remote_link:
|
||||||
if not no_output:
|
if not no_output:
|
||||||
print("\r \r", end="")
|
print(output_rst_str, end="")
|
||||||
print("Requesting paths on remote instances not implemented")
|
print("Requesting paths on remote instances not implemented")
|
||||||
exit(255)
|
exit(255)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
if len(destination_hexhash) != dest_len:
|
if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||||
raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
try: destination_hash = bytes.fromhex(destination_hexhash)
|
||||||
try:
|
except Exception as e: raise ValueError("Invalid destination entered. Check your input.")
|
||||||
destination_hash = bytes.fromhex(destination_hexhash)
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError("Invalid destination entered. Check your input.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
@ -349,7 +480,7 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
||||||
limit = time.time()+timeout
|
limit = time.time()+max(timeout, reticulum.get_medium_path_timeout())
|
||||||
while not RNS.Transport.has_path(destination_hash) and time.time()<limit:
|
while not RNS.Transport.has_path(destination_hash) and time.time()<limit:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
print(("\b\b"+syms[i]+" "), end="")
|
print(("\b\b"+syms[i]+" "), end="")
|
||||||
|
|
@ -366,207 +497,178 @@ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity,
|
||||||
next_hop = RNS.prettyhexrep(next_hop_bytes)
|
next_hop = RNS.prettyhexrep(next_hop_bytes)
|
||||||
next_hop_interface = reticulum.get_next_hop_if_name(destination_hash)
|
next_hop_interface = reticulum.get_next_hop_if_name(destination_hash)
|
||||||
|
|
||||||
if hops != 1:
|
if hops != 1: ms = "s"
|
||||||
ms = "s"
|
else: ms = ""
|
||||||
else:
|
|
||||||
ms = ""
|
|
||||||
|
|
||||||
print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface)
|
print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface)
|
||||||
else:
|
else:
|
||||||
print("\r \rPath not found")
|
print("\r \rPath not found")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
parser = argparse.ArgumentParser(description="Reticulum Path Discovery Utility")
|
parser = argparse.ArgumentParser(description="Reticulum Path Management Utility")
|
||||||
|
parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
|
||||||
parser.add_argument("--config",
|
parser.add_argument("--version", action="version", version="rnpath {version}".format(version=__version__))
|
||||||
action="store",
|
parser.add_argument("-t", "--table", action="store_true", help="show all known paths", default=False)
|
||||||
default=None,
|
parser.add_argument("-a", "--active", action="store_true", help="show in-use paths only", default=False)
|
||||||
help="path to alternative Reticulum config directory",
|
parser.add_argument("-u", "--usage", action="store_true", help="show path usage time", default=False)
|
||||||
type=str
|
parser.add_argument("-V", "--vias", action="store_true", help="show via transport IDs", default=False)
|
||||||
)
|
parser.add_argument("-E", "--expiry", action="store_true", help="show path expiry times", default=False)
|
||||||
|
parser.add_argument("-m", "--max", action="store", metavar="hops", type=int, help="maximum hops to filter path table by", default=None)
|
||||||
parser.add_argument(
|
parser.add_argument("-M", "--min-value", action="store", metavar="value", type=float, help="minimum value to filter path table by", default=None)
|
||||||
"--version",
|
parser.add_argument("-r", "--rates", action="store_true", help="show announce rate info", default=False)
|
||||||
action="version",
|
parser.add_argument("-d", "--drop", action="store_true", help="remove the path to a destination", default=False)
|
||||||
version="rnpath {version}".format(version=__version__)
|
parser.add_argument("-D", "--drop-announces", action="store_true", help="drop all queued announces", default=False)
|
||||||
)
|
parser.add_argument("-x", "--drop-via", action="store_true", help="drop all paths via specified transport instance", default=False)
|
||||||
|
parser.add_argument("-w", action="store", metavar="seconds", type=float, help="timeout before giving up", default=RNS.Transport.PATH_REQUEST_TIMEOUT)
|
||||||
parser.add_argument(
|
parser.add_argument("-R", action="store", metavar="hash", help="transport identity hash of remote instance to manage", default=None, type=str)
|
||||||
"-t",
|
parser.add_argument("-i", action="store", metavar="path", help="path to identity used for remote management", default=None, type=str)
|
||||||
"--table",
|
parser.add_argument("-W", action="store", metavar="seconds", type=float, help="timeout before giving up on remote queries", default=RNS.Transport.PATH_REQUEST_TIMEOUT)
|
||||||
action="store_true",
|
parser.add_argument("-b", "--blackholed", action="store_true", help="list blackholed identities", default=False)
|
||||||
help="show all known paths",
|
parser.add_argument("-B", "--blackhole", action="store_true", help="blackhole identity", default=False)
|
||||||
default=False
|
parser.add_argument("-U", "--unblackhole", action="store_true", help="unblackhole identity", default=False)
|
||||||
)
|
parser.add_argument( "--duration", action="store", metavar="hours", type=float, help="duration of blackhole enforcement in hours", default=None)
|
||||||
|
parser.add_argument( "--reason", action="store", metavar="reason", type=str, help="reason for blackholing identity", default=None)
|
||||||
parser.add_argument(
|
parser.add_argument("-p", "--blackholed-list", action="store_true", help="view published blackhole list for remote transport instance", default=False)
|
||||||
"-m",
|
parser.add_argument("-j", "--json", action="store_true", help="output in JSON format", default=False)
|
||||||
"--max",
|
parser.add_argument("-A", "--analyze", action="store_true", help="Analyze tables", default=False)
|
||||||
action="store",
|
parser.add_argument("--capture", action="store", metavar="path", type=str, help="path to capture file for analyze mode", default=None)
|
||||||
metavar="hops",
|
parser.add_argument("destination", nargs="?", default=None, help="hexadecimal hash of the destination", type=str)
|
||||||
type=int,
|
parser.add_argument("list_filter", nargs="?", default=None, help="filter for remote blackhole list view", type=str)
|
||||||
help="maximum hops to filter path table by",
|
|
||||||
default=None
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-r",
|
|
||||||
"--rates",
|
|
||||||
action="store_true",
|
|
||||||
help="show announce rate info",
|
|
||||||
default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-d",
|
|
||||||
"--drop",
|
|
||||||
action="store_true",
|
|
||||||
help="remove the path to a destination",
|
|
||||||
default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-D",
|
|
||||||
"--drop-announces",
|
|
||||||
action="store_true",
|
|
||||||
help="drop all queued announces",
|
|
||||||
default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-x", "--drop-via",
|
|
||||||
action="store_true",
|
|
||||||
help="drop all paths via specified transport instance",
|
|
||||||
default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-w",
|
|
||||||
action="store",
|
|
||||||
metavar="seconds",
|
|
||||||
type=float,
|
|
||||||
help="timeout before giving up",
|
|
||||||
default=RNS.Transport.PATH_REQUEST_TIMEOUT
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-R",
|
|
||||||
action="store",
|
|
||||||
metavar="hash",
|
|
||||||
help="transport identity hash of remote instance to manage",
|
|
||||||
default=None,
|
|
||||||
type=str
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-i",
|
|
||||||
action="store",
|
|
||||||
metavar="path",
|
|
||||||
help="path to identity used for remote management",
|
|
||||||
default=None,
|
|
||||||
type=str
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-W",
|
|
||||||
action="store",
|
|
||||||
metavar="seconds",
|
|
||||||
type=float,
|
|
||||||
help="timeout before giving up on remote queries",
|
|
||||||
default=RNS.Transport.PATH_REQUEST_TIMEOUT
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"-j",
|
|
||||||
"--json",
|
|
||||||
action="store_true",
|
|
||||||
help="output in JSON format",
|
|
||||||
default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"destination",
|
|
||||||
nargs="?",
|
|
||||||
default=None,
|
|
||||||
help="hexadecimal hash of the destination",
|
|
||||||
type=str
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument('-v', '--verbose', action='count', default=0)
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.config:
|
if args.config: configarg = args.config
|
||||||
configarg = args.config
|
else: configarg = None
|
||||||
else:
|
|
||||||
configarg = None
|
|
||||||
|
|
||||||
if not args.drop_announces and not args.table and not args.rates and not args.destination and not args.drop_via:
|
if not args.drop_announces and not args.table and not args.rates and not args.destination and not args.drop_via and not args.blackholed and not args.analyze:
|
||||||
print("")
|
print("")
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
print("")
|
print("")
|
||||||
else:
|
else:
|
||||||
program_setup(
|
program_setup(configdir=configarg, table=args.table, rates=args.rates, drop=args.drop, destination_hexhash=args.destination,
|
||||||
configdir = configarg,
|
verbosity=args.verbose, timeout=args.w, drop_queues=args.drop_announces, drop_via=args.drop_via, max_hops=args.max, min_value=args.min_value,
|
||||||
table = args.table,
|
remote=args.R, management_identity=args.i, remote_timeout=args.W, blackholed=args.blackholed, blackhole=args.blackhole,
|
||||||
rates = args.rates,
|
unblackhole=args.unblackhole, blackhole_duration=args.duration, blackhole_reason=args.reason, remote_blackhole_list=args.blackholed_list,
|
||||||
drop = args.drop,
|
remote_blackhole_list_filter=args.list_filter, usage=args.usage, vias=args.vias, active=args.active, expinfo=args.expiry,
|
||||||
destination_hexhash = args.destination,
|
analyze=args.analyze, capture=args.capture, json=args.json)
|
||||||
verbosity = args.verbose,
|
|
||||||
timeout = args.w,
|
|
||||||
drop_queues = args.drop_announces,
|
|
||||||
drop_via = args.drop_via,
|
|
||||||
max_hops = args.max,
|
|
||||||
remote=args.R,
|
|
||||||
management_identity=args.i,
|
|
||||||
remote_timeout=args.W,
|
|
||||||
json=args.json,
|
|
||||||
)
|
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("")
|
print("")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
def analyze_tables(path_table=None, rate_table=None, link_distribution=None, capture=None):
|
||||||
|
print("Not yet implemented")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
# if not path_table:
|
||||||
|
# print("No path table to analyze")
|
||||||
|
# exit()
|
||||||
|
|
||||||
|
# else:
|
||||||
|
# print(f"Analyzing tables...")
|
||||||
|
|
||||||
|
# # TODO: If a `capture` path is specified, we save and (if existing) load from
|
||||||
|
# # this file, so that `rnpath --analyze` can be run at intervals, and continuously
|
||||||
|
# # collect data, instead of only providing an at-the-moment snapshot view.
|
||||||
|
|
||||||
|
# # Locally initialize announce cache to be able to
|
||||||
|
# # fetch full announce packet data
|
||||||
|
# if RNS.Transport.owner.is_connected_to_shared_instance: RNS.Transport.init_announce_cache()
|
||||||
|
# tm = RNS.Transport.destination_type_map
|
||||||
|
# total = len(path_table)
|
||||||
|
# destinations = {}
|
||||||
|
# unresolvable = 0
|
||||||
|
# unknown_types = set()
|
||||||
|
# known_types = set()
|
||||||
|
# unknown_type_dsts = 0
|
||||||
|
# known_type_dsts = 0
|
||||||
|
# for path in path_table:
|
||||||
|
# dst_hash = path["hash"]
|
||||||
|
# dst_hops = path["hops"]
|
||||||
|
# last_used = path["used"]
|
||||||
|
# name_hash = None
|
||||||
|
# kd_entry = RNS.Identity.known_destinations.get(dst_hash, None)
|
||||||
|
|
||||||
|
# # New destinations may have arrived after our initial known
|
||||||
|
# # destinations snapshot, which is fine, we just count the few
|
||||||
|
# # that miss here for now.
|
||||||
|
# if not kd_entry: unresolvable += 1
|
||||||
|
|
||||||
|
# # Enrich data with information from cached announces
|
||||||
|
# else:
|
||||||
|
# dst_value = kd_entry[6]
|
||||||
|
# dst_lr_cost = kd_entry[7]
|
||||||
|
# announce_hash = kd_entry[1]
|
||||||
|
# announce_packet = RNS.Transport.get_cached_packet(announce_hash, packet_type="announce")
|
||||||
|
# if not announce_packet: unresolvable += 1; continue
|
||||||
|
|
||||||
|
# try: announce_packet.unpack()
|
||||||
|
# except: unresolvable +=1; continue
|
||||||
|
|
||||||
|
# keysize = RNS.Identity.KEYSIZE//8
|
||||||
|
# name_hash_len = RNS.Identity.NAME_HASH_LENGTH//8
|
||||||
|
# name_hash = announce_packet.data[keysize:keysize+name_hash_len]
|
||||||
|
# app_data = kd_entry[3] or RNS.Identity.recall_app_data(dst_hash, _no_use=True)
|
||||||
|
# app_data_len = len(app_data) if app_data else None # No len counting if no app data
|
||||||
|
|
||||||
|
# if name_hash in tm: known_type_dsts += 1; known_types.add(name_hash)
|
||||||
|
# else: unknown_type_dsts += 1; unknown_types.add(name_hash)
|
||||||
|
|
||||||
|
# # TODO: Create actual data structure for use in analysis
|
||||||
|
# entry = {}
|
||||||
|
|
||||||
|
# destinations[dst_hash] = entry
|
||||||
|
|
||||||
|
# if rate_table:
|
||||||
|
# # TODO: Incorporate and enrich with data from rate table
|
||||||
|
# pass
|
||||||
|
|
||||||
|
# if link_distribution:
|
||||||
|
# # TODO: Incorporate and enrich with link distribution data
|
||||||
|
# pass
|
||||||
|
|
||||||
|
# # TODO: Actually analyze tables
|
||||||
|
# pass
|
||||||
|
|
||||||
|
# # TODO: Actually print a comprehensive, beautifully formatted
|
||||||
|
# # overview, charts and data. This is a stub.
|
||||||
|
# print(f"Known types : {len(known_types)}")
|
||||||
|
# print(f"Unknown types : {len(unknown_types)}")
|
||||||
|
# print(f"Total types : {len(known_types)+len(unknown_types)}")
|
||||||
|
# print(f"Known type dests : {known_type_dsts}")
|
||||||
|
# print(f"Unknown type dests : {unknown_type_dsts}")
|
||||||
|
# print(f"Total type dests : {known_type_dsts+unknown_type_dsts}")
|
||||||
|
|
||||||
|
# # TODO: Ensure we get the terminal width if possible, and fall back to
|
||||||
|
# # a sensible default if not, so everything is rendered beautifully.
|
||||||
|
|
||||||
|
# exit()
|
||||||
|
|
||||||
def pretty_date(time=False):
|
def pretty_date(time=False):
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
now = datetime.now()
|
now = datetime.now()
|
||||||
if type(time) is int:
|
if type(time) is int: diff = now - datetime.fromtimestamp(time)
|
||||||
diff = now - datetime.fromtimestamp(time)
|
elif isinstance(time,datetime): diff = now - time
|
||||||
elif isinstance(time,datetime):
|
elif not time: diff = now - now
|
||||||
diff = now - time
|
|
||||||
elif not time:
|
|
||||||
diff = now - now
|
|
||||||
second_diff = diff.seconds
|
second_diff = diff.seconds
|
||||||
day_diff = diff.days
|
day_diff = diff.days
|
||||||
if day_diff < 0:
|
if day_diff < 0: return ''
|
||||||
return ''
|
|
||||||
if day_diff == 0:
|
if day_diff == 0:
|
||||||
if second_diff < 10:
|
if second_diff < 10: return str(second_diff) + " seconds"
|
||||||
return str(second_diff) + " seconds"
|
if second_diff < 60: return str(second_diff) + " seconds"
|
||||||
if second_diff < 60:
|
if second_diff < 70: return "1 minute"
|
||||||
return str(second_diff) + " seconds"
|
if second_diff < 7200: return str(int(second_diff / 60)) + " minutes"
|
||||||
if second_diff < 120:
|
if second_diff < 86400: return str(int(second_diff / 3600)) + " hours"
|
||||||
return "1 minute"
|
if day_diff == 1: return "1 day"
|
||||||
if second_diff < 3600:
|
if day_diff < 7: return str(day_diff) + " days"
|
||||||
return str(int(second_diff / 60)) + " minutes"
|
if day_diff < 31: return str(int(day_diff / 7)) + " weeks"
|
||||||
if second_diff < 7200:
|
if day_diff < 365: return str(int(day_diff / 30)) + " months"
|
||||||
return "an hour"
|
|
||||||
if second_diff < 86400:
|
|
||||||
return str(int(second_diff / 3600)) + " hours"
|
|
||||||
if day_diff == 1:
|
|
||||||
return "1 day"
|
|
||||||
if day_diff < 7:
|
|
||||||
return str(day_diff) + " days"
|
|
||||||
if day_diff < 31:
|
|
||||||
return str(int(day_diff / 7)) + " weeks"
|
|
||||||
if day_diff < 365:
|
|
||||||
return str(int(day_diff / 30)) + " months"
|
|
||||||
return str(int(day_diff / 365)) + " years"
|
return str(int(day_diff / 365)) + " years"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__": main()
|
||||||
main()
|
|
||||||
78
RNS/Utilities/rnpkg.py
Normal file
78
RNS/Utilities/rnpkg.py
Normal file
|
|
@ -0,0 +1,78 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
import argparse
|
||||||
|
import time
|
||||||
|
|
||||||
|
from RNS._version import __version__
|
||||||
|
|
||||||
|
def program_setup(configdir, verbosity = 0, quietness = 0, service = False):
|
||||||
|
targetverbosity = verbosity-quietness
|
||||||
|
|
||||||
|
if service:
|
||||||
|
targetlogdest = RNS.LOG_FILE
|
||||||
|
targetverbosity = None
|
||||||
|
else:
|
||||||
|
targetlogdest = RNS.LOG_STDOUT
|
||||||
|
|
||||||
|
reticulum = RNS.Reticulum(configdir=configdir, verbosity=targetverbosity, logdest=targetlogdest)
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
try:
|
||||||
|
parser = argparse.ArgumentParser(description="Reticulum Meta Package Manager")
|
||||||
|
parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
|
||||||
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
||||||
|
parser.add_argument('-q', '--quiet', action='count', default=0)
|
||||||
|
parser.add_argument("--exampleconfig", action='store_true', default=False, help="print verbose configuration example to stdout and exit")
|
||||||
|
parser.add_argument("--version", action="version", version="rnpkg {version}".format(version=__version__))
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.exampleconfig:
|
||||||
|
print(__example_rnpkg_config__)
|
||||||
|
exit()
|
||||||
|
|
||||||
|
if args.config: configarg = args.config
|
||||||
|
else: configarg = None
|
||||||
|
|
||||||
|
program_setup(configdir = configarg, verbosity=args.verbose, quietness=args.quiet)
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
__example_rnpkg_config__ = '''# This is an example package manager configuration file.
|
||||||
|
'''
|
||||||
|
|
||||||
|
if __name__ == "__main__": main()
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -73,7 +81,7 @@ def program_setup(configdir, destination_hexhash, size=None, full_name = None, v
|
||||||
print("Path to "+RNS.prettyhexrep(destination_hash)+" requested ", end=" ")
|
print("Path to "+RNS.prettyhexrep(destination_hash)+" requested ", end=" ")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
_timeout = time.time() + (timeout or DEFAULT_TIMEOUT+reticulum.get_first_hop_timeout(destination_hash))
|
_timeout = time.time() + (timeout or max(DEFAULT_TIMEOUT+reticulum.get_first_hop_timeout(destination_hash), reticulum.get_medium_path_timeout()))
|
||||||
i = 0
|
i = 0
|
||||||
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
||||||
while not RNS.Transport.has_path(destination_hash) and not time.time() > _timeout:
|
while not RNS.Transport.has_path(destination_hash) and not time.time() > _timeout:
|
||||||
|
|
@ -123,7 +131,7 @@ def program_setup(configdir, destination_hexhash, size=None, full_name = None, v
|
||||||
|
|
||||||
print("\rSent probe "+str(sent)+" ("+str(size)+" bytes) to "+RNS.prettyhexrep(destination_hash)+more+" ", end=" ")
|
print("\rSent probe "+str(sent)+" ("+str(size)+" bytes) to "+RNS.prettyhexrep(destination_hash)+more+" ", end=" ")
|
||||||
|
|
||||||
_timeout = time.time() + (timeout or DEFAULT_TIMEOUT+reticulum.get_first_hop_timeout(destination_hash))
|
_timeout = time.time() + (timeout or max(DEFAULT_TIMEOUT+reticulum.get_first_hop_timeout(destination_hash), reticulum.get_medium_path_timeout()))
|
||||||
i = 0
|
i = 0
|
||||||
while receipt.status == RNS.PacketReceipt.SENT and not time.time() > _timeout:
|
while receipt.status == RNS.PacketReceipt.SENT and not time.time() > _timeout:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# MIT License
|
# Reticulum License
|
||||||
#
|
#
|
||||||
# Copyright (c) 2016-2022 Mark Qvist / unsigned.io
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
#
|
#
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
# of this software and associated documentation files (the "Software"), to deal
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
@ -11,8 +11,16 @@
|
||||||
# copies of the Software, and to permit persons to whom the Software is
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
# furnished to do so, subject to the following conditions:
|
# furnished to do so, subject to the following conditions:
|
||||||
#
|
#
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
# copies or substantial portions of the Software.
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
#
|
#
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
|
@ -29,7 +37,7 @@ import time
|
||||||
from RNS._version import __version__
|
from RNS._version import __version__
|
||||||
|
|
||||||
|
|
||||||
def program_setup(configdir, verbosity = 0, quietness = 0, service = False):
|
def program_setup(configdir, verbosity = 0, quietness = 0, service = False, interactive=False):
|
||||||
targetverbosity = verbosity-quietness
|
targetverbosity = verbosity-quietness
|
||||||
|
|
||||||
if service:
|
if service:
|
||||||
|
|
@ -42,12 +50,14 @@ def program_setup(configdir, verbosity = 0, quietness = 0, service = False):
|
||||||
if reticulum.is_connected_to_shared_instance:
|
if reticulum.is_connected_to_shared_instance:
|
||||||
RNS.log("Started rnsd version {version} connected to another shared local instance, this is probably NOT what you want!".format(version=__version__), RNS.LOG_WARNING)
|
RNS.log("Started rnsd version {version} connected to another shared local instance, this is probably NOT what you want!".format(version=__version__), RNS.LOG_WARNING)
|
||||||
else:
|
else:
|
||||||
if RNS.Reticulum.get_instance().shared_instance_interface:
|
# TODO: Rethink why this was added
|
||||||
RNS.Reticulum.get_instance().shared_instance_interface.server.daemon_threads = True
|
# if RNS.Reticulum.get_instance().shared_instance_interface:
|
||||||
|
# RNS.Reticulum.get_instance().shared_instance_interface.server.daemon_threads = True
|
||||||
RNS.log("Started rnsd version {version}".format(version=__version__), RNS.LOG_NOTICE)
|
RNS.log("Started rnsd version {version}".format(version=__version__), RNS.LOG_NOTICE)
|
||||||
|
|
||||||
while True:
|
if interactive: import code; code.interact(local=globals())
|
||||||
time.sleep(1)
|
else:
|
||||||
|
while True: time.sleep(1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
try:
|
try:
|
||||||
|
|
@ -56,6 +66,7 @@ def main():
|
||||||
parser.add_argument('-v', '--verbose', action='count', default=0)
|
parser.add_argument('-v', '--verbose', action='count', default=0)
|
||||||
parser.add_argument('-q', '--quiet', action='count', default=0)
|
parser.add_argument('-q', '--quiet', action='count', default=0)
|
||||||
parser.add_argument('-s', '--service', action='store_true', default=False, help="rnsd is running as a service and should log to file")
|
parser.add_argument('-s', '--service', action='store_true', default=False, help="rnsd is running as a service and should log to file")
|
||||||
|
parser.add_argument('-i', '--interactive', action='store_true', default=False, help="drop into interactive shell after initialisation")
|
||||||
parser.add_argument("--exampleconfig", action='store_true', default=False, help="print verbose configuration example to stdout and exit")
|
parser.add_argument("--exampleconfig", action='store_true', default=False, help="print verbose configuration example to stdout and exit")
|
||||||
parser.add_argument("--version", action="version", version="rnsd {version}".format(version=__version__))
|
parser.add_argument("--version", action="version", version="rnsd {version}".format(version=__version__))
|
||||||
|
|
||||||
|
|
@ -70,7 +81,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
configarg = None
|
configarg = None
|
||||||
|
|
||||||
program_setup(configdir = configarg, verbosity=args.verbose, quietness=args.quiet, service=args.service)
|
program_setup(configdir = configarg, verbosity=args.verbose, quietness=args.quiet, service=args.service, interactive=args.interactive)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("")
|
print("")
|
||||||
|
|
@ -89,7 +100,7 @@ __example_rns_config__ = '''# This is an example Reticulum config file.
|
||||||
# always-on. This directive is optional and can be removed
|
# always-on. This directive is optional and can be removed
|
||||||
# for brevity.
|
# for brevity.
|
||||||
|
|
||||||
enable_transport = No
|
enable_transport = no
|
||||||
|
|
||||||
|
|
||||||
# By default, the first program to launch the Reticulum
|
# By default, the first program to launch the Reticulum
|
||||||
|
|
@ -101,17 +112,30 @@ enable_transport = No
|
||||||
# user, and should generally be turned on. This directive
|
# user, and should generally be turned on. This directive
|
||||||
# is optional and can be removed for brevity.
|
# is optional and can be removed for brevity.
|
||||||
|
|
||||||
share_instance = Yes
|
share_instance = yes
|
||||||
|
|
||||||
|
|
||||||
# If you want to run multiple *different* shared instances
|
# If you want to run multiple *different* shared instances
|
||||||
# on the same system, you will need to specify different
|
# on the same system, you will need to specify different
|
||||||
# shared instance ports for each. The defaults are given
|
# instance names for each. On platforms supporting domain
|
||||||
# below, and again, these options can be left out if you
|
# sockets, this can be done with the instance_name option:
|
||||||
# don't need them.
|
|
||||||
|
|
||||||
shared_instance_port = 37428
|
instance_name = default
|
||||||
instance_control_port = 37429
|
|
||||||
|
|
||||||
|
# Some platforms don't support domain sockets, and if that
|
||||||
|
# is the case, you can isolate different instances by
|
||||||
|
# specifying a unique set of ports for each:
|
||||||
|
|
||||||
|
# shared_instance_port = 37428
|
||||||
|
# instance_control_port = 37429
|
||||||
|
|
||||||
|
|
||||||
|
# If you want to explicitly use TCP for shared instance
|
||||||
|
# communication, instead of domain sockets, this is also
|
||||||
|
# possible, by using the following option:
|
||||||
|
|
||||||
|
# shared_instance_type = tcp
|
||||||
|
|
||||||
|
|
||||||
# On systems where running instances may not have access
|
# On systems where running instances may not have access
|
||||||
|
|
@ -137,13 +161,95 @@ instance_control_port = 37429
|
||||||
# remote_management_allowed = 9fb6d773498fb3feda407ed8ef2c3229, 2d882c5586e548d79b5af27bca1776dc
|
# remote_management_allowed = 9fb6d773498fb3feda407ed8ef2c3229, 2d882c5586e548d79b5af27bca1776dc
|
||||||
|
|
||||||
|
|
||||||
|
# For easier management, discovery and configuration of
|
||||||
|
# networks with many individual transport instances,
|
||||||
|
# you can specify a network identity to be used across
|
||||||
|
# a set of instances. If sending interface discovery
|
||||||
|
# announces, these will all be signed by the specified
|
||||||
|
# network identity, and other nodes discovering your
|
||||||
|
# interfaces will be able to identify that they belong
|
||||||
|
# to the same network, even though they exist on different
|
||||||
|
# transport nodes.
|
||||||
|
|
||||||
|
# network_identity = ~/.reticulum/storage/identity/network
|
||||||
|
|
||||||
|
|
||||||
|
# It is possible to configure the global default interface
|
||||||
|
# gravity. This value will be used if an interface does
|
||||||
|
# not have a specific gravity value in its configuration.
|
||||||
|
|
||||||
|
# default_gravity = 0
|
||||||
|
|
||||||
|
# You can configure whether Reticulum should discover
|
||||||
|
# available interfaces from other Transport Instances over
|
||||||
|
# the network. If this option is enabled, Reticulum will
|
||||||
|
# collect interface information discovered from the network.
|
||||||
|
|
||||||
|
# discover_interfaces = no
|
||||||
|
|
||||||
|
|
||||||
|
# If you only want to discover interfaces from specific
|
||||||
|
# networks, you can provide a list of network identities
|
||||||
|
# from which to discover interfaces. If this option is not
|
||||||
|
# provided, interfaces will be discovered from all transport
|
||||||
|
# instances on all connected networks.
|
||||||
|
|
||||||
|
# interface_discovery_sources = 78616ff7c4b8d3886d67d494b440f333, cb127015e13aa6ea1e0a606cdc9123d0
|
||||||
|
|
||||||
|
|
||||||
|
# It is possible to automatically bring up and connect new
|
||||||
|
# interfaces discovered over the network. This option is
|
||||||
|
# disabled by default, but allows you to specify a maximum
|
||||||
|
# number of discovered interfaces to automatically connect.
|
||||||
|
# Additionally, if this option is enabled, Reticulum will
|
||||||
|
# also try to autoconnect available auto-discovered inter-
|
||||||
|
# faces on startup, up to the maximum number specified.
|
||||||
|
|
||||||
|
# autoconnect_discovered_interfaces = 0
|
||||||
|
|
||||||
|
|
||||||
|
# To prevent interface discovery spamming, a valid crypto-
|
||||||
|
# graphic stamp is required per announced interface. You
|
||||||
|
# can configure the minimum required value to accept as
|
||||||
|
# valid for discovered interfaces.
|
||||||
|
|
||||||
|
# required_discovery_value = 14
|
||||||
|
|
||||||
|
|
||||||
|
# You can disable allowing interfaces to be attached, de-
|
||||||
|
# tached or reloaded via rnstatus, for example if running
|
||||||
|
# on a shared system where users with normal RPC access
|
||||||
|
# should not be allowed to control interface states.
|
||||||
|
|
||||||
|
# enable_interface_management = no
|
||||||
|
|
||||||
|
|
||||||
# You can configure Reticulum to panic and forcibly close
|
# You can configure Reticulum to panic and forcibly close
|
||||||
# if an unrecoverable interface error occurs, such as the
|
# if an unrecoverable interface error occurs, such as the
|
||||||
# hardware device for an interface disappearing. This is
|
# hardware device for an interface disappearing. This is
|
||||||
# an optional directive, and can be left out for brevity.
|
# an optional directive, and can be left out for brevity.
|
||||||
# This behaviour is disabled by default.
|
# This behaviour is disabled by default.
|
||||||
|
|
||||||
panic_on_interface_error = No
|
# panic_on_interface_error = no
|
||||||
|
|
||||||
|
# You can specify which mode discovered interfaces should
|
||||||
|
# be created with when auto-connecting.
|
||||||
|
|
||||||
|
# autoconnect_interface_mode = gw
|
||||||
|
|
||||||
|
|
||||||
|
# It is possible to allow announces from auto-connected
|
||||||
|
# interfaces to propagate announces to internal-mode
|
||||||
|
# interfaces, even if the auto-connected interface's mode
|
||||||
|
# would normally not allow for this.
|
||||||
|
|
||||||
|
# autoconnect_announces_to_internal = yes
|
||||||
|
|
||||||
|
|
||||||
|
# You can configure the gravity for discovered interfaces
|
||||||
|
# that are auto-connected.
|
||||||
|
|
||||||
|
# autoconnect_interface_gravity = 0
|
||||||
|
|
||||||
|
|
||||||
# When Transport is enabled, it is possible to allow the
|
# When Transport is enabled, it is possible to allow the
|
||||||
|
|
@ -154,11 +260,121 @@ panic_on_interface_error = No
|
||||||
# Transport Instance, and printed to the log at startup.
|
# Transport Instance, and printed to the log at startup.
|
||||||
# Optional, and disabled by default.
|
# Optional, and disabled by default.
|
||||||
|
|
||||||
respond_to_probes = No
|
# respond_to_probes = no
|
||||||
|
|
||||||
|
|
||||||
|
# You can publish your local list of blackholed identities
|
||||||
|
# for other transport instances to use for automatic,
|
||||||
|
# network-wide blackhole management.
|
||||||
|
|
||||||
|
# publish_blackhole = no
|
||||||
|
|
||||||
|
|
||||||
|
# List of remote transport identities from which to auto-
|
||||||
|
# matically source lists of blackholed identities.
|
||||||
|
#
|
||||||
|
# If you're connecting to a large external network, you
|
||||||
|
# can use one or more external blackhole list to block
|
||||||
|
# spammy and excessive announces onto your network. This
|
||||||
|
# funtionality is especially useful if you're hosting public
|
||||||
|
# entrypoints or gateways. The list source below provides a
|
||||||
|
# functional example, but better, more timely maintained
|
||||||
|
# lists probably exist in the community.
|
||||||
|
|
||||||
|
# blackhole_sources = 521c87a83afb8f29e4455e77930b973b
|
||||||
|
|
||||||
|
|
||||||
|
# You can set the interval in minutes at which remote
|
||||||
|
# blackhole sources are updated. Defaults to one hour.
|
||||||
|
|
||||||
|
# blackhole_update_interval = 60
|
||||||
|
|
||||||
|
|
||||||
|
# You can enable a hop limiter, that will drop all packets
|
||||||
|
# exceeding the specified maximum hop count.
|
||||||
|
|
||||||
|
# hop_limiter = 16
|
||||||
|
|
||||||
|
|
||||||
|
# When not running as a transport node, it is possible to
|
||||||
|
# force the use of the same, static transport identity
|
||||||
|
# for every instance start. Defaults to using a new identity
|
||||||
|
# at every instance start, if transport is disabled.
|
||||||
|
|
||||||
|
# static_transport_identity = no
|
||||||
|
|
||||||
|
[stamps]
|
||||||
|
# This section configures stamping behaviour and defaults.
|
||||||
|
# In most cases, you do not need to change this, but on
|
||||||
|
# constrained systems especially, tuning these parameters
|
||||||
|
# can be very beneficial
|
||||||
|
|
||||||
|
# During the 1.6.x phased transition to network stamp usage,
|
||||||
|
# it is possible to enable early opt-in on stamp generation
|
||||||
|
# and enforcement. RNS 1.6.0 includes all the required stamp
|
||||||
|
# generation and verification functionality, and actively
|
||||||
|
# validates stamps, while leaving generation, emission and
|
||||||
|
# enforcement disabled by default for destinations and PRs.
|
||||||
|
# Link requests will be stamped already from 1.6.0 if a cost
|
||||||
|
# has been announced by the destination. Version 1.6.1 will
|
||||||
|
# enable generation and emission by default, and version
|
||||||
|
# 1.6.2 will finally activate enforcement. Any of these
|
||||||
|
# options can be enabled early by configuration:
|
||||||
|
|
||||||
|
# stamp_destinations = yes
|
||||||
|
# stamp_prs = yes
|
||||||
|
# enforce_stamps = yes
|
||||||
|
|
||||||
|
|
||||||
|
# The default required stamp cost for an announce, and its
|
||||||
|
# destination to be considered valid. This applies both to
|
||||||
|
# announces received directly, and transported by this node,
|
||||||
|
# if transport is enabled. Any announced destination not
|
||||||
|
# meeting the cost will be ignored.
|
||||||
|
|
||||||
|
# destination_cost = 23.5
|
||||||
|
|
||||||
|
|
||||||
|
# The default value this instance will target when generating
|
||||||
|
# new destination stamps. An application may set a higher
|
||||||
|
# target, but applications requesting a lower value than the
|
||||||
|
# default will always generate at least the default value.
|
||||||
|
# Destination stamps are cached and reused, so a one-time
|
||||||
|
# high-value generation is usually a good idea.
|
||||||
|
|
||||||
|
# destination_value = 25.0
|
||||||
|
|
||||||
|
|
||||||
|
# The default required path request stamp cost. Path requests
|
||||||
|
# that do not meet the requirement are ignored.
|
||||||
|
|
||||||
|
# pr_cost = 15.8
|
||||||
|
|
||||||
|
|
||||||
|
# The default target value of path request stamps generated
|
||||||
|
# by this instance.
|
||||||
|
|
||||||
|
# pr_value = 15.8
|
||||||
|
|
||||||
|
|
||||||
|
# The default link request stamp cost to use, if one has not
|
||||||
|
# been announced by the destination the link is established
|
||||||
|
# to, or if it is unknown. In practice, all destinations will
|
||||||
|
# include their required link cost in each announce, and this
|
||||||
|
# option is rarely necessary.
|
||||||
|
|
||||||
|
# lr_cost = 12.5
|
||||||
|
|
||||||
|
|
||||||
|
# The maximum amount of time to spend attempting to generate
|
||||||
|
# a link request and path request stamps, in seconds.
|
||||||
|
|
||||||
|
# lr_stamp_timeout = 15
|
||||||
|
# pr_stamp_timeout = 15
|
||||||
|
|
||||||
|
|
||||||
[logging]
|
[logging]
|
||||||
# Valid log levels are 0 through 7:
|
# Valid log levels are 0 through 8:
|
||||||
# 0: Log only critical information
|
# 0: Log only critical information
|
||||||
# 1: Log errors and lower log levels
|
# 1: Log errors and lower log levels
|
||||||
# 2: Log warnings and lower log levels
|
# 2: Log warnings and lower log levels
|
||||||
|
|
@ -166,10 +382,17 @@ respond_to_probes = No
|
||||||
# 4: Log info and lower (this is the default)
|
# 4: Log info and lower (this is the default)
|
||||||
# 5: Verbose logging
|
# 5: Verbose logging
|
||||||
# 6: Debug logging
|
# 6: Debug logging
|
||||||
# 7: Extreme logging
|
# 7: Path logging
|
||||||
|
# 8: Extreme logging
|
||||||
|
|
||||||
loglevel = 4
|
loglevel = 4
|
||||||
|
|
||||||
|
# You can disable timestamp inclusion in logs. Useful if
|
||||||
|
# you want to use an external logging tool that provides
|
||||||
|
# its own timestamps or custom formatting.
|
||||||
|
|
||||||
|
# logtimestamps = no
|
||||||
|
|
||||||
|
|
||||||
# The interfaces section defines the physical and virtual
|
# The interfaces section defines the physical and virtual
|
||||||
# interfaces Reticulum will use to communicate on. This
|
# interfaces Reticulum will use to communicate on. This
|
||||||
|
|
|
||||||
49
RNS/Utilities/rnsh/__init__.py
Normal file
49
RNS/Utilities/rnsh/__init__.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from ._version import __version__
|
||||||
|
|
||||||
|
import os
|
||||||
|
import glob
|
||||||
|
module_abs_filename = os.path.abspath(__file__)
|
||||||
|
module_dir = os.path.dirname(module_abs_filename)
|
||||||
|
|
||||||
|
py_modules = glob.glob(os.path.dirname(__file__)+"/*.py")
|
||||||
|
pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc")
|
||||||
|
so_modules = glob.glob(os.path.dirname(__file__)+"/*.so")
|
||||||
|
pyd_modules = glob.glob(os.path.dirname(__file__)+"/*.pyd")
|
||||||
|
modules = py_modules+pyc_modules+so_modules+pyd_modules
|
||||||
|
__all__ = list(set([os.path.basename(f).split(".")[0] for f in modules if not os.path.basename(f).split(".")[0] == "__init__"]))
|
||||||
|
|
||||||
|
def _get_version(): return __version__
|
||||||
1
RNS/Utilities/rnsh/_version.py
Normal file
1
RNS/Utilities/rnsh/_version.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
__version__ = "0.3.0"
|
||||||
94
RNS/Utilities/rnsh/args.py
Normal file
94
RNS/Utilities/rnsh/args.py
Normal file
|
|
@ -0,0 +1,94 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from RNS.Utilities.rnsh._version import __version__ as __rnsh_version__
|
||||||
|
from RNS._version import __version__
|
||||||
|
|
||||||
|
DEFAULT_SERVICE_NAME = "default"
|
||||||
|
|
||||||
|
def setup_argument_parser():
|
||||||
|
parser = argparse.ArgumentParser(description="Reticulum Remote Shell Utility", epilog="When specifying a command to execute, separate rnsh\noptions from the command and its arguments with --\n\nFor example:\n rnsh -l -- /bin/bash --login\n rnsh <destination> -- ls -la /tmp", formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
|
||||||
|
# Common options
|
||||||
|
parser.add_argument("--config", "-c", action="store", default=None, help="path to config directory", type=str)
|
||||||
|
parser.add_argument("--rnsconfig", action="store", default=None, help="path to alternative Reticulum config directory", type=str)
|
||||||
|
parser.add_argument("--identity", "-i", action="store", default=None, help="path to identity file to use", type=str)
|
||||||
|
parser.add_argument("-v", "--verbose", action="count", default=0, help="increase verbosity")
|
||||||
|
parser.add_argument("-q", "--quiet", action="count", default=0, help="decrease verbosity")
|
||||||
|
parser.add_argument("-p", "--print-identity", action="store_true", default=False, help="print identity and destination info and exit")
|
||||||
|
parser.add_argument("--version", action="version", version="rnsh {rv} (protocol {pv})".format(rv=__version__, pv=__rnsh_version__))
|
||||||
|
|
||||||
|
# Listener options
|
||||||
|
parser.add_argument("-l", "--listen", action="store_true", default=False, help="listen (server) mode; any command specified after -- will be used as the default command when the initiator does not provide one or when remote command execution is disabled; if no command is specified, the default shell of the user running rnsh will be used")
|
||||||
|
parser.add_argument("-s", "--service", action="store", default=None, help="service name for identity file if not the default", type=str)
|
||||||
|
parser.add_argument("-b", "--announce",action="store", default=None,help="announce on startup and every PERIOD seconds; specify 0 to announce on startup only",metavar="PERIOD", type=int)
|
||||||
|
parser.add_argument("-a", "--allowed", action="append", default=None, metavar="HASH", type=str, help="allow this identity to connect (may be specified multiple times); allowed identities can also be specified in ~/.rnsh/allowed_identities or ~/.config/rnsh/allowed_identities, one hash per line")
|
||||||
|
parser.add_argument("-n", "--no-auth", action="store_true", default=False, help="disable authentication (allow any identity to connect)")
|
||||||
|
parser.add_argument("-A", "--remote-command-as-args", action="store_true", default=False, help="concatenate remote command to the argument list of the default program or shell")
|
||||||
|
parser.add_argument("-C", "--no-remote-command", action="store_true", default=False, help="disable executing command lines received from the remote initiator")
|
||||||
|
|
||||||
|
# Initiator options
|
||||||
|
parser.add_argument("-N", "--no-id", action="store_true", default=False, help="disable identity announcement on connect")
|
||||||
|
parser.add_argument("-m", "--mirror", action="store_true", default=False, help="return with the exit code of the remote process")
|
||||||
|
parser.add_argument("-w", "--timeout", action="store", default=None, help="connect and request timeout in seconds", metavar="SECONDS", type=float)
|
||||||
|
|
||||||
|
parser.add_argument("destination", nargs="?", default=None, help="hexadecimal hash of the destination to connect to", type=str)
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments(argv=None):
|
||||||
|
if argv is None: argv = sys.argv[1:]
|
||||||
|
|
||||||
|
# Split at -- to separate rnsh options from the command to execute.
|
||||||
|
# Everything before -- (or the entire argv if no --) goes to argparse.
|
||||||
|
# Everything after -- becomes the command list.
|
||||||
|
try:
|
||||||
|
split_idx = argv.index("--")
|
||||||
|
rnsh_argv = argv[:split_idx]
|
||||||
|
command = argv[split_idx + 1:]
|
||||||
|
except ValueError:
|
||||||
|
rnsh_argv = argv
|
||||||
|
command = []
|
||||||
|
|
||||||
|
parser = setup_argument_parser()
|
||||||
|
args = parser.parse_args(rnsh_argv)
|
||||||
|
args.command = command
|
||||||
|
|
||||||
|
if args.listen and not args.service: args.service = DEFAULT_SERVICE_NAME
|
||||||
|
|
||||||
|
return args, parser
|
||||||
58
RNS/Utilities/rnsh/exception.py
Normal file
58
RNS/Utilities/rnsh/exception.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
from contextlib import AbstractContextManager
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class permit(AbstractContextManager):
|
||||||
|
"""Context manager to allow specified exceptions
|
||||||
|
|
||||||
|
The specified exceptions will be allowed to bubble up. Other
|
||||||
|
exceptions are suppressed.
|
||||||
|
|
||||||
|
After a non-matching exception is suppressed, execution proceeds
|
||||||
|
with the next statement following the with statement.
|
||||||
|
|
||||||
|
with allow(KeyboardInterrupt):
|
||||||
|
time.sleep(300)
|
||||||
|
# Execution still resumes here if no KeyboardInterrupt
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, *exceptions): self._exceptions = exceptions
|
||||||
|
def __enter__(self): pass
|
||||||
|
def __exit__(self, exctype, excinst, exctb):
|
||||||
|
return exctype is not None and not issubclass(exctype, self._exceptions)
|
||||||
58
RNS/Utilities/rnsh/helpers.py
Normal file
58
RNS/Utilities/rnsh/helpers.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import time
|
||||||
|
|
||||||
|
def bitwise_or_if(value: int, condition: bool, orval: int):
|
||||||
|
if not condition: return value
|
||||||
|
return value | orval
|
||||||
|
|
||||||
|
def check_and(value: int, andval: int) -> bool:
|
||||||
|
return (value & andval) > 0
|
||||||
|
|
||||||
|
class SleepRate:
|
||||||
|
def __init__(self, target_period: float):
|
||||||
|
self.target_period = target_period
|
||||||
|
self.last_wake = time.time()
|
||||||
|
|
||||||
|
def next_sleep_time(self) -> float:
|
||||||
|
old_last_wake = self.last_wake
|
||||||
|
self.last_wake = time.time()
|
||||||
|
next_wake = max(old_last_wake + 0.01, self.last_wake)
|
||||||
|
sleep_for = next_wake - self.last_wake
|
||||||
|
return sleep_for if sleep_for > 0 else 0
|
||||||
|
|
||||||
|
async def sleep_async(self): await asyncio.sleep(self.next_sleep_time())
|
||||||
|
def sleep_block(self): time.sleep(self.next_sleep_time())
|
||||||
457
RNS/Utilities/rnsh/initiator.py
Normal file
457
RNS/Utilities/rnsh/initiator.py
Normal file
|
|
@ -0,0 +1,457 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import enum
|
||||||
|
import functools
|
||||||
|
import os
|
||||||
|
import queue
|
||||||
|
import shlex
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import tty
|
||||||
|
from typing import Callable, TypeVar
|
||||||
|
import RNS
|
||||||
|
import RNS.Utilities.rnsh.exception as exception
|
||||||
|
import RNS.Utilities.rnsh.process as process
|
||||||
|
import RNS.Utilities.rnsh.retry as retry
|
||||||
|
import RNS.Utilities.rnsh.session as session
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
import bz2
|
||||||
|
import RNS.Utilities.rnsh.protocol as protocol
|
||||||
|
import RNS.Utilities.rnsh.helpers as helpers
|
||||||
|
import RNS.Utilities.rnsh.rnsh as rnsh
|
||||||
|
|
||||||
|
_identity = None
|
||||||
|
_reticulum = None
|
||||||
|
_cmd: [str] | None = None
|
||||||
|
DATA_AVAIL_MSG = "data available"
|
||||||
|
_finished: asyncio.Event = None
|
||||||
|
_retry_timer: retry.RetryThread | None = None
|
||||||
|
_destination: RNS.Destination | None = None
|
||||||
|
_loop: asyncio.AbstractEventLoop | None = None
|
||||||
|
|
||||||
|
async def _check_finished(timeout: float = 0):
|
||||||
|
return _finished is not None and await process.event_wait(_finished, timeout=timeout)
|
||||||
|
|
||||||
|
def _sigint_handler(sig, loop):
|
||||||
|
global _finished
|
||||||
|
RNS.log(f"{signal.Signals(sig).name}", RNS.LOG_DEBUG)
|
||||||
|
if _finished is not None: _finished.set()
|
||||||
|
else: raise KeyboardInterrupt()
|
||||||
|
|
||||||
|
async def _spin_tty(until=None, msg=None, timeout=None):
|
||||||
|
i = 0
|
||||||
|
syms = "⢄⢂⢁⡁⡈⡐⡠"
|
||||||
|
if timeout != None: timeout = time.time()+timeout
|
||||||
|
|
||||||
|
print(msg+" ", end=" ")
|
||||||
|
while (timeout == None or time.time()<timeout) and not until():
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
print(("\b\b"+syms[i]+" "), end="")
|
||||||
|
sys.stdout.flush()
|
||||||
|
i = (i+1)%len(syms)
|
||||||
|
print("\r"+" "*len(msg)+" \r", end="")
|
||||||
|
|
||||||
|
if timeout != None and time.time() > timeout: return False
|
||||||
|
else: return True
|
||||||
|
|
||||||
|
async def _spin_pipe(until: callable = None, msg=None, timeout: float | None = None) -> bool:
|
||||||
|
if timeout is not None: timeout += time.time()
|
||||||
|
|
||||||
|
while (timeout is None or time.time() < timeout) and not until():
|
||||||
|
if await _check_finished(0.1): raise asyncio.CancelledError()
|
||||||
|
|
||||||
|
if timeout is not None and time.time() > timeout: return False
|
||||||
|
else: return True
|
||||||
|
|
||||||
|
async def _spin(until: callable = None, msg=None, timeout: float | None = None, quiet: bool = False) -> bool:
|
||||||
|
if not quiet and os.isatty(1): return await _spin_tty(until, msg, timeout)
|
||||||
|
else: return await _spin_pipe(until, msg, timeout)
|
||||||
|
|
||||||
|
_link: RNS.Link | None = None
|
||||||
|
_remote_exec_grace = 2.0
|
||||||
|
_pq = queue.Queue()
|
||||||
|
|
||||||
|
|
||||||
|
class InitiatorState(enum.IntEnum):
|
||||||
|
IS_INITIAL = 0
|
||||||
|
IS_LINKED = 1
|
||||||
|
IS_WAIT_VERS = 2
|
||||||
|
IS_RUNNING = 3
|
||||||
|
IS_TERMINATE = 4
|
||||||
|
IS_TEARDOWN = 5
|
||||||
|
|
||||||
|
def _client_link_closed(link):
|
||||||
|
if _finished: _finished.set()
|
||||||
|
|
||||||
|
def _client_message_handler(message: RNS.MessageBase): _pq.put(message)
|
||||||
|
|
||||||
|
def compute_target_rns_loglevel(verbosity: int, quietness: int, base_level: int = RNS.LOG_INFO) -> int:
|
||||||
|
try:
|
||||||
|
target = int(base_level) + int(verbosity) - int(quietness)
|
||||||
|
if target < RNS.LOG_NONE: target = RNS.LOG_NONE
|
||||||
|
if target > RNS.LOG_EXTREME: target = RNS.LOG_EXTREME
|
||||||
|
return target
|
||||||
|
except Exception: return base_level
|
||||||
|
|
||||||
|
class RemoteExecutionError(Exception):
|
||||||
|
def __init__(self, msg): self.msg = msg
|
||||||
|
|
||||||
|
async def _initiate_link(configdir, rnsconfigdir, identitypath=None, logfile=None, verbosity=0, quietness=0, noid=False, destination=None,
|
||||||
|
timeout=RNS.Transport.PATH_REQUEST_TIMEOUT):
|
||||||
|
global _identity, _reticulum, _link, _destination, _remote_exec_grace
|
||||||
|
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH // 8) * 2
|
||||||
|
if len(destination) != dest_len:
|
||||||
|
raise RemoteExecutionError(
|
||||||
|
"Allowed destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(
|
||||||
|
hex=dest_len, byte=dest_len // 2))
|
||||||
|
|
||||||
|
try: destination_hash = bytes.fromhex(destination)
|
||||||
|
except Exception as e: raise RemoteExecutionError("Invalid destination entered. Check your input.")
|
||||||
|
|
||||||
|
if _reticulum is None:
|
||||||
|
targetloglevel = compute_target_rns_loglevel(verbosity, quietness, RNS.LOG_INFO)
|
||||||
|
RNS.logfile = logfile
|
||||||
|
_reticulum = RNS.Reticulum(configdir=rnsconfigdir, loglevel=targetloglevel, logdest=RNS.LOG_FILE)
|
||||||
|
|
||||||
|
_reticulum.wants_destination_type(f"{rnsh.APP_NAME}")
|
||||||
|
|
||||||
|
if _identity is None:
|
||||||
|
_identity = rnsh.prepare_identity(identity_path=identitypath, service_name=None, configdir=configdir)
|
||||||
|
|
||||||
|
if not RNS.Transport.has_path(destination_hash):
|
||||||
|
RNS.Transport.request_path(destination_hash)
|
||||||
|
RNS.log(f"Requesting path to {RNS.prettyhexrep(destination_hash)}", RNS.LOG_INFO)
|
||||||
|
if not await _spin(until=lambda: RNS.Transport.has_path(destination_hash), msg=f"Requesting path to {RNS.prettyhexrep(destination_hash)}",
|
||||||
|
timeout=timeout, quiet=quietness > 0):
|
||||||
|
raise RemoteExecutionError("Path not found")
|
||||||
|
|
||||||
|
if _destination is None:
|
||||||
|
listener_identity = RNS.Identity.recall(destination_hash)
|
||||||
|
_destination = RNS.Destination(listener_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, rnsh.APP_NAME)
|
||||||
|
|
||||||
|
RNS.log(f"Establishing link with {RNS.prettyhexrep(destination_hash)}", RNS.LOG_INFO)
|
||||||
|
if _link is None or _link.status == RNS.Link.PENDING:
|
||||||
|
_link = RNS.Link(_destination)
|
||||||
|
_link.did_identify = False
|
||||||
|
|
||||||
|
_link.set_link_closed_callback(_client_link_closed)
|
||||||
|
|
||||||
|
if not await _spin(until=lambda: _link.status == RNS.Link.ACTIVE, msg=f"Establishing link with {RNS.prettyhexrep(destination_hash)}",
|
||||||
|
timeout=timeout, quiet=quietness > 0):
|
||||||
|
raise RemoteExecutionError("Could not establish link with " + RNS.prettyhexrep(destination_hash))
|
||||||
|
|
||||||
|
RNS.log(f"Link established with {RNS.prettyhexrep(destination_hash)}", RNS.LOG_INFO)
|
||||||
|
if not noid and not _link.did_identify:
|
||||||
|
# Delay a tiny bit to allow listener to fully enter WAIT_IDENT state
|
||||||
|
await asyncio.sleep(min(1, _link.rtt * 1.1 + 0.05))
|
||||||
|
_link.identify(_identity)
|
||||||
|
_link.did_identify = True
|
||||||
|
|
||||||
|
async def _handle_error(errmsg: RNS.MessageBase):
|
||||||
|
if isinstance(errmsg, protocol.ErrorMessage):
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
if _link and _link.status == RNS.Link.ACTIVE: _link.teardown()
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
raise RemoteExecutionError(f"Remote error: {errmsg.msg}")
|
||||||
|
|
||||||
|
async def initiate(configdir: str, rnsconfigdir:str, identitypath: str, logfile:str, verbosity: int, quietness: int, noid: bool, destination: str,
|
||||||
|
timeout: float, command: [str] | None = None):
|
||||||
|
|
||||||
|
global _finished, _link
|
||||||
|
if timeout is None: timeout = RNS.Transport.PATH_REQUEST_TIMEOUT
|
||||||
|
with process.TTYRestorer(sys.stdin.fileno()) as ttyRestorer:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
state = InitiatorState.IS_INITIAL
|
||||||
|
data_buffer = bytearray(sys.stdin.buffer.read()) if not os.isatty(sys.stdin.fileno()) else bytearray()
|
||||||
|
line_buffer = bytearray()
|
||||||
|
|
||||||
|
await _initiate_link(configdir=configdir,
|
||||||
|
rnsconfigdir=rnsconfigdir,
|
||||||
|
identitypath=identitypath,
|
||||||
|
logfile=logfile,
|
||||||
|
verbosity=verbosity,
|
||||||
|
quietness=quietness,
|
||||||
|
noid=noid,
|
||||||
|
destination=destination,
|
||||||
|
timeout=timeout)
|
||||||
|
|
||||||
|
if not _link or _link.status not in [RNS.Link.ACTIVE, RNS.Link.PENDING]: return 255
|
||||||
|
|
||||||
|
state = InitiatorState.IS_LINKED
|
||||||
|
outlet = session.RNSOutlet(_link)
|
||||||
|
channel = _link.get_channel()
|
||||||
|
protocol.register_message_types(channel)
|
||||||
|
channel.add_message_handler(_client_message_handler)
|
||||||
|
channel.send(protocol.VersionInfoMessage())
|
||||||
|
try:
|
||||||
|
vm = _pq.get(timeout=max(outlet.rtt * 20, 5))
|
||||||
|
await _handle_error(vm)
|
||||||
|
if not isinstance(vm, protocol.VersionInfoMessage): raise Exception("Invalid message received")
|
||||||
|
RNS.log(f"Connected server version info: sw {vm.sw_version}, proto {vm.protocol_version}", RNS.LOG_INFO)
|
||||||
|
state = InitiatorState.IS_RUNNING
|
||||||
|
except queue.Empty:
|
||||||
|
print("Protocol error")
|
||||||
|
return 254
|
||||||
|
|
||||||
|
winch = False
|
||||||
|
def sigwinch_handler():
|
||||||
|
nonlocal winch
|
||||||
|
winch = True
|
||||||
|
|
||||||
|
esc = False
|
||||||
|
pre_esc = True
|
||||||
|
line_mode = False
|
||||||
|
line_flush = False
|
||||||
|
blind_write_count = 0
|
||||||
|
flush_chars = ["\x01", "\x03", "\x04", "\x05", "\x0c", "\x11", "\x13", "\x15", "\x19", "\t", "\x1A", "\x1B"]
|
||||||
|
def handle_escape(b):
|
||||||
|
nonlocal line_mode
|
||||||
|
if b == "?":
|
||||||
|
os.write(1, "\n\r\n\rSupported rnsh escape sequences:".encode("utf-8"))
|
||||||
|
os.write(1, "\n\r ~~ Send the escape character by typing it twice".encode("utf-8"))
|
||||||
|
os.write(1, "\n\r ~. Terminate session and exit immediately".encode("utf-8"))
|
||||||
|
os.write(1, "\n\r ~L Toggle line-interactive mode".encode("utf-8"))
|
||||||
|
os.write(1, "\n\r ~? Display this quick reference\n\r".encode("utf-8"))
|
||||||
|
os.write(1, "\n\r(Escape sequences are only recognized immediately after newline)\n\r".encode("utf-8"))
|
||||||
|
return None
|
||||||
|
elif b == ".":
|
||||||
|
_link.teardown()
|
||||||
|
return None
|
||||||
|
elif b == "L":
|
||||||
|
line_mode = not line_mode
|
||||||
|
if line_mode: os.write(1, "\n\rLine-interactive mode enabled\n\r".encode("utf-8"))
|
||||||
|
else: os.write(1, "\n\rLine-interactive mode disabled\n\r".encode("utf-8"))
|
||||||
|
return None
|
||||||
|
|
||||||
|
return b
|
||||||
|
|
||||||
|
stdin_eof = False
|
||||||
|
def stdin():
|
||||||
|
nonlocal stdin_eof, pre_esc, esc, line_mode
|
||||||
|
nonlocal line_flush, blind_write_count
|
||||||
|
try:
|
||||||
|
in_data = process.tty_read(sys.stdin.fileno())
|
||||||
|
if in_data is not None:
|
||||||
|
data = bytearray()
|
||||||
|
for b in bytes(in_data):
|
||||||
|
c = chr(b)
|
||||||
|
if c == "\r":
|
||||||
|
pre_esc = True
|
||||||
|
line_flush = True
|
||||||
|
data.append(b)
|
||||||
|
elif line_mode and c in flush_chars:
|
||||||
|
pre_esc = False
|
||||||
|
line_flush = True
|
||||||
|
data.append(b)
|
||||||
|
elif line_mode and (c == "\b" or c == "\x7f"):
|
||||||
|
pre_esc = False
|
||||||
|
if len(line_buffer)>0:
|
||||||
|
line_buffer.pop(-1)
|
||||||
|
blind_write_count -= 1
|
||||||
|
os.write(1, "\b \b".encode("utf-8"))
|
||||||
|
elif pre_esc == True and c == "~":
|
||||||
|
pre_esc = False
|
||||||
|
esc = True
|
||||||
|
elif esc == True:
|
||||||
|
ret = handle_escape(c)
|
||||||
|
if ret != None:
|
||||||
|
if ret != "~":
|
||||||
|
data.append(ord("~"))
|
||||||
|
data.append(ord(ret))
|
||||||
|
esc = False
|
||||||
|
else:
|
||||||
|
pre_esc = False
|
||||||
|
data.append(b)
|
||||||
|
|
||||||
|
if not line_mode: data_buffer.extend(data)
|
||||||
|
else:
|
||||||
|
line_buffer.extend(data)
|
||||||
|
if line_flush:
|
||||||
|
data_buffer.extend(line_buffer)
|
||||||
|
line_buffer.clear()
|
||||||
|
os.write(1, ("\b \b"*blind_write_count).encode("utf-8"))
|
||||||
|
line_flush = False
|
||||||
|
blind_write_count = 0
|
||||||
|
else:
|
||||||
|
os.write(1, data)
|
||||||
|
blind_write_count += len(data)
|
||||||
|
|
||||||
|
except EOFError:
|
||||||
|
if os.isatty(0): data_buffer.extend(process.CTRL_D)
|
||||||
|
stdin_eof = True
|
||||||
|
process.tty_unset_reader_callbacks(sys.stdin.fileno())
|
||||||
|
|
||||||
|
process.tty_add_reader_callback(sys.stdin.fileno(), stdin)
|
||||||
|
|
||||||
|
tcattr = None
|
||||||
|
rows, cols, hpix, vpix = (None, None, None, None)
|
||||||
|
try:
|
||||||
|
tcattr = termios.tcgetattr(0)
|
||||||
|
rows, cols, hpix, vpix = process.tty_get_winsize(0)
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
tcattr = termios.tcgetattr(1)
|
||||||
|
rows, cols, hpix, vpix = process.tty_get_winsize(1)
|
||||||
|
except:
|
||||||
|
try:
|
||||||
|
tcattr = termios.tcgetattr(2)
|
||||||
|
rows, cols, hpix, vpix = process.tty_get_winsize(2)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
await _spin(lambda: channel.is_ready_to_send(), "Waiting for channel...", 1, quietness > 0)
|
||||||
|
channel.send(protocol.ExecuteCommandMesssage(cmdline=command,
|
||||||
|
pipe_stdin=not os.isatty(0),
|
||||||
|
pipe_stdout=not os.isatty(1),
|
||||||
|
pipe_stderr=not os.isatty(2),
|
||||||
|
tcflags=tcattr,
|
||||||
|
term=os.environ.get("TERM", None),
|
||||||
|
rows=rows,
|
||||||
|
cols=cols,
|
||||||
|
hpix=hpix,
|
||||||
|
vpix=vpix))
|
||||||
|
|
||||||
|
loop.add_signal_handler(signal.SIGWINCH, sigwinch_handler)
|
||||||
|
_finished = asyncio.Event()
|
||||||
|
loop.add_signal_handler(signal.SIGINT, functools.partial(_sigint_handler, signal.SIGINT, loop))
|
||||||
|
loop.add_signal_handler(signal.SIGTERM, functools.partial(_sigint_handler, signal.SIGTERM, loop))
|
||||||
|
mdu = _link.MDU - 16
|
||||||
|
sent_eof = False
|
||||||
|
last_winch = time.time()
|
||||||
|
sleeper = helpers.SleepRate(0.01)
|
||||||
|
processed = False
|
||||||
|
while not await _check_finished() and state in [InitiatorState.IS_RUNNING]:
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
message = _pq.get(timeout=sleeper.next_sleep_time() if not processed else 0.0005)
|
||||||
|
await _handle_error(message)
|
||||||
|
processed = True
|
||||||
|
if isinstance(message, protocol.StreamDataMessage):
|
||||||
|
if message.stream_id == protocol.StreamDataMessage.STREAM_ID_STDOUT:
|
||||||
|
if message.data and len(message.data) > 0:
|
||||||
|
ttyRestorer.raw()
|
||||||
|
# RNS.log(f"stdout: {message.data}", RNS.LOG_EXTREME)
|
||||||
|
os.write(1, message.data)
|
||||||
|
sys.stdout.flush()
|
||||||
|
if message.eof:
|
||||||
|
os.close(1)
|
||||||
|
if message.stream_id == protocol.StreamDataMessage.STREAM_ID_STDERR:
|
||||||
|
if message.data and len(message.data) > 0:
|
||||||
|
ttyRestorer.raw()
|
||||||
|
# RNS.log(f"stdout: {message.data}", RNS.LOG_EXTREME)
|
||||||
|
os.write(2, message.data)
|
||||||
|
sys.stderr.flush()
|
||||||
|
if message.eof:
|
||||||
|
os.close(2)
|
||||||
|
elif isinstance(message, protocol.CommandExitedMessage):
|
||||||
|
RNS.log(f"received return code {message.return_code}, exiting", RNS.LOG_DEBUG)
|
||||||
|
return message.return_code
|
||||||
|
elif isinstance(message, protocol.ErrorMessage):
|
||||||
|
RNS.log(f"Remote error: {message.data}", RNS.LOG_ERROR)
|
||||||
|
if message.fatal:
|
||||||
|
_link.teardown()
|
||||||
|
return 200
|
||||||
|
|
||||||
|
except queue.Empty: processed = False
|
||||||
|
|
||||||
|
if channel.is_ready_to_send():
|
||||||
|
def compress_adaptive(buf: bytes):
|
||||||
|
comp_tries = RNS.RawChannelWriter.COMPRESSION_TRIES
|
||||||
|
comp_try = 1
|
||||||
|
comp_success = False
|
||||||
|
|
||||||
|
chunk_len = len(buf)
|
||||||
|
if chunk_len > RNS.RawChannelWriter.MAX_CHUNK_LEN:
|
||||||
|
chunk_len = RNS.RawChannelWriter.MAX_CHUNK_LEN
|
||||||
|
chunk_segment = None
|
||||||
|
|
||||||
|
chunk_segment = None
|
||||||
|
max_data_len = channel.mdu - protocol.StreamDataMessage.HEADER_LEN
|
||||||
|
while chunk_len > 32 and comp_try < comp_tries:
|
||||||
|
chunk_segment_length = int(chunk_len/comp_try)
|
||||||
|
compressed_chunk = bz2.compress(buf[:chunk_segment_length])
|
||||||
|
compressed_length = len(compressed_chunk)
|
||||||
|
if compressed_length < max_data_len and compressed_length < chunk_segment_length:
|
||||||
|
comp_success = True
|
||||||
|
break
|
||||||
|
else: comp_try += 1
|
||||||
|
|
||||||
|
if comp_success:
|
||||||
|
diff = max_data_len - len(compressed_chunk)
|
||||||
|
chunk = compressed_chunk
|
||||||
|
processed_length = chunk_segment_length
|
||||||
|
else:
|
||||||
|
chunk = bytes(buf[:max_data_len])
|
||||||
|
processed_length = len(chunk)
|
||||||
|
|
||||||
|
return comp_success, processed_length, chunk
|
||||||
|
|
||||||
|
comp_success, processed_length, chunk = compress_adaptive(data_buffer)
|
||||||
|
stdin = chunk
|
||||||
|
data_buffer = data_buffer[processed_length:]
|
||||||
|
eof = not sent_eof and stdin_eof and len(stdin) == 0
|
||||||
|
if len(stdin) > 0 or eof:
|
||||||
|
channel.send(protocol.StreamDataMessage(protocol.StreamDataMessage.STREAM_ID_STDIN, stdin, eof, comp_success))
|
||||||
|
sent_eof = eof
|
||||||
|
processed = True
|
||||||
|
|
||||||
|
# send window change, but rate limited
|
||||||
|
if winch and time.time() - last_winch > _link.rtt * 25:
|
||||||
|
last_winch = time.time()
|
||||||
|
winch = False
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
r, c, h, v = process.tty_get_winsize(0)
|
||||||
|
channel.send(protocol.WindowSizeMessage(r, c, h, v))
|
||||||
|
processed = True
|
||||||
|
|
||||||
|
except RemoteExecutionError as e:
|
||||||
|
print(e.msg)
|
||||||
|
return 255
|
||||||
|
except Exception as ex:
|
||||||
|
print(f"Client exception: {ex}")
|
||||||
|
if _link and _link.status != RNS.Link.CLOSED:
|
||||||
|
_link.teardown()
|
||||||
|
return 127
|
||||||
|
|
||||||
|
RNS.log("Main loop done", RNS.LOG_DEBUG)
|
||||||
|
return 0
|
||||||
234
RNS/Utilities/rnsh/listener.py
Normal file
234
RNS/Utilities/rnsh/listener.py
Normal file
|
|
@ -0,0 +1,234 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import queue
|
||||||
|
import shlex
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import tty
|
||||||
|
from typing import Callable, TypeVar
|
||||||
|
import RNS
|
||||||
|
import RNS.Utilities.rnsh.exception as exception
|
||||||
|
import RNS.Utilities.rnsh.process as process
|
||||||
|
import RNS.Utilities.rnsh.retry as retry
|
||||||
|
import RNS.Utilities.rnsh.session as session
|
||||||
|
import contextlib
|
||||||
|
|
||||||
|
import pwd
|
||||||
|
import RNS.Utilities.rnsh.protocol as protocol
|
||||||
|
import RNS.Utilities.rnsh.helpers as helpers
|
||||||
|
import RNS.Utilities.rnsh.rnsh as rnsh
|
||||||
|
|
||||||
|
|
||||||
|
_identity = None
|
||||||
|
_reticulum = None
|
||||||
|
_allow_all = False
|
||||||
|
_allowed_file = None
|
||||||
|
_allowed_identity_hashes = []
|
||||||
|
_allowed_file_identity_hashes = []
|
||||||
|
_cmd: [str] | None = None
|
||||||
|
DATA_AVAIL_MSG = "data available"
|
||||||
|
_finished: asyncio.Event = None
|
||||||
|
_retry_timer: retry.RetryThread | None = None
|
||||||
|
_destination: RNS.Destination | None = None
|
||||||
|
_loop: asyncio.AbstractEventLoop | None = None
|
||||||
|
_no_remote_command = True
|
||||||
|
_remote_cmd_as_args = False
|
||||||
|
|
||||||
|
|
||||||
|
async def _check_finished(timeout: float = 0):
|
||||||
|
return await process.event_wait(_finished, timeout=timeout)
|
||||||
|
|
||||||
|
|
||||||
|
def _sigint_handler(sig, loop):
|
||||||
|
global _finished
|
||||||
|
RNS.log(f"Signal: {signal.Signals(sig).name}", RNS.LOG_DEBUG)
|
||||||
|
if _finished is not None: _finished.set()
|
||||||
|
else: raise KeyboardInterrupt()
|
||||||
|
|
||||||
|
def _reload_allowed_file():
|
||||||
|
global _allowed_file, _allowed_file_identity_hashes
|
||||||
|
if _allowed_file != None:
|
||||||
|
try:
|
||||||
|
with open(_allowed_file, "r") as file:
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH // 8) * 2
|
||||||
|
added = 0
|
||||||
|
line = 0
|
||||||
|
_allowed_file_identity_hashes = []
|
||||||
|
for allow in file.read().replace("\r", "").split("\n"):
|
||||||
|
line += 1
|
||||||
|
if len(allow) == dest_len:
|
||||||
|
try:
|
||||||
|
destination_hash = bytes.fromhex(allow)
|
||||||
|
_allowed_file_identity_hashes.append(destination_hash)
|
||||||
|
added += 1
|
||||||
|
except Exception:
|
||||||
|
RNS.log(f"Discarded invalid Identity hash in {_allowed_file} at line {line}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
ms = "y" if added == 1 else "ies"
|
||||||
|
RNS.log(f"Loaded {added} allowed identit{ms} from "+str(_allowed_file), RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
except Exception as e: RNS.log(f"Error while reloading allowed indetities file: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
def compute_target_rns_loglevel(verbosity: int, quietness: int, base_level: int = RNS.LOG_INFO) -> int:
|
||||||
|
try:
|
||||||
|
target = int(base_level) + int(verbosity) - int(quietness)
|
||||||
|
if target < RNS.LOG_NONE: target = RNS.LOG_NONE
|
||||||
|
if target > RNS.LOG_EXTREME: target = RNS.LOG_EXTREME
|
||||||
|
return target
|
||||||
|
|
||||||
|
except Exception: return base_level
|
||||||
|
|
||||||
|
async def listen(configdir, rnsconfigdir, command, identitypath=None, logfile=None, service_name=None, verbosity=0, quietness=0,
|
||||||
|
allowed=None, allowed_file=None, disable_auth=None, announce_period=900, no_remote_command=True, remote_cmd_as_args=False,
|
||||||
|
loop: asyncio.AbstractEventLoop = None):
|
||||||
|
global _identity, _allow_all, _allowed_identity_hashes, _allowed_file, _allowed_file_identity_hashes
|
||||||
|
global _reticulum, _cmd, _destination, _no_remote_command, _remote_cmd_as_args, _finished
|
||||||
|
|
||||||
|
if not loop: loop = asyncio.get_running_loop()
|
||||||
|
if service_name is None or len(service_name) == 0: service_name = "default"
|
||||||
|
|
||||||
|
RNS.log(f"Using service name {service_name}", RNS.LOG_INFO)
|
||||||
|
if logfile:
|
||||||
|
RNS.log(f"Logging to {logfile}", RNS.LOG_NOTICE)
|
||||||
|
logdest = RNS.LOG_FILE
|
||||||
|
RNS.logfile = logfile
|
||||||
|
else:
|
||||||
|
RNS.log(f"Logging to console", RNS.LOG_NOTICE)
|
||||||
|
logdest = RNS.LOG_STDOUT
|
||||||
|
|
||||||
|
# More -v should increase verbosity (higher RNS.loglevel); -q should decrease it
|
||||||
|
targetloglevel = compute_target_rns_loglevel(verbosity, quietness, RNS.LOG_INFO)
|
||||||
|
_reticulum = RNS.Reticulum(configdir=rnsconfigdir, loglevel=targetloglevel)
|
||||||
|
_identity = rnsh.prepare_identity(identity_path=identitypath, service_name=service_name, configdir=configdir)
|
||||||
|
_destination = RNS.Destination(_identity, RNS.Destination.IN, RNS.Destination.SINGLE, rnsh.APP_NAME)
|
||||||
|
|
||||||
|
RNS.log(f"rnsh listening for commands on {RNS.prettyhexrep(_destination.hash)}", RNS.LOG_NOTICE)
|
||||||
|
RNS.logdest = logdest
|
||||||
|
|
||||||
|
_cmd = command
|
||||||
|
if _cmd is None or len(_cmd) == 0:
|
||||||
|
shell = None
|
||||||
|
try: shell = pwd.getpwuid(os.getuid()).pw_shell
|
||||||
|
except Exception as e: RNS.log(f"Error looking up shell: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.log(f"Using {shell} for default command.", RNS.LOG_INFO)
|
||||||
|
|
||||||
|
# Ensure a sane shell default. Fall back to /bin/sh if lookup fails.
|
||||||
|
if not shell or len(shell) == 0: shell = "/bin/sh"
|
||||||
|
_cmd = [shell]
|
||||||
|
|
||||||
|
else: RNS.log(f"Using command {shlex.join(_cmd)}", RNS.LOG_INFO)
|
||||||
|
|
||||||
|
_no_remote_command = no_remote_command
|
||||||
|
session.ListenerSession.allow_remote_command = not no_remote_command
|
||||||
|
_remote_cmd_as_args = remote_cmd_as_args
|
||||||
|
if (_cmd is None or len(_cmd) == 0 or _cmd[0] is None or len(_cmd[0]) == 0) and (_no_remote_command or _remote_cmd_as_args):
|
||||||
|
raise Exception(f"Unable to look up shell for {os.getlogin()}, cannot proceed with -A or -C and no <program>.")
|
||||||
|
|
||||||
|
session.ListenerSession.default_command = _cmd
|
||||||
|
session.ListenerSession.remote_cmd_as_args = _remote_cmd_as_args
|
||||||
|
|
||||||
|
if disable_auth:
|
||||||
|
_allow_all = True
|
||||||
|
session.ListenerSession.allow_all = True
|
||||||
|
else:
|
||||||
|
if allowed_file is not None:
|
||||||
|
_allowed_file = allowed_file
|
||||||
|
_reload_allowed_file()
|
||||||
|
|
||||||
|
if allowed is not None:
|
||||||
|
for a in allowed:
|
||||||
|
try:
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH // 8) * 2
|
||||||
|
if len(a) != dest_len:
|
||||||
|
raise ValueError(
|
||||||
|
"Allowed destination length is invalid, must be {hex} hexadecimal " +
|
||||||
|
"characters ({byte} bytes).".format(
|
||||||
|
hex=dest_len, byte=dest_len // 2))
|
||||||
|
try:
|
||||||
|
destination_hash = bytes.fromhex(a)
|
||||||
|
_allowed_identity_hashes.append(destination_hash)
|
||||||
|
session.ListenerSession.allowed_identity_hashes.append(destination_hash)
|
||||||
|
except Exception:
|
||||||
|
raise ValueError("Invalid destination entered. Check your input.")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Unhandled error: {e}", RNS.LOG_ERROR)
|
||||||
|
RNS.trace_exception(e)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
if (len(_allowed_identity_hashes) < 1 and len(_allowed_file_identity_hashes) < 1) and not disable_auth:
|
||||||
|
RNS.log("Warning: No allowed identities configured, rnsh will not accept any connections!", RNS.LOG_WARNING)
|
||||||
|
|
||||||
|
def link_established(lnk: RNS.Link):
|
||||||
|
_reload_allowed_file()
|
||||||
|
session.ListenerSession.allowed_file_identity_hashes = _allowed_file_identity_hashes
|
||||||
|
session.ListenerSession(session.RNSOutlet.get_outlet(lnk), lnk.get_channel(), loop)
|
||||||
|
_destination.set_link_established_callback(link_established)
|
||||||
|
|
||||||
|
_finished = asyncio.Event()
|
||||||
|
signal.signal(signal.SIGINT, _sigint_handler)
|
||||||
|
|
||||||
|
if announce_period is not None: _destination.announce()
|
||||||
|
|
||||||
|
last_announce = time.time()
|
||||||
|
sleeper = helpers.SleepRate(0.01)
|
||||||
|
|
||||||
|
try:
|
||||||
|
while not await _check_finished():
|
||||||
|
if announce_period and 0 < announce_period < time.time() - last_announce:
|
||||||
|
last_announce = time.time()
|
||||||
|
_destination.announce()
|
||||||
|
if len(session.ListenerSession.sessions) > 0:
|
||||||
|
# no sleep if there's work to do
|
||||||
|
if not await session.ListenerSession.pump_all():
|
||||||
|
await sleeper.sleep_async()
|
||||||
|
else:
|
||||||
|
await asyncio.sleep(0.25)
|
||||||
|
finally:
|
||||||
|
RNS.log("Shutting down", RNS.LOG_NOTICE)
|
||||||
|
await session.ListenerSession.terminate_all("Shutting down")
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
links_still_active = list(filter(lambda l: l.status != RNS.Link.CLOSED, _destination.links))
|
||||||
|
for link in links_still_active:
|
||||||
|
if link.status not in [RNS.Link.CLOSED]:
|
||||||
|
link.teardown()
|
||||||
|
await asyncio.sleep(0.01)
|
||||||
46
RNS/Utilities/rnsh/loop.py
Normal file
46
RNS/Utilities/rnsh/loop.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import functools
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
def sig_handler_sys_to_loop(handler: Callable[[int, any], None]) -> Callable[[int, asyncio.AbstractEventLoop], None]:
|
||||||
|
def wrapped(cb: Callable[[int, any], None], signal: int, loop: asyncio.AbstractEventLoop): cb(signal, None)
|
||||||
|
return functools.partial(wrapped, handler)
|
||||||
|
|
||||||
|
def loop_set_signal(sig, handler: Callable[[int, asyncio.AbstractEventLoop], None], loop: asyncio.AbstractEventLoop = None):
|
||||||
|
if loop is None: loop = asyncio.get_running_loop()
|
||||||
|
loop.remove_signal_handler(sig)
|
||||||
|
loop.add_signal_handler(sig, functools.partial(handler, sig, loop))
|
||||||
769
RNS/Utilities/rnsh/process.py
Normal file
769
RNS/Utilities/rnsh/process.py
Normal file
|
|
@ -0,0 +1,769 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
import asyncio
|
||||||
|
import contextlib
|
||||||
|
import copy
|
||||||
|
import errno
|
||||||
|
import fcntl
|
||||||
|
import functools
|
||||||
|
import os
|
||||||
|
import pty
|
||||||
|
import select
|
||||||
|
import signal
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
import threading
|
||||||
|
import tty
|
||||||
|
import types
|
||||||
|
import typing
|
||||||
|
import RNS
|
||||||
|
|
||||||
|
import RNS.Utilities.rnsh.exception as exception
|
||||||
|
|
||||||
|
CTRL_C = "\x03".encode("utf-8")
|
||||||
|
CTRL_D = "\x04".encode("utf-8")
|
||||||
|
|
||||||
|
def tty_add_reader_callback(fd: int, callback: callable, loop: asyncio.AbstractEventLoop = None):
|
||||||
|
"""
|
||||||
|
Add an async reader callback for a tty file descriptor.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
|
||||||
|
def reader():
|
||||||
|
data = tty_read(fd)
|
||||||
|
# do something with data
|
||||||
|
|
||||||
|
tty_add_reader_callback(self._child_fd, reader, self._loop)
|
||||||
|
|
||||||
|
:param fd: file descriptor
|
||||||
|
:param callback: callback function
|
||||||
|
:param loop: asyncio event loop to which the reader should be added. If None, use the currently-running loop.
|
||||||
|
"""
|
||||||
|
if loop is None:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
loop.add_reader(fd, callback)
|
||||||
|
|
||||||
|
|
||||||
|
def tty_read(fd: int) -> bytes:
|
||||||
|
"""
|
||||||
|
Read available bytes from a tty file descriptor. When used in a callback added to a file descriptor using
|
||||||
|
tty_add_reader_callback(...), this function creates a solution for non-blocking reads from ttys.
|
||||||
|
:param fd: tty file descriptor
|
||||||
|
:return: bytes read
|
||||||
|
"""
|
||||||
|
if fd_is_closed(fd):
|
||||||
|
raise EOFError
|
||||||
|
|
||||||
|
try:
|
||||||
|
run = True
|
||||||
|
result = bytearray()
|
||||||
|
while not fd_is_closed(fd):
|
||||||
|
ready, _, _ = select.select([fd], [], [], 0)
|
||||||
|
if len(ready) == 0:
|
||||||
|
break
|
||||||
|
for f in ready:
|
||||||
|
try:
|
||||||
|
data = os.read(f, 4096)
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno != errno.EIO and e.errno != errno.EWOULDBLOCK:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
if not data: # EOF
|
||||||
|
if data is not None and len(data) > 0:
|
||||||
|
result.extend(data)
|
||||||
|
return result
|
||||||
|
elif len(result) > 0:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
raise EOFError
|
||||||
|
if data is not None and len(data) > 0:
|
||||||
|
result.extend(data)
|
||||||
|
return result
|
||||||
|
|
||||||
|
except EOFError: raise
|
||||||
|
except Exception as e: RNS.log(f"TTY read error: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
|
||||||
|
def tty_read_poll(fd: int) -> bytes:
|
||||||
|
"""
|
||||||
|
Read available bytes from a tty file descriptor. When used in a callback added to a file descriptor using
|
||||||
|
tty_add_reader_callback(...), this function creates a solution for non-blocking reads from ttys.
|
||||||
|
:param fd: tty file descriptor
|
||||||
|
:return: bytes read
|
||||||
|
"""
|
||||||
|
if fd_is_closed(fd):
|
||||||
|
raise EOFError
|
||||||
|
|
||||||
|
result = bytearray()
|
||||||
|
try:
|
||||||
|
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
|
||||||
|
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data = os.read(fd, 4096)
|
||||||
|
if not data:
|
||||||
|
# EOF
|
||||||
|
if len(result) > 0:
|
||||||
|
return result
|
||||||
|
raise EOFError
|
||||||
|
result.extend(data)
|
||||||
|
# continue loop to drain
|
||||||
|
except OSError as e:
|
||||||
|
if e.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
|
||||||
|
break
|
||||||
|
if e.errno == errno.EIO:
|
||||||
|
if len(result) > 0:
|
||||||
|
return result
|
||||||
|
raise EOFError
|
||||||
|
raise
|
||||||
|
except EOFError: raise
|
||||||
|
except Exception as e: RNS.log(f"TTY read error: {e}", RNS.LOG_ERROR)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def fd_is_closed(fd: int) -> bool:
|
||||||
|
"""
|
||||||
|
Check if file descriptor is closed
|
||||||
|
:param fd: file descriptor
|
||||||
|
:return: True if file descriptor is closed
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
fcntl.fcntl(fd, fcntl.F_GETFL) < 0
|
||||||
|
except OSError as ose:
|
||||||
|
return ose.errno == errno.EBADF
|
||||||
|
|
||||||
|
|
||||||
|
def tty_unset_reader_callbacks(fd: int, loop: asyncio.AbstractEventLoop = None):
|
||||||
|
"""
|
||||||
|
Remove async reader callbacks for file descriptor.
|
||||||
|
:param fd: file descriptor
|
||||||
|
:param loop: asyncio event loop from which to remove callbacks
|
||||||
|
"""
|
||||||
|
with exception.permit(SystemExit):
|
||||||
|
if loop is None:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
loop.remove_reader(fd)
|
||||||
|
|
||||||
|
|
||||||
|
def tty_get_winsize(fd: int) -> [int, int, int, int]:
|
||||||
|
"""
|
||||||
|
Ge the window size of a tty.
|
||||||
|
:param fd: file descriptor of tty
|
||||||
|
:return: (rows, cols, h_pixels, v_pixels)
|
||||||
|
"""
|
||||||
|
packed = fcntl.ioctl(fd, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0))
|
||||||
|
rows, cols, h_pixels, v_pixels = struct.unpack('HHHH', packed)
|
||||||
|
return rows, cols, h_pixels, v_pixels
|
||||||
|
|
||||||
|
|
||||||
|
def tty_set_winsize(fd: int, rows: int, cols: int, h_pixels: int, v_pixels: int):
|
||||||
|
"""
|
||||||
|
Set the window size on a tty.
|
||||||
|
:param fd: file descriptor of tty
|
||||||
|
:param rows: number of visible rows
|
||||||
|
:param cols: number of visible columns
|
||||||
|
:param h_pixels: number of visible horizontal pixels
|
||||||
|
:param v_pixels: number of visible vertical pixels
|
||||||
|
"""
|
||||||
|
if fd < 0:
|
||||||
|
return
|
||||||
|
packed = struct.pack('HHHH', rows, cols, h_pixels, v_pixels)
|
||||||
|
fcntl.ioctl(fd, termios.TIOCSWINSZ, packed)
|
||||||
|
|
||||||
|
|
||||||
|
def process_exists(pid) -> bool:
|
||||||
|
"""
|
||||||
|
Check For the existence of a unix pid.
|
||||||
|
:param pid: process id to check
|
||||||
|
:return: True if process exists
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
os.kill(pid, 0)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class TTYRestorer(contextlib.AbstractContextManager):
|
||||||
|
# Indexes of flags within the attrs array
|
||||||
|
ATTR_IDX_IFLAG = 0
|
||||||
|
ATTR_IDX_OFLAG = 1
|
||||||
|
ATTR_IDX_CFLAG = 2
|
||||||
|
ATTR_IDX_LFLAG = 4
|
||||||
|
ATTR_IDX_CC = 5
|
||||||
|
|
||||||
|
def __init__(self, fd: int, suppress_logs=False):
|
||||||
|
"""
|
||||||
|
Saves termios attributes for a tty for later restoration.
|
||||||
|
|
||||||
|
The attributes are an array of values with the following meanings.
|
||||||
|
|
||||||
|
tcflag_t c_iflag; /* input modes */
|
||||||
|
tcflag_t c_oflag; /* output modes */
|
||||||
|
tcflag_t c_cflag; /* control modes */
|
||||||
|
tcflag_t c_lflag; /* local modes */
|
||||||
|
cc_t c_cc[NCCS]; /* special characters */
|
||||||
|
|
||||||
|
:param fd: file descriptor of tty
|
||||||
|
"""
|
||||||
|
self._fd = fd
|
||||||
|
self._tattr = None
|
||||||
|
self._suppress_logs = suppress_logs
|
||||||
|
self._tattr = self.current_attr()
|
||||||
|
if not self._tattr and not self._suppress_logs: RNS.log(f"Could not get attrs for fd {fd}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
def raw(self):
|
||||||
|
"""
|
||||||
|
Set raw mode on tty
|
||||||
|
"""
|
||||||
|
if self._fd is None:
|
||||||
|
return
|
||||||
|
with contextlib.suppress(termios.error):
|
||||||
|
tty.setraw(self._fd, termios.TCSANOW)
|
||||||
|
|
||||||
|
def original_attr(self) -> [any]:
|
||||||
|
return copy.deepcopy(self._tattr)
|
||||||
|
|
||||||
|
def current_attr(self) -> [any]:
|
||||||
|
"""
|
||||||
|
Get the current termios attributes for the wrapped fd.
|
||||||
|
:return: attribute array
|
||||||
|
"""
|
||||||
|
if self._fd is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
with contextlib.suppress(termios.error):
|
||||||
|
return copy.deepcopy(termios.tcgetattr(self._fd))
|
||||||
|
return None
|
||||||
|
|
||||||
|
def set_attr(self, attr: [any], when: int = termios.TCSADRAIN):
|
||||||
|
"""
|
||||||
|
Set termios attributes
|
||||||
|
:param attr: attribute list to set
|
||||||
|
:param when: when attributes should be applied (termios.TCSANOW, termios.TCSADRAIN, termios.TCSAFLUSH)
|
||||||
|
"""
|
||||||
|
if not attr or self._fd is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
with contextlib.suppress(termios.error):
|
||||||
|
termios.tcsetattr(self._fd, when, attr)
|
||||||
|
|
||||||
|
def isatty(self):
|
||||||
|
return os.isatty(self._fd) if self._fd is not None else None
|
||||||
|
|
||||||
|
def restore(self):
|
||||||
|
"""
|
||||||
|
Restore termios settings to state captured in constructor.
|
||||||
|
"""
|
||||||
|
self.set_attr(self._tattr, termios.TCSADRAIN)
|
||||||
|
|
||||||
|
def __exit__(self, __exc_type: typing.Type[BaseException], __exc_value: BaseException,
|
||||||
|
__traceback: types.TracebackType) -> bool:
|
||||||
|
self.restore()
|
||||||
|
return False #__exc_type is not None and issubclass(__exc_type, termios.error)
|
||||||
|
|
||||||
|
|
||||||
|
def _task_from_event(evt: asyncio.Event, loop: asyncio.AbstractEventLoop = None):
|
||||||
|
if not loop:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
|
||||||
|
#TODO: this is hacky
|
||||||
|
async def wait():
|
||||||
|
while not evt.is_set():
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
return True
|
||||||
|
|
||||||
|
return loop.create_task(wait())
|
||||||
|
|
||||||
|
|
||||||
|
class AggregateException(Exception):
|
||||||
|
def __init__(self, inner_exceptions: [Exception]):
|
||||||
|
super().__init__()
|
||||||
|
self.inner_exceptions = inner_exceptions
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Multiple exceptions encountered: \n\n" + "\n\n".join(map(lambda e: str(e), self.inner_exceptions))
|
||||||
|
|
||||||
|
|
||||||
|
async def event_wait_any(evts: [asyncio.Event], timeout: float = None) -> (any, any):
|
||||||
|
tasks = list(map(lambda evt: (evt, _task_from_event(evt)), evts))
|
||||||
|
try:
|
||||||
|
finished, unfinished = await asyncio.wait(map(lambda t: t[1], tasks),
|
||||||
|
timeout=timeout,
|
||||||
|
return_when=asyncio.FIRST_COMPLETED)
|
||||||
|
|
||||||
|
if len(unfinished) > 0:
|
||||||
|
for task in unfinished:
|
||||||
|
task.cancel()
|
||||||
|
await asyncio.wait(unfinished)
|
||||||
|
|
||||||
|
exceptions = []
|
||||||
|
|
||||||
|
for f in finished:
|
||||||
|
ex = f.exception()
|
||||||
|
if ex and not isinstance(ex, asyncio.CancelledError) and not isinstance(ex, TimeoutError):
|
||||||
|
exceptions.append(ex)
|
||||||
|
|
||||||
|
if len(exceptions) > 0:
|
||||||
|
raise AggregateException(exceptions)
|
||||||
|
|
||||||
|
return next(map(lambda t: next(map(lambda tt: tt[0], tasks)), finished), None)
|
||||||
|
finally:
|
||||||
|
unfinished = []
|
||||||
|
for task in map(lambda t: t[1], tasks):
|
||||||
|
if task.done():
|
||||||
|
if not task.cancelled():
|
||||||
|
task.exception()
|
||||||
|
else:
|
||||||
|
task.cancel()
|
||||||
|
unfinished.append(task)
|
||||||
|
if len(unfinished) > 0:
|
||||||
|
await asyncio.wait(unfinished)
|
||||||
|
|
||||||
|
|
||||||
|
async def event_wait(evt: asyncio.Event, timeout: float) -> bool:
|
||||||
|
"""
|
||||||
|
Wait for event to be set, or timeout to expire.
|
||||||
|
:param evt: asyncio.Event to wait on
|
||||||
|
:param timeout: maximum number of seconds to wait.
|
||||||
|
:return: True if event was set, False if timeout expired
|
||||||
|
"""
|
||||||
|
await event_wait_any([evt], timeout=timeout)
|
||||||
|
return evt.is_set()
|
||||||
|
|
||||||
|
|
||||||
|
def _launch_child(cmd_line: list[str], env: dict[str, str], stdin_is_pipe: bool, stdout_is_pipe: bool,
|
||||||
|
stderr_is_pipe: bool) -> tuple[int, int, int, int]:
|
||||||
|
# Set up PTY and/or pipes
|
||||||
|
child_fd = parent_fd = None
|
||||||
|
if not (stdin_is_pipe and stdout_is_pipe and stderr_is_pipe):
|
||||||
|
parent_fd, child_fd = pty.openpty()
|
||||||
|
child_stdin, parent_stdin = (os.pipe() if stdin_is_pipe else (child_fd, parent_fd))
|
||||||
|
parent_stdout, child_stdout = (os.pipe() if stdout_is_pipe else (parent_fd, child_fd))
|
||||||
|
parent_stderr, child_stderr = (os.pipe() if stderr_is_pipe else (parent_fd, child_fd))
|
||||||
|
|
||||||
|
# Fork
|
||||||
|
pid = os.fork()
|
||||||
|
|
||||||
|
if pid == 0:
|
||||||
|
try:
|
||||||
|
# We are in the child process, so close all open sockets and pipes except for the PTY and/or pipes
|
||||||
|
max_fd = os.sysconf("SC_OPEN_MAX")
|
||||||
|
for fd in range(3, max_fd):
|
||||||
|
if fd not in (child_stdin, child_stdout, child_stderr):
|
||||||
|
try:
|
||||||
|
os.close(fd)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Set up PTY and/or pipes
|
||||||
|
os.dup2(child_stdin, 0)
|
||||||
|
os.dup2(child_stdout, 1)
|
||||||
|
os.dup2(child_stderr, 2)
|
||||||
|
# Make PTY controlling if necessary so that CTRL_C/CTRL_D behave as expected
|
||||||
|
if child_fd is not None:
|
||||||
|
os.setsid()
|
||||||
|
try:
|
||||||
|
tty_fd = 0 if not stdin_is_pipe else (1 if not stdout_is_pipe else 2)
|
||||||
|
# Set controlling TTY for this session
|
||||||
|
fcntl.ioctl(tty_fd, termios.TIOCSCTTY, 0)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
# Ensure the child is the foreground process group for the TTY
|
||||||
|
try:
|
||||||
|
os.setpgid(0, 0)
|
||||||
|
pgid = os.getpgrp()
|
||||||
|
import struct as _struct
|
||||||
|
fcntl.ioctl(tty_fd, termios.TIOCSPGRP, _struct.pack('i', pgid))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
# Ensure canonical input with signals and local echo enabled
|
||||||
|
try:
|
||||||
|
tty_fd = 0 if not stdin_is_pipe else (1 if not stdout_is_pipe else 2)
|
||||||
|
attrs = termios.tcgetattr(tty_fd)
|
||||||
|
lflag = attrs[3]
|
||||||
|
lflag |= termios.ICANON | termios.ISIG | termios.ECHO
|
||||||
|
attrs[3] = lflag
|
||||||
|
termios.tcsetattr(tty_fd, termios.TCSANOW, attrs)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Execute the command
|
||||||
|
os.execvpe(cmd_line[0], cmd_line, env)
|
||||||
|
except Exception as err:
|
||||||
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
||||||
|
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
|
||||||
|
print(f"Unable to start {cmd_line[0]}: {err} ({fname}:{exc_tb.tb_lineno})")
|
||||||
|
sys.stdout.flush()
|
||||||
|
# don't let any other modules get in our way, do an immediate silent exit.
|
||||||
|
os._exit(255)
|
||||||
|
|
||||||
|
else:
|
||||||
|
# We are in the parent process, so close the child-side of the PTY and/or pipes
|
||||||
|
if child_fd is not None:
|
||||||
|
os.close(child_fd)
|
||||||
|
if child_stdin != child_fd:
|
||||||
|
os.close(child_stdin)
|
||||||
|
if child_stdout != child_fd:
|
||||||
|
os.close(child_stdout)
|
||||||
|
if child_stderr != child_fd:
|
||||||
|
os.close(child_stderr)
|
||||||
|
# # Close the write end of the pipe if a pipe is used for standard input
|
||||||
|
# if not stdin_is_pipe:
|
||||||
|
# os.close(parent_stdin)
|
||||||
|
# Return the child PID and the file descriptors for the PTY and/or pipes
|
||||||
|
return pid, parent_stdin, parent_stdout, parent_stderr
|
||||||
|
|
||||||
|
|
||||||
|
class CallbackSubprocess:
|
||||||
|
# time between checks of child process
|
||||||
|
PROCESS_POLL_TIME: float = 0.1
|
||||||
|
# Close pipes soon after process exit to avoid scheduling on closed event loops
|
||||||
|
PROCESS_PIPE_TIME: int = 1
|
||||||
|
|
||||||
|
def __init__(self, argv: [str], env: dict, loop: asyncio.AbstractEventLoop, stdout_callback: callable,
|
||||||
|
stderr_callback: callable, terminated_callback: callable, stdin_is_pipe: bool, stdout_is_pipe: bool,
|
||||||
|
stderr_is_pipe: bool):
|
||||||
|
"""
|
||||||
|
Fork a child process and generate callbacks with output from the process.
|
||||||
|
:param argv: the command line, tokenized. The first element must be the absolute path to an executable file.
|
||||||
|
:param env: environment variables to override
|
||||||
|
:param loop: the asyncio event loop to use
|
||||||
|
:param stdout_callback: callback for data, e.g. def callback(data:bytes) -> None
|
||||||
|
:param terminated_callback: callback for termination/return code, e.g. def callback(return_code:int) -> None
|
||||||
|
"""
|
||||||
|
assert loop is not None, "loop should not be None"
|
||||||
|
assert stdout_callback is not None, "stdout_callback should not be None"
|
||||||
|
assert terminated_callback is not None, "terminated_callback should not be None"
|
||||||
|
|
||||||
|
self._command: [str] = argv
|
||||||
|
self._env = env or {}
|
||||||
|
self._loop = loop
|
||||||
|
self._stdout_cb = stdout_callback
|
||||||
|
self._stderr_cb = stderr_callback
|
||||||
|
self._terminated_cb = terminated_callback
|
||||||
|
self._pid: int = None
|
||||||
|
self._child_stdin: int = None
|
||||||
|
self._child_stdout: int = None
|
||||||
|
self._child_stderr: int = None
|
||||||
|
self._return_code: int = None
|
||||||
|
self._stdout_eof: bool = False
|
||||||
|
self._stderr_eof: bool = False
|
||||||
|
self._stdin_is_pipe = stdin_is_pipe
|
||||||
|
self._stdout_is_pipe = stdout_is_pipe
|
||||||
|
self._stderr_is_pipe = stderr_is_pipe
|
||||||
|
|
||||||
|
def _ensure_pipes_closed(self):
|
||||||
|
stdin = self._child_stdin
|
||||||
|
stdout = self._child_stdout
|
||||||
|
stderr = self._child_stderr
|
||||||
|
fds = set(filter(lambda x: x is not None, list({stdin, stdout, stderr})))
|
||||||
|
RNS.log(f"Queuing close of pipes for ended process (fds: {fds})", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
def ensure_pipes_closed_inner():
|
||||||
|
RNS.log(f"Ensuring pipes are closed (fds: {fds})", RNS.LOG_DEBUG)
|
||||||
|
for fd in fds:
|
||||||
|
RNS.log(f"Closing fd {fd}", RNS.LOG_DEBUG)
|
||||||
|
with contextlib.suppress(OSError): tty_unset_reader_callbacks(fd)
|
||||||
|
with contextlib.suppress(OSError): os.close(fd)
|
||||||
|
|
||||||
|
self._child_stdin = None
|
||||||
|
self._child_stdout = None
|
||||||
|
self._child_stderr = None
|
||||||
|
|
||||||
|
# Avoid scheduling on a closed loop
|
||||||
|
if self._loop.is_closed(): ensure_pipes_closed_inner()
|
||||||
|
else: self._loop.call_later(CallbackSubprocess.PROCESS_PIPE_TIME, ensure_pipes_closed_inner)
|
||||||
|
|
||||||
|
def terminate(self, kill_delay: float = 1.0):
|
||||||
|
"""
|
||||||
|
Terminate child process if running
|
||||||
|
:param kill_delay: if after kill_delay seconds the child process has not exited, escalate to SIGHUP and SIGKILL
|
||||||
|
"""
|
||||||
|
|
||||||
|
RNS.log("terminate()", RNS.LOG_EXTREME)
|
||||||
|
if not self.running: return
|
||||||
|
|
||||||
|
with exception.permit(SystemExit): os.kill(self._pid, signal.SIGTERM)
|
||||||
|
|
||||||
|
def kill():
|
||||||
|
if process_exists(self._pid):
|
||||||
|
RNS.log("kill()", RNS.LOG_EXTREME)
|
||||||
|
with exception.permit(SystemExit):
|
||||||
|
os.kill(self._pid, signal.SIGHUP)
|
||||||
|
os.kill(self._pid, signal.SIGKILL)
|
||||||
|
|
||||||
|
self._loop.call_later(kill_delay, kill)
|
||||||
|
|
||||||
|
def wait():
|
||||||
|
RNS.log("wait()", RNS.LOG_EXTREME)
|
||||||
|
with contextlib.suppress(OSError): os.waitpid(self._pid, 0)
|
||||||
|
self._ensure_pipes_closed()
|
||||||
|
RNS.log("wait() finish", RNS.LOG_EXTREME)
|
||||||
|
|
||||||
|
threading.Thread(target=wait, daemon=True).start()
|
||||||
|
|
||||||
|
def close_stdin(self):
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
os.close(self._child_stdin)
|
||||||
|
# Encourage prompt shutdown if child lingers after stdin close
|
||||||
|
def _ensure_terminate():
|
||||||
|
if self.running:
|
||||||
|
self.terminate(kill_delay=0.2)
|
||||||
|
if not self._loop.is_closed():
|
||||||
|
self._loop.call_later(0.05, _ensure_terminate)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def started(self) -> bool:
|
||||||
|
"""
|
||||||
|
:return: True if child process has been started
|
||||||
|
"""
|
||||||
|
return self._pid is not None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def running(self) -> bool:
|
||||||
|
"""
|
||||||
|
:return: True if child process is still running
|
||||||
|
"""
|
||||||
|
return self._pid is not None and process_exists(self._pid)
|
||||||
|
|
||||||
|
def write(self, data: bytes):
|
||||||
|
"""
|
||||||
|
Write bytes to the stdin of the child process.
|
||||||
|
:param data: bytes to write
|
||||||
|
"""
|
||||||
|
|
||||||
|
os.write(self._child_stdin, data)
|
||||||
|
|
||||||
|
# TODO: Check what this is actually supposed to solve.
|
||||||
|
#
|
||||||
|
# For pipe-in + TTY-out, echo should be visible immediately
|
||||||
|
if self._stdin_is_pipe and not self._stdout_is_pipe and self._stdout_cb is not None and data not in (CTRL_C, CTRL_D):
|
||||||
|
try: self._stdout_cb(data)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
def set_winsize(self, r: int, c: int, h: int, v: int):
|
||||||
|
"""
|
||||||
|
Set the window size on the tty of the child process.
|
||||||
|
:param r: rows visible
|
||||||
|
:param c: columns visible
|
||||||
|
:param h: horizontal pixels visible
|
||||||
|
:param v: vertical pixels visible
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
RNS.log(f"set_winsize({r},{c},{h},{v}", RNS.LOG_DEBUG)
|
||||||
|
tty_set_winsize(self._child_stdout, r, c, h, v)
|
||||||
|
|
||||||
|
def copy_winsize(self, fromfd: int):
|
||||||
|
"""
|
||||||
|
Copy window size from one tty to another.
|
||||||
|
:param fromfd: source tty file descriptor
|
||||||
|
"""
|
||||||
|
r, c, h, v = tty_get_winsize(fromfd)
|
||||||
|
self.set_winsize(r, c, h, v)
|
||||||
|
|
||||||
|
def tcsetattr(self, when: int, attr: list[any]): # actual type is list[int | list[int | bytes]]
|
||||||
|
"""
|
||||||
|
Set tty attributes.
|
||||||
|
:param when: when to apply change: termios.TCSANOW or termios.TCSADRAIN or termios.TCSAFLUSH
|
||||||
|
:param attr: attributes to set
|
||||||
|
"""
|
||||||
|
termios.tcsetattr(self._child_stdin, when, attr)
|
||||||
|
|
||||||
|
def tcgetattr(self) -> list[any]: # actual type is list[int | list[int | bytes]]
|
||||||
|
"""
|
||||||
|
Get tty attributes.
|
||||||
|
:return: tty attributes value
|
||||||
|
"""
|
||||||
|
return termios.tcgetattr(self._child_stdout)
|
||||||
|
|
||||||
|
def ttysetraw(self):
|
||||||
|
tty.setraw(self._child_stdout, termios.TCSADRAIN)
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
"""
|
||||||
|
Start the child process.
|
||||||
|
"""
|
||||||
|
RNS.log("start()", RNS.LOG_EXTREME)
|
||||||
|
|
||||||
|
env = os.environ.copy()
|
||||||
|
for key in self._env:
|
||||||
|
env[key] = self._env[key]
|
||||||
|
|
||||||
|
program = self._command[0]
|
||||||
|
assert isinstance(program, str)
|
||||||
|
|
||||||
|
self._pid, \
|
||||||
|
self._child_stdin, \
|
||||||
|
self._child_stdout, \
|
||||||
|
self._child_stderr = _launch_child(self._command, env, self._stdin_is_pipe, self._stdout_is_pipe,
|
||||||
|
self._stderr_is_pipe)
|
||||||
|
RNS.log(f"Started pid {self.pid}, fds: {self._child_stdin}, {self._child_stdout}, {self._child_stderr}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
def poll():
|
||||||
|
try:
|
||||||
|
pid, self._return_code = os.waitpid(self._pid, os.WNOHANG)
|
||||||
|
if self._return_code is not None:
|
||||||
|
self._return_code = self._return_code & 0xff
|
||||||
|
if self._return_code is not None and not process_exists(self._pid):
|
||||||
|
RNS.log(f"polled return code {self._return_code}", RNS.LOG_DEBUG)
|
||||||
|
self._terminated_cb(self._return_code)
|
||||||
|
if self.running:
|
||||||
|
self._loop.call_later(CallbackSubprocess.PROCESS_POLL_TIME, poll)
|
||||||
|
else:
|
||||||
|
self._ensure_pipes_closed()
|
||||||
|
except Exception as e:
|
||||||
|
if not hasattr(e, "errno") or e.errno != errno.ECHILD:
|
||||||
|
RNS.log(f"Error in process poll: {e}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
self._loop.call_later(CallbackSubprocess.PROCESS_POLL_TIME, poll)
|
||||||
|
|
||||||
|
def stdout():
|
||||||
|
try:
|
||||||
|
with exception.permit(SystemExit):
|
||||||
|
data = tty_read_poll(self._child_stdout)
|
||||||
|
if data is not None and len(data) > 0:
|
||||||
|
self._stdout_cb(data)
|
||||||
|
# Opportunistically drain shortly after to coalesce immediate follow-up output
|
||||||
|
if not self._loop.is_closed():
|
||||||
|
self._loop.call_later(0.01, stdout)
|
||||||
|
except EOFError:
|
||||||
|
self._stdout_eof = True
|
||||||
|
tty_unset_reader_callbacks(self._child_stdout)
|
||||||
|
self._stdout_cb(bytearray())
|
||||||
|
|
||||||
|
def stderr():
|
||||||
|
try:
|
||||||
|
with exception.permit(SystemExit):
|
||||||
|
data = tty_read_poll(self._child_stderr)
|
||||||
|
if data is not None and len(data) > 0:
|
||||||
|
self._stderr_cb(data)
|
||||||
|
if not self._loop.is_closed():
|
||||||
|
self._loop.call_later(0.01, stderr)
|
||||||
|
except EOFError:
|
||||||
|
self._stderr_eof = True
|
||||||
|
tty_unset_reader_callbacks(self._child_stderr)
|
||||||
|
self._stderr_cb(bytearray())
|
||||||
|
|
||||||
|
tty_add_reader_callback(self._child_stdout, stdout, self._loop)
|
||||||
|
if self._child_stderr != self._child_stdout:
|
||||||
|
tty_add_reader_callback(self._child_stderr, stderr, self._loop)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stdout_eof(self):
|
||||||
|
return self._stdout_eof or not self.running
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stderr_eof(self):
|
||||||
|
return self._stderr_eof or not self.running
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def return_code(self) -> int:
|
||||||
|
return self._return_code
|
||||||
|
|
||||||
|
@property
|
||||||
|
def pid(self) -> int:
|
||||||
|
return self._pid
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
"""
|
||||||
|
A test driver for the CallbackProcess class.
|
||||||
|
python ./process.py /bin/zsh --login
|
||||||
|
"""
|
||||||
|
|
||||||
|
if len(sys.argv) <= 1:
|
||||||
|
print(f"Usage: {sys.argv} <absolute_path_to_child_executable> [child_arg ...]")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
# asyncio.set_event_loop(loop)
|
||||||
|
retcode = loop.create_future()
|
||||||
|
|
||||||
|
def stdout(data: bytes): os.write(sys.stdout.fileno(), data)
|
||||||
|
|
||||||
|
def terminated(rc: int): retcode.set_result(rc)
|
||||||
|
|
||||||
|
process = CallbackSubprocess(argv=sys.argv[1:],
|
||||||
|
env={"TERM": os.environ.get("TERM", "xterm")},
|
||||||
|
loop=loop,
|
||||||
|
stdout_callback=stdout,
|
||||||
|
terminated_callback=terminated)
|
||||||
|
|
||||||
|
def sigint_handler(sig, frame):
|
||||||
|
if process is None or process.started and not process.running:
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
elif process.running:
|
||||||
|
process.write("\x03".encode("utf-8"))
|
||||||
|
|
||||||
|
def sigwinch_handler(sig, frame):
|
||||||
|
process.copy_winsize(sys.stdin.fileno())
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, sigint_handler)
|
||||||
|
signal.signal(signal.SIGWINCH, sigwinch_handler)
|
||||||
|
|
||||||
|
def stdin():
|
||||||
|
try:
|
||||||
|
data = tty_read(sys.stdin.fileno())
|
||||||
|
if data is not None:
|
||||||
|
process.write(data)
|
||||||
|
|
||||||
|
except EOFError:
|
||||||
|
tty_unset_reader_callbacks(sys.stdin.fileno())
|
||||||
|
process.write(CTRL_D)
|
||||||
|
|
||||||
|
tty_add_reader_callback(sys.stdin.fileno(), stdin)
|
||||||
|
process.start()
|
||||||
|
# call_soon called it too soon, not sure why.
|
||||||
|
loop.call_later(0.001, functools.partial(process.copy_winsize, sys.stdin.fileno()))
|
||||||
|
|
||||||
|
val = await retcode
|
||||||
|
RNS.log(f"Got return code {val}", RNS.LOG_DEBUG)
|
||||||
|
return val
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
tr = TTYRestorer(sys.stdin.fileno())
|
||||||
|
try:
|
||||||
|
tr.raw()
|
||||||
|
asyncio.run(main())
|
||||||
|
finally:
|
||||||
|
tty_unset_reader_callbacks(sys.stdin.fileno())
|
||||||
|
tr.restore()
|
||||||
149
RNS/Utilities/rnsh/protocol.py
Normal file
149
RNS/Utilities/rnsh/protocol.py
Normal file
|
|
@ -0,0 +1,149 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
from RNS.vendor import umsgpack
|
||||||
|
from RNS.Buffer import StreamDataMessage as RNSStreamDataMessage
|
||||||
|
import RNS.Utilities.rnsh.retry
|
||||||
|
import abc
|
||||||
|
import contextlib
|
||||||
|
import struct
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
MSG_MAGIC = 0xac
|
||||||
|
PROTOCOL_VERSION = 1
|
||||||
|
|
||||||
|
def _make_MSGTYPE(val: int):
|
||||||
|
return ((MSG_MAGIC << 8) & 0xff00) | (val & 0x00ff)
|
||||||
|
|
||||||
|
|
||||||
|
class NoopMessage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(0)
|
||||||
|
def pack(self) -> bytes: return bytes()
|
||||||
|
def unpack(self, raw): pass
|
||||||
|
|
||||||
|
|
||||||
|
class WindowSizeMessage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(2)
|
||||||
|
|
||||||
|
def __init__(self, rows: int = None, cols: int = None, hpix: int = None, vpix: int = None):
|
||||||
|
super().__init__()
|
||||||
|
self.rows = rows
|
||||||
|
self.cols = cols
|
||||||
|
self.hpix = hpix
|
||||||
|
self.vpix = vpix
|
||||||
|
|
||||||
|
def pack(self) -> bytes: return umsgpack.packb((self.rows, self.cols, self.hpix, self.vpix))
|
||||||
|
def unpack(self, raw): self.rows, self.cols, self.hpix, self.vpix = umsgpack.unpackb(raw)
|
||||||
|
|
||||||
|
|
||||||
|
class ExecuteCommandMesssage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(3)
|
||||||
|
|
||||||
|
def __init__(self, cmdline: [str] = None, pipe_stdin: bool = False, pipe_stdout: bool = False,
|
||||||
|
pipe_stderr: bool = False, tcflags: [any] = None, term: str | None = None, rows: int = None,
|
||||||
|
cols: int = None, hpix: int = None, vpix: int = None):
|
||||||
|
|
||||||
|
super().__init__()
|
||||||
|
self.cmdline = cmdline
|
||||||
|
self.pipe_stdin = pipe_stdin
|
||||||
|
self.pipe_stdout = pipe_stdout
|
||||||
|
self.pipe_stderr = pipe_stderr
|
||||||
|
self.tcflags = tcflags
|
||||||
|
self.term = term
|
||||||
|
self.rows = rows
|
||||||
|
self.cols = cols
|
||||||
|
self.hpix = hpix
|
||||||
|
self.vpix = vpix
|
||||||
|
|
||||||
|
def pack(self) -> bytes:
|
||||||
|
return umsgpack.packb((self.cmdline, self.pipe_stdin, self.pipe_stdout, self.pipe_stderr,
|
||||||
|
self.tcflags, self.term, self.rows, self.cols, self.hpix, self.vpix))
|
||||||
|
|
||||||
|
def unpack(self, raw):
|
||||||
|
self.cmdline, self.pipe_stdin, self.pipe_stdout, self.pipe_stderr, self.tcflags, self.term, self.rows, \
|
||||||
|
self.cols, self.hpix, self.vpix = umsgpack.unpackb(raw)
|
||||||
|
|
||||||
|
|
||||||
|
# Create a version of RNS.Buffer.StreamDataMessage that we control
|
||||||
|
class StreamDataMessage(RNSStreamDataMessage):
|
||||||
|
MSGTYPE = _make_MSGTYPE(4)
|
||||||
|
STREAM_ID_STDIN = 0
|
||||||
|
STREAM_ID_STDOUT = 1
|
||||||
|
STREAM_ID_STDERR = 2
|
||||||
|
|
||||||
|
|
||||||
|
class VersionInfoMessage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(5)
|
||||||
|
|
||||||
|
def __init__(self, sw_version: str = None):
|
||||||
|
super().__init__()
|
||||||
|
self.sw_version = sw_version or RNS.Utilities.rnsh.__version__
|
||||||
|
self.protocol_version = PROTOCOL_VERSION
|
||||||
|
|
||||||
|
def pack(self) -> bytes: return umsgpack.packb((self.sw_version, self.protocol_version))
|
||||||
|
def unpack(self, raw): self.sw_version, self.protocol_version = umsgpack.unpackb(raw)
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorMessage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(6)
|
||||||
|
|
||||||
|
def __init__(self, msg: str = None, fatal: bool = False, data: dict = None):
|
||||||
|
super().__init__()
|
||||||
|
self.msg = msg
|
||||||
|
self.fatal = fatal
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def pack(self) -> bytes: return umsgpack.packb((self.msg, self.fatal, self.data))
|
||||||
|
def unpack(self, raw: bytes): self.msg, self.fatal, self.data = umsgpack.unpackb(raw)
|
||||||
|
|
||||||
|
|
||||||
|
class CommandExitedMessage(RNS.MessageBase):
|
||||||
|
MSGTYPE = _make_MSGTYPE(7)
|
||||||
|
|
||||||
|
def __init__(self, return_code: int = None):
|
||||||
|
super().__init__()
|
||||||
|
self.return_code = return_code
|
||||||
|
|
||||||
|
def pack(self) -> bytes: return umsgpack.packb(self.return_code)
|
||||||
|
def unpack(self, raw: bytes): self.return_code = umsgpack.unpackb(raw)
|
||||||
|
|
||||||
|
|
||||||
|
message_types = [NoopMessage, VersionInfoMessage, WindowSizeMessage, ExecuteCommandMesssage, StreamDataMessage,
|
||||||
|
CommandExitedMessage, ErrorMessage]
|
||||||
|
|
||||||
|
def register_message_types(channel: RNS.Channel.Channel):
|
||||||
|
for message_type in message_types: channel.register_message_type(message_type)
|
||||||
196
RNS/Utilities/rnsh/retry.py
Normal file
196
RNS/Utilities/rnsh/retry.py
Normal file
|
|
@ -0,0 +1,196 @@
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
import asyncio
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import RNS.Utilities.rnsh.exception as exception
|
||||||
|
from typing import Callable
|
||||||
|
from contextlib import AbstractContextManager
|
||||||
|
import types
|
||||||
|
import typing
|
||||||
|
|
||||||
|
|
||||||
|
class RetryStatus:
|
||||||
|
def __init__(self, tag: any, try_limit: int, wait_delay: float, retry_callback: Callable[[any, int], any],
|
||||||
|
timeout_callback: Callable[[any, int], None], tries: int = 1):
|
||||||
|
|
||||||
|
self.tag = tag
|
||||||
|
self.try_limit = try_limit
|
||||||
|
self.tries = tries
|
||||||
|
self.wait_delay = wait_delay
|
||||||
|
self.retry_callback = retry_callback
|
||||||
|
self.timeout_callback = timeout_callback
|
||||||
|
self.try_time = time.time()
|
||||||
|
self.completed = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def ready(self):
|
||||||
|
ready = time.time() > self.try_time + self.wait_delay
|
||||||
|
RNS.log(f"ready check {self.tag} try_time {self.try_time} wait_delay {self.wait_delay} " +
|
||||||
|
f"next_try {self.try_time + self.wait_delay} now {time.time()} " +
|
||||||
|
f"exceeded {time.time() - self.try_time - self.wait_delay} ready {ready}", RNS.LOG_DEBUG)
|
||||||
|
return ready
|
||||||
|
|
||||||
|
@property
|
||||||
|
def timed_out(self):
|
||||||
|
return self.ready and self.tries >= self.try_limit
|
||||||
|
|
||||||
|
def timeout(self):
|
||||||
|
self.completed = True
|
||||||
|
self.timeout_callback(self.tag, self.tries)
|
||||||
|
|
||||||
|
def retry(self) -> any:
|
||||||
|
self.tries = self.tries + 1
|
||||||
|
self.try_time = time.time()
|
||||||
|
return self.retry_callback(self.tag, self.tries)
|
||||||
|
|
||||||
|
|
||||||
|
class RetryThread(AbstractContextManager):
|
||||||
|
def __init__(self, loop_period: float = 0.25, name: str = "retry thread"):
|
||||||
|
self._loop_period = loop_period
|
||||||
|
self._statuses: list[RetryStatus] = []
|
||||||
|
self._tag_counter = 0
|
||||||
|
self._lock = threading.RLock()
|
||||||
|
self._run = True
|
||||||
|
self._finished: asyncio.Future = None
|
||||||
|
self._thread = threading.Thread(name=name, target=self._thread_run, daemon=True)
|
||||||
|
self._thread.start()
|
||||||
|
|
||||||
|
def is_alive(self): return self._thread.is_alive()
|
||||||
|
|
||||||
|
def close(self, loop: asyncio.AbstractEventLoop = None) -> asyncio.Future:
|
||||||
|
RNS.log("Stopping timer thread", RNS.LOG_DEBUG)
|
||||||
|
if loop is None:
|
||||||
|
self._run = False
|
||||||
|
self._thread.join()
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
self._finished = loop.create_future()
|
||||||
|
return self._finished
|
||||||
|
|
||||||
|
def wait(self, timeout: float = None):
|
||||||
|
if timeout: timeout = timeout + time.time()
|
||||||
|
while timeout is None or time.time() < timeout:
|
||||||
|
with self._lock: task_count = len(self._statuses)
|
||||||
|
if task_count == 0: return
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def _thread_run(self):
|
||||||
|
while self._run and self._finished is None:
|
||||||
|
time.sleep(self._loop_period)
|
||||||
|
ready: list[RetryStatus] = []
|
||||||
|
prune: list[RetryStatus] = []
|
||||||
|
with self._lock: ready.extend(list(filter(lambda s: s.ready, self._statuses)))
|
||||||
|
|
||||||
|
for retry in ready:
|
||||||
|
try:
|
||||||
|
if not retry.completed:
|
||||||
|
if retry.timed_out:
|
||||||
|
RNS.log(f"Timed out {retry.tag} after {retry.try_limit} tries", RNS.LOG_DEBUG)
|
||||||
|
retry.timeout()
|
||||||
|
prune.append(retry)
|
||||||
|
elif retry.ready:
|
||||||
|
RNS.log(f"Retrying {retry.tag}, try {retry.tries + 1}/{retry.try_limit}", RNS.LOG_DEBUG)
|
||||||
|
should_continue = retry.retry()
|
||||||
|
if not should_continue: self.complete(retry.tag)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Error processing retry id {retry.tag}: {e}", RNS.LOG_ERROR)
|
||||||
|
prune.append(retry)
|
||||||
|
|
||||||
|
with self._lock:
|
||||||
|
for retry in prune:
|
||||||
|
RNS.log(f"pruned retry {retry.tag}, retry count {retry.tries}/{retry.try_limit}", RNS.LOG_DEBUG)
|
||||||
|
with exception.permit(SystemExit): self._statuses.remove(retry)
|
||||||
|
|
||||||
|
if self._finished is not None: self._finished.set_result(None)
|
||||||
|
|
||||||
|
def _get_next_tag(self):
|
||||||
|
self._tag_counter += 1
|
||||||
|
return self._tag_counter
|
||||||
|
|
||||||
|
def has_tag(self, tag: any) -> bool:
|
||||||
|
with self._lock: return next(filter(lambda s: s.tag == tag, self._statuses), None) is not None
|
||||||
|
|
||||||
|
def begin(self, try_limit: int, wait_delay: float, try_callback: Callable[[any, int], any],
|
||||||
|
timeout_callback: Callable[[any, int], None]) -> any:
|
||||||
|
|
||||||
|
RNS.log(f"Running first try", RNS.LOG_DEBUG)
|
||||||
|
tag = try_callback(None, 1)
|
||||||
|
RNS.log(f"First try got id {tag}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
if not tag:
|
||||||
|
RNS.log(f"Callback returned None/False/0, considering complete.", RNS.LOG_DEBUG)
|
||||||
|
return None
|
||||||
|
|
||||||
|
with self._lock:
|
||||||
|
if tag is None: tag = self._get_next_tag()
|
||||||
|
self.complete(tag)
|
||||||
|
|
||||||
|
self._statuses.append(RetryStatus(tag=tag,
|
||||||
|
tries=1,
|
||||||
|
try_limit=try_limit,
|
||||||
|
wait_delay=wait_delay,
|
||||||
|
retry_callback=try_callback,
|
||||||
|
timeout_callback=timeout_callback))
|
||||||
|
|
||||||
|
RNS.log(f"Added retry timer for {tag}", RNS.LOG_DEBUG)
|
||||||
|
return tag
|
||||||
|
|
||||||
|
def complete(self, tag: any):
|
||||||
|
assert tag is not None
|
||||||
|
with self._lock:
|
||||||
|
status = next(filter(lambda l: l.tag == tag, self._statuses), None)
|
||||||
|
if status is not None:
|
||||||
|
status.completed = True
|
||||||
|
self._statuses.remove(status)
|
||||||
|
RNS.log(f"completed {tag}", RNS.LOG_DEBUG)
|
||||||
|
return
|
||||||
|
|
||||||
|
RNS.log(f"status not found to complete {tag}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
def complete_all(self):
|
||||||
|
with self._lock:
|
||||||
|
for status in self._statuses:
|
||||||
|
status.completed = True
|
||||||
|
RNS.log(f"completed {status.tag}", RNS.LOG_DEBUG)
|
||||||
|
|
||||||
|
self._statuses.clear()
|
||||||
|
|
||||||
|
def __exit__(self, __exc_type: typing.Type[BaseException], __exc_value: BaseException,
|
||||||
|
__traceback: types.TracebackType) -> bool:
|
||||||
|
self.close()
|
||||||
|
return False
|
||||||
189
RNS/Utilities/rnsh/rnsh.py
Normal file
189
RNS/Utilities/rnsh/rnsh.py
Normal file
|
|
@ -0,0 +1,189 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
#
|
||||||
|
# Based on the original rnsh program by Aaron Heise (@acehoss)
|
||||||
|
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
|
||||||
|
# This version of rnsh is included in RNS under the Reticulum License
|
||||||
|
#
|
||||||
|
# Reticulum License
|
||||||
|
#
|
||||||
|
# Copyright (c) 2016-2026 Mark Qvist
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# - The Software shall not be used in any kind of system which includes amongst
|
||||||
|
# its functions the ability to purposefully do harm to human beings.
|
||||||
|
#
|
||||||
|
# - The Software shall not be used, directly or indirectly, in the creation of
|
||||||
|
# an artificial intelligence, machine learning or language model training
|
||||||
|
# dataset, including but not limited to any use that contributes to the
|
||||||
|
# training or development of such a model or algorithm.
|
||||||
|
#
|
||||||
|
# - The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import re
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import RNS
|
||||||
|
import RNS.Utilities.rnsh.process as process
|
||||||
|
import RNS.Utilities.rnsh.session as session
|
||||||
|
import RNS.Utilities.rnsh.args
|
||||||
|
import RNS.Utilities.rnsh.loop
|
||||||
|
import RNS.Utilities.rnsh.listener as listener
|
||||||
|
import RNS.Utilities.rnsh.initiator as initiator
|
||||||
|
from RNS.Utilities.rnsh.args import parse_arguments
|
||||||
|
|
||||||
|
APP_NAME = "rnsh"
|
||||||
|
loop: asyncio.AbstractEventLoop | None = None
|
||||||
|
|
||||||
|
def _sanitize_service_name(service_name:str) -> str: return re.sub(r'\W+', '', service_name)
|
||||||
|
|
||||||
|
def prepare_identity(identity_path=None, service_name=None, configdir=None):
|
||||||
|
service_name = _sanitize_service_name(service_name or "")
|
||||||
|
if not identity_path:
|
||||||
|
identity_path = f"{configdir}/identity" + (f".{service_name}" if service_name and len(service_name) > 0 else "")
|
||||||
|
|
||||||
|
identity = None
|
||||||
|
if os.path.isfile(identity_path):
|
||||||
|
RNS.log(f"Loading identity from {identity_path}", RNS.LOG_VERBOSE)
|
||||||
|
identity = RNS.Identity.from_file(identity_path)
|
||||||
|
|
||||||
|
if identity is None:
|
||||||
|
RNS.log(f"Creating identity {identity_path}", RNS.LOG_NOTICE)
|
||||||
|
identity = RNS.Identity()
|
||||||
|
identity.to_file(identity_path)
|
||||||
|
|
||||||
|
return identity
|
||||||
|
|
||||||
|
|
||||||
|
def print_identity(configdir, rnsconfigdir, identitypath, service_name, include_destination: bool):
|
||||||
|
reticulum = RNS.Reticulum(configdir=rnsconfigdir, loglevel=RNS.LOG_INFO)
|
||||||
|
if service_name and len(service_name) > 0: print(f"Using service name \"{service_name}\"")
|
||||||
|
identity = prepare_identity(identity_path=identitypath, service_name=service_name, configdir=configdir)
|
||||||
|
destination = RNS.Destination(identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME)
|
||||||
|
print("Identity : " + str(identity))
|
||||||
|
if include_destination: print("Listening on : " + RNS.prettyhexrep(destination.hash))
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
verbose_set = False
|
||||||
|
|
||||||
|
def ensure_config_directory(configdir=None):
|
||||||
|
if configdir:
|
||||||
|
if os.path.isdir(os.path.expanduser(configdir)): return os.path.expanduser(configdir)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
RNS.log(f"Creating configuration directory: {os.path.expanduser(configdir)}")
|
||||||
|
os.makedirs(os.path.expanduser(configdir))
|
||||||
|
return os.path.expanduser(configdir)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not get or create rnsh configuration directory, aborting", RNS.LOG_CRITICAL)
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
|
elif os.path.isdir(os.path.expanduser("~/.config/rnsh")): return os.path.expanduser("~/.config/rnsh")
|
||||||
|
elif os.path.isdir(os.path.expanduser("~/.rnsh")): return os.path.expanduser("~/.rnsh")
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
RNS.log(f"Creating configuration directory: {os.path.expanduser('~/.rnsh')}")
|
||||||
|
os.makedirs(os.path.expanduser("~/.rnsh"))
|
||||||
|
return os.path.expanduser("~/.rnsh")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
RNS.log(f"Could not get or create rnsh configuration directory, aborting", RNS.LOG_CRITICAL)
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
async def _rnsh_cli_main():
|
||||||
|
global verbose_set
|
||||||
|
args, parser = parse_arguments()
|
||||||
|
verbose_set = args.verbose > 0
|
||||||
|
configdir = ensure_config_directory(args.config)
|
||||||
|
|
||||||
|
if not configdir:
|
||||||
|
RNS.log(f"Could not resolve rnsh configuration directory", RNS.LOG_CRITICAL)
|
||||||
|
os._exit(1)
|
||||||
|
|
||||||
|
if args.print_identity:
|
||||||
|
print_identity(configdir, args.rnsconfig, args.identity, args.service, args.listen)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
logfile = f"{configdir}/logfile"
|
||||||
|
|
||||||
|
if args.listen:
|
||||||
|
allowed_file = None
|
||||||
|
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||||
|
if os.path.isfile(os.path.expanduser(f"{configdir}/allowed_identities")):
|
||||||
|
allowed_file = os.path.expanduser(f"{configdir}/allowed_identities")
|
||||||
|
|
||||||
|
await listener.listen(configdir=configdir,
|
||||||
|
rnsconfigdir=args.rnsconfig,
|
||||||
|
command=args.command,
|
||||||
|
identitypath=args.identity,
|
||||||
|
logfile=logfile,
|
||||||
|
service_name=args.service,
|
||||||
|
verbosity=args.verbose,
|
||||||
|
quietness=args.quiet,
|
||||||
|
allowed=args.allowed or [],
|
||||||
|
allowed_file=allowed_file,
|
||||||
|
disable_auth=args.no_auth,
|
||||||
|
announce_period=args.announce,
|
||||||
|
no_remote_command=args.no_remote_command,
|
||||||
|
remote_cmd_as_args=args.remote_command_as_args)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if args.destination is not None:
|
||||||
|
return_code = await initiator.initiate(configdir=configdir,
|
||||||
|
rnsconfigdir=args.rnsconfig,
|
||||||
|
identitypath=args.identity,
|
||||||
|
logfile=f"{logfile}.initiator",
|
||||||
|
verbosity=args.verbose,
|
||||||
|
quietness=args.quiet,
|
||||||
|
noid=args.no_id,
|
||||||
|
destination=args.destination,
|
||||||
|
timeout=args.timeout,
|
||||||
|
command=args.command
|
||||||
|
)
|
||||||
|
RNS.log(f"Sesssion with <{args.destination}> ended", RNS.LOG_INFO)
|
||||||
|
return return_code if args.mirror else 0
|
||||||
|
else:
|
||||||
|
print("")
|
||||||
|
parser.print_help()
|
||||||
|
print("")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
global verbose_set
|
||||||
|
return_code = 1
|
||||||
|
exc = None
|
||||||
|
try: return_code = asyncio.run(_rnsh_cli_main())
|
||||||
|
except SystemExit: pass
|
||||||
|
except KeyboardInterrupt: pass
|
||||||
|
except Exception as e:
|
||||||
|
print(f"{e}")
|
||||||
|
exc = e
|
||||||
|
|
||||||
|
process.tty_unset_reader_callbacks(0)
|
||||||
|
if verbose_set and exc: raise exc
|
||||||
|
sys.exit(return_code if return_code is not None else 255)
|
||||||
|
|
||||||
|
if __name__ == "__main__": main()
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue