Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

119
LINES

< > BotCompany Repo | #1017127 // Execute Signed Instructions From Other Computer (Make Me Remote Controlled)

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1113K of libraries. Click here for Pure Java version (16211L/92K).

1  
!7
2  
3  
set flag DynModule. // central transpileRaw
4  
5  
cm RemoteControlled > DynPrintLog {
6  
  S controllingComputers;
7  
  bool enabled;
8  
  switchable bool logAll;
9  
  transient Q q;
10  
  
11  
  visual
12  
    centerAndSouthWithMargins(super.visualize(),
13  
      vstackWithSpacing(jrightalignedline(dm_fieldCheckBox('enabled)),
14  
        withLabel("Computers that can control me:", dm_fieldTextField('controllingComputers))));
15  
16  
  start {
17  
    dm_useLocalMechListCopies();
18  
    dm_privateChat();
19  
    q = dm_startQ();
20  
    ownResource(vmBus_onMessage('gotFromChat, voidfunc(fS msg) { q.add(r { processMsg(null, msg) }) }));
21  
    ownResource(vmBus_onMessage('gotFromPrivateChat, voidfunc(fS sender, fS msg) { q.add(r { processMsg(sender, msg) }) }));
22  
    print("Remote Control " + (enabled ? "ENABLED" : "DISABLED") + ".  Controlling computers: " + or2(controllingComputers, "-"));
23  
  }
24  
    
25  
  void processMsg(S sender, S msg) {
26  
    S originalMsg = msg;
27  
    LS computers = words2(controllingComputers);
28  
    PairS authorPair = getAuthorOfSignedText2(msg);
29  
    S author = pairB(authorPair);
30  
    //print("Author: " + author + ", controlling computers: " + computers);
31  
    bool allowed = computers.contains(author);
32  
    msg = trim(unsign(msg));
33  
    new Matches m;
34  
    
35  
    if (jMatchStart("<id> eval fresh:", msg, m)) {
36  
      if (allowed) dm_refreshTranspilerAndLoadedFunctions();
37  
      msg = $1 + " eval: " + m.rest();
38  
    }
39  
    bool realEval = jMatchStart("<id> eval real", msg, m);
40  
    if (realEval)
41  
      msg = $1 + " eval " + m.rest();
42  
    
43  
    if (jMatchStart("<id> eval fresh <id>:", msg, m)) {
44  
      if (allowed) dm_refreshTranspiler();
45  
      msg = $1 + " eval " + $2 + ": " + m.rest();
46  
    }
47  
48  
    if (jMatchStart("<id> eval <id>:", msg, m)) {
49  
      if (neqOneOf($1, "all", computerID())) ret with print("Not for me.");
50  
      else if (!enabled) print("Will not eval (module disabled).");
51  
      else {
52  
        print(trim(indentx("EVAL from " + author + ": " + shorten(m.rest(), 1000))));
53  
        O result = null;
54  
        S error = null;
55  
        try {
56  
          if (logAll) logQuoted(javaxCachesDir("signed-instructions.txt"), originalMsg);
57  
          if (!allowed)
58  
            if (pairB(authorPair) == null) {
59  
              print(l(originalMsg) + " " + quote(shorten(originalMsg, 1000)));
60  
              print(quote(takeLast(originalMsg, 1000)));
61  
              fail("Signature didn't verify (computer ID: " + pairA(authorPair) + ", public key md5: " + md5(getPublicKeyOfComputer(pairA(authorPair))) + ")");
62  
            } else
63  
              fail("not accepting commands from " + author);
64  
          result = realEval ? dm_javaEval(m.rest()) : dm_javaEvalOrInterpret(m.rest());
65  
        } catch print e {
66  
          error = getStackTrace(e);
67  
        }
68  
        S mode = "direct";
69  
        S out;
70  
        if (result != null) {
71  
          int l = l_opt(result);
72  
          print(">> " + (l != 0 ? "[l=" + l + "] " : "") + result);
73  
        }
74  
        
75  
        if (error != null) {
76  
          mode = "error";
77  
          out = error;
78  
        } else if (result instanceof byte[]) {
79  
          mode = "base64";
80  
          out = base64((byte[]) result);
81  
        } else {
82  
          out = str(result);
83  
          if (empty(out) || trimmable(out) || containsNewLine(out)) {
84  
            out = quote(out);
85  
            mode = "quoted";
86  
          }
87  
        }
88  
        S line = "done " + $2 + " " + mode + ": " + out;
89  
        if (sender == null)
90  
          dm_osChat_postSigned(line);
91  
        else
92  
          dm_privateChat_sendSigned(sender, line);
93  
      }
94  
      ret;
95  
    }
96  
97  
    if (!allowed) ret;
98  
    // old-style formats follow
99  
    
100  
    //print("Got signed message from " + author + ": " + msg);
101  
    if (jMatchStart("<id> eval:", msg, m)) {
102  
      if (neqOneOf($1, "all", computerID())) print("Not for me.");
103  
      else if (!enabled) print("Will not eval (module disabled).");
104  
      else {
105  
        print(trim(indentx("EVAL: " + shorten(m.rest(), 1000))));
106  
        O result = dm_javaEvalOrInterpret(m.rest());
107  
        print(">> " + result);
108  
      }
109  
    }
110  
  }
111  
  
112  
  // API
113  
  
114  
  bool addControllingComputer(S computerID) {
115  
    if (contains(controllingComputers, computerID)) false;
116  
    setField(controllingComputers := trim(controllingComputers + " " + computerID));
117  
    true;
118  
  }
119  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, hpgrupgrauku, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney

No comments. add comment

Snippet ID: #1017127
Snippet name: Execute Signed Instructions From Other Computer (Make Me Remote Controlled)
Eternal ID of this version: #1017127/65
Text MD5: b6e7a6ebe9897aef90aeb32069624f3d
Transpilation MD5: cff3343387a52126bd189a9bb6bc5725
Author: stefan
Category: javax / stefan's os
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-14 02:29:12
Source code size: 4329 bytes / 119 lines
Pitched / IR pitched: No / No
Views / Downloads: 590 / 387902
Version history: 64 change(s)
Referenced in: [show references]