diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a811f6..e6448a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 2.6) +cmake_minimum_required (VERSION 3.6) project(openrouter) set(CMAKE_CXX_STANDARD 20) @@ -33,10 +33,10 @@ add_executable( src/settings/ftp/ftp.cpp - src/system/reboot/reboot.cpp - src/system/syslog/syslog.cpp - src/system/uptime/uptime.cpp - src/system/update/update.cpp + src/system/reboot.cpp + src/system/syslog.cpp + src/system/uptime.cpp + src/system/update.cpp src/OpenRouter/OpenRouter.cpp diff --git a/Makefile b/Makefile index 2e9212b..6d4d4e9 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,10 @@ SOURCES=src/openrouter.cpp \ src/settings/wifi/qr.cpp \ src/settings/wifi/channel.cpp \ src/settings/ftp/ftp.cpp \ - src/system/reboot/reboot.cpp \ - src/system/syslog/syslog.cpp \ - src/system/uptime/uptime.cpp \ - src/system/update/update.cpp \ + src/system/reboot.cpp \ + src/system/syslog.cpp \ + src/system/uptime.cpp \ + src/system/update.cpp \ src/OpenRouter/OpenRouter.cpp \ src/auth/auth.cpp \ src/libs/QR-Code-generator/qrcodegen.cpp \ diff --git a/meson.build b/meson.build index 15b0285..320fef1 100644 --- a/meson.build +++ b/meson.build @@ -32,10 +32,10 @@ executable( 'src/settings/ftp/ftp.cpp', - 'src/system/reboot/reboot.cpp', - 'src/system/syslog/syslog.cpp', - 'src/system/uptime/uptime.cpp', - 'src/system/update/update.cpp', + 'src/system/reboot.cpp', + 'src/system/syslog.cpp', + 'src/system/uptime.cpp', + 'src/system/update.cpp', 'src/OpenRouter/OpenRouter.cpp', 'src/auth/auth.cpp', diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index 85f8d73..ecd01d1 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -6,41 +6,44 @@ #include #include -OpenRouter::OpenRouter() +namespace OpenRouter { - this->srv.Post("/api/settings/wifi/password/set", api::settings::wifi::set_wifi_password); - this->srv.Post("/api/settings/wifi/qr/get", api::settings::wifi::get_wifi_qr); + OpenRouter::OpenRouter() + { + this->srv.Post("/api/settings/wifi/password/set", api::settings::wifi::Wifi::set_wifi_password); + this->srv.Post("/api/settings/wifi/qr/get", api::settings::wifi::Wifi::get_wifi_qr); - this->srv.Post("/api/settings/get", api::settings::get); - this->srv.Post("/api/settings/set", api::settings::set); - this->srv.Get("/api/settings/backup", api::settings::backup); - this->srv.Post("/api/settings/restore", api::settings::restore); - this->srv.Post("/api/settings/restore/defaults", api::settings::restore_defaults); + this->srv.Post("/api/settings/get", api::settings::Settings::get); + this->srv.Post("/api/settings/set", api::settings::Settings::set); + this->srv.Get("/api/settings/backup", api::settings::Settings::backup); + this->srv.Post("/api/settings/restore", api::settings::Settings::restore); + this->srv.Post("/api/settings/restore/defaults", api::settings::Settings::restore_defaults); - this->srv.Post("/api/system/reboot", api::system::reboot); - this->srv.Get("/api/system/syslog", api::system::get_syslog); + this->srv.Post("/api/system/reboot", api::system::System::reboot); + this->srv.Get("/api/system/syslog", api::system::System::get_syslog); #ifdef __linux__ - this->srv.Get("/api/system/uptime", api::system::get_uptime); + this->srv.Get("/api/system/uptime", api::system::System::get_uptime); #endif // __linux__ - this->srv.Post("/api/system/update/upload", api::system::update::upload); - this->srv.Post("/api/system/update/update", api::system::update::update); - this->srv.Get("/api/system/update/progress", api::system::update::progress); + this->srv.Post("/api/system/update/upload", api::system::System::upload); + this->srv.Post("/api/system/update/update", api::system::System::update); + this->srv.Get("/api/system/update/progress", api::system::System::progress); - this->srv.Options("/(.*)", api::security::set_cors_headers); + this->srv.Options("/(.*)", api::security::set_cors_headers); - this->srv.set_mount_point("/", config::config.getConfig()["static"]["root"]); - this->srv.set_mount_point("/js", config::config.getConfig()["static"]["js"]); - this->srv.set_mount_point("/css", config::config.getConfig()["static"]["css"]); -} + this->srv.set_mount_point("/", config::config.getConfig()["static"]["root"]); + this->srv.set_mount_point("/js", config::config.getConfig()["static"]["js"]); + this->srv.set_mount_point("/css", config::config.getConfig()["static"]["css"]); + } -OpenRouter::~OpenRouter() = default; + OpenRouter::~OpenRouter() = default; -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; + 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."); - this->srv.listen(ip, port, socket_flags); - return 0; + syslog(LOG_INFO, "openrouter administration software started."); + this->srv.listen(ip, port, socket_flags); + return 0; + } } diff --git a/src/OpenRouter/OpenRouter.hpp b/src/OpenRouter/OpenRouter.hpp index e2f5479..14401b4 100644 --- a/src/OpenRouter/OpenRouter.hpp +++ b/src/OpenRouter/OpenRouter.hpp @@ -4,13 +4,16 @@ #define CORS_ENABLE -class OpenRouter +namespace OpenRouter { -private: - httplib::Server srv; + class OpenRouter + { + private: + httplib::Server srv; -public: - OpenRouter(); - ~OpenRouter(); - int serve(const std::string& ip, int port, int socket_flags = 0); -}; + public: + OpenRouter(); + ~OpenRouter(); + int serve(const std::string& ip, int port, int socket_flags = 0); + }; +} diff --git a/src/openrouter.cpp b/src/openrouter.cpp index 04aef39..3af5888 100644 --- a/src/openrouter.cpp +++ b/src/openrouter.cpp @@ -3,10 +3,13 @@ int main() { - OpenRouter router; + OpenRouter::OpenRouter router; #ifdef TEST return 0; #else - return router.serve(config::config.getConfig()["server"]["ip"], config::config.getConfig()["server"]["port"]); + return router.serve( + config::config.getConfig()["server"]["ip"], + config::config.getConfig()["server"]["port"] + ); #endif } diff --git a/src/security/cors.cpp b/src/security/cors.cpp index a637a1a..fd1562f 100755 --- a/src/security/cors.cpp +++ b/src/security/cors.cpp @@ -7,7 +7,7 @@ namespace api { void set_cors_headers(const httplib::Request& request, httplib::Response& response) { - std::cout << GREEN << request.path << " " << std::endl; + std::cout << GREEN << request.path << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); response.set_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS"); response.set_header("Access-Control-Allow-Headers", "Authorization,X-Custom,Content-Type"); diff --git a/src/settings/backup.cpp b/src/settings/backup.cpp index add1cc8..ac7cec9 100644 --- a/src/settings/backup.cpp +++ b/src/settings/backup.cpp @@ -9,7 +9,7 @@ namespace api { namespace settings { - void backup(const httplib::Request& request, httplib::Response& response) + void Settings::backup(const httplib::Request& request, httplib::Response& response) { std::ifstream backup_file(config::config.getConfigFileName()); std::stringstream backup_file_buffer; @@ -18,7 +18,7 @@ namespace api response.set_content(backup_file_buffer.str(), JSON_TYPE); } - void restore(const httplib::Request& request, httplib::Response& response) + void Settings::restore(const httplib::Request& request, httplib::Response& response) { const httplib::MultipartFormData& file = request.get_file_value("restore_settings"); if (file.content_type == JSON_TYPE) { @@ -34,7 +34,7 @@ namespace api response.set_content(R"({"status":false})", JSON_TYPE); } - void restore_defaults(const httplib::Request& request, httplib::Response& response) + void Settings::restore_defaults(const httplib::Request& request, httplib::Response& response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); diff --git a/src/settings/dns/dns.hpp b/src/settings/dns/dns.hpp index 0f73f27..78042e7 100644 --- a/src/settings/dns/dns.hpp +++ b/src/settings/dns/dns.hpp @@ -7,9 +7,12 @@ namespace api { namespace dns { - void set_dns(bool new_dns_enabled); - void set_dns_servers(nlohmann::json servers); - void set_local_domains(nlohmann::json domains); + class Dns { + public: + static void set_dns(bool new_dns_enabled); + static void set_dns_servers(nlohmann::json servers); + static void set_local_domains(nlohmann::json domains); + }; } } } diff --git a/src/settings/dns/enabled.cpp b/src/settings/dns/enabled.cpp index 2c19ffe..e562e78 100644 --- a/src/settings/dns/enabled.cpp +++ b/src/settings/dns/enabled.cpp @@ -7,7 +7,7 @@ namespace api { namespace dns { - void set_dns(bool new_dns_enabled) + void Dns::set_dns(bool new_dns_enabled) { if (!new_dns_enabled) { diff --git a/src/settings/dns/local_domains.cpp b/src/settings/dns/local_domains.cpp index 7e889b9..2a4c9bd 100644 --- a/src/settings/dns/local_domains.cpp +++ b/src/settings/dns/local_domains.cpp @@ -8,7 +8,7 @@ namespace api { namespace dns { - void set_local_domains(nlohmann::json domains) + void Dns::set_local_domains(nlohmann::json domains) { std::ofstream domainsfile("/etc/dnsmasq.conf.d/hosts"); for (nlohmann::json_abi_v3_11_3::json domain : domains) diff --git a/src/settings/dns/servers.cpp b/src/settings/dns/servers.cpp index a3347f5..733fbdd 100644 --- a/src/settings/dns/servers.cpp +++ b/src/settings/dns/servers.cpp @@ -8,7 +8,7 @@ namespace api { namespace dns { - void set_dns_servers(nlohmann::json servers) + void Dns::set_dns_servers(nlohmann::json servers) { std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); for (const nlohmann::json_abi_v3_11_3::json& server : servers) diff --git a/src/settings/ftp/ftp.cpp b/src/settings/ftp/ftp.cpp index 4edfb1f..567b08a 100644 --- a/src/settings/ftp/ftp.cpp +++ b/src/settings/ftp/ftp.cpp @@ -10,7 +10,7 @@ namespace api { namespace ftp { - void set_ftp(bool new_ftp_enabled) + void Ftp::set_ftp(bool new_ftp_enabled) { if (!new_ftp_enabled) { diff --git a/src/settings/ftp/ftp.hpp b/src/settings/ftp/ftp.hpp index 8d55032..d28e694 100644 --- a/src/settings/ftp/ftp.hpp +++ b/src/settings/ftp/ftp.hpp @@ -5,7 +5,10 @@ namespace api { namespace ftp { - void set_ftp(bool new_ftp_enabled); + class Ftp { + public: + static void set_ftp(bool new_ftp_enabled); + }; } } } diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp index 56f2b4c..d179baa 100644 --- a/src/settings/settings.cpp +++ b/src/settings/settings.cpp @@ -13,31 +13,31 @@ namespace api { namespace settings { - void set_all_settings(nlohmann::json json_body) + void Settings::set_all_settings(nlohmann::json json_body) { - api::settings::wifi::set_wifi_status(json_body["settings"]["wifi"]["enabled"]); + api::settings::wifi::Wifi::set_wifi_status(json_body["settings"]["wifi"]["enabled"]); std::string wifichannel = json_body["settings"]["wifi"]["channel"]; - api::settings::wifi::set_wifi_channel(std::stoi(wifichannel)); - api::settings::wifi::set_wifi_name(json_body["settings"]["wifi"]["name"]); + api::settings::wifi::Wifi::set_wifi_channel(std::stoi(wifichannel)); + api::settings::wifi::Wifi::set_wifi_name(json_body["settings"]["wifi"]["name"]); - api::settings::telnet::set_telnet(json_body["settings"]["telnet"]["enabled"]); + api::settings::telnet::Telnet::set_telnet(json_body["settings"]["telnet"]["enabled"]); std::string telnetport = json_body["settings"]["telnet"]["port"]; - api::settings::telnet::set_telnet_port(std::stoi(telnetport)); + api::settings::telnet::Telnet::set_telnet_port(std::stoi(telnetport)); - api::settings::ssh::set_ssh_status(json_body["settings"]["ssh"]["enabled"]); - api::settings::ssh::set_ssh_server(json_body["settings"]["ssh"]["server"]); + api::settings::ssh::Ssh::set_ssh_status(json_body["settings"]["ssh"]["enabled"]); + api::settings::ssh::Ssh::set_ssh_server(json_body["settings"]["ssh"]["server"]); std::string sshport = json_body["settings"]["ssh"]["port"]; - api::settings::ssh::set_ssh_port(std::stoi(sshport)); - api::settings::ssh::set_ssh_keys(json_body["settings"]["ssh"]["keys"]); + api::settings::ssh::Ssh::set_ssh_port(std::stoi(sshport)); + api::settings::ssh::Ssh::set_ssh_keys(json_body["settings"]["ssh"]["keys"]); - api::settings::ftp::set_ftp(json_body["settings"]["ftp"]["enabled"]); + api::settings::ftp::Ftp::set_ftp(json_body["settings"]["ftp"]["enabled"]); - api::settings::dns::set_dns(json_body["settings"]["dns"]["enabled"]); - api::settings::dns::set_dns_servers(json_body["settings"]["dns"]["servers"]); - api::settings::dns::set_local_domains(json_body["settings"]["dns"]["localdomains"]); + api::settings::dns::Dns::set_dns(json_body["settings"]["dns"]["enabled"]); + api::settings::dns::Dns::set_dns_servers(json_body["settings"]["dns"]["servers"]); + api::settings::dns::Dns::set_local_domains(json_body["settings"]["dns"]["localdomains"]); } - void get(const httplib::Request& request, httplib::Response& response) + void Settings::get(const httplib::Request& request, httplib::Response& response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); @@ -53,7 +53,7 @@ namespace api response.set_content(responsedata, JSON_TYPE); } - void set(const httplib::Request& request, httplib::Response& response) + void Settings::set(const httplib::Request& request, httplib::Response& response) { std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; response.set_header("Access-Control-Allow-Origin", "*"); diff --git a/src/settings/settings.hpp b/src/settings/settings.hpp index 23eb8bd..64c3334 100644 --- a/src/settings/settings.hpp +++ b/src/settings/settings.hpp @@ -9,11 +9,15 @@ namespace api { namespace settings { - void set_all_settings(nlohmann::json json_body); - void backup(const httplib::Request& request, httplib::Response& response); - void restore(const httplib::Request& request, httplib::Response& response); - void restore_defaults(const httplib::Request& request, httplib::Response& response); - void get(const httplib::Request& request, httplib::Response& response); - void set(const httplib::Request& request, httplib::Response& response); + class Settings { + private: + static void set_all_settings(nlohmann::json json_body); + public: + static void backup(const httplib::Request& request, httplib::Response& response); + static void restore(const httplib::Request& request, httplib::Response& response); + static void restore_defaults(const httplib::Request& request, httplib::Response& response); + static void get(const httplib::Request& request, httplib::Response& response); + static void set(const httplib::Request& request, httplib::Response& response); + }; } } diff --git a/src/settings/ssh/keys.cpp b/src/settings/ssh/keys.cpp index 9c31d8e..8f2b90e 100644 --- a/src/settings/ssh/keys.cpp +++ b/src/settings/ssh/keys.cpp @@ -13,19 +13,20 @@ namespace api { namespace ssh { - void set_ssh_keys(nlohmann::json keys) + void Ssh::set_ssh_keys(nlohmann::json keys) { std::ofstream keysfile(KEYS_PATH); + std::string ssh_server = get_ssh_server(); for (std::string key : keys) { keysfile << key << "\n"; } keysfile.close(); - if (get_ssh_server() == OPENSSH_SSH) + if (ssh_server == OPENSSH_SSH) { system("rc restart ssh"); } - else if (get_ssh_server() == DROPBEAR_SSH) + else if (ssh_server == DROPBEAR_SSH) { system("rc restart dropbear"); } diff --git a/src/settings/ssh/port.cpp b/src/settings/ssh/port.cpp index c3b0cbd..c02ffbb 100644 --- a/src/settings/ssh/port.cpp +++ b/src/settings/ssh/port.cpp @@ -15,12 +15,13 @@ namespace api { namespace ssh { - void set_ssh_port(int new_ssh_port) + void Ssh::set_ssh_port(int new_ssh_port) { std::string old_ssh_port_string = config::config.getConfig()["settings"]["ssh"]["port"]; int old_ssh_port = std::stoi(old_ssh_port_string); std::string systemrequest; - if (get_ssh_server() == DROPBEAR_SSH) + std::string ssh_server = get_ssh_server(); + if (ssh_server == DROPBEAR_SSH) { systemrequest += "sed -i \"s/DROPBEAR_PORT=" \ + std::to_string(old_ssh_port) \ @@ -29,7 +30,7 @@ namespace api + "/g\" " \ + DROPBEAR_DEFAULT_FILE; } - else if (get_ssh_server() == OPENSSH_SSH) + else if (ssh_server == OPENSSH_SSH) { systemrequest += "sed -i \"/Port " \ + std::to_string(old_ssh_port) \ @@ -40,11 +41,11 @@ namespace api } std::cout << systemrequest << std::endl; system(systemrequest.c_str()); - if (get_ssh_server() == OPENSSH_SSH) + if (ssh_server == OPENSSH_SSH) { system("rc restart ssh"); } - else if (get_ssh_server() == DROPBEAR_SSH) + else if (ssh_server == DROPBEAR_SSH) { system("rc restart dropbear"); } diff --git a/src/settings/ssh/server.cpp b/src/settings/ssh/server.cpp index 8989bda..48de980 100644 --- a/src/settings/ssh/server.cpp +++ b/src/settings/ssh/server.cpp @@ -9,13 +9,13 @@ namespace api { namespace ssh { - std::string get_ssh_server() + std::string Ssh::get_ssh_server() { config::config.setConfig(config::config.getConfigFileName()); return config::config.getConfig()["settings"]["ssh"]["server"]; } - void set_ssh_server(std::string new_server) + void Ssh::set_ssh_server(std::string new_server) { if (new_server == DROPBEAR_SSH) { diff --git a/src/settings/ssh/ssh.hpp b/src/settings/ssh/ssh.hpp index cad9d15..84e0849 100644 --- a/src/settings/ssh/ssh.hpp +++ b/src/settings/ssh/ssh.hpp @@ -10,11 +10,15 @@ namespace api { namespace ssh { - std::string get_ssh_server(); - void set_ssh_server(std::string new_server); - void set_ssh_status(bool new_ssh_status); - void set_ssh_keys(nlohmann::json keys); - void set_ssh_port(int port); + class Ssh { + private: + static std::string get_ssh_server(); + public: + static void set_ssh_server(std::string new_server); + static void set_ssh_status(bool new_ssh_status); + static void set_ssh_keys(nlohmann::json keys); + static void set_ssh_port(int port); + }; } } } diff --git a/src/settings/ssh/status.cpp b/src/settings/ssh/status.cpp index 2f2024f..5f8dd29 100644 --- a/src/settings/ssh/status.cpp +++ b/src/settings/ssh/status.cpp @@ -9,16 +9,17 @@ namespace api { namespace ssh { - void set_ssh_status(bool new_ssh_status) + void Ssh::set_ssh_status(bool new_ssh_status) { + std::string ssh_server = get_ssh_server(); if (!new_ssh_status) { - if (get_ssh_server() == DROPBEAR_SSH) + if (ssh_server == DROPBEAR_SSH) { system("rc stop dropbear"); system("rc del dropbear"); } - else if (get_ssh_server() == OPENSSH_SSH) { + else if (ssh_server == OPENSSH_SSH) { system("rc stop ssh"); system("rc del ssh"); } @@ -26,12 +27,12 @@ namespace api } else { - if (get_ssh_server() == DROPBEAR_SSH) + if (ssh_server == DROPBEAR_SSH) { system("rc start dropbear"); system("rc add dropbear"); } - else if (get_ssh_server() == OPENSSH_SSH) { + else if (ssh_server == OPENSSH_SSH) { system("rc start ssh"); system("rc add ssh"); } diff --git a/src/settings/telnet/enabled.cpp b/src/settings/telnet/enabled.cpp index 5d67ae4..6d6b7dc 100644 --- a/src/settings/telnet/enabled.cpp +++ b/src/settings/telnet/enabled.cpp @@ -10,7 +10,7 @@ namespace api { namespace telnet { - void set_telnet(bool new_telnet_enabled) + void Telnet::set_telnet(bool new_telnet_enabled) { if (!new_telnet_enabled) { diff --git a/src/settings/telnet/port.cpp b/src/settings/telnet/port.cpp index 5b32277..d417884 100644 --- a/src/settings/telnet/port.cpp +++ b/src/settings/telnet/port.cpp @@ -11,7 +11,7 @@ namespace api { namespace telnet { - void set_telnet_port(int new_telnet_port) + void Telnet::set_telnet_port(int new_telnet_port) { std::string old_telnet_port_string = config::config.getConfig()["settings"]["telnet"]["port"]; int old_telnet_port = std::stoi(old_telnet_port_string); diff --git a/src/settings/telnet/telnet.hpp b/src/settings/telnet/telnet.hpp index 975e634..f62ae5c 100644 --- a/src/settings/telnet/telnet.hpp +++ b/src/settings/telnet/telnet.hpp @@ -5,8 +5,11 @@ namespace api { namespace telnet { - void set_telnet(bool new_telnet_enabled); - void set_telnet_port(int new_telnet_port); + class Telnet { + public: + static void set_telnet(bool new_telnet_enabled); + static void set_telnet_port(int new_telnet_port); + }; } } } diff --git a/src/settings/wifi/channel.cpp b/src/settings/wifi/channel.cpp index 9608cd8..94372dc 100644 --- a/src/settings/wifi/channel.cpp +++ b/src/settings/wifi/channel.cpp @@ -11,7 +11,7 @@ namespace api { namespace wifi { - void set_wifi_channel(int new_channel) + void Wifi::set_wifi_channel(int new_channel) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_channel_string; diff --git a/src/settings/wifi/name.cpp b/src/settings/wifi/name.cpp index bb948cd..9cf6eb8 100644 --- a/src/settings/wifi/name.cpp +++ b/src/settings/wifi/name.cpp @@ -11,7 +11,7 @@ namespace api { namespace wifi { - void set_wifi_name(std::string new_wifi_name) + void Wifi::set_wifi_name(std::string new_wifi_name) { std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_wifi_name; diff --git a/src/settings/wifi/password.cpp b/src/settings/wifi/password.cpp index 68be158..28a967d 100644 --- a/src/settings/wifi/password.cpp +++ b/src/settings/wifi/password.cpp @@ -17,7 +17,7 @@ namespace api { namespace wifi { - void set_wifi_password(const httplib::Request& request, httplib::Response& response) + void Wifi::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", "*"); diff --git a/src/settings/wifi/qr.cpp b/src/settings/wifi/qr.cpp index 112d453..0d340a1 100644 --- a/src/settings/wifi/qr.cpp +++ b/src/settings/wifi/qr.cpp @@ -14,7 +14,7 @@ namespace api { namespace wifi { - void get_wifi_qr(const httplib::Request& request, httplib::Response& response) + void Wifi::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", "*"); diff --git a/src/settings/wifi/status.cpp b/src/settings/wifi/status.cpp index 8011fd0..48dc523 100644 --- a/src/settings/wifi/status.cpp +++ b/src/settings/wifi/status.cpp @@ -9,7 +9,7 @@ namespace api { namespace wifi { - void set_wifi_status(bool new_wifi_status) + void Wifi::set_wifi_status(bool new_wifi_status) { if (!new_wifi_status) { diff --git a/src/settings/wifi/wifi.hpp b/src/settings/wifi/wifi.hpp index 6d9a2d9..8c1737e 100644 --- a/src/settings/wifi/wifi.hpp +++ b/src/settings/wifi/wifi.hpp @@ -9,11 +9,14 @@ namespace api { namespace wifi { - void set_wifi_status(bool new_wifi_status); - void set_wifi_name(std::string new_wifi_name); - void set_wifi_password(const httplib::Request& request, httplib::Response& response); - void get_wifi_qr(const httplib::Request& request, httplib::Response& response); - void set_wifi_channel(int new_channel); + class Wifi { + public: + static void set_wifi_status(bool new_wifi_status); + static void set_wifi_name(std::string new_wifi_name); + static void set_wifi_password(const httplib::Request& request, httplib::Response& response); + static void get_wifi_qr(const httplib::Request& request, httplib::Response& response); + static void set_wifi_channel(int new_channel); + }; } } } diff --git a/src/system/reboot/reboot.cpp b/src/system/reboot.cpp similarity index 86% rename from src/system/reboot/reboot.cpp rename to src/system/reboot.cpp index 79c1e08..dfb7aeb 100644 --- a/src/system/reboot/reboot.cpp +++ b/src/system/reboot.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -9,7 +9,7 @@ namespace api { namespace system { - void reboot(const httplib::Request& request, httplib::Response& response) + void System::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", "*"); diff --git a/src/system/reboot/reboot.hpp b/src/system/reboot/reboot.hpp deleted file mode 100644 index 4a75856..0000000 --- a/src/system/reboot/reboot.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -namespace api -{ - namespace system - { - void reboot(const httplib::Request& request, httplib::Response& response); - } -} diff --git a/src/system/syslog/syslog.cpp b/src/system/syslog.cpp similarity index 79% rename from src/system/syslog/syslog.cpp rename to src/system/syslog.cpp index 5e1b8c8..56aaf20 100644 --- a/src/system/syslog/syslog.cpp +++ b/src/system/syslog.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -11,7 +11,7 @@ namespace api { namespace system { - void get_syslog(const httplib::Request& request, httplib::Response& response) + void System::get_syslog(const httplib::Request& request, httplib::Response& response) { std::stringstream responsedata; std::ifstream syslogfile(SYSLOG_FILE); diff --git a/src/system/syslog/syslog.hpp b/src/system/syslog/syslog.hpp deleted file mode 100644 index f899097..0000000 --- a/src/system/syslog/syslog.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -namespace api -{ - namespace system - { - void get_syslog(const httplib::Request& request, httplib::Response& response); - } -} diff --git a/src/system/system.hpp b/src/system/system.hpp index 6cd36cd..2fe9c00 100644 --- a/src/system/system.hpp +++ b/src/system/system.hpp @@ -1,8 +1,21 @@ #pragma once -#ifdef __linux__ -#include -#endif // __linux__ -#include -#include -#include +#include + +namespace api +{ + namespace system + { + class System { + private: + static bool compareFile(std::string tmpfilename, std::string dstfilename); + public: + static void get_uptime(const httplib::Request& request, httplib::Response& response); + static void upload(const httplib::Request& request, httplib::Response& response); + static void update(const httplib::Request& request, httplib::Response& response); + static void progress(const httplib::Request& request, httplib::Response& response); + static void get_syslog(const httplib::Request& request, httplib::Response& response); + static void reboot(const httplib::Request& request, httplib::Response& response); + }; + } +} diff --git a/src/system/update.cpp b/src/system/update.cpp new file mode 100644 index 0000000..835acdd --- /dev/null +++ b/src/system/update.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_TMP_FILE_NAME "/tmp/image" +#define DEFAULT_DST_FILE_NAME "/images/image" +#define BLOCK_SIZE 131072 + +namespace api +{ + namespace system + { + static int update_progress = 0; + static bool compare = false; + + bool System::compareFile(std::string tmpfilename, std::string dstfilename) + { + FILE* tmp = fopen(tmpfilename.c_str(), "r"); + FILE* dst = fopen(dstfilename.c_str(), "r"); + char tmpbuffer[BLOCK_SIZE]; + char dstbuffer[BLOCK_SIZE]; + + do { + size_t tmpread = fread(tmpbuffer, 1, BLOCK_SIZE, tmp); + size_t dstread = fread(dstbuffer, 1, BLOCK_SIZE, dst); + if (tmpread != dstread || memcmp(tmpbuffer, dstbuffer, tmpread)) { + fclose(tmp); + fclose(dst); + return false; + } + } while (!feof(tmp) || !feof(dst)); + fclose(tmp); + fclose(dst); + return true; + } + + void System::upload(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + bool ret = request.has_file("image"); + if (!ret) { + response.set_content(R"({"required":[token, file]})", JSON_TYPE); + } + bool has_token = request.has_file("token"); + if (!has_token) { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + if (!authenticate(request.get_file_value("token").content)) + { + response.set_content(R"({"access":"reject"})", JSON_TYPE); + return; + } + const httplib::MultipartFormData& file = request.get_file_value("image"); + std::ofstream tmp_image_file(DEFAULT_TMP_FILE_NAME); + tmp_image_file << file.content; + tmp_image_file.close(); + response.set_content(R"({"status":true})", JSON_TYPE); + } + + void System::update(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + response.set_header("Access-Control-Allow-Origin", "*"); + check_body_empty(request, response, ACCESS_REQUIRED_STRING); + nlohmann::json json_body = nlohmann::json::parse(request.body); + check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING); + if (!authenticate(json_body["token"])) + { + response.set_content(R"({"access":"reject"})", JSON_TYPE); + return; + } + + bool needAutoReboot = json_body["needAutoReboot"]; + update_progress = 0; + compare = false; + int file_image_size = std::filesystem::file_size(DEFAULT_TMP_FILE_NAME); + int block_count = file_image_size / BLOCK_SIZE; + int remainder = file_image_size % BLOCK_SIZE; + + int skip = 0; + + char buffer[BLOCK_SIZE]; + std::ifstream tmp(DEFAULT_TMP_FILE_NAME); + std::ofstream dst(DEFAULT_DST_FILE_NAME); + + while (skip < block_count) { + tmp.read(buffer, BLOCK_SIZE); + dst.write(buffer, BLOCK_SIZE); + update_progress = 100 * skip / block_count; + skip++; + std::cout << update_progress << "% was copied" << std::endl; + memset(buffer, 0, BLOCK_SIZE); + } + if (remainder > 0) { + tmp.read(buffer, remainder); + dst.write(buffer, remainder); + update_progress = 100; + std::cout << update_progress << "% was copied" << std::endl; + } + update_progress = 100; + std::cout << update_progress << "% was copied" << std::endl; + tmp.close(); + dst.close(); + compare = compareFile(DEFAULT_TMP_FILE_NAME, DEFAULT_DST_FILE_NAME); + if (!compare) { + std::cout << "files not equvalents: writing file error." << std::endl; + response.set_content(R"({"success":false})", JSON_TYPE); + return; + } + std::remove(DEFAULT_TMP_FILE_NAME); + if (needAutoReboot) { + std::cout << "reboot" << std::endl; + std::system("sleep 2; reboot;"); + return; + } + response.set_content(R"({"success":true})", JSON_TYPE); + } + + void System::progress(const httplib::Request& request, httplib::Response& response) + { + std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; + std::string responsedata = R"({"progress":)" + std::to_string(update_progress) + "}"; + response.set_content(responsedata, JSON_TYPE); + } + } +} diff --git a/src/system/update/update.cpp b/src/system/update/update.cpp deleted file mode 100644 index 973365d..0000000 --- a/src/system/update/update.cpp +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define DEFAULT_TMP_FILE_NAME "/tmp/image" -#define DEFAULT_DST_FILE_NAME "/images/image" -#define BLOCK_SIZE 131072 - -namespace api -{ - namespace system - { - namespace update - { - static int update_progress = 0; - static bool compare = false; - - bool compareFile(std::string tmpfilename, std::string dstfilename) { - FILE *tmp = fopen(tmpfilename.c_str(), "r"); - FILE *dst = fopen(dstfilename.c_str(), "r"); - char tmpbuffer[BLOCK_SIZE]; - char dstbuffer[BLOCK_SIZE]; - - do { - size_t tmpread = fread(tmpbuffer, 1, BLOCK_SIZE, tmp); - size_t dstread = fread(dstbuffer, 1, BLOCK_SIZE, dst); - if (tmpread != dstread || memcmp(tmpbuffer, dstbuffer, tmpread)) { - fclose(tmp); - fclose(dst); - return false; - } - } while (!feof(tmp) || !feof(dst)); - fclose(tmp); - fclose(dst); - return true; - } - - void upload(const httplib::Request& request, httplib::Response& response) - { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - bool ret = request.has_file("image"); - if (!ret) { - response.set_content(R"({"required":[token, file]})", JSON_TYPE); - } - bool has_token = request.has_file("token"); - if (!has_token) { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - if (!authenticate(request.get_file_value("token").content)) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - const httplib::MultipartFormData& file = request.get_file_value("image"); - std::ofstream tmp_image_file(DEFAULT_TMP_FILE_NAME); - tmp_image_file << file.content; - tmp_image_file.close(); - response.set_content(R"({"status":true})", JSON_TYPE); - } - - void update(const httplib::Request& request, httplib::Response& response) - { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - check_body_empty(request, response, ACCESS_REQUIRED_STRING); - nlohmann::json json_body = nlohmann::json::parse(request.body); - check_body_data(json_body, "token", response, ACCESS_REQUIRED_STRING); - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - - bool needAutoReboot = json_body["needAutoReboot"]; - update_progress = 0; - compare = false; - int file_image_size = std::filesystem::file_size(DEFAULT_TMP_FILE_NAME); - int block_count = file_image_size / BLOCK_SIZE; - int remainder = file_image_size % BLOCK_SIZE; - - int skip = 0; - - char buffer[BLOCK_SIZE]; - std::ifstream tmp(DEFAULT_TMP_FILE_NAME); - std::ofstream dst(DEFAULT_DST_FILE_NAME); - - while (skip < block_count) { - tmp.read(buffer, BLOCK_SIZE); - dst.write(buffer, BLOCK_SIZE); - update_progress = 100 * skip / block_count; - skip++; - std::cout << update_progress << "% was copied" << std::endl; - memset(buffer, 0, BLOCK_SIZE); - } - if (remainder > 0) { - tmp.read(buffer, remainder); - dst.write(buffer, remainder); - update_progress = 100; - std::cout << update_progress << "% was copied" << std::endl; - } - update_progress = 100; - std::cout << update_progress << "% was copied" << std::endl; - tmp.close(); - dst.close(); - compare = compareFile(DEFAULT_TMP_FILE_NAME, DEFAULT_DST_FILE_NAME); - if (!compare) { - std::cout << "files not equvalents: writing file error." << std::endl; - response.set_content(R"({"success":false})", JSON_TYPE); - return; - } - std::remove(DEFAULT_TMP_FILE_NAME); - if (needAutoReboot) { - std::cout << "reboot" << std::endl; - std::system("sleep 2; reboot;"); - return; - } - response.set_content(R"({"success":true})", JSON_TYPE); - } - - void progress(const httplib::Request& request, httplib::Response& response) - { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - std::string responsedata = R"({"progress":)" + std::to_string(update_progress) + "}"; - response.set_content(responsedata, JSON_TYPE); - } - } - } -} diff --git a/src/system/update/update.hpp b/src/system/update/update.hpp deleted file mode 100644 index 2bfd24f..0000000 --- a/src/system/update/update.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -namespace api -{ - namespace system - { - namespace update - { - void upload(const httplib::Request& request, httplib::Response& response); - void update(const httplib::Request& request, httplib::Response& response); - void progress(const httplib::Request& request, httplib::Response& response); - } - } -} diff --git a/src/system/uptime/uptime.cpp b/src/system/uptime.cpp similarity index 87% rename from src/system/uptime/uptime.cpp rename to src/system/uptime.cpp index 1884abf..7e3685f 100644 --- a/src/system/uptime/uptime.cpp +++ b/src/system/uptime.cpp @@ -1,4 +1,4 @@ -#include +#include #ifdef __linux__ #include #endif // __linux__ @@ -8,7 +8,7 @@ namespace api { namespace system { - void get_uptime(const httplib::Request& request, httplib::Response& response) + void System::get_uptime(const httplib::Request& request, httplib::Response& response) { #ifdef __linux__ struct sysinfo x; diff --git a/src/system/uptime/uptime.hpp b/src/system/uptime/uptime.hpp deleted file mode 100644 index 9823c0e..0000000 --- a/src/system/uptime/uptime.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include - -namespace api -{ - namespace system - { - void get_uptime(const httplib::Request& request, httplib::Response& response); - } -} diff --git a/src/web/css/form.css b/src/web/css/form.css index 3c50825..06ff652 100644 --- a/src/web/css/form.css +++ b/src/web/css/form.css @@ -11,7 +11,6 @@ padding: 10px; transform: scale(0.5); opacity: 0; - transition: 0.3s; position: absolute; margin: auto; } @@ -58,44 +57,6 @@ gap: 10px; } -/* .forminputbody.confirmform>button { - width: 100%; -} - -.forminputlabel { - text-align: center; - font-style: normal; - font-size: 20px; - margin: 5px 0; -} - -.forminput { - padding: 10px; - border-radius: 10px; - border: 2px solid; - transition: 0.2s; - background-color: inherit; -} - -.forminput>option { - background-color: inherit; - color: black; -} - -.forminput:focus { - border: 5px solid; -} - -.formbutton { - padding: 10px; - border-radius: 10px; - border: none; - cursor: pointer; - transition: 0.3s; - width: 100%; -} */ - - @media screen and (max-width: 800px) { .forminputbody { width: 100%; diff --git a/src/web/css/index.css b/src/web/css/index.css index 657ff28..9900bd4 100644 --- a/src/web/css/index.css +++ b/src/web/css/index.css @@ -1,10 +1,13 @@ +* { + transition: 0.3s; +} + .adminpanel { position: relative; width: 100%; height: 100vh; transform: scale(0.5); opacity: 0; - transition: 0.3s; display: flex; flex-direction: column; gap: 10px; @@ -22,3 +25,18 @@ height: 100%; width: 100%; } + +.somesection { + position: absolute; + top: 0; + left: 0; + width: 100%; + opacity: 0; + pointer-events: none; +} + +.somesection.active { + opacity: 1; + pointer-events: all; +} + diff --git a/src/web/css/info.css b/src/web/css/info.css index f5ba40f..3606e63 100644 --- a/src/web/css/info.css +++ b/src/web/css/info.css @@ -3,10 +3,8 @@ top: 0; left: 0; width: 100%; - /* height: 100%; */ opacity: 0; pointer-events: none; - transition: 0.3s; padding: 10px; } diff --git a/src/web/css/notification.css b/src/web/css/notification.css index d9e6707..b751f92 100644 --- a/src/web/css/notification.css +++ b/src/web/css/notification.css @@ -2,7 +2,7 @@ width: max-content; height: 100vh; position: fixed; - top: 0; + top: 50px; right: 0; pointer-events: none; z-index: 1; @@ -18,7 +18,6 @@ /* position: fixed; */ top: 0; right: 0; - transition: 0.3s; cursor: pointer; opacity: 0.8; margin: 10px; @@ -50,4 +49,4 @@ pointer-events: all; height: 10%; background-color: rgb(76, 209, 76); -} \ No newline at end of file +} diff --git a/src/web/css/settings.css b/src/web/css/settings.css index 725bff5..bd9a7b3 100644 --- a/src/web/css/settings.css +++ b/src/web/css/settings.css @@ -8,7 +8,6 @@ margin: 0; position: relative; width: 30px; - transition: 0.3s; } .checkbox-wrapper-2 .ikxBAC::before { @@ -18,13 +17,11 @@ position: absolute; right: -6px; top: -6px; - transition: 0.3s; cursor: pointer; } .checkbox-wrapper-2 .ikxBAC, .checkbox-wrapper-2 .ikxBAC::after { - transition: 300ms; } .checkbox-wrapper-2 .ikxBAC::after { @@ -45,7 +42,6 @@ .checkbox-wrapper-2 .ikxBAC:hover { background-color: #c9cbcd; - transition-duration: 0s; } .checkbox-wrapper-2 .ikxBAC:checked { @@ -72,7 +68,6 @@ width: 100%; height: calc(100vh - 50px); padding: 10px; - transition: 0.3s; opacity: 0; pointer-events: none; overflow-y: auto; diff --git a/src/web/css/syslog.css b/src/web/css/syslog.css index 6b60a56..d600295 100644 --- a/src/web/css/syslog.css +++ b/src/web/css/syslog.css @@ -6,7 +6,6 @@ height: 100%; opacity: 0; pointer-events: none; - transition: 0.3s; padding: 10px; } diff --git a/src/web/index.html b/src/web/index.html index 562f8df..5894ace 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -19,7 +19,7 @@