diff --git a/openrouter/CMakeLists.txt b/openrouter/CMakeLists.txt index 2cce541..8c0cebb 100644 --- a/openrouter/CMakeLists.txt +++ b/openrouter/CMakeLists.txt @@ -28,10 +28,12 @@ add_executable( src/settings/ftp/ftp.cpp - src/reboot/reboot.cpp + src/system/reboot/reboot.cpp - src/syslog/syslog.cpp - src/uptime/uptime.cpp + src/system/syslog/syslog.cpp + src/system/uptime/uptime.cpp + + src/OpenRouter/OpenRouter.cpp src/libs/QR-Code-generator/qrcodegen.cpp src/libs/QR-Code-generator/qrtosvg.cpp diff --git a/openrouter/src/OpenRouter/OpenRouter.cpp b/openrouter/src/OpenRouter/OpenRouter.cpp new file mode 100644 index 0000000..01ebd22 --- /dev/null +++ b/openrouter/src/OpenRouter/OpenRouter.cpp @@ -0,0 +1,59 @@ +#include "OpenRouter.hpp" + +OpenRouter::OpenRouter() { + this->srv.Post("/api/settings/telnet/get", settings::get_telnet); + this->srv.Post("/api/settings/telnet/set", settings::set_telnet); + this->srv.Post("/api/settings/telnet/port/get", settings::get_telnet_port); + this->srv.Post("/api/settings/telnet/port/set", settings::set_telnet_port); + + this->srv.Post("/api/settings/ssh/status/get", settings::get_ssh_status); + this->srv.Post("/api/settings/ssh/status/set", settings::set_ssh_status); + this->srv.Post("/api/settings/ssh/keys/get", settings::get_ssh_keys); + this->srv.Post("/api/settings/ssh/keys/set", settings::set_ssh_keys); + this->srv.Post("/api/settings/ssh/port/get", settings::get_ssh_port); + this->srv.Post("/api/settings/ssh/port/set", settings::set_ssh_port); + + this->srv.Post("/api/settings/dns/get", settings::get_dns); + this->srv.Post("/api/settings/dns/set", settings::set_dns); + this->srv.Post("/api/settings/dns/servers/get", settings::get_dns_servers); + this->srv.Post("/api/settings/dns/servers/set", settings::set_dns_servers); + this->srv.Post("/api/settings/dns/domains/get", settings::get_local_domains); + this->srv.Post("/api/settings/dns/domains/set", settings::set_local_domains); + + this->srv.Post("/api/settings/ftp/get", settings::get_ftp); + this->srv.Post("/api/settings/ftp/set", settings::set_ftp); + + this->srv.Post("/api/settings/wifi/status/get", settings::get_wifi_status); + this->srv.Post("/api/settings/wifi/status/set", settings::set_wifi_status); + this->srv.Post("/api/settings/wifi/name/get", settings::get_wifi_name); + this->srv.Post("/api/settings/wifi/name/set", settings::set_wifi_name); + this->srv.Post("/api/settings/wifi/password/get", settings::get_wifi_password); + this->srv.Post("/api/settings/wifi/password/set", settings::set_wifi_password); + this->srv.Post("/api/settings/wifi/qr/get", settings::get_wifi_qr); + this->srv.Post("/api/settings/wifi/channel/get", settings::get_wifi_channel); + this->srv.Post("/api/settings/wifi/channel/set", settings::set_wifi_channel); + + this->srv.Post("/api/reboot", systemfuncs::reboot); + + this->srv.Get("/api/syslog", systemfuncs::get_syslog); + this->srv.Get("/api/uptime", systemfuncs::get_uptime); + + this->srv.Options("/(.*)", set_cors_headers); + + this->srv.set_mount_point("/" , "../src/web"); + this->srv.set_mount_point("/js", "../src/web/js"); + this->srv.set_mount_point("/css", "../src/web/css"); +} + +OpenRouter::~OpenRouter() { + +} + +int OpenRouter::serve(const std::string ip, int port, int socket_flags) { + std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW + << "0.0.0.0" << RESET << ":" << CYAN << "43244" << RESET << " ]" << std::endl; + + syslog(LOG_INFO, "openrouter administration software started."); + srv.listen("127.0.0.1", 43244); + return 0; +} diff --git a/openrouter/src/OpenRouter/OpenRouter.hpp b/openrouter/src/OpenRouter/OpenRouter.hpp new file mode 100644 index 0000000..e1dc2c1 --- /dev/null +++ b/openrouter/src/OpenRouter/OpenRouter.hpp @@ -0,0 +1,24 @@ +#ifndef OPENROUTER_HPP +#define OPENROUTER_HPP + +#include +#include +#include +#include "../settings/settings.hpp" +#include "../system/system.hpp" +#include + +#define CORS_ENABLE +#include "../security/cors.hpp" + +class OpenRouter { + private: + httplib::Server srv; + + public: + OpenRouter(); + ~OpenRouter(); + int serve(const std::string ip, int port, int socket_flags = 0); +}; + +#endif // OPENROUTER_HPP \ No newline at end of file diff --git a/openrouter/src/openrouter.cpp b/openrouter/src/openrouter.cpp index 4531aed..8ea5ec6 100644 --- a/openrouter/src/openrouter.cpp +++ b/openrouter/src/openrouter.cpp @@ -1,69 +1,6 @@ -#include -#include -#include -#include "settings/telnet/telnet.hpp" -#include "settings/ssh/ssh.hpp" -#include "settings/dns/dns.hpp" -#include "settings/ftp/ftp.hpp" -#include "settings/wifi/wifi.hpp" -#include "reboot/reboot.hpp" -#include "syslog/syslog.hpp" -#include "uptime/uptime.hpp" -#include - -#define CORS_ENABLE -#include "security/cors.hpp" +#include "OpenRouter/OpenRouter.hpp" int main() { - httplib::Server srv; - - srv.Post("/api/settings/telnet/get", get_telnet); - srv.Post("/api/settings/telnet/set", set_telnet); - srv.Post("/api/settings/telnet/port/get", get_telnet_port); - srv.Post("/api/settings/telnet/port/set", set_telnet_port); - - srv.Post("/api/settings/ssh/status/get", get_ssh_status); - srv.Post("/api/settings/ssh/status/set", set_ssh_status); - srv.Post("/api/settings/ssh/keys/get", get_ssh_keys); - srv.Post("/api/settings/ssh/keys/set", set_ssh_keys); - srv.Post("/api/settings/ssh/port/get", get_ssh_port); - srv.Post("/api/settings/ssh/port/set", set_ssh_port); - - srv.Post("/api/settings/dns/get", get_dns); - srv.Post("/api/settings/dns/set", set_dns); - srv.Post("/api/settings/dns/servers/get", get_dns_servers); - srv.Post("/api/settings/dns/servers/set", set_dns_servers); - srv.Post("/api/settings/dns/domains/get", get_local_domains); - srv.Post("/api/settings/dns/domains/set", set_local_domains); - - srv.Post("/api/settings/ftp/get", get_ftp); - srv.Post("/api/settings/ftp/set", set_ftp); - - srv.Post("/api/settings/wifi/status/get", get_wifi_status); - srv.Post("/api/settings/wifi/status/set", set_wifi_status); - srv.Post("/api/settings/wifi/name/get", get_wifi_name); - srv.Post("/api/settings/wifi/name/set", set_wifi_name); - srv.Post("/api/settings/wifi/password/get", get_wifi_password); - srv.Post("/api/settings/wifi/password/set", set_wifi_password); - srv.Post("/api/settings/wifi/qr/get", get_wifi_qr); - srv.Post("/api/settings/wifi/channel/get", get_wifi_channel); - srv.Post("/api/settings/wifi/channel/set", set_wifi_channel); - - srv.Post("/api/reboot", reboot); - - srv.Get("/api/syslog", get_syslog); - srv.Get("/api/uptime", get_uptime); - - srv.Options("/(.*)", set_cors_headers); - - srv.set_mount_point("/" , "../src/web"); - srv.set_mount_point("/js", "../src/web/js"); - srv.set_mount_point("/css", "../src/web/css"); - - std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW - << "0.0.0.0" << RESET << ":" << CYAN << "43244" << RESET << " ]" << std::endl; - - syslog(LOG_INFO, "openrouter administration software started."); - srv.listen("127.0.0.1", 43244); - return 0; + OpenRouter router; + return router.serve("127.0.0.1", 43244, 0); } diff --git a/openrouter/src/reboot/reboot.cpp b/openrouter/src/reboot/reboot.cpp deleted file mode 100644 index 437384d..0000000 --- a/openrouter/src/reboot/reboot.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "reboot.hpp" -#include -#include "../routes.hpp" -#include -#include "../types.hpp" - -void reboot(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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; - } - system("reboot"); - } -} diff --git a/openrouter/src/reboot/reboot.hpp b/openrouter/src/reboot/reboot.hpp deleted file mode 100644 index 4ad585b..0000000 --- a/openrouter/src/reboot/reboot.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef REBOOT_HPP -#define REBOOT_HPP - -#include - -void reboot(const httplib::Request &request, httplib::Response &response); - -#endif // REBOOT_HPP diff --git a/openrouter/src/settings/dns/dns.hpp b/openrouter/src/settings/dns/dns.hpp index 81b5ebb..6e61c80 100644 --- a/openrouter/src/settings/dns/dns.hpp +++ b/openrouter/src/settings/dns/dns.hpp @@ -3,13 +3,15 @@ #include -void get_dns(const httplib::Request &request, httplib::Response &response); -void set_dns(const httplib::Request &request, httplib::Response &response); +namespace settings { + void get_dns(const httplib::Request &request, httplib::Response &response); + void set_dns(const httplib::Request &request, httplib::Response &response); -void get_dns_servers(const httplib::Request &request, httplib::Response &response); -void set_dns_servers(const httplib::Request &request, httplib::Response &response); + void get_dns_servers(const httplib::Request &request, httplib::Response &response); + void set_dns_servers(const httplib::Request &request, httplib::Response &response); -void get_local_domains(const httplib::Request &request, httplib::Response &response); -void set_local_domains(const httplib::Request &request, httplib::Response &response); + void get_local_domains(const httplib::Request &request, httplib::Response &response); + void set_local_domains(const httplib::Request &request, httplib::Response &response); +} #endif // DNS_HPP \ No newline at end of file diff --git a/openrouter/src/settings/dns/enabled.cpp b/openrouter/src/settings/dns/enabled.cpp index dbaf147..bdedf96 100644 --- a/openrouter/src/settings/dns/enabled.cpp +++ b/openrouter/src/settings/dns/enabled.cpp @@ -7,92 +7,94 @@ #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" -void get_dns(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_dns(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream dnsfile("/etc/openrouter/dns"); - int dns_enabled; - dnsfile >> dns_enabled; - dnsfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << dns_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_dns(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["enabled"] == nullptr) { - response.set_content(DNS_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream dnsfileout("/etc/openrouter/dns"); - std::string new_dns_enabled = json_body["enabled"]; - if (new_dns_enabled == "0") { - system("rc stop dnsmasq"); - system("rc del dnsmasq"); - syslog(LOG_INFO, "DNS is disabled"); - } else { - system("rc restart dnsmasq"); - system("rc add dnsmasq"); - syslog(LOG_INFO, "DNS is enabled"); + + 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; + } + std::ifstream dnsfile("/etc/openrouter/dns"); + int dns_enabled; + dnsfile >> dns_enabled; + dnsfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << dns_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_dns(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["enabled"] == nullptr) { + response.set_content(DNS_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream dnsfileout("/etc/openrouter/dns"); + std::string new_dns_enabled = json_body["enabled"]; + if (new_dns_enabled == "0") { + system("rc stop dnsmasq"); + system("rc del dnsmasq"); + syslog(LOG_INFO, "DNS is disabled"); + } else { + system("rc restart dnsmasq"); + system("rc add dnsmasq"); + syslog(LOG_INFO, "DNS is enabled"); + } + dnsfileout << stoi(new_dns_enabled); + dnsfileout.close(); + std::ifstream dnsfile("/etc/openrouter/dns"); + int dns_enabled; + dnsfile >> dns_enabled; + dnsfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << dns_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - dnsfileout << stoi(new_dns_enabled); - dnsfileout.close(); - std::ifstream dnsfile("/etc/openrouter/dns"); - int dns_enabled; - dnsfile >> dns_enabled; - dnsfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << dns_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/dns/local_domains.cpp b/openrouter/src/settings/dns/local_domains.cpp index 4f76866..489eb22 100644 --- a/openrouter/src/settings/dns/local_domains.cpp +++ b/openrouter/src/settings/dns/local_domains.cpp @@ -5,96 +5,98 @@ #include "../../types.hpp" #include -void get_local_domains(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_local_domains(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); - std::stringstream responsedata; - responsedata << R"({"domains":[)"; - std::string ip_string = ""; - std::string domain_string = ""; - int iter = 0; - while (!domainsfile.eof()) { - domainsfile >> ip_string; - domainsfile >> domain_string; - if (domain_string == "") { - continue; + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; } - responsedata << R"({"ip":")" << ip_string << R"(","domain":")" << domain_string << R"("},)"; - ip_string = ""; - domain_string = ""; - iter++; + std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); + std::stringstream responsedata; + responsedata << R"({"domains":[)"; + std::string ip_string = ""; + std::string domain_string = ""; + int iter = 0; + while (!domainsfile.eof()) { + domainsfile >> ip_string; + domainsfile >> domain_string; + if (domain_string == "") { + continue; + } + responsedata << R"({"ip":")" << ip_string << R"(","domain":")" << domain_string << R"("},)"; + ip_string = ""; + domain_string = ""; + iter++; + } + if (iter != 0) { + responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); + } else { + responsedata << "]}"; + } + domainsfile.close(); + response.set_content(responsedata.str(), JSON_TYPE); } - if (iter != 0) { - responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); - } else { - responsedata << "]}"; - } - domainsfile.close(); - response.set_content(responsedata.str(), JSON_TYPE); - } -} - -void set_local_domains(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; } - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); + void set_local_domains(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream domainsfile("/etc/dnsmasq.conf.d/hosts"); - nlohmann::json domains = json_body["domains"]; - for (nlohmann::json_abi_v3_11_3::json domain : domains) { - std::string ip = domain["ip"]; - std::string dom = domain["domain"]; - domainsfile << ip << " " << dom << "\n"; + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream domainsfile("/etc/dnsmasq.conf.d/hosts"); + nlohmann::json domains = json_body["domains"]; + for (nlohmann::json_abi_v3_11_3::json domain : domains) { + std::string ip = domain["ip"]; + std::string dom = domain["domain"]; + domainsfile << ip << " " << dom << "\n"; + } + domainsfile.close(); + system("rc restart dnsmasq"); + syslog(LOG_INFO, "DNS local domains changed"); + std::string responsedata = R"({"success":"true"})"; + response.set_content(responsedata, JSON_TYPE); } - domainsfile.close(); - system("rc restart dnsmasq"); - syslog(LOG_INFO, "DNS local domains changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); } } diff --git a/openrouter/src/settings/dns/servers.cpp b/openrouter/src/settings/dns/servers.cpp index 17581d2..41c719d 100644 --- a/openrouter/src/settings/dns/servers.cpp +++ b/openrouter/src/settings/dns/servers.cpp @@ -7,92 +7,94 @@ // #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" -void get_dns_servers(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_dns_servers(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); - std::stringstream responsedata; - responsedata << R"({"servers":[)"; - std::string dns_server_string = ""; - int iter = 0; - while (std::getline(dnsserversfile, dns_server_string)) { - dns_server_string.erase(0, 11); - responsedata << "\"" << dns_server_string << R"(",)"; - dns_server_string = ""; - iter++; - } - if (iter != 0) { - responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); - } else { - responsedata << "]}"; - } - dnsserversfile.close(); - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_dns_servers(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - 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) { - std::string srv = server; - if (server == "") { - continue; + + 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; } - dnsserversfile << "nameserver " << srv << "\n"; + std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); + std::stringstream responsedata; + responsedata << R"({"servers":[)"; + std::string dns_server_string = ""; + int iter = 0; + while (std::getline(dnsserversfile, dns_server_string)) { + dns_server_string.erase(0, 11); + responsedata << "\"" << dns_server_string << R"(",)"; + dns_server_string = ""; + iter++; + } + if (iter != 0) { + responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); + } else { + responsedata << "]}"; + } + dnsserversfile.close(); + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_dns_servers(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; + } + 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) { + std::string srv = server; + if (server == "") { + continue; + } + dnsserversfile << "nameserver " << srv << "\n"; + } + dnsserversfile.close(); + system("rc restart dnsmasq"); + syslog(LOG_INFO, "DNS servers changed"); + std::string responsedata = R"({"success":"true"})"; + response.set_content(responsedata, JSON_TYPE); } - dnsserversfile.close(); - system("rc restart dnsmasq"); - syslog(LOG_INFO, "DNS servers changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); } } diff --git a/openrouter/src/settings/ftp/ftp.cpp b/openrouter/src/settings/ftp/ftp.cpp index fac8e3a..fa11efc 100644 --- a/openrouter/src/settings/ftp/ftp.cpp +++ b/openrouter/src/settings/ftp/ftp.cpp @@ -7,92 +7,94 @@ #define FTP_REQUIRED_STRING R"({"required":"[token,enabled]"})" -void get_ftp(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_ftp(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream ftpfile("/etc/openrouter/ftp"); - int ftp_enabled; - ftpfile >> ftp_enabled; - ftpfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_ftp(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["enabled"] == nullptr) { - response.set_content(FTP_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream ftpfileout("/etc/openrouter/ftp"); - std::string new_ftp_enabled = json_body["enabled"]; - if (new_ftp_enabled == "0") { - system("rc stop ftp"); - system("rc del ftp"); - syslog(LOG_INFO, "Ftp is disabled"); - } else { - system("rc start ftp"); - system("rc add ftp"); - syslog(LOG_INFO, "Ftp is enabled"); + + 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; + } + std::ifstream ftpfile("/etc/openrouter/ftp"); + int ftp_enabled; + ftpfile >> ftp_enabled; + ftpfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_ftp(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["enabled"] == nullptr) { + response.set_content(FTP_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream ftpfileout("/etc/openrouter/ftp"); + std::string new_ftp_enabled = json_body["enabled"]; + if (new_ftp_enabled == "0") { + system("rc stop ftp"); + system("rc del ftp"); + syslog(LOG_INFO, "Ftp is disabled"); + } else { + system("rc start ftp"); + system("rc add ftp"); + syslog(LOG_INFO, "Ftp is enabled"); + } + ftpfileout << stoi(new_ftp_enabled); + ftpfileout.close(); + std::ifstream ftpfile("/etc/openrouter/ftp"); + int ftp_enabled; + ftpfile >> ftp_enabled; + ftpfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - ftpfileout << stoi(new_ftp_enabled); - ftpfileout.close(); - std::ifstream ftpfile("/etc/openrouter/ftp"); - int ftp_enabled; - ftpfile >> ftp_enabled; - ftpfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/ftp/ftp.hpp b/openrouter/src/settings/ftp/ftp.hpp index 0139558..dfe6be3 100644 --- a/openrouter/src/settings/ftp/ftp.hpp +++ b/openrouter/src/settings/ftp/ftp.hpp @@ -3,7 +3,9 @@ #include -void get_ftp(const httplib::Request &request, httplib::Response &response); -void set_ftp(const httplib::Request &request, httplib::Response &response); +namespace settings { + void get_ftp(const httplib::Request &request, httplib::Response &response); + void set_ftp(const httplib::Request &request, httplib::Response &response); +} #endif // FTP_HPP \ No newline at end of file diff --git a/openrouter/src/settings/settings.hpp b/openrouter/src/settings/settings.hpp new file mode 100644 index 0000000..fe0c231 --- /dev/null +++ b/openrouter/src/settings/settings.hpp @@ -0,0 +1,10 @@ +#ifndef SETTINGS_HPP +#define SETTINGS_HPP + +#include "wifi/wifi.hpp" +#include "telnet/telnet.hpp" +#include "ssh/ssh.hpp" +#include "ftp/ftp.hpp" +#include "dns/dns.hpp" + +#endif // SETTINGS_HPP \ No newline at end of file diff --git a/openrouter/src/settings/ssh/keys.cpp b/openrouter/src/settings/ssh/keys.cpp index b95dbfd..edb8c8d 100644 --- a/openrouter/src/settings/ssh/keys.cpp +++ b/openrouter/src/settings/ssh/keys.cpp @@ -7,89 +7,91 @@ #define KEYS_PATH "/root/.ssh/authorized_keys" -void get_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream keysfile(KEYS_PATH); - std::stringstream responsedata; - responsedata << R"({"keys":[)"; - std::string key_string = ""; - int iter = 0; - while (std::getline(keysfile, key_string)) { - if (key_string == "") { - continue; + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; } - responsedata << R"(")" << key_string << R"(",)"; - key_string = ""; - iter++; + std::ifstream keysfile(KEYS_PATH); + std::stringstream responsedata; + responsedata << R"({"keys":[)"; + std::string key_string = ""; + int iter = 0; + while (std::getline(keysfile, key_string)) { + if (key_string == "") { + continue; + } + responsedata << R"(")" << key_string << R"(",)"; + key_string = ""; + iter++; + } + if (iter != 0) { + responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); + } else { + responsedata << "]}"; + } + keysfile.close(); + response.set_content(responsedata.str(), JSON_TYPE); } - if (iter != 0) { - responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}"); - } else { - responsedata << "]}"; + } + 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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream keysfile(KEYS_PATH); + nlohmann::json keys = json_body["keys"]; + for (std::string key : keys) { + keysfile << key << "\n"; + } + keysfile.close(); + system("rc restart ssh"); + syslog(LOG_INFO, "SSH keys changed"); + std::string responsedata = R"({"success":"true"})"; + response.set_content(responsedata, JSON_TYPE); } - keysfile.close(); - response.set_content(responsedata.str(), 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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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; - } - std::ofstream keysfile(KEYS_PATH); - nlohmann::json keys = json_body["keys"]; - for (std::string key : keys) { - keysfile << key << "\n"; - } - keysfile.close(); - system("rc restart ssh"); - syslog(LOG_INFO, "SSH keys changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); - } -} \ No newline at end of file diff --git a/openrouter/src/settings/ssh/port.cpp b/openrouter/src/settings/ssh/port.cpp index c55e844..1f90082 100644 --- a/openrouter/src/settings/ssh/port.cpp +++ b/openrouter/src/settings/ssh/port.cpp @@ -12,93 +12,95 @@ #define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" #define SSH_PORT_PATH "/etc/openrouter/ssh/port" -void get_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream sshportfile(SSH_PORT_PATH); - int ssh_port; - sshportfile >> ssh_port; - sshportfile.close(); - std::stringstream responsedata; - responsedata << R"({"port":")" << ssh_port << R"("})"; - response.set_content(responsedata.str(), 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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["port"] == nullptr) { - response.set_content(SSH_PORT_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream oldsshportfile(SSH_PORT_PATH); - int old_ssh_port; - oldsshportfile >> old_ssh_port; - oldsshportfile.close(); - std::ofstream sshportfileout(SSH_PORT_PATH); - std::string new_ssh_port = json_body["port"]; - sshportfileout << stoi(new_ssh_port); - sshportfileout.close(); - std::stringstream systemrequest; - systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config"; - std::cout << systemrequest.str() << std::endl; - system(systemrequest.str().c_str()); - system("rc restart ssh"); - std::string syslogstring = "SSH port changed to " + new_ssh_port; - syslog(LOG_INFO, syslogstring.c_str()); - std::ifstream sshportfile(SSH_PORT_PATH); - int ssh_port; - sshportfile >> ssh_port; - sshportfile.close(); - std::stringstream responsedata; - responsedata << R"({"port":")" << ssh_port << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); + + 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; + } + std::ifstream sshportfile(SSH_PORT_PATH); + int ssh_port; + sshportfile >> ssh_port; + sshportfile.close(); + std::stringstream responsedata; + responsedata << R"({"port":")" << ssh_port << R"("})"; + response.set_content(responsedata.str(), 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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["port"] == nullptr) { + response.set_content(SSH_PORT_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ifstream oldsshportfile(SSH_PORT_PATH); + int old_ssh_port; + oldsshportfile >> old_ssh_port; + oldsshportfile.close(); + std::ofstream sshportfileout(SSH_PORT_PATH); + std::string new_ssh_port = json_body["port"]; + sshportfileout << stoi(new_ssh_port); + sshportfileout.close(); + std::stringstream systemrequest; + systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config"; + std::cout << systemrequest.str() << std::endl; + system(systemrequest.str().c_str()); + system("rc restart ssh"); + std::string syslogstring = "SSH port changed to " + new_ssh_port; + syslog(LOG_INFO, syslogstring.c_str()); + std::ifstream sshportfile(SSH_PORT_PATH); + int ssh_port; + sshportfile >> ssh_port; + sshportfile.close(); + std::stringstream responsedata; + responsedata << R"({"port":")" << ssh_port << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } } } diff --git a/openrouter/src/settings/ssh/ssh.hpp b/openrouter/src/settings/ssh/ssh.hpp index d6671fb..9f7a948 100644 --- a/openrouter/src/settings/ssh/ssh.hpp +++ b/openrouter/src/settings/ssh/ssh.hpp @@ -3,13 +3,15 @@ #include -void get_ssh_status(const httplib::Request &request, httplib::Response &response); -void set_ssh_status(const httplib::Request &request, httplib::Response &response); +namespace settings { + void get_ssh_status(const httplib::Request &request, httplib::Response &response); + void set_ssh_status(const httplib::Request &request, httplib::Response &response); -void get_ssh_keys(const httplib::Request &request, httplib::Response &response); -void set_ssh_keys(const httplib::Request &request, httplib::Response &response); + void get_ssh_keys(const httplib::Request &request, httplib::Response &response); + void set_ssh_keys(const httplib::Request &request, httplib::Response &response); -void get_ssh_port(const httplib::Request &request, httplib::Response &response); -void set_ssh_port(const httplib::Request &request, httplib::Response &response); + void get_ssh_port(const httplib::Request &request, httplib::Response &response); + void set_ssh_port(const httplib::Request &request, httplib::Response &response); +} #endif // SSH_HPP diff --git a/openrouter/src/settings/ssh/status.cpp b/openrouter/src/settings/ssh/status.cpp index 185f32d..955a219 100644 --- a/openrouter/src/settings/ssh/status.cpp +++ b/openrouter/src/settings/ssh/status.cpp @@ -7,91 +7,93 @@ #define SSH_REQUIRED_STRING R"({"required":"[token,status]"})" -void get_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); - int ssh_status; - sshstatusfile >> ssh_status; - sshstatusfile.close(); - std::stringstream responsedata; - responsedata << R"({"status":")" << ssh_status << R"("})"; - response.set_content(responsedata.str(), 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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["status"] == nullptr) { - response.set_content(SSH_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream sshfileout("/etc/openrouter/ssh/status"); - std::string new_ssh_status = json_body["status"]; - if (new_ssh_status == "0") { - system("rc stop ssh"); - system("rc del ssh"); - syslog(LOG_INFO, "SSH is disabled"); - } else { - system("rc start ssh"); - system("rc add ssh"); - syslog(LOG_INFO, "SSH is enabled"); + + 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; + } + std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); + int ssh_status; + sshstatusfile >> ssh_status; + sshstatusfile.close(); + std::stringstream responsedata; + responsedata << R"({"status":")" << ssh_status << R"("})"; + response.set_content(responsedata.str(), 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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["status"] == nullptr) { + response.set_content(SSH_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream sshfileout("/etc/openrouter/ssh/status"); + std::string new_ssh_status = json_body["status"]; + if (new_ssh_status == "0") { + system("rc stop ssh"); + system("rc del ssh"); + syslog(LOG_INFO, "SSH is disabled"); + } else { + system("rc start ssh"); + system("rc add ssh"); + syslog(LOG_INFO, "SSH is enabled"); + } + sshfileout << stoi(new_ssh_status); + sshfileout.close(); + std::ifstream sshfile("/etc/openrouter/ssh/status"); + int ssh_status; + sshfile >> ssh_status; + sshfile.close(); + std::stringstream responsedata; + responsedata << R"({"status":")" << ssh_status << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - sshfileout << stoi(new_ssh_status); - sshfileout.close(); - std::ifstream sshfile("/etc/openrouter/ssh/status"); - int ssh_status; - sshfile >> ssh_status; - sshfile.close(); - std::stringstream responsedata; - responsedata << R"({"status":")" << ssh_status << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/telnet/enabled.cpp b/openrouter/src/settings/telnet/enabled.cpp index 6e727fd..b811914 100644 --- a/openrouter/src/settings/telnet/enabled.cpp +++ b/openrouter/src/settings/telnet/enabled.cpp @@ -11,92 +11,94 @@ #define TELNET_REQUIRED_STRING R"({"required":"[token,enabled]"})" -void get_telnet(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_telnet(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - int telnet_enabled; - telnetfile >> telnet_enabled; - telnetfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_telnet(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["enabled"] == nullptr) { - response.set_content(TELNET_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); - std::string new_telnet_enabled = json_body["enabled"]; - if (new_telnet_enabled == "0") { - system("rc stop telnet"); - system("rc del telnet"); - syslog(LOG_INFO, "Telnet is disabled"); - } else { - system("rc start telnet"); - system("rc add telnet"); - syslog(LOG_INFO, "Telnet is enabled"); + + 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; + } + std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); + int telnet_enabled; + telnetfile >> telnet_enabled; + telnetfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_telnet(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["enabled"] == nullptr) { + response.set_content(TELNET_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); + std::string new_telnet_enabled = json_body["enabled"]; + if (new_telnet_enabled == "0") { + system("rc stop telnet"); + system("rc del telnet"); + syslog(LOG_INFO, "Telnet is disabled"); + } else { + system("rc start telnet"); + system("rc add telnet"); + syslog(LOG_INFO, "Telnet is enabled"); + } + telnetfileout << stoi(new_telnet_enabled); + telnetfileout.close(); + std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); + int telnet_enabled; + telnetfile >> telnet_enabled; + telnetfile.close(); + std::stringstream responsedata; + responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - telnetfileout << stoi(new_telnet_enabled); - telnetfileout.close(); - std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - int telnet_enabled; - telnetfile >> telnet_enabled; - telnetfile.close(); - std::stringstream responsedata; - responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/telnet/port.cpp b/openrouter/src/settings/telnet/port.cpp index 80a0605..1224780 100644 --- a/openrouter/src/settings/telnet/port.cpp +++ b/openrouter/src/settings/telnet/port.cpp @@ -11,86 +11,88 @@ #define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" -void get_telnet_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_telnet_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - int telnet_port; - telnetportfile >> telnet_port; - telnetportfile.close(); - std::stringstream responsedata; - responsedata << R"({"port":")" << telnet_port << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_telnet_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["port"] == nullptr) { - response.set_content(TELNET_PORT_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); - std::string new_telnet_port = json_body["port"]; - telnetportfileout << stoi(new_telnet_port); - telnetportfileout.close(); - system("rc restart telnet"); - std::string syslogstring = "Telnet port changed to " + new_telnet_port; - syslog(LOG_INFO, syslogstring.c_str()); - std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - int telnet_port; - telnetportfile >> telnet_port; - telnetportfile.close(); - std::stringstream responsedata; - responsedata << R"({"port":")" << telnet_port << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); + + 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; + } + std::ifstream telnetportfile("/etc/openrouter/telnet/port"); + int telnet_port; + telnetportfile >> telnet_port; + telnetportfile.close(); + std::stringstream responsedata; + responsedata << R"({"port":")" << telnet_port << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_telnet_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["port"] == nullptr) { + response.set_content(TELNET_PORT_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); + std::string new_telnet_port = json_body["port"]; + telnetportfileout << stoi(new_telnet_port); + telnetportfileout.close(); + system("rc restart telnet"); + std::string syslogstring = "Telnet port changed to " + new_telnet_port; + syslog(LOG_INFO, syslogstring.c_str()); + std::ifstream telnetportfile("/etc/openrouter/telnet/port"); + int telnet_port; + telnetportfile >> telnet_port; + telnetportfile.close(); + std::stringstream responsedata; + responsedata << R"({"port":")" << telnet_port << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } } } diff --git a/openrouter/src/settings/telnet/telnet.hpp b/openrouter/src/settings/telnet/telnet.hpp index dccc2c2..09149bb 100644 --- a/openrouter/src/settings/telnet/telnet.hpp +++ b/openrouter/src/settings/telnet/telnet.hpp @@ -3,10 +3,12 @@ #include -void get_telnet(const httplib::Request &request, httplib::Response &response); -void set_telnet(const httplib::Request &request, httplib::Response &response); +namespace settings { + void get_telnet(const httplib::Request &request, httplib::Response &response); + void set_telnet(const httplib::Request &request, httplib::Response &response); -void get_telnet_port(const httplib::Request &request, httplib::Response &response); -void set_telnet_port(const httplib::Request &request, httplib::Response &response); + void get_telnet_port(const httplib::Request &request, httplib::Response &response); + void set_telnet_port(const httplib::Request &request, httplib::Response &response); +} #endif // SETTINGS_TELNET_HPP diff --git a/openrouter/src/settings/wifi/channel.cpp b/openrouter/src/settings/wifi/channel.cpp index cb0a341..7e3e457 100644 --- a/openrouter/src/settings/wifi/channel.cpp +++ b/openrouter/src/settings/wifi/channel.cpp @@ -11,113 +11,115 @@ #define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})" -void get_wifi_channel(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_wifi_channel(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_channel; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("channel=") == 0) { - wifi_channel = wifisettingsbuffer; - wifi_channel.erase(0, 8); - break; - } - } - wifisettingsfile.close(); - std::stringstream responsedata; - responsedata << R"({"channel":")" << wifi_channel << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_wifi_channel(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["channel"] == nullptr) { - response.set_content(WIFI_CHANNEL_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string old_channel; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("channel=") == 0) { - old_channel = wifisettingsbuffer; - old_channel.erase(0, 8); - break; - } - } - wifisettingsfile.close(); - std::string new_channel = json_body["channel"]; - std::stringstream systemrequest; - systemrequest << "sed -i \"/channel=" << old_channel << "/s//channel=" << new_channel << "/\" /etc/hostapd/hostapd.conf"; - std::cout << systemrequest.str() << std::endl; - system(systemrequest.str().c_str()); - system("rc restart hostapd"); - std::string syslogstring = "WIFI channel changed to " + new_channel; - syslog(LOG_INFO, syslogstring.c_str()); - std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_channel; - std::string changedwifisettingsbuffer; - while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) { - if (changedwifisettingsbuffer.find("channel=") == 0) { - wifi_channel = changedwifisettingsbuffer; - wifi_channel.erase(0, 8); - break; + 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; } + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_channel; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("channel=") == 0) { + wifi_channel = wifisettingsbuffer; + wifi_channel.erase(0, 8); + break; + } + } + wifisettingsfile.close(); + std::stringstream responsedata; + responsedata << R"({"channel":")" << wifi_channel << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_wifi_channel(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["channel"] == nullptr) { + response.set_content(WIFI_CHANNEL_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string old_channel; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("channel=") == 0) { + old_channel = wifisettingsbuffer; + old_channel.erase(0, 8); + break; + } + } + wifisettingsfile.close(); + std::string new_channel = json_body["channel"]; + std::stringstream systemrequest; + systemrequest << "sed -i \"/channel=" << old_channel << "/s//channel=" << new_channel << "/\" /etc/hostapd/hostapd.conf"; + std::cout << systemrequest.str() << std::endl; + system(systemrequest.str().c_str()); + system("rc restart hostapd"); + std::string syslogstring = "WIFI channel changed to " + new_channel; + syslog(LOG_INFO, syslogstring.c_str()); + + std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_channel; + std::string changedwifisettingsbuffer; + while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) { + if (changedwifisettingsbuffer.find("channel=") == 0) { + wifi_channel = changedwifisettingsbuffer; + wifi_channel.erase(0, 8); + break; + } + } + changedwifisettingsfile.close(); + std::stringstream responsedata; + responsedata << R"({"channel":")" << wifi_channel << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - changedwifisettingsfile.close(); - std::stringstream responsedata; - responsedata << R"({"channel":")" << wifi_channel << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/name.cpp b/openrouter/src/settings/wifi/name.cpp index cadf8dc..e7e7f53 100644 --- a/openrouter/src/settings/wifi/name.cpp +++ b/openrouter/src/settings/wifi/name.cpp @@ -11,113 +11,115 @@ #define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})" -void get_wifi_name(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_wifi_name(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_name; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("ssid=") == 0) { - wifi_name = wifisettingsbuffer; - wifi_name.erase(0, 5); - break; - } - } - wifisettingsfile.close(); - std::stringstream responsedata; - responsedata << R"({"name":")" << wifi_name << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_wifi_name(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["name"] == nullptr) { - response.set_content(WIFI_NAME_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string old_wifi_name; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("ssid=") == 0) { - old_wifi_name = wifisettingsbuffer; - old_wifi_name.erase(0, 5); - break; - } - } - wifisettingsfile.close(); - std::string new_wifi_name = json_body["name"]; - std::stringstream systemrequest; - systemrequest << "sed -i \"/ssid=" << old_wifi_name << "/s//ssid=" << new_wifi_name << "/\" /etc/hostapd/hostapd.conf"; - std::cout << systemrequest.str() << std::endl; - system(systemrequest.str().c_str()); - system("rc restart hostapd"); - std::string syslogstring = "WIFI name changed to " + new_wifi_name; - syslog(LOG_INFO, syslogstring.c_str()); - std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_name; - std::string changedwifisettingsbuffer; - while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) { - if (changedwifisettingsbuffer.find("ssid=") == 0) { - wifi_name = changedwifisettingsbuffer; - wifi_name.erase(0, 5); - break; + 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; } + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_name; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("ssid=") == 0) { + wifi_name = wifisettingsbuffer; + wifi_name.erase(0, 5); + break; + } + } + wifisettingsfile.close(); + std::stringstream responsedata; + responsedata << R"({"name":")" << wifi_name << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - changedwifisettingsfile.close(); - std::stringstream responsedata; - responsedata << R"({"name":")" << wifi_name << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } -} + + void set_wifi_name(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["name"] == nullptr) { + response.set_content(WIFI_NAME_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string old_wifi_name; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("ssid=") == 0) { + old_wifi_name = wifisettingsbuffer; + old_wifi_name.erase(0, 5); + break; + } + } + wifisettingsfile.close(); + std::string new_wifi_name = json_body["name"]; + std::stringstream systemrequest; + systemrequest << "sed -i \"/ssid=" << old_wifi_name << "/s//ssid=" << new_wifi_name << "/\" /etc/hostapd/hostapd.conf"; + std::cout << systemrequest.str() << std::endl; + system(systemrequest.str().c_str()); + system("rc restart hostapd"); + std::string syslogstring = "WIFI name changed to " + new_wifi_name; + syslog(LOG_INFO, syslogstring.c_str()); + + std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_name; + std::string changedwifisettingsbuffer; + while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) { + if (changedwifisettingsbuffer.find("ssid=") == 0) { + wifi_name = changedwifisettingsbuffer; + wifi_name.erase(0, 5); + break; + } + } + changedwifisettingsfile.close(); + std::stringstream responsedata; + responsedata << R"({"name":")" << wifi_name << R"("})"; + response.set_content(responsedata.str(), 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 674ea8b..50f1008 100644 --- a/openrouter/src/settings/wifi/password.cpp +++ b/openrouter/src/settings/wifi/password.cpp @@ -11,110 +11,112 @@ #define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})" -void get_wifi_password(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_wifi_password(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { - wifi_password = wifisettingsbuffer; - wifi_password.erase(0, 15); - break; - } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; } - wifisettingsfile.close(); - std::stringstream responsedata; - responsedata << R"({"password":")" << wifi_password << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_wifi_password(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } + httplib::Client cli(AUTH_SERVICE); - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } + std::stringstream body; + body << R"({"token":)" << json_body["token"] << R"(})"; - if (json_body["password"] == nullptr) { - response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE); - return; + 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; + } + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_password; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { + wifi_password = wifisettingsbuffer; + wifi_password.erase(0, 15); + break; + } + } + wifisettingsfile.close(); + std::stringstream responsedata; + responsedata << R"({"password":")" << wifi_password << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } } - if (json_body["newpassword"] == nullptr) { - response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE); - return; - } + void set_wifi_password(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } - httplib::Client cli(AUTH_SERVICE); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } - std::stringstream body; - body << R"({"token":)" << json_body["token"] << R"(})"; + if (json_body["password"] == nullptr) { + response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE); + return; + } - 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); + if (json_body["newpassword"] == nullptr) { + response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string old_wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { - old_wifi_password = wifisettingsbuffer; - old_wifi_password.erase(0, 15); - break; + + 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; } - } - wifisettingsfile.close(); - std::string req_old_password = json_body["password"]; - if (old_wifi_password != req_old_password) { - response.set_content(R"({"status":"reject"})", JSON_TYPE); - return; + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string old_wifi_password; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { + old_wifi_password = wifisettingsbuffer; + old_wifi_password.erase(0, 15); + break; + } + } + wifisettingsfile.close(); + std::string req_old_password = json_body["password"]; + if (old_wifi_password != req_old_password) { + response.set_content(R"({"status":"reject"})", JSON_TYPE); + return; + } + std::string new_wifi_password = json_body["newpassword"]; + std::stringstream systemrequest; + systemrequest << "sed -i \"/wpa_passphrase=" << old_wifi_password << "/s//wpa_passphrase=" << new_wifi_password << "/\" /etc/hostapd/hostapd.conf"; + system(systemrequest.str().c_str()); + system("rc restart hostapd"); + std::string syslogstring = "WIFI password changed"; + syslog(LOG_INFO, syslogstring.c_str()); + std::stringstream responsedata; + responsedata << R"({"success":"ok"})"; + response.set_content(responsedata.str(), JSON_TYPE); } - std::string new_wifi_password = json_body["newpassword"]; - std::stringstream systemrequest; - systemrequest << "sed -i \"/wpa_passphrase=" << old_wifi_password << "/s//wpa_passphrase=" << new_wifi_password << "/\" /etc/hostapd/hostapd.conf"; - system(systemrequest.str().c_str()); - system("rc restart hostapd"); - std::string syslogstring = "WIFI password changed"; - syslog(LOG_INFO, syslogstring.c_str()); - std::stringstream responsedata; - responsedata << R"({"success":"ok"})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/qr.cpp b/openrouter/src/settings/wifi/qr.cpp index c111a72..d9c3d7b 100644 --- a/openrouter/src/settings/wifi/qr.cpp +++ b/openrouter/src/settings/wifi/qr.cpp @@ -6,55 +6,57 @@ #include #include -void get_wifi_qr(const httplib::Request &request, httplib::Response &response) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_wifi_qr(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_name; - std::string wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) { - if (wifisettingsbuffer.find("ssid=") == 0) { - wifi_name = wifisettingsbuffer; - wifi_name.erase(0, 5); - } - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { - wifi_password = wifisettingsbuffer; - wifi_password.erase(0, 15); - break; - } + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; } - wifisettingsfile.close(); - std::stringstream toqrstring; - toqrstring << "WIFI:T:WPA;P:" << wifi_password << ";S:" << wifi_name << ";"; + httplib::Client cli(AUTH_SERVICE); - 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); + 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; + } + + std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + std::string wifi_name; + std::string wifi_password; + std::string wifisettingsbuffer; + while (std::getline(wifisettingsfile, wifisettingsbuffer)) { + if (wifisettingsbuffer.find("ssid=") == 0) { + wifi_name = wifisettingsbuffer; + wifi_name.erase(0, 5); + } + if (wifisettingsbuffer.find("wpa_passphrase=") == 0) { + wifi_password = wifisettingsbuffer; + wifi_password.erase(0, 15); + break; + } + } + wifisettingsfile.close(); + + std::stringstream toqrstring; + toqrstring << "WIFI:T:WPA;P:" << wifi_password << ";S:" << wifi_name << ";"; + + 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); + } } } diff --git a/openrouter/src/settings/wifi/status.cpp b/openrouter/src/settings/wifi/status.cpp index 660bd39..d1962dc 100644 --- a/openrouter/src/settings/wifi/status.cpp +++ b/openrouter/src/settings/wifi/status.cpp @@ -6,93 +6,94 @@ #include #define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})" - -void get_wifi_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - 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); +namespace settings { + void get_wifi_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); - int wifi_status; - wifistatusfile >> wifi_status; - wifistatusfile.close(); - std::stringstream responsedata; - responsedata << R"({"status":")" << wifi_status << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } -} -void set_wifi_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", "*"); - if (request.body.empty()) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - nlohmann::json json_body = nlohmann::json::parse(request.body); - if (json_body["token"] == nullptr) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (json_body["status"] == nullptr) { - response.set_content(WIFI_REQUIRED_STRING, JSON_TYPE); - 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); + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); return; } - std::ofstream wififileout("/etc/openrouter/wifi/status"); - std::string new_wifi_status = json_body["status"]; - if (new_wifi_status == "0") { - system("rc stop hostapd"); - system("rc del hostapd"); - syslog(LOG_INFO, "WIFI is disabled"); - } else { - system("rc start hostapd"); - system("rc add hostapd"); - syslog(LOG_INFO, "WIFI is enabled"); + + 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; + } + std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); + int wifi_status; + wifistatusfile >> wifi_status; + wifistatusfile.close(); + std::stringstream responsedata; + responsedata << R"({"status":")" << wifi_status << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } + + void set_wifi_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", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (json_body["status"] == nullptr) { + response.set_content(WIFI_REQUIRED_STRING, JSON_TYPE); + 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; + } + std::ofstream wififileout("/etc/openrouter/wifi/status"); + std::string new_wifi_status = json_body["status"]; + if (new_wifi_status == "0") { + system("rc stop hostapd"); + system("rc del hostapd"); + syslog(LOG_INFO, "WIFI is disabled"); + } else { + system("rc start hostapd"); + system("rc add hostapd"); + syslog(LOG_INFO, "WIFI is enabled"); + } + wififileout << stoi(new_wifi_status); + wififileout.close(); + std::ifstream wififile("/etc/openrouter/wifi/status"); + int wifi_status; + wififile >> wifi_status; + wififile.close(); + std::stringstream responsedata; + responsedata << R"({"status":")" << wifi_status << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); } - wififileout << stoi(new_wifi_status); - wififileout.close(); - std::ifstream wififile("/etc/openrouter/wifi/status"); - int wifi_status; - wififile >> wifi_status; - wififile.close(); - std::stringstream responsedata; - responsedata << R"({"status":")" << wifi_status << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } diff --git a/openrouter/src/settings/wifi/wifi.hpp b/openrouter/src/settings/wifi/wifi.hpp index ca01575..5df8182 100644 --- a/openrouter/src/settings/wifi/wifi.hpp +++ b/openrouter/src/settings/wifi/wifi.hpp @@ -5,18 +5,20 @@ #define WIFI_SETTINGS_PATH "/etc/hostapd/hostapd.conf" -void get_wifi_status(const httplib::Request &request, httplib::Response &response); -void set_wifi_status(const httplib::Request &request, httplib::Response &response); +namespace settings { + void get_wifi_status(const httplib::Request &request, httplib::Response &response); + void set_wifi_status(const httplib::Request &request, httplib::Response &response); -void get_wifi_name(const httplib::Request &request, httplib::Response &response); -void set_wifi_name(const httplib::Request &request, httplib::Response &response); + void get_wifi_name(const httplib::Request &request, httplib::Response &response); + void set_wifi_name(const httplib::Request &request, httplib::Response &response); -void get_wifi_password(const httplib::Request &request, httplib::Response &response); -void set_wifi_password(const httplib::Request &request, httplib::Response &response); + void get_wifi_password(const httplib::Request &request, httplib::Response &response); + void set_wifi_password(const httplib::Request &request, httplib::Response &response); -void get_wifi_qr(const httplib::Request &request, httplib::Response &response); + void get_wifi_qr(const httplib::Request &request, httplib::Response &response); -void get_wifi_channel(const httplib::Request &request, httplib::Response &response); -void set_wifi_channel(const httplib::Request &request, httplib::Response &response); + void get_wifi_channel(const httplib::Request &request, httplib::Response &response); + void set_wifi_channel(const httplib::Request &request, httplib::Response &response); +} #endif // WIFI_HPP \ No newline at end of file diff --git a/openrouter/src/syslog/syslog.cpp b/openrouter/src/syslog/syslog.cpp deleted file mode 100644 index 1fad8a7..0000000 --- a/openrouter/src/syslog/syslog.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "syslog.hpp" -#include -#include "../types.hpp" -#include "../routes.hpp" -#include -#include -#include -#include -#include - -#define SYSLOG_FILE "/var/log/messages" - -void get_syslog(const httplib::Request &request, httplib::Response &response) { - std::stringstream responsedata; - std::ifstream syslogfile(SYSLOG_FILE); - responsedata << syslogfile.rdbuf(); - syslogfile.close(); - response.set_content(responsedata.str(), PLAIN_TEXT_TYPE); -} diff --git a/openrouter/src/syslog/syslog.hpp b/openrouter/src/syslog/syslog.hpp deleted file mode 100644 index fd676c0..0000000 --- a/openrouter/src/syslog/syslog.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SYSLOG_HPP -#define SYSLOG_HPP - -#include - -void get_syslog(const httplib::Request &request, httplib::Response &response); - -#endif //SYSLOG_HPP \ No newline at end of file diff --git a/openrouter/src/system/reboot/reboot.cpp b/openrouter/src/system/reboot/reboot.cpp new file mode 100644 index 0000000..7f79615 --- /dev/null +++ b/openrouter/src/system/reboot/reboot.cpp @@ -0,0 +1,37 @@ +#include "reboot.hpp" +#include +#include "../routes.hpp" +#include +#include "../types.hpp" + +namespace systemfuncs { + void reboot(const httplib::Request &request, httplib::Response &response) { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + if (request.body.empty()) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + + nlohmann::json json_body = nlohmann::json::parse(request.body); + if (json_body["token"] == nullptr) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + 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; + } + system("reboot"); + } + } +} diff --git a/openrouter/src/system/reboot/reboot.hpp b/openrouter/src/system/reboot/reboot.hpp new file mode 100644 index 0000000..b5abe1f --- /dev/null +++ b/openrouter/src/system/reboot/reboot.hpp @@ -0,0 +1,9 @@ +#ifndef REBOOT_HPP +#define REBOOT_HPP + +#include +namespace systemfuncs { + void reboot(const httplib::Request &request, httplib::Response &response); +} + +#endif // REBOOT_HPP diff --git a/openrouter/src/system/syslog/syslog.cpp b/openrouter/src/system/syslog/syslog.cpp new file mode 100644 index 0000000..4480485 --- /dev/null +++ b/openrouter/src/system/syslog/syslog.cpp @@ -0,0 +1,20 @@ +#include "syslog.hpp" +#include +#include "../types.hpp" +#include "../routes.hpp" +#include +#include +#include +#include +#include + +#define SYSLOG_FILE "/var/log/messages" +namespace systemfuncs { + void get_syslog(const httplib::Request &request, httplib::Response &response) { + std::stringstream responsedata; + std::ifstream syslogfile(SYSLOG_FILE); + responsedata << syslogfile.rdbuf(); + syslogfile.close(); + response.set_content(responsedata.str(), PLAIN_TEXT_TYPE); + } +} diff --git a/openrouter/src/system/syslog/syslog.hpp b/openrouter/src/system/syslog/syslog.hpp new file mode 100644 index 0000000..9d7901a --- /dev/null +++ b/openrouter/src/system/syslog/syslog.hpp @@ -0,0 +1,10 @@ +#ifndef SYSLOG_HPP +#define SYSLOG_HPP + +#include + +namespace systemfuncs { + void get_syslog(const httplib::Request &request, httplib::Response &response); +} + +#endif //SYSLOG_HPP \ No newline at end of file diff --git a/openrouter/src/system/system.hpp b/openrouter/src/system/system.hpp new file mode 100644 index 0000000..a14f4e0 --- /dev/null +++ b/openrouter/src/system/system.hpp @@ -0,0 +1,8 @@ +#ifndef SYSTEM_HPP +#define SYSTEM_HPP + +#include "uptime/uptime.hpp" +#include "syslog/syslog.hpp" +#include "reboot/reboot.hpp" + +#endif // SYSTEM_HPP \ No newline at end of file diff --git a/openrouter/src/system/uptime/uptime.cpp b/openrouter/src/system/uptime/uptime.cpp new file mode 100644 index 0000000..fdd3cac --- /dev/null +++ b/openrouter/src/system/uptime/uptime.cpp @@ -0,0 +1,28 @@ +#include "uptime.hpp" +#include +#include +#include "../types.hpp" + +namespace systemfuncs { + void get_uptime(const httplib::Request &request, httplib::Response &response) { + struct sysinfo x; + if (sysinfo(&x) == 0) + { + // std::cout << static_cast(x.uptime)/3600ULL << "h " + // << static_cast(x.uptime)/60ULL%60ULL << "m " + // << static_cast(x.uptime)%60ULL << "s " + // << std::endl; + std::stringstream responsedata; + responsedata << R"({"uptime":{"hour":")" + << static_cast(x.uptime)/3600ULL + << R"(","minute":")" + << static_cast(x.uptime)/60ULL%60ULL + << R"(","second":")" + << static_cast(x.uptime)%60ULL + << R"("}})"; + response.set_content(responsedata.str(), JSON_TYPE); + return; + } + response.set_content(R"({"error":"unknown"})", JSON_TYPE); + } +} diff --git a/openrouter/src/system/uptime/uptime.hpp b/openrouter/src/system/uptime/uptime.hpp new file mode 100644 index 0000000..72d9759 --- /dev/null +++ b/openrouter/src/system/uptime/uptime.hpp @@ -0,0 +1,10 @@ +#ifndef UPTIME_HPP +#define UPTIME_HPP + +#include + +namespace systemfuncs { + void get_uptime(const httplib::Request &request, httplib::Response &response); +} + +#endif // UPTIME_HPP \ No newline at end of file diff --git a/openrouter/src/uptime/uptime.cpp b/openrouter/src/uptime/uptime.cpp deleted file mode 100644 index 066c00f..0000000 --- a/openrouter/src/uptime/uptime.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "uptime.hpp" -#include -#include -#include "../types.hpp" - -void get_uptime(const httplib::Request &request, httplib::Response &response) { - struct sysinfo x; - if (sysinfo(&x) == 0) - { - // std::cout << static_cast(x.uptime)/3600ULL << "h " - // << static_cast(x.uptime)/60ULL%60ULL << "m " - // << static_cast(x.uptime)%60ULL << "s " - // << std::endl; - std::stringstream responsedata; - responsedata << R"({"uptime":{"hour":")" - << static_cast(x.uptime)/3600ULL - << R"(","minute":")" - << static_cast(x.uptime)/60ULL%60ULL - << R"(","second":")" - << static_cast(x.uptime)%60ULL - << R"("}})"; - response.set_content(responsedata.str(), JSON_TYPE); - return; - } - response.set_content(R"({"error":"unknown"})", JSON_TYPE); -} diff --git a/openrouter/src/uptime/uptime.hpp b/openrouter/src/uptime/uptime.hpp deleted file mode 100644 index fc532a0..0000000 --- a/openrouter/src/uptime/uptime.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef UPTIME_HPP -#define UPTIME_HPP - -#include - -void get_uptime(const httplib::Request &request, httplib::Response &response); - -#endif // UPTIME_HPP \ No newline at end of file