dropshell/runner/runner_demo.cpp
Your Name 8827ea5a42
Some checks failed
Dropshell Test / Build_and_Test (push) Failing after 20s
.
2025-05-10 13:47:17 +12:00

63 lines
1.9 KiB
C++

#include "runner.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <nlohmann/json.hpp>
#include <openssl/bio.h>
#include <openssl/evp.h>
using json = nlohmann::json;
std::string base64_decode(const std::string& encoded) {
BIO* bio, *b64;
int decodeLen = (encoded.length() * 3) / 4;
std::string decoded(decodeLen, '\0');
bio = BIO_new_mem_buf(encoded.data(), encoded.length());
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
int len = BIO_read(bio, &decoded[0], encoded.length());
decoded.resize(len > 0 ? len : 0);
BIO_free_all(bio);
return decoded;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: runner BASE64COMMAND\n";
return -1;
}
std::string decoded = base64_decode(argv[1]);
json j;
try {
j = json::parse(decoded);
} catch (...) {
std::cerr << "Invalid JSON in decoded command\n";
return -1;
}
std::string command = j.value("command", "");
std::vector<std::string> args = j.value("args", std::vector<std::string>{});
std::string working_dir = j.value("working_dir", "");
std::map<std::string, std::string> env = j.value("env", std::map<std::string, std::string>{});
bool silent = j.value("silent", false);
bool interactive = j.value("interactive", false);
sSSHInfo* sshinfo = nullptr;
sSSHInfo ssh;
if (j.contains("sshinfo")) {
ssh.host = j["sshinfo"].value("host", "");
ssh.user = j["sshinfo"].value("user", "");
ssh.port = j["sshinfo"].value("port", "");
sshinfo = &ssh;
}
std::string output;
int ret = execute_cmd(command, args, working_dir, env, silent, interactive, sshinfo, &output);
if (!silent && !output.empty()) {
std::cout << output;
}
return ret;
}