From 5f0959b521f469f4b831f06efe77d52244cb9b51 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Wed, 22 Jan 2025 16:03:15 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F/=D0=B2=D1=8B=D0=BA=D0=BB=D1=8E=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BB=D0=BE=D0=B3=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20?= =?UTF-8?q?=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devtool.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 devtool.py 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":)";