diff --git a/devtool.py b/devtool.py new file mode 100644 index 0000000..47ee440 --- /dev/null +++ b/devtool.py @@ -0,0 +1,41 @@ +import json +import sys +import requests + + +USERNAME = "admin" +PASSWORD = "admin" + + +def do_login(base_url): + session = requests.session() + + login_data = json.dumps({ + "username": USERNAME, + "password": PASSWORD + }, ensure_ascii=True, indent=0) + + session.get(f"{base_url}/login") + + login_result = json.loads(session.post(f"{base_url}/login", headers={'Content-Type': 'application/json'}, data=login_data).content.decode('utf-8')) + if "error" in login_result: + raise RuntimeError(f"Failed to login: {login_result}") + + return session + + +def set_logging(base_url, value): + session = do_login(base_url) + res = session.post(f"{base_url}/dev/cpapicall?f=SetDmaDebug¶m=log_bool&value={value}") + print(res.content.decode('utf-8')) + + +if __name__ == '__main__': + if len(sys.argv) < 3: + print(f"Usage: {sys.argv[0]} http(s)://terminal-api logging on|off") + exit(1) + + if sys.argv[2] == "logging": + set_logging(sys.argv[1], {"on": "true", "off": "false"}[sys.argv[3]]) + else: + print(f"Unknown action: {sys.argv[1]}") diff --git a/src/main.cpp b/src/main.cpp index 43c3943..82d1ff8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -402,6 +402,7 @@ public: s.resources.emplace_back(std::make_unique("/api/firmwareUpdate", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { if (req.method != "PUT") { http::server::stockReply(http::server::bad_request, rep); + return; } this->upgradeOrRebootRunning = true; onUploadFirmware(req); @@ -420,6 +421,7 @@ public: s.resources.emplace_back(std::make_unique("/api/doFirmwareUpgrade", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) { if (req.method != "POST") { http::server::stockReply(http::server::bad_request, rep); + return; } this->upgradeOrRebootRunning = true; doTerminalUpgrade(); @@ -435,6 +437,42 @@ public: sf->serve(DEV_HTML, rep); })); + s.resources.emplace_back(std::make_unique("/dev/cpapicall", this->auth, http::auth::User::SUPERUSER, [this](const http::server::Request& req, auto& rep) { + if (req.method != "POST") { + http::server::stockReply(http::server::bad_request, rep); + return; + } + if (req.url->params.find("f") == req.url->params.end()) { + http::server::stockReply(http::server::bad_request, rep); + return; + } + const auto func = req.url->params["f"]; + std::string result = R"({"status":"ok"})"; + if (func == "SetDmaDebug") { + if (req.url->params.find("param") == req.url->params.end()) { http::server::stockReply(http::server::bad_request, rep); return; } + if (req.url->params.find("value") == req.url->params.end()) { http::server::stockReply(http::server::bad_request, rep); return; } + this->api->executeInApi([&](auto sid) { + CP_SetDmaDebug(sid, req.url->params["param"].c_str(), req.url->params["value"]); + }); + } else if (func == "GetDmaDebug") { + if (req.url->params.find("param") == req.url->params.end()) { http::server::stockReply(http::server::bad_request, rep); return; } + this->api->executeInApi([&](auto sid) { + std::string tmp{}; + CP_GetDmaDebug(sid, req.url->params["param"].c_str(), &tmp); + result = R"({"status":"ok","result":)"; + result += api_driver::buildEscapedString(tmp); + result += "}"; + }); + } else { + http::server::stockReply(http::server::not_implemented, rep); + return; + } + + rep.status = http::server::ok; + rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); + rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); + })); + // s.resources.emplace_back(std::make_unique("/dev/fetchParams", this->auth, http::auth::User::SUPERUSER, [this](const auto& req, auto& rep) { // rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)}); // std::string result = R"({"status":"ok","fwsize":)";