1 | static L<S> pinging = synchroList(); // who we ping |
2 | static volatile L<ScannedBot> scannedBots; |
3 | |
4 | static double pingLoopInterval = 30; |
5 | sbool allowDangerousCommands; // eval from any machine on the subnet |
6 | |
7 | concept MachineInfo {
|
8 | S ip; |
9 | bool me, aware; |
10 | S computerID, os; |
11 | L<S> bots; |
12 | long lastChecked; |
13 | } |
14 | |
15 | sclass GeneralInfo {
|
16 | S computerID, os; |
17 | L<S> bots; |
18 | } |
19 | |
20 | static S awarenessBot(S s) {
|
21 | new Matches m; |
22 | |
23 | if "get computer id" |
24 | ret ok(computerID()); |
25 | |
26 | if "who are you pinging" |
27 | ret structure(pinging); |
28 | |
29 | if "list bots" |
30 | ret structure(or(scannedBots, fullBotScan())); |
31 | |
32 | if "general info" {
|
33 | new GeneralInfo gi; |
34 | gi.computerID = computerID(); |
35 | //gi.isAndroid = isAndroid(); |
36 | gi.os = System.getProperty("os.name");
|
37 | gi.bots = asList(collectTreeSet(or(scannedBots, fullBotScan()), "helloString")); |
38 | ret structure(gi); |
39 | } |
40 | |
41 | if (allowDangerousCommands) {
|
42 | if "please run snippet *" {
|
43 | final Class c = hotwire($1); |
44 | thread { callMain(c); }
|
45 | ret "ok, running main in new thread"; // TODO: return injection ID |
46 | } |
47 | |
48 | if "please hotwire snippet *" {
|
49 | hotwire($1); |
50 | ret "ok"; // TODO: return injection ID |
51 | } |
52 | |
53 | if "please forward to machine *: *" |
54 | ret sendToPublicCommBot($1, $2); |
55 | } |
56 | |
57 | null; |
58 | } |
59 | |
60 | static void getClientsFrom(S ip) {
|
61 | pcall {
|
62 | MachineInfo info = uniq(MachineInfo, +ip); |
63 | info.lastChecked = now(); |
64 | info.change(); |
65 | try {
|
66 | O gi = safeUnstructure(sendToAwareness(ip, "general info")); |
67 | info.aware = true; |
68 | copyFields(gi, info, "os", "computerID", "bots"); |
69 | info.me = eq(info.computerID, computerID()); |
70 | info.change(); |
71 | } catch e {
|
72 | info.aware = false; |
73 | info.bots = null; |
74 | throw(asRuntimeException(e)); |
75 | } |
76 | } |
77 | pcall {
|
78 | addPeers(getClientsListFrom(ip)); |
79 | } |
80 | pcall {
|
81 | addPeers(getPeersListFrom(ip)); |
82 | } |
83 | } |
84 | |
85 | static S sendToLocalBotOpt(S bot, S text, O... args) {
|
86 | if (bot == null) return null; |
87 | text = format(text, args); |
88 | |
89 | if (swic("Awareness Bot.", bot))
|
90 | ret callStaticAnswerMethod(text); |
91 | |
92 | DialogIO channel = findBot(bot); |
93 | if (channel == null) {
|
94 | print(quote(bot) + " not found, skipping send: " + quote(text)); |
95 | return null; |
96 | } |
97 | try {
|
98 | channel.readLine(); |
99 | print(shorten(bot + "> " + text, 200)); |
100 | channel.sendLine(text); |
101 | S s = channel.readLine(); |
102 | print(shorten(bot + "< " + s, 200)); |
103 | return s; |
104 | } catch (Throwable e) {
|
105 | e.printStackTrace(); |
106 | return null; |
107 | } finally {
|
108 | channel.close(); |
109 | } |
110 | } |
111 | |
112 | static S sendToLocalBotOpt_original(S bot, S text, O... args) {
|
113 | if (bot == null) return null; |
114 | text = format(text, args); |
115 | DialogIO channel = findBot(bot); |
116 | if (channel == null) {
|
117 | print(quote(bot) + " not found, skipping send: " + quote(text)); |
118 | return null; |
119 | } |
120 | try {
|
121 | channel.readLine(); |
122 | print(shorten(bot + "> " + text, 200)); |
123 | channel.sendLine(text); |
124 | S s = channel.readLine(); |
125 | print(shorten(bot + "< " + s, 200)); |
126 | return s; |
127 | } catch (Throwable e) {
|
128 | e.printStackTrace(); |
129 | return null; |
130 | } finally {
|
131 | channel.close(); |
132 | } |
133 | } |
134 | |
135 | svoid pingThread(fO extraCode) {
|
136 | thread "Ping Loop" {
|
137 | addPeers(getMyIPs()); |
138 | int pingingIdx = 0; |
139 | new L<S> pinging; |
140 | while licensed {
|
141 | try {
|
142 | pinging = cloneList(main.pinging); |
143 | |
144 | // ping the gateway |
145 | S gateway = gateway(); |
146 | if (gateway != null && !pinging.contains(gateway)) {
|
147 | print("Pinging gateway " + gateway);
|
148 | addPeers(ll(gateway)); |
149 | } |
150 | setAddAll(pinging, keys(phonePublicCommBot_clients)); |
151 | |
152 | // ping one known party |
153 | if (nempty(pinging)) {
|
154 | S ip = get(pinging, pingingIdx++ % l(pinging)); |
155 | print("Pinging peer " + ip);
|
156 | getClientsFrom(ip); |
157 | } |
158 | } catch e {
|
159 | logQuoted("pingloop.log", chatTime() + " " + e);
|
160 | } |
161 | |
162 | pcall {
|
163 | scannedBots = fullBotScan(); |
164 | } |
165 | |
166 | pcallF(extraCode); |
167 | |
168 | sleepSeconds(pingLoopInterval); |
169 | } |
170 | } |
171 | } |
172 | |
173 | svoid addPeers(L<S> peers) {
|
174 | for (S peer : unnull(peers)) |
175 | if (isIPv4(peer) && !isLoopbackIP(peer) && !pinging.contains(peer)) {
|
176 | pinging.add(peer); |
177 | getClientsFrom(peer); |
178 | } |
179 | } |
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1005410 |
| Snippet name: | Awareness Include [PC/Android] |
| Eternal ID of this version: | #1005410/4 |
| Text MD5: | e8388fedbd32c2ce4d882ce7640aaed4 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2018-11-26 19:29:18 |
| Source code size: | 4406 bytes / 179 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 814 / 1273 |
| Version history: | 3 change(s) |
| Referenced in: | [show references] |