From 48b15f450f8abc77e065af79501a19423485e98d Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Tue, 16 Jul 2024 00:37:08 +0700 Subject: [PATCH] refactorings --- openrouter/CMakeLists.txt | 2 ++ openrouter/src/auth/auth.cpp | 22 +++++++++++++ openrouter/src/auth/auth.hpp | 8 +++++ openrouter/src/settings/dns/enabled.cpp | 31 ++++------------- openrouter/src/settings/dns/local_domains.cpp | 31 ++++------------- openrouter/src/settings/dns/servers.cpp | 33 ++++--------------- openrouter/src/settings/ftp/ftp.cpp | 31 ++++------------- openrouter/src/settings/ssh/keys.cpp | 32 +++++------------- openrouter/src/settings/ssh/port.cpp | 32 +++++------------- openrouter/src/settings/ssh/status.cpp | 32 +++++------------- openrouter/src/settings/telnet/enabled.cpp | 31 ++++------------- openrouter/src/settings/telnet/port.cpp | 31 ++++------------- openrouter/src/settings/wifi/channel.cpp | 31 ++++------------- openrouter/src/settings/wifi/name.cpp | 31 ++++------------- openrouter/src/settings/wifi/password.cpp | 31 ++++------------- openrouter/src/settings/wifi/qr.cpp | 17 +++------- openrouter/src/settings/wifi/status.cpp | 31 ++++------------- openrouter/src/system/reboot/reboot.cpp | 15 ++------- openrouter/src/web/js/reboot.js | 5 +-- 19 files changed, 134 insertions(+), 343 deletions(-) create mode 100644 openrouter/src/auth/auth.cpp create mode 100644 openrouter/src/auth/auth.hpp diff --git a/openrouter/CMakeLists.txt b/openrouter/CMakeLists.txt index 8c0cebb..59291c0 100644 --- a/openrouter/CMakeLists.txt +++ b/openrouter/CMakeLists.txt @@ -35,6 +35,8 @@ add_executable( src/OpenRouter/OpenRouter.cpp + src/auth/auth.cpp + src/libs/QR-Code-generator/qrcodegen.cpp src/libs/QR-Code-generator/qrtosvg.cpp ) diff --git a/openrouter/src/auth/auth.cpp b/openrouter/src/auth/auth.cpp new file mode 100644 index 0000000..8ea8405 --- /dev/null +++ b/openrouter/src/auth/auth.cpp @@ -0,0 +1,22 @@ +#include "auth.hpp" +#include +#include +#include "../routes.hpp" +#include +#include "../types.hpp" + +bool authenticate(std::string token) { + httplib::Client cli(AUTH_SERVICE); + + std::stringstream body; + body << R"({"token":")" << token << R"("})"; + httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); + if (res->status == httplib::OK_200) { + nlohmann::json resjson = nlohmann::json::parse(res->body); + if (resjson["access"] == "reject") { + return false; + } + return true; + } + return false; +} diff --git a/openrouter/src/auth/auth.hpp b/openrouter/src/auth/auth.hpp new file mode 100644 index 0000000..86c992d --- /dev/null +++ b/openrouter/src/auth/auth.hpp @@ -0,0 +1,8 @@ +#ifndef AUTH_HPP +#define AUTH_HPP + +#include + +bool authenticate(std::string token); + +#endif // AUTH_HPP \ No newline at end of file diff --git a/openrouter/src/settings/dns/enabled.cpp b/openrouter/src/settings/dns/enabled.cpp index bdedf96..e96e2bb 100644 --- a/openrouter/src/settings/dns/enabled.cpp +++ b/openrouter/src/settings/dns/enabled.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -22,18 +23,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream dnsfile("/etc/openrouter/dns"); int dns_enabled; dnsfile >> dns_enabled; @@ -41,7 +31,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << dns_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_dns(const httplib::Request &request, httplib::Response &response) { @@ -63,18 +55,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream dnsfileout("/etc/openrouter/dns"); std::string new_dns_enabled = json_body["enabled"]; if (new_dns_enabled == "0") { @@ -95,6 +76,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << dns_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/dns/local_domains.cpp b/openrouter/src/settings/dns/local_domains.cpp index 489eb22..a4914f5 100644 --- a/openrouter/src/settings/dns/local_domains.cpp +++ b/openrouter/src/settings/dns/local_domains.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" namespace settings { void get_local_domains(const httplib::Request &request, httplib::Response &response) { @@ -20,18 +21,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); std::stringstream responsedata; responsedata << R"({"domains":[)"; @@ -56,7 +46,9 @@ namespace settings { } domainsfile.close(); response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_local_domains(const httplib::Request &request, httplib::Response &response) { @@ -73,18 +65,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream domainsfile("/etc/dnsmasq.conf.d/hosts"); nlohmann::json domains = json_body["domains"]; for (nlohmann::json_abi_v3_11_3::json domain : domains) { @@ -97,6 +78,8 @@ namespace settings { syslog(LOG_INFO, "DNS local domains changed"); std::string responsedata = R"({"success":"true"})"; response.set_content(responsedata, JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/dns/servers.cpp b/openrouter/src/settings/dns/servers.cpp index 41c719d..fac9eca 100644 --- a/openrouter/src/settings/dns/servers.cpp +++ b/openrouter/src/settings/dns/servers.cpp @@ -4,8 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include - -// #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" +#include "../../auth/auth.hpp" namespace settings { void get_dns_servers(const httplib::Request &request, httplib::Response &response) { @@ -22,18 +21,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); std::stringstream responsedata; responsedata << R"({"servers":[)"; @@ -52,7 +40,9 @@ namespace settings { } dnsserversfile.close(); response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_dns_servers(const httplib::Request &request, httplib::Response &response) { @@ -69,18 +59,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); nlohmann::json servers = json_body["servers"]; for (nlohmann::json_abi_v3_11_3::json server : servers) { @@ -95,6 +74,8 @@ namespace settings { syslog(LOG_INFO, "DNS servers changed"); std::string responsedata = R"({"success":"true"})"; response.set_content(responsedata, JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/ftp/ftp.cpp b/openrouter/src/settings/ftp/ftp.cpp index fa11efc..fa9399f 100644 --- a/openrouter/src/settings/ftp/ftp.cpp +++ b/openrouter/src/settings/ftp/ftp.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" #define FTP_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -22,18 +23,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream ftpfile("/etc/openrouter/ftp"); int ftp_enabled; ftpfile >> ftp_enabled; @@ -41,7 +31,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_ftp(const httplib::Request &request, httplib::Response &response) { @@ -63,18 +55,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream ftpfileout("/etc/openrouter/ftp"); std::string new_ftp_enabled = json_body["enabled"]; if (new_ftp_enabled == "0") { @@ -95,6 +76,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/ssh/keys.cpp b/openrouter/src/settings/ssh/keys.cpp index edb8c8d..c96cbad 100644 --- a/openrouter/src/settings/ssh/keys.cpp +++ b/openrouter/src/settings/ssh/keys.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" #define KEYS_PATH "/root/.ssh/authorized_keys" @@ -22,18 +23,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream keysfile(KEYS_PATH); std::stringstream responsedata; responsedata << R"({"keys":[)"; @@ -54,8 +44,11 @@ namespace settings { } keysfile.close(); response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } + void set_ssh_keys(const httplib::Request &request, httplib::Response &response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); @@ -70,18 +63,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream keysfile(KEYS_PATH); nlohmann::json keys = json_body["keys"]; for (std::string key : keys) { @@ -92,6 +74,8 @@ namespace settings { syslog(LOG_INFO, "SSH keys changed"); std::string responsedata = R"({"success":"true"})"; response.set_content(responsedata, JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/ssh/port.cpp b/openrouter/src/settings/ssh/port.cpp index 1f90082..cdc2c3b 100644 --- a/openrouter/src/settings/ssh/port.cpp +++ b/openrouter/src/settings/ssh/port.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" #define SSH_PORT_PATH "/etc/openrouter/ssh/port" @@ -27,18 +28,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream sshportfile(SSH_PORT_PATH); int ssh_port; sshportfile >> ssh_port; @@ -46,8 +36,11 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"port":")" << ssh_port << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } + void set_ssh_port(const httplib::Request &request, httplib::Response &response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); @@ -67,18 +60,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream oldsshportfile(SSH_PORT_PATH); int old_ssh_port; oldsshportfile >> old_ssh_port; @@ -101,6 +83,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"port":")" << ssh_port << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/ssh/status.cpp b/openrouter/src/settings/ssh/status.cpp index 955a219..d812a81 100644 --- a/openrouter/src/settings/ssh/status.cpp +++ b/openrouter/src/settings/ssh/status.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" #define SSH_REQUIRED_STRING R"({"required":"[token,status]"})" @@ -22,18 +23,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); int ssh_status; sshstatusfile >> ssh_status; @@ -41,8 +31,11 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"status":")" << ssh_status << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } + void set_ssh_status(const httplib::Request &request, httplib::Response &response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); @@ -62,18 +55,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream sshfileout("/etc/openrouter/ssh/status"); std::string new_ssh_status = json_body["status"]; if (new_ssh_status == "0") { @@ -94,6 +76,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"status":")" << ssh_status << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/telnet/enabled.cpp b/openrouter/src/settings/telnet/enabled.cpp index b811914..ae4b5f2 100644 --- a/openrouter/src/settings/telnet/enabled.cpp +++ b/openrouter/src/settings/telnet/enabled.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define TELNET_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -26,18 +27,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); int telnet_enabled; telnetfile >> telnet_enabled; @@ -45,7 +35,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_telnet(const httplib::Request &request, httplib::Response &response) { @@ -67,18 +59,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); std::string new_telnet_enabled = json_body["enabled"]; if (new_telnet_enabled == "0") { @@ -99,6 +80,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/telnet/port.cpp b/openrouter/src/settings/telnet/port.cpp index 1224780..702bf39 100644 --- a/openrouter/src/settings/telnet/port.cpp +++ b/openrouter/src/settings/telnet/port.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" @@ -26,18 +27,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream telnetportfile("/etc/openrouter/telnet/port"); int telnet_port; telnetportfile >> telnet_port; @@ -45,7 +35,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"port":")" << telnet_port << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_telnet_port(const httplib::Request &request, httplib::Response &response) { @@ -67,18 +59,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); std::string new_telnet_port = json_body["port"]; telnetportfileout << stoi(new_telnet_port); @@ -93,6 +74,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"port":")" << telnet_port << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/channel.cpp b/openrouter/src/settings/wifi/channel.cpp index 7e3e457..0e9024b 100644 --- a/openrouter/src/settings/wifi/channel.cpp +++ b/openrouter/src/settings/wifi/channel.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})" @@ -26,18 +27,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string wifi_channel; std::string wifisettingsbuffer; @@ -52,7 +42,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"channel":")" << wifi_channel << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_wifi_channel(const httplib::Request &request, httplib::Response &response) { @@ -74,18 +66,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_channel; std::string wifisettingsbuffer; @@ -120,6 +101,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"channel":")" << wifi_channel << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/name.cpp b/openrouter/src/settings/wifi/name.cpp index e7e7f53..4fd844a 100644 --- a/openrouter/src/settings/wifi/name.cpp +++ b/openrouter/src/settings/wifi/name.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})" @@ -26,18 +27,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string wifi_name; std::string wifisettingsbuffer; @@ -52,7 +42,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"name":")" << wifi_name << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_wifi_name(const httplib::Request &request, httplib::Response &response) { @@ -74,18 +66,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_wifi_name; std::string wifisettingsbuffer; @@ -120,6 +101,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"name":")" << wifi_name << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } \ No newline at end of file diff --git a/openrouter/src/settings/wifi/password.cpp b/openrouter/src/settings/wifi/password.cpp index 50f1008..5daa58a 100644 --- a/openrouter/src/settings/wifi/password.cpp +++ b/openrouter/src/settings/wifi/password.cpp @@ -8,6 +8,7 @@ #include #include #include +#include "../../auth/auth.hpp" #define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})" @@ -26,18 +27,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string wifi_password; std::string wifisettingsbuffer; @@ -52,7 +42,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"password":")" << wifi_password << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_wifi_password(const httplib::Request &request, httplib::Response &response) { @@ -79,18 +71,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_wifi_password; std::string wifisettingsbuffer; @@ -117,6 +98,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"success":"ok"})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/qr.cpp b/openrouter/src/settings/wifi/qr.cpp index d9c3d7b..b8f1028 100644 --- a/openrouter/src/settings/wifi/qr.cpp +++ b/openrouter/src/settings/wifi/qr.cpp @@ -5,6 +5,7 @@ #include "../../types.hpp" #include #include +#include "../../auth/auth.hpp" namespace settings { void get_wifi_qr(const httplib::Request &request, httplib::Response &response) { @@ -21,19 +22,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } - + if (authenticate(json_body["token"])) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string wifi_name; std::string wifi_password; @@ -57,6 +46,8 @@ namespace settings { qrcodegen::QrCode qr0 = qrcodegen::QrCode::encodeText(toqrstring.str().c_str(), qrcodegen::QrCode::Ecc::MEDIUM); std::string responsedata = toSvgString(qr0, 4); response.set_content(responsedata, SVG_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/status.cpp b/openrouter/src/settings/wifi/status.cpp index d1962dc..76e76b0 100644 --- a/openrouter/src/settings/wifi/status.cpp +++ b/openrouter/src/settings/wifi/status.cpp @@ -4,6 +4,7 @@ #include "../../routes.hpp" #include "../../types.hpp" #include +#include "../../auth/auth.hpp" #define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})" namespace settings { @@ -21,18 +22,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); int wifi_status; wifistatusfile >> wifi_status; @@ -40,7 +30,9 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"status":")" << wifi_status << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } void set_wifi_status(const httplib::Request &request, httplib::Response &response) { @@ -62,18 +54,7 @@ namespace settings { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { std::ofstream wififileout("/etc/openrouter/wifi/status"); std::string new_wifi_status = json_body["status"]; if (new_wifi_status == "0") { @@ -94,6 +75,8 @@ namespace settings { std::stringstream responsedata; responsedata << R"({"status":")" << wifi_status << R"("})"; response.set_content(responsedata.str(), JSON_TYPE); + return; } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/system/reboot/reboot.cpp b/openrouter/src/system/reboot/reboot.cpp index 7f79615..7d960e3 100644 --- a/openrouter/src/system/reboot/reboot.cpp +++ b/openrouter/src/system/reboot/reboot.cpp @@ -3,6 +3,7 @@ #include "../routes.hpp" #include #include "../types.hpp" +#include "../../auth/auth.hpp" namespace systemfuncs { void reboot(const httplib::Request &request, httplib::Response &response) { @@ -19,19 +20,9 @@ namespace systemfuncs { return; } - httplib::Client cli(AUTH_SERVICE); - - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; - - httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE); - if (res->status == httplib::OK_200) { - nlohmann::json resjson = nlohmann::json::parse(res->body); - if (resjson["access"] == "reject") { - response.set_content(resjson.dump().c_str(), JSON_TYPE); - return; - } + if (authenticate(json_body["token"])) { system("reboot"); } + response.set_content(R"({"access":"reject"})", JSON_TYPE); } } diff --git a/openrouter/src/web/js/reboot.js b/openrouter/src/web/js/reboot.js index e61835e..460d6f4 100644 --- a/openrouter/src/web/js/reboot.js +++ b/openrouter/src/web/js/reboot.js @@ -20,10 +20,7 @@ const reboot = () => { } const waitreboot = () => { - fetch(routes.telnet_status(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(res => res.json()).then(jsondata => { + fetch(routes.uptime()).then(res => res.json()).then(jsondata => { stop_loader() }).catch((error) => { waitreboot()