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).

!7

set flag DynModule. // central transpileRaw

cm RemoteControlled > DynPrintLog {
  S controllingComputers;
  bool enabled;
  switchable bool logAll;
  transient Q q;
  
  visual
    centerAndSouthWithMargins(super.visualize(),
      vstackWithSpacing(jrightalignedline(dm_fieldCheckBox('enabled)),
        withLabel("Computers that can control me:", dm_fieldTextField('controllingComputers))));

  start {
    dm_useLocalMechListCopies();
    dm_privateChat();
    q = dm_startQ();
    ownResource(vmBus_onMessage('gotFromChat, voidfunc(fS msg) { q.add(r { processMsg(null, msg) }) }));
    ownResource(vmBus_onMessage('gotFromPrivateChat, voidfunc(fS sender, fS msg) { q.add(r { processMsg(sender, msg) }) }));
    print("Remote Control " + (enabled ? "ENABLED" : "DISABLED") + ".  Controlling computers: " + or2(controllingComputers, "-"));
  }
    
  void processMsg(S sender, S msg) {
    S originalMsg = msg;
    LS computers = words2(controllingComputers);
    PairS authorPair = getAuthorOfSignedText2(msg);
    S author = pairB(authorPair);
    //print("Author: " + author + ", controlling computers: " + computers);
    bool allowed = computers.contains(author);
    msg = trim(unsign(msg));
    new Matches m;
    
    if (jMatchStart("<id> eval fresh:", msg, m)) {
      if (allowed) dm_refreshTranspilerAndLoadedFunctions();
      msg = $1 + " eval: " + m.rest();
    }
    bool realEval = jMatchStart("<id> eval real", msg, m);
    if (realEval)
      msg = $1 + " eval " + m.rest();
    
    if (jMatchStart("<id> eval fresh <id>:", msg, m)) {
      if (allowed) dm_refreshTranspiler();
      msg = $1 + " eval " + $2 + ": " + m.rest();
    }

    if (jMatchStart("<id> eval <id>:", msg, m)) {
      if (neqOneOf($1, "all", computerID())) ret with print("Not for me.");
      else if (!enabled) print("Will not eval (module disabled).");
      else {
        print(trim(indentx("EVAL from " + author + ": " + shorten(m.rest(), 1000))));
        O result = null;
        S error = null;
        try {
          if (logAll) logQuoted(javaxCachesDir("signed-instructions.txt"), originalMsg);
          if (!allowed)
            if (pairB(authorPair) == null) {
              print(l(originalMsg) + " " + quote(shorten(originalMsg, 1000)));
              print(quote(takeLast(originalMsg, 1000)));
              fail("Signature didn't verify (computer ID: " + pairA(authorPair) + ", public key md5: " + md5(getPublicKeyOfComputer(pairA(authorPair))) + ")");
            } else
              fail("not accepting commands from " + author);
          result = realEval ? dm_javaEval(m.rest()) : dm_javaEvalOrInterpret(m.rest());
        } catch print e {
          error = getStackTrace(e);
        }
        S mode = "direct";
        S out;
        if (result != null) {
          int l = l_opt(result);
          print(">> " + (l != 0 ? "[l=" + l + "] " : "") + result);
        }
        
        if (error != null) {
          mode = "error";
          out = error;
        } else if (result instanceof byte[]) {
          mode = "base64";
          out = base64((byte[]) result);
        } else {
          out = str(result);
          if (empty(out) || trimmable(out) || containsNewLine(out)) {
            out = quote(out);
            mode = "quoted";
          }
        }
        S line = "done " + $2 + " " + mode + ": " + out;
        if (sender == null)
          dm_osChat_postSigned(line);
        else
          dm_privateChat_sendSigned(sender, line);
      }
      ret;
    }

    if (!allowed) ret;
    // old-style formats follow
    
    //print("Got signed message from " + author + ": " + msg);
    if (jMatchStart("<id> eval:", msg, m)) {
      if (neqOneOf($1, "all", computerID())) print("Not for me.");
      else if (!enabled) print("Will not eval (module disabled).");
      else {
        print(trim(indentx("EVAL: " + shorten(m.rest(), 1000))));
        O result = dm_javaEvalOrInterpret(m.rest());
        print(">> " + result);
      }
    }
  }
  
  // API
  
  bool addControllingComputer(S computerID) {
    if (contains(controllingComputers, computerID)) false;
    setField(controllingComputers := trim(controllingComputers + " " + computerID));
    true;
  }
}

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: 670 / 422280
Version history: 64 change(s)
Referenced in: #1016478 - Stefan's OS v6
#1020353 - dm_addControllingComputer - only works when the Execute Signed Instructions module is loaded
#1022729 - Stefan's OS v6 - copy for transpiler test
#1024922 - programPackager_standardExcludedSnippets
#1024932 - Stefan's OS v7 [LIVE]
#1031320 - Stefan's OS v7 [backup before allowing dynamic main classes]