From cedc9cd9951110863ca6dd7514897a1254fae9da Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Thu, 20 Feb 2025 18:07:59 +0700 Subject: [PATCH] making easy --- CMakeLists.txt | 24 ++--- Makefile | 1 + config.json | 16 +-- meson.build | 2 + src/OpenRouter/OpenRouter.cpp | 34 +------ src/auth/auth.cpp | 3 +- src/routes.hpp | 6 -- src/settings/dns/dns.hpp | 13 +-- src/settings/dns/enabled.cpp | 63 +----------- src/settings/dns/local_domains.cpp | 55 +---------- src/settings/dns/servers.cpp | 55 +---------- src/settings/ftp/ftp.cpp | 62 +----------- src/settings/ftp/ftp.hpp | 5 +- src/settings/settings.cpp | 91 +++++++++++++++++ src/settings/settings.hpp | 9 ++ src/settings/ssh/keys.cpp | 50 +--------- src/settings/ssh/port.cpp | 63 +----------- src/settings/ssh/server.cpp | 63 +----------- src/settings/ssh/ssh.hpp | 19 +--- src/settings/ssh/status.cpp | 61 +----------- src/settings/telnet/enabled.cpp | 66 +------------ src/settings/telnet/port.cpp | 68 +------------ src/settings/telnet/telnet.hpp | 9 +- src/settings/wifi/channel.cpp | 63 +----------- src/settings/wifi/name.cpp | 76 +-------------- src/settings/wifi/password.cpp | 23 ----- src/settings/wifi/status.cpp | 62 +----------- src/settings/wifi/wifi.hpp | 14 +-- src/web/index.html | 7 +- src/web/js/login.js | 2 +- src/web/js/routes.js | 9 +- src/web/js/settings/confirm.js | 33 ------- src/web/js/settings/dns.js | 152 ++++++++--------------------- src/web/js/settings/ftp.js | 28 +----- src/web/js/settings/settings.js | 40 ++++++-- src/web/js/settings/ssh.js | 142 +++++++-------------------- src/web/js/settings/telnet.js | 75 +++----------- src/web/js/settings/wifi.js | 113 ++++----------------- 38 files changed, 313 insertions(+), 1364 deletions(-) delete mode 100644 src/routes.hpp create mode 100644 src/settings/settings.cpp delete mode 100644 src/web/js/settings/confirm.js diff --git a/CMakeLists.txt b/CMakeLists.txt index eb8004d..60fbef9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,16 @@ -cmake_minimum_required (VERSION 2.6) -project(openrouter) -set(CMAKE_CXX_STANDARD 20) - -include_directories(./src/libs/ ./src) - -add_executable( - openrouter - src/openrouter.cpp +cmake_minimum_required (VERSION 2.6) +project(openrouter) +set(CMAKE_CXX_STANDARD 20) + +include_directories(./src/libs/ ./src) + +add_executable( + openrouter + src/openrouter.cpp src/security/cors.cpp - + + src/settings/settings.cpp + src/settings/telnet/enabled.cpp src/settings/telnet/port.cpp @@ -41,7 +43,7 @@ add_executable( src/libs/QR-Code-generator/qrcodegen.cpp src/libs/QR-Code-generator/qrtosvg.cpp -) +) install(TARGETS openrouter DESTINATION /usr/bin) install(DIRECTORY src/web DESTINATION /usr/share/openrouter/web) diff --git a/Makefile b/Makefile index e418295..358c142 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ STRIP=strip CXXFLAGS=-Wall -I src/libs -I src -std=c++20 SOURCES=src/openrouter.cpp \ src/security/cors.cpp \ + src/settings/settings.cpp \ src/settings/telnet/enabled.cpp \ src/settings/telnet/port.cpp \ src/settings/ssh/status.cpp \ diff --git a/config.json b/config.json index a08a57d..0479aa3 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,8 @@ { + "auth_service": "http://127.0.0.1:43243", "settings": { "dns": { - "enabled": false, + "enabled": true, "localdomains": [ { "domain": "localhost", @@ -25,17 +26,17 @@ "key1", "key2" ], - "port": 22, + "port": "22", "server": "dropbear" }, "telnet": { "enabled": false, - "port": 23 + "port": "29" }, "wifi": { - "channel": 6, + "channel": "1", "enabled": false, - "name": "OpenRouterWifi", + "name": "home", "password": "OpenRouterPassword" } }, @@ -43,6 +44,5 @@ "css": "../src/web/css", "js": "../src/web/js", "root": "../src/web" - }, - "auth_service": "http://127.0.0.1:43243" -} + } +} \ No newline at end of file diff --git a/meson.build b/meson.build index b8256db..3c081fc 100644 --- a/meson.build +++ b/meson.build @@ -5,6 +5,8 @@ executable( 'src/openrouter.cpp', 'src/security/cors.cpp', + 'src/settings/settings.cpp', + 'src/settings/telnet/enabled.cpp', 'src/settings/telnet/port.cpp', diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index 5bfd107..85230af 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -8,39 +8,11 @@ OpenRouter::OpenRouter() { - this->srv.Post("/api/settings/telnet/get", api::settings::telnet::get_telnet); - this->srv.Post("/api/settings/telnet/set", api::settings::telnet::set_telnet); - this->srv.Post("/api/settings/telnet/port/get", api::settings::telnet::get_telnet_port); - this->srv.Post("/api/settings/telnet/port/set", api::settings::telnet::set_telnet_port); - - this->srv.Post("/api/settings/ssh/status/get", api::settings::ssh::get_ssh_status); - this->srv.Post("/api/settings/ssh/status/set", api::settings::ssh::set_ssh_status); - this->srv.Post("/api/settings/ssh/keys/get", api::settings::ssh::get_ssh_keys); - this->srv.Post("/api/settings/ssh/keys/set", api::settings::ssh::set_ssh_keys); - this->srv.Post("/api/settings/ssh/port/get", api::settings::ssh::get_ssh_port); - this->srv.Post("/api/settings/ssh/port/set", api::settings::ssh::set_ssh_port); - this->srv.Post("/api/settings/ssh/server/get", api::settings::ssh::get_ssh_server_http); - this->srv.Post("/api/settings/ssh/server/set", api::settings::ssh::set_ssh_server_http); - - this->srv.Post("/api/settings/dns/get", api::settings::dns::get_dns); - this->srv.Post("/api/settings/dns/set", api::settings::dns::set_dns); - this->srv.Post("/api/settings/dns/servers/get", api::settings::dns::get_dns_servers); - this->srv.Post("/api/settings/dns/servers/set", api::settings::dns::set_dns_servers); - this->srv.Post("/api/settings/dns/domains/get", api::settings::dns::get_local_domains); - this->srv.Post("/api/settings/dns/domains/set", api::settings::dns::set_local_domains); - - this->srv.Post("/api/settings/ftp/get", api::settings::ftp::get_ftp); - this->srv.Post("/api/settings/ftp/set", api::settings::ftp::set_ftp); - - this->srv.Post("/api/settings/wifi/status/get", api::settings::wifi::get_wifi_status); - this->srv.Post("/api/settings/wifi/status/set", api::settings::wifi::set_wifi_status); - this->srv.Post("/api/settings/wifi/name/get", api::settings::wifi::get_wifi_name); - this->srv.Post("/api/settings/wifi/name/set", api::settings::wifi::set_wifi_name); - this->srv.Post("/api/settings/wifi/password/get", api::settings::wifi::get_wifi_password); 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); - this->srv.Post("/api/settings/wifi/channel/get", api::settings::wifi::get_wifi_channel); - this->srv.Post("/api/settings/wifi/channel/set", api::settings::wifi::set_wifi_channel); + + this->srv.Post("/api/settings/get", api::settings::get); + this->srv.Post("/api/settings/set", api::settings::set); this->srv.Post("/api/reboot", api::system::reboot); diff --git a/src/auth/auth.cpp b/src/auth/auth.cpp index ece2d52..1d599d2 100644 --- a/src/auth/auth.cpp +++ b/src/auth/auth.cpp @@ -1,13 +1,12 @@ #include #include -#include #include #include #include bool authenticate(const std::string &token) { - config::config.setConfig("../config.json") + // config::config.setConfig(std::string("../config.json")); httplib::Client cli(config::config.getConfig()["auth_service"]); std::string body = R"({"token":")" + token + R"("})"; diff --git a/src/routes.hpp b/src/routes.hpp deleted file mode 100644 index ef16532..0000000 --- a/src/routes.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef ROUTES_HPP -#define ROUTES_HPP - -#define AUTH_SERVICE "http://localhost:43243" - -#endif // ROUTES_HPP diff --git a/src/settings/dns/dns.hpp b/src/settings/dns/dns.hpp index 4e2af17..91227d0 100644 --- a/src/settings/dns/dns.hpp +++ b/src/settings/dns/dns.hpp @@ -1,7 +1,7 @@ #ifndef DNS_HPP #define DNS_HPP -#include +#include namespace api { @@ -9,14 +9,9 @@ namespace api { namespace dns { - 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_local_domains(const httplib::Request& request, httplib::Response& response); - void set_local_domains(const httplib::Request& request, httplib::Response& response); + void set_dns(bool new_dns_enabled); + void set_dns_servers(nlohmann::json servers); + void set_local_domains(nlohmann::json domains); } } } diff --git a/src/settings/dns/enabled.cpp b/src/settings/dns/enabled.cpp index db90db6..2c19ffe 100644 --- a/src/settings/dns/enabled.cpp +++ b/src/settings/dns/enabled.cpp @@ -1,13 +1,5 @@ #include -#include -#include -#include #include -#include -#include -#include - -#define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" namespace api { @@ -15,52 +7,8 @@ namespace api { namespace dns { - void get_dns(const httplib::Request& request, httplib::Response& response) + void set_dns(bool new_dns_enabled) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int dns_enabled = config::config.getConfig()["settings"]["dns"]["enabled"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["enabled"] == nullptr) - { - response.set_content(DNS_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - bool new_dns_enabled = json_body["enabled"]; if (!new_dns_enabled) { system("rc stop dnsmasq"); @@ -73,15 +21,6 @@ namespace api system("rc add dnsmasq"); syslog(LOG_INFO, "%s", "DNS is enabled"); } - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["dns"]["enabled"] = new_dns_enabled; - config::config.setConfig(new_config); - config::config.saveConfig(); - int dns_enabled = config::config.getConfig()["settings"]["dns"]["enabled"]; - std::stringstream responsedata; - responsedata << R"({"enabled":")" << dns_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/dns/local_domains.cpp b/src/settings/dns/local_domains.cpp index 08af734..7e889b9 100644 --- a/src/settings/dns/local_domains.cpp +++ b/src/settings/dns/local_domains.cpp @@ -1,11 +1,6 @@ #include -#include -#include -#include #include -#include -#include -#include +#include namespace api { @@ -13,48 +8,9 @@ namespace api { namespace dns { - void get_local_domains(const httplib::Request& request, httplib::Response& response) + void set_local_domains(nlohmann::json domains) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - std::stringstream responsedata; - responsedata << R"({"domains":)"; - responsedata << config::config.getConfig()["settings"]["dns"]["localdomains"]; - responsedata << "}"; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", 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"]; @@ -62,15 +18,8 @@ namespace api domainsfile << ip << " " << dom << "\n"; } domainsfile.close(); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["dns"]["localdomains"] = json_body["domains"]; - config::config.setConfig(new_config); - config::config.saveConfig(); system("rc restart dnsmasq"); syslog(LOG_INFO, "%s", "DNS local domains changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); } } } diff --git a/src/settings/dns/servers.cpp b/src/settings/dns/servers.cpp index 35e50f9..a3347f5 100644 --- a/src/settings/dns/servers.cpp +++ b/src/settings/dns/servers.cpp @@ -1,11 +1,6 @@ #include -#include -#include -#include #include -#include -#include -#include +#include namespace api { @@ -13,48 +8,9 @@ namespace api { namespace dns { - void get_dns_servers(const httplib::Request& request, httplib::Response& response) + void set_dns_servers(nlohmann::json servers) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - std::stringstream responsedata; - responsedata << R"({"servers":)"; - responsedata << config::config.getConfig()["settings"]["dns"]["servers"]; - responsedata << "}"; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); - nlohmann::json servers = json_body["servers"]; for (const nlohmann::json_abi_v3_11_3::json& server : servers) { std::string srv = server; @@ -65,15 +21,8 @@ namespace api dnsserversfile << "nameserver " << srv << "\n"; } dnsserversfile.close(); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["dns"]["servers"] = json_body["servers"]; - config::config.setConfig(new_config); - config::config.saveConfig(); system("rc restart dnsmasq"); syslog(LOG_INFO, "%s", "DNS servers changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); } } } diff --git a/src/settings/ftp/ftp.cpp b/src/settings/ftp/ftp.cpp index 50a7271..33c0026 100644 --- a/src/settings/ftp/ftp.cpp +++ b/src/settings/ftp/ftp.cpp @@ -1,11 +1,6 @@ #include -#include -#include -#include #include -#include -#include -#include +#include #define FTP_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -15,52 +10,8 @@ namespace api { namespace ftp { - void get_ftp(const httplib::Request& request, httplib::Response& response) + void set_ftp(bool new_ftp_enabled) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int ftp_enabled = config::config.getConfig()["settings"]["ftp"]["enabled"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["enabled"] == nullptr) - { - response.set_content(FTP_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - bool new_ftp_enabled = json_body["enabled"]; if (!new_ftp_enabled) { system("rc stop ftp"); @@ -73,15 +24,6 @@ namespace api system("rc add ftp"); syslog(LOG_INFO, "%s", "Ftp is enabled"); } - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["ftp"]["enabled"] = new_ftp_enabled; - config::config.setConfig(new_config); - config::config.saveConfig(); - int ftp_enabled = config::config.getConfig()["settings"]["ftp"]["enabled"]; - std::stringstream responsedata; - responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/ftp/ftp.hpp b/src/settings/ftp/ftp.hpp index d04e9b7..a7daca2 100644 --- a/src/settings/ftp/ftp.hpp +++ b/src/settings/ftp/ftp.hpp @@ -1,16 +1,13 @@ #ifndef FTP_HPP #define FTP_HPP -#include - namespace api { namespace settings { namespace ftp { - void get_ftp(const httplib::Request& request, httplib::Response& response); - void set_ftp(const httplib::Request& request, httplib::Response& response); + void set_ftp(bool new_ftp_enabled); } } } diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp new file mode 100644 index 0000000..1f06827 --- /dev/null +++ b/src/settings/settings.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define SETTINGS_REQUIRED_STRING R"({"required":"[settings]"})" + +namespace api +{ + namespace settings + { + void 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", "*"); + if (!check_body(request)) + { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); + + if (!authenticate(json_body["token"])) + { + response.set_content(R"({"access":"reject"})", JSON_TYPE); + return; + } + config::config.setConfig(std::string("../config.json")); + std::string responsedata = R"({"settings":)" + config::config.getConfig()["settings"].dump() + R"(})"; + response.set_content(responsedata, JSON_TYPE); + } + + void 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", "*"); + if (!check_body(request)) + { + response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); + return; + } + nlohmann::json json_body = nlohmann::json::parse(request.body); + + if (json_body["settings"] == nullptr) + { + response.set_content(SETTINGS_REQUIRED_STRING, JSON_TYPE); + return; + } + + if (!authenticate(json_body["token"])) + { + response.set_content(R"({"access":"reject"})", JSON_TYPE); + return; + } + sleep(1); + config::config.setConfig(std::string("../config.json")); + nlohmann::json new_config = config::config.getConfig(); + new_config["settings"] = json_body["settings"]; + config::config.setConfig(new_config); + config::config.saveConfig(); + + api::settings::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::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::ssh::set_ssh_status(json_body["settings"]["ssh"]["enabled"]); + api::settings::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::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"]); + + std::string responsedata = R"({"settings":)" + config::config.getConfig()["settings"].dump() + R"(})"; + response.set_content(responsedata, JSON_TYPE); + } + } +} diff --git a/src/settings/settings.hpp b/src/settings/settings.hpp index 678166c..bd8b57e 100644 --- a/src/settings/settings.hpp +++ b/src/settings/settings.hpp @@ -7,4 +7,13 @@ #include #include +namespace api +{ + namespace settings + { + void get(const httplib::Request& request, httplib::Response& response); + void set(const httplib::Request& request, httplib::Response& response); + } +} + #endif // SETTINGS_HPP diff --git a/src/settings/ssh/keys.cpp b/src/settings/ssh/keys.cpp index f602a6e..5afe324 100644 --- a/src/settings/ssh/keys.cpp +++ b/src/settings/ssh/keys.cpp @@ -1,10 +1,8 @@ #include #include #include -#include #include #include -#include #include #define KEYS_PATH "~/.ssh/authorized_keys" @@ -15,62 +13,16 @@ namespace api { namespace ssh { - void get_ssh_keys(const httplib::Request& request, httplib::Response& response) + void set_ssh_keys(nlohmann::json keys) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - std::stringstream responsedata; - responsedata << R"({"keys":)"; - responsedata << config::config.getConfig()["settings"]["ssh"]["keys"]; - responsedata << "}"; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } std::ofstream keysfile(KEYS_PATH); - nlohmann::json keys = json_body["keys"]; for (std::string key : keys) { keysfile << key << "\n"; } keysfile.close(); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["ssh"]["keys"] = json_body["keys"]; - config::config.setConfig(new_config); - config::config.saveConfig(); system("rc restart ssh"); syslog(LOG_INFO, "%s", "SSH keys changed"); - std::string responsedata = R"({"success":"true"})"; - response.set_content(responsedata, JSON_TYPE); } } } diff --git a/src/settings/ssh/port.cpp b/src/settings/ssh/port.cpp index c8c6ccc..f0732b6 100644 --- a/src/settings/ssh/port.cpp +++ b/src/settings/ssh/port.cpp @@ -1,15 +1,9 @@ #include -#include -#include -#include #include #include #include #include #include -#include -#include -#include #define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" #define SSH_PORT_PATH "/etc/openrouter/ssh/port" @@ -20,58 +14,14 @@ namespace api { namespace ssh { - void get_ssh_port(const httplib::Request& request, httplib::Response& response) + void set_ssh_port(int new_ssh_port) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int ssh_port = config::config.getConfig()["settings"]["ssh"]["port"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["port"] == nullptr) - { - response.set_content(SSH_PORT_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", 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 << new_ssh_port; sshportfileout.close(); std::stringstream systemrequest; if (get_ssh_server() == DROPBEAR_SSH) @@ -94,15 +44,6 @@ namespace api } std::string syslogstring = "SSH port changed to " + new_ssh_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["ssh"]["port"] = std::stoi(new_ssh_port); - config::config.setConfig(new_config); - config::config.saveConfig(); - int ssh_port = config::config.getConfig()["settings"]["ssh"]["port"]; - std::stringstream responsedata; - responsedata << R"({"port":")" << ssh_port << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/ssh/server.cpp b/src/settings/ssh/server.cpp index 8f19c34..c6c8e73 100644 --- a/src/settings/ssh/server.cpp +++ b/src/settings/ssh/server.cpp @@ -1,10 +1,4 @@ #include -#include -#include -#include -#include -#include -#include #include #define SSH_SERVER_REQUIRED_STRING R"({"required":"[token,server]"})" @@ -21,61 +15,8 @@ namespace api return config::config.getConfig()["settings"]["ssh"]["server"]; } - std::string set_ssh_server(std::string& server) + void set_ssh_server(std::string new_server) { - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["ssh"]["server"] = server; - config::config.setConfig(new_config); - config::config.saveConfig(); - return config::config.getConfig()["settings"]["ssh"]["server"]; - } - - void get_ssh_server_http(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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - std::stringstream responsedata; - responsedata << R"({"server":")" << get_ssh_server() << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); - } - void set_ssh_server_http(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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["server"] == nullptr) - { - response.set_content(SSH_SERVER_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - std::stringstream responsedata; - std::string server = json_body["server"]; - std::string new_server = set_ssh_server(server); if (new_server == DROPBEAR_SSH) { system("rc stop ssh"); @@ -89,8 +30,6 @@ namespace api system("rc start ssh"); system("rc add ssh"); } - responsedata << R"({"server":")" << new_server << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/ssh/ssh.hpp b/src/settings/ssh/ssh.hpp index 4d9a23b..2326685 100644 --- a/src/settings/ssh/ssh.hpp +++ b/src/settings/ssh/ssh.hpp @@ -1,7 +1,7 @@ #ifndef SSH_HPP #define SSH_HPP -#include +#include #define DROPBEAR_SSH "dropbear" #define OPENSSH_SSH "openssh" @@ -13,19 +13,10 @@ namespace api namespace ssh { std::string get_ssh_server(); - std::string set_ssh_server(std::string& server); - - void get_ssh_server_http(const httplib::Request& request, httplib::Response& response); - void set_ssh_server_http(const httplib::Request& request, httplib::Response& response); - - 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_port(const httplib::Request& request, httplib::Response& response); - void set_ssh_port(const httplib::Request& request, httplib::Response& response); + 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); } } } diff --git a/src/settings/ssh/status.cpp b/src/settings/ssh/status.cpp index f8dd807..2f2024f 100644 --- a/src/settings/ssh/status.cpp +++ b/src/settings/ssh/status.cpp @@ -1,11 +1,5 @@ #include -#include -#include -#include #include -#include -#include -#include #define SSH_REQUIRED_STRING R"({"required":"[token,status]"})" @@ -15,52 +9,8 @@ namespace api { namespace ssh { - void get_ssh_status(const httplib::Request& request, httplib::Response& response) + void set_ssh_status(bool new_ssh_status) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int ssh_status = config::config.getConfig()["settings"]["ssh"]["enabled"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["status"] == nullptr) - { - response.set_content(SSH_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - bool new_ssh_status = json_body["status"]; if (!new_ssh_status) { if (get_ssh_server() == DROPBEAR_SSH) @@ -87,15 +37,6 @@ namespace api } syslog(LOG_INFO, "%s", "SSH is enabled"); } - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["ssh"]["enabled"] = new_ssh_status; - config::config.setConfig(new_config); - config::config.saveConfig(); - int ssh_status = config::config.getConfig()["settings"]["ssh"]["enabled"]; - std::stringstream responsedata; - responsedata << R"({"status":")" << ssh_status << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/telnet/enabled.cpp b/src/settings/telnet/enabled.cpp index 811e6c6..eadfb80 100644 --- a/src/settings/telnet/enabled.cpp +++ b/src/settings/telnet/enabled.cpp @@ -1,15 +1,6 @@ #include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include +#include #define TELNET_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -19,52 +10,8 @@ namespace api { namespace telnet { - void get_telnet(const httplib::Request& request, httplib::Response& response) + void set_telnet(bool new_telnet_enabled) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int telnet_enabled = config::config.getConfig()["settings"]["telnet"]["enabled"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["enabled"] == nullptr) - { - response.set_content(TELNET_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - bool new_telnet_enabled = json_body["enabled"]; if (!new_telnet_enabled) { system("rc stop telnet"); @@ -77,15 +24,6 @@ namespace api system("rc add telnet"); syslog(LOG_INFO, "%s", "Telnet is enabled"); } - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["telnet"]["enabled"] = new_telnet_enabled; - config::config.setConfig(new_config); - config::config.saveConfig(); - int telnet_enabled = config::config.getConfig()["settings"]["telnet"]["enabled"]; - std::stringstream responsedata; - responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/telnet/port.cpp b/src/settings/telnet/port.cpp index a55b7fd..0f69b0d 100644 --- a/src/settings/telnet/port.cpp +++ b/src/settings/telnet/port.cpp @@ -1,17 +1,6 @@ #include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include - -#define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" +#include namespace api { @@ -19,64 +8,11 @@ namespace api { namespace telnet { - void get_telnet_port(const httplib::Request& request, httplib::Response& response) + void set_telnet_port(int new_telnet_port) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int telnet_port = config::config.getConfig()["settings"]["telnet"]["port"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["port"] == nullptr) - { - response.set_content(TELNET_PORT_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - std::string new_telnet_port = json_body["port"]; system("rc restart telnet"); std::string syslogstring = "Telnet port changed to " + new_telnet_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); - std::stringstream responsedata; - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["telnet"]["port"] = stoi(new_telnet_port); - config::config.setConfig(new_config); - config::config.saveConfig(); - int telnet_port = config::config.getConfig()["settings"]["telnet"]["port"]; - responsedata << R"({"port":")" << telnet_port << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/telnet/telnet.hpp b/src/settings/telnet/telnet.hpp index cd3041c..e59d085 100644 --- a/src/settings/telnet/telnet.hpp +++ b/src/settings/telnet/telnet.hpp @@ -1,19 +1,14 @@ #ifndef SETTINGS_TELNET_HPP #define SETTINGS_TELNET_HPP -#include - namespace api { namespace settings { namespace telnet { - 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 set_telnet(bool new_telnet_enabled); + void set_telnet_port(int new_telnet_port); } } } diff --git a/src/settings/wifi/channel.cpp b/src/settings/wifi/channel.cpp index 7b7056f..001d40b 100644 --- a/src/settings/wifi/channel.cpp +++ b/src/settings/wifi/channel.cpp @@ -1,15 +1,8 @@ #include -#include -#include -#include #include #include #include -#include #include -#include -#include -#include #define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})" @@ -19,51 +12,8 @@ namespace api { namespace wifi { - void get_wifi_channel(const httplib::Request& request, httplib::Response& response) + void set_wifi_channel(int new_channel) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int wifi_channel = config::config.getConfig()["settings"]["wifi"]["channel"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["channel"] == nullptr) - { - response.set_content(WIFI_CHANNEL_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_channel; std::string wifisettingsbuffer; @@ -77,7 +27,6 @@ namespace api } } 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; @@ -85,16 +34,6 @@ namespace api system("rc restart hostapd"); std::string syslogstring = "WIFI channel changed to " + new_channel; syslog(LOG_INFO, "%s", syslogstring.c_str()); - sleep(1); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["wifi"]["channel"] = std::stoi(new_channel); - config::config.setConfig(new_config); - config::config.saveConfig(); - int wifi_channel = config::config.getConfig()["settings"]["wifi"]["channel"]; - std::stringstream responsedata; - responsedata << R"({"channel":")" << wifi_channel << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/wifi/name.cpp b/src/settings/wifi/name.cpp index d6827d3..c386f65 100644 --- a/src/settings/wifi/name.cpp +++ b/src/settings/wifi/name.cpp @@ -1,15 +1,8 @@ #include -#include -#include -#include #include #include #include -#include #include -#include -#include -#include #define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})" @@ -19,51 +12,8 @@ namespace api { namespace wifi { - void get_wifi_name(const httplib::Request& request, httplib::Response& response) + void set_wifi_name(std::string new_wifi_name) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - std::string wifi_name = config::config.getConfig()["settings"]["wifi"]["name"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["name"] == nullptr) - { - response.set_content(WIFI_NAME_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); std::string old_wifi_name; std::string wifisettingsbuffer; @@ -77,7 +27,6 @@ namespace api } } 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; @@ -85,29 +34,6 @@ namespace api system("rc restart hostapd"); std::string syslogstring = "WIFI name changed to " + new_wifi_name; syslog(LOG_INFO, "%s", 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(); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["wifi"]["name"] = new_wifi_name; - config::config.setConfig(new_config); - config::config.saveConfig(); - wifi_name = config::config.getConfig()["settings"]["wifi"]["name"]; - std::stringstream responsedata; - responsedata << R"({"name":")" << wifi_name << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/wifi/password.cpp b/src/settings/wifi/password.cpp index 2c2e3f1..06ee209 100644 --- a/src/settings/wifi/password.cpp +++ b/src/settings/wifi/password.cpp @@ -19,29 +19,6 @@ namespace api { namespace wifi { - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - std::string wifi_password = config::config.getConfig()["settings"]["wifi"]["password"]; - 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; diff --git a/src/settings/wifi/status.cpp b/src/settings/wifi/status.cpp index 83f836c..8011fd0 100644 --- a/src/settings/wifi/status.cpp +++ b/src/settings/wifi/status.cpp @@ -1,11 +1,5 @@ #include -#include -#include -#include #include -#include -#include -#include #define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})" @@ -15,52 +9,8 @@ namespace api { namespace wifi { - void get_wifi_status(const httplib::Request& request, httplib::Response& response) + void set_wifi_status(bool new_wifi_status) { - std::cout << GREEN << request.path << RESET << " " << request.method << std::endl; - response.set_header("Access-Control-Allow-Origin", "*"); - if (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - config::config.setConfig(std::string("../config.json")); - int wifi_status = config::config.getConfig()["settings"]["wifi"]["enabled"]; - 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 (!check_body(request)) - { - response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE); - return; - } - nlohmann::json json_body = nlohmann::json::parse(request.body); - - if (json_body["status"] == nullptr) - { - response.set_content(WIFI_REQUIRED_STRING, JSON_TYPE); - return; - } - - if (!authenticate(json_body["token"])) - { - response.set_content(R"({"access":"reject"})", JSON_TYPE); - return; - } - bool new_wifi_status = json_body["status"]; if (!new_wifi_status) { system("rc stop hostapd"); @@ -73,16 +23,6 @@ namespace api system("rc add hostapd"); syslog(LOG_INFO, "%s", "WIFI is enabled"); } - sleep(1); - config::config.setConfig(std::string("../config.json")); - nlohmann::json new_config = config::config.getConfig(); - new_config["settings"]["wifi"]["enabled"] = new_wifi_status; - config::config.setConfig(new_config); - config::config.saveConfig(); - int wifi_status = config::config.getConfig()["settings"]["wifi"]["enabled"]; - std::stringstream responsedata; - responsedata << R"({"status":")" << wifi_status << R"("})"; - response.set_content(responsedata.str(), JSON_TYPE); } } } diff --git a/src/settings/wifi/wifi.hpp b/src/settings/wifi/wifi.hpp index 0d6b343..12e06a2 100644 --- a/src/settings/wifi/wifi.hpp +++ b/src/settings/wifi/wifi.hpp @@ -11,19 +11,11 @@ namespace api { namespace wifi { - 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_password(const httplib::Request& request, httplib::Response& response); + 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 get_wifi_channel(const httplib::Request& request, httplib::Response& response); - void set_wifi_channel(const httplib::Request& request, httplib::Response& response); + void set_wifi_channel(int new_channel); } } } diff --git a/src/web/index.html b/src/web/index.html index e8a14fa..0b5925f 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -209,7 +209,7 @@
-

0.1.1

+

0.1.1

@@ -265,13 +265,12 @@ - + - @@ -279,4 +278,4 @@ - \ No newline at end of file + diff --git a/src/web/js/login.js b/src/web/js/login.js index b415053..ee0b795 100644 --- a/src/web/js/login.js +++ b/src/web/js/login.js @@ -49,7 +49,7 @@ const login = async (username, password) => { notification("Успешно", "success") loginform.classList.remove("active") localStorage.setItem("token", response.token) - get_all_settings() + await get_all_settings() adminpanelsection.classList.add('active') await get_user_data() loginusername.value = "" diff --git a/src/web/js/routes.js b/src/web/js/routes.js index a328433..43c8808 100644 --- a/src/web/js/routes.js +++ b/src/web/js/routes.js @@ -2,8 +2,8 @@ const host = window.location.origin const prefix = 'api' const routes = { - login: () => [host, prefix, 'token'].join('/'), - user: () => [host, prefix, 'users', 'user'].join('/'), + login: () => ["http://localhost:43243", prefix, 'token'].join('/'), + user: () => ["http://localhost:43243", prefix, 'users', 'user'].join('/'), telnet_status: () => [host, prefix, 'settings', 'telnet', 'get'].join('/'), telnet_status_set: () => [host, prefix, 'settings', 'telnet', 'set'].join('/'), @@ -39,7 +39,10 @@ const routes = { wifi_channel_get: () => [host, prefix, 'settings', 'wifi', 'channel', 'get'].join('/'), wifi_channel_set: () => [host, prefix, 'settings', 'wifi', 'channel', 'set'].join('/'), + settings_get: () => [host, prefix, 'settings', 'get'].join('/'), + settings_set: () => [host, prefix, 'settings', 'set'].join('/'), + reboot: () => [host, prefix, 'reboot'].join('/'), syslog: () => [host, prefix, 'syslog'].join('/'), uptime: () => [host, prefix, 'uptime'].join('/'), -} \ No newline at end of file +} diff --git a/src/web/js/settings/confirm.js b/src/web/js/settings/confirm.js deleted file mode 100644 index 197ea86..0000000 --- a/src/web/js/settings/confirm.js +++ /dev/null @@ -1,33 +0,0 @@ -const confirmform = document.querySelector('#confirmform') -const confirmformheader = document.querySelector('#confirmformheader') -const cancelbutton = document.querySelector('#cancel') -const confirmformbuttons = document.querySelector('#confirmformbuttons') - -const defaultcancelreqfunc = () => { - confirmform.classList.remove('active') -} - -const get_confirm_form = (header, reqfunc = defaultcancelreqfunc, cancelreqfunc = defaultcancelreqfunc) => { - confirmformheader.innerHTML = header - confirmform.classList.add('active') - - var confirmbutton = document.createElement('button') - confirmbutton.classList.add('formbutton') - confirmbutton.id = "confirm" - confirmbutton.innerHTML = "Confirm" - confirmbutton.addEventListener('click', () => { - reqfunc() - confirmform.classList.remove('active') - }) - confirmformbuttons.replaceChild(confirmbutton, document.querySelector('#confirm')) - - var cancelbutton = document.createElement('button') - cancelbutton.classList.add('formbutton') - cancelbutton.id = "cancel" - cancelbutton.innerHTML = "Cancel" - cancelbutton.addEventListener('click', () => { - cancelreqfunc() - confirmform.classList.remove('active') - }) - confirmformbuttons.replaceChild(cancelbutton, document.querySelector('#cancel')) -} diff --git a/src/web/js/settings/dns.js b/src/web/js/settings/dns.js index a202fab..409d14b 100644 --- a/src/web/js/settings/dns.js +++ b/src/web/js/settings/dns.js @@ -12,46 +12,24 @@ let dnsservers = [] let localdomains = [] const get_dns_enabled_status = () => { - fetch(routes.dns_status(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - dnsenabled.checked = true - dnssettingbody.classList.add('active') - adddnsserverbutton.classList.add('active') - addlocaldomainbutton.classList.add('active') - return; - } else { - adddnsserverbutton.parentElement.classList.add('onebutton') - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_dns_enabled_status = () => { - fetch(routes.dns_status_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":${dnsenabled.checked}}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - dnsenabled.checked = true - dnssettingbody.classList.add('active') - adddnsserverbutton.classList.add('active') - } - notification("Настройки dns сохранены", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + dnsenabled.checked = settings.dns.enabled + if (settings.dns.enabled) { + dnssettingbody.classList.add('active') + adddnsserverbutton.classList.add('active') + addlocaldomainbutton.classList.add('active') + } else { + adddnsserverbutton.parentElement.classList.add('onebutton') + } } const set_dns_settings = () => { - set_dns_enabled_status() - if (dnsenabled.checked) { - set_dns_server() - set_local_domains() - } + settings.dns.enabled = dnsenabled.checked + settings.dns.servers = dnsservers + settings.dns.localdomains = localdomains + set_all_settings() + dnsenabled.checked = settings.dns.enabled + get_dns_server() + get_local_domains() } setdnssettingsbutton.addEventListener('click', () => { @@ -76,41 +54,16 @@ dnsenabled.addEventListener('click', () => { }) const get_dns_server = () => { - fetch(routes.dns_servers_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(res => res.json()).then(jsondata => { - dnsservers = jsondata.servers - dnsserverstablebody.innerHTML = "" - for (const server in jsondata.servers) { - dnsserverstablebody.innerHTML += ` - - ${jsondata.servers[server]} - - - ` - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_dns_server = () => { - start_loader("Load DNS") - fetch(routes.dns_servers_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","servers":${JSON.stringify(dnsservers)}}` - }).then(data => data.json()).then(jsondata => { - stop_loader() - if (jsondata.success) { - get_dns_server() - notification("Настройки dns сохранены", "success") - } else { - notification("Неизвестная ошибка", "error") - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + dnsservers = settings.dns.servers + dnsserverstablebody.innerHTML = "" + for (const server in settings.dns.servers) { + dnsserverstablebody.innerHTML += ` + + ${settings.dns.servers[server]} + + + ` + } } adddnsserverbutton.addEventListener('click', () => { @@ -118,7 +71,7 @@ adddnsserverbutton.addEventListener('click', () => { if (dnsserver === null) return if (confirm('Confirm DNS settings? (add DNS server)')) { dnsservers.push(dnsserver) - set_dns_server() + set_dns_settings() } }) @@ -126,7 +79,7 @@ dnsserverstablebody.addEventListener('click', (e) => { if (e.target.classList.contains('deldnsserverbutton')) { dnsservers.splice(dnsservers.indexOf(e.target.id), 1) if (confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)) { - set_dns_server() + set_dns_settings() } get_dns_server() } @@ -135,42 +88,17 @@ dnsserverstablebody.addEventListener('click', (e) => { // ---------------------- local domains --------------------------- const get_local_domains = () => { - fetch(routes.dns_local_domains_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}"}` - }).then(data => data.json()).then(jsondata => { - localdomains = jsondata.domains - localdomainstablebody.innerHTML = "" - for (const domain in jsondata.domains) { - localdomainstablebody.innerHTML += ` - ${jsondata.domains[domain].ip} - - ${jsondata.domains[domain].domain} - - - ` - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_local_domains = (domains = localdomains) => { - start_loader() - fetch(routes.dns_local_domains_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","domains":${JSON.stringify(domains)}}` - }).then(data => data.json()).then(jsondata => { - stop_loader() - if (jsondata.success) { - get_local_domains() - notification("Настройки dns сохранены", "success") - } else { - notification("Неизвестная ошибка", "error") - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + localdomains = settings.dns.localdomains + localdomainstablebody.innerHTML = "" + for (const domain in settings.dns.localdomains) { + localdomainstablebody.innerHTML += ` + ${settings.dns.localdomains[domain].ip} + + ${settings.dns.localdomains[domain].domain} + + + ` + } } addlocaldomainbutton.addEventListener('click', () => { @@ -180,7 +108,7 @@ addlocaldomainbutton.addEventListener('click', () => { if (domainip === null || domain === null) return if (confirm(`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`)) { localdomains.push({ "ip": domainip, "domain": domain }) - set_local_domains() + set_dns_settings() } }) @@ -188,7 +116,7 @@ localdomainstablebody.addEventListener('click', (e) => { if (e.target.classList.contains('deldomain')) { localdomains.splice(localdomains.indexOf(localdomains.find(dom => dom.domain === e.target.id)), 1) if (confirm(`Confirm DNS settings? (delete Local Domain: ${e.target.id})`)) { - set_local_domains() + set_dns_settings() } get_local_domains() } diff --git a/src/web/js/settings/ftp.js b/src/web/js/settings/ftp.js index 61de91e..a732aca 100644 --- a/src/web/js/settings/ftp.js +++ b/src/web/js/settings/ftp.js @@ -2,33 +2,13 @@ const ftpenabled = document.querySelector('#ftpenable') const setftpsettingsbutton = document.querySelector('#set_ftp_settings') const get_ftp_enabled_status = () => { - fetch(routes.ftp_status(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - ftpenabled.checked = true - // ftpsettingbody.classList.add('active') - return; - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + ftpenabled.checked = settings.ftp.enabled } const set_ftp_enabled_status = () => { - fetch(routes.ftp_status_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":${ftpenabled.checked}}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - ftpenabled.checked = true - // ftpsettingbody.classList.add('active') - } - notification("Настройки ftp сохранены", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + settings.ftp.enabled = ftpenabled.checked + set_all_settings() + ftpenabled.checked = settings.ftp.enabled } setftpsettingsbutton.addEventListener('click', () => { diff --git a/src/web/js/settings/settings.js b/src/web/js/settings/settings.js index 9f1a4db..55d2f7f 100644 --- a/src/web/js/settings/settings.js +++ b/src/web/js/settings/settings.js @@ -1,20 +1,42 @@ -const get_all_settings = () => { - get_dns_enabled_status() - get_dns_server() - get_local_domains() +let settings = {} - get_ftp_enabled_status() - - get_telnet_enabled_status() - get_telnet_port() +const get_all_settings = async () => { + settings = await fetch(routes.settings_get(), { + method: 'POST', + body: `{"token":"${localStorage.getItem("token")}"}` + }).then(data => data.json()).then(jsondata => { + return jsondata.settings + }).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) get_ssh_status() get_ssh_keys() get_ssh_port() get_ssh_server() + get_telnet_enabled_status() + get_telnet_port() + + get_ftp_enabled_status() + + get_dns_enabled_status() + get_dns_server() + get_local_domains() + get_wifi_status() get_wifi_name() get_wifi_channel() - // get_wifi_password() +} + +const set_all_settings = async () => { + settings = await fetch(routes.settings_set(), { + method: 'POST', + body: `{"token":"${localStorage.getItem("token")}","settings":${JSON.stringify(settings)}}` + }).then(res => res.json()).then(jsondata => { + notification("Настройки сохранены", "success") + return jsondata.settings + }).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) } diff --git a/src/web/js/settings/ssh.js b/src/web/js/settings/ssh.js index fbb93cc..f1e4887 100644 --- a/src/web/js/settings/ssh.js +++ b/src/web/js/settings/ssh.js @@ -10,42 +10,34 @@ let sshkeys = [] // -------------------- SSH status -------------------------------- const get_ssh_status = () => { - fetch(routes.ssh_status_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => { - if (jsondata.status === '1') { - sshenable.checked = true - sshsettingsbody.classList.add('active') - add_ssh_key_button.classList.add('active') - add_ssh_key_button.parentElement.classList.remove('onebutton') - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + sshenable.checked = settings.ssh.enabled + if (settings.ssh.enabled) { + sshsettingsbody.classList.add('active') + add_ssh_key_button.classList.add('active') + add_ssh_key_button.parentElement.classList.remove('onebutton') + } } -const set_ssh_status = () => { - fetch(routes.ssh_status_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","status":${sshenable.checked}}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.status === '1') { - sshenable.checked = true - // telnetsettingbody.classList.add('active') +const set_ssh_settings_func = () => { + settings.ssh.enabled = sshenable.checked + settings.ssh.keys = sshkeys + settings.ssh.port = sshportinput.value + settings.ssh.server = sshserverinput.options[sshserverinput.selectedIndex].value + set_all_settings() + sshenable.checked = settings.ssh.enabled + get_ssh_keys() + sshportinput.value = settings.ssh.port + for (let i = 0; i < sshserverinput.options.length; i++) { + if (sshserverinput.options[i].value === settings.ssh.server) { + sshserverinput.selectedIndex = i; + break; } - notification("Настройки SSH сохранены", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + } } -// -------------------------------- end SSH status --------------------------------------- set_ssh_settings.addEventListener('click', () => { if (confirm("Confirm SSH settings?")) { - set_ssh_status() - set_ssh_port() - set_ssh_server() + set_ssh_settings_func() } }) @@ -63,41 +55,16 @@ sshenable.addEventListener('click', () => { // -------------------- SSH keys --------------------------------- const get_ssh_keys = () => { - fetch(routes.ssh_keys_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => { - sshkeys = jsondata.keys - sshkeystablebody.innerHTML = "" - for (const key in sshkeys) { - sshkeystablebody.innerHTML += ` + sshkeys = settings.ssh.keys + sshkeystablebody.innerHTML = "" + for (const key in sshkeys) { + sshkeystablebody.innerHTML += ` ${sshkeys[key]} ` - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_ssh_keys = () => { - start_loader() - fetch(routes.ssh_keys_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","keys":${JSON.stringify(sshkeys)}}` - }).then(data => data.json()).then(jsondata => { - stop_loader() - if (jsondata.success) { - get_ssh_keys() - notification("Настройки SSH сохранены", "success") - } else { - notification("Неизвестная ошибка", "error") - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + } } // ------------------- end SSH keys ------------------------------ @@ -105,7 +72,7 @@ sshkeystablebody.addEventListener('click', (e) => { if (e.target.classList.contains('delsshkeybutton')) { if (confirm(`Confirm SSH settings?: (delete SSH key)`)) { sshkeys.splice(e.target.id) - set_ssh_keys() + set_ssh_settings_func() } } }) @@ -115,63 +82,22 @@ add_ssh_key_button.addEventListener('click', () => { if (sshkey === null) return if (confirm('Confirm SSH settings? (add SSH key)')) { sshkeys.push(sshkey) - set_ssh_keys() + set_ssh_settings_func() } }) // -------------------- SSH port ---------------------- const get_ssh_port = () => { - fetch(routes.ssh_port_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => { - sshportinput.value = jsondata.port - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_ssh_port = () => { - fetch(routes.ssh_port_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","port":"${sshportinput.value}"}` - }).then(data => data.json()).then(jsondata => { - sshportinput.value = jsondata.port - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + sshportinput.value = settings.ssh.port } // ------------------- end SSH port ------------------- const get_ssh_server = () => { - fetch(routes.ssh_server_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => { - for (let i = 0; i < sshserverinput.options.length; i++) { - if (sshserverinput.options[i].value === jsondata.server) { - sshserverinput.selectedIndex = i; - break; - } + for (let i = 0; i < sshserverinput.options.length; i++) { + if (sshserverinput.options[i].value === settings.ssh.server) { + sshserverinput.selectedIndex = i; + break; } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_ssh_server = () => { - fetch(routes.ssh_server_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","server":"${sshserverinput.options[sshserverinput.selectedIndex].value}"}` - }).then(data => data.json()).then(jsondata => { - for (let i = 0; i < sshserverinput.options.length; i++) { - if (sshserverinput.options[i].value === jsondata.server) { - sshserverinput.selectedIndex = i; - break; - } - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + } } diff --git a/src/web/js/settings/telnet.js b/src/web/js/settings/telnet.js index 1bf8ff8..7dd2707 100644 --- a/src/web/js/settings/telnet.js +++ b/src/web/js/settings/telnet.js @@ -1,61 +1,31 @@ const telnetenabled = document.querySelector('#telnetenable') const telnetsettingbody = document.querySelector('.telnetsettingbody') const settelnetsettingsbutton = document.querySelector('#set_telnet_settings') -// const addtelnetuserbutton = document.querySelector('#add_telnet_user_button') - const telnetportinput = document.querySelector('#telnetportinput') const get_telnet_enabled_status = () => { - fetch(routes.telnet_status(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - telnetenabled.checked = true - // addtelnetuserbutton.classList.add('active') - // addtelnetuserbutton.parentElement.classList.remove('onebutton') - telnetsettingbody.classList.add('active') - return; - } else { - // addtelnetuserbutton.parentElement.classList.add('onebutton') - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_telnet_enabled_status = () => { - fetch(routes.telnet_status_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":${telnetenabled.checked}}` - }).then(res => res.json()).then(jsondata => { - if (jsondata.enabled === '1') { - telnetenabled.checked = true - // telnetsettingbody.classList.add('active') - } - notification("Настройки telnet сохранены", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + telnetenabled.checked = settings.telnet.enabled + if (settings.telnet.enabled) { + telnetsettingbody.classList.add('active') + } } telnetenabled.addEventListener('click', () => { if (telnetenabled.checked) { - // addtelnetuserbutton.classList.add('active') - // addtelnetuserbutton.parentElement.classList.remove('onebutton') telnetsettingbody.classList.add('active') } else { telnetsettingbody.classList.remove('active') - // addtelnetuserbutton.classList.remove('active') - // addtelnetuserbutton.parentElement.classList.add('onebutton') } }) const set_telnet_settings = () => { - set_telnet_enabled_status() - if (telnetenabled.checked) { - set_telnet_port() - } + start_loader() + settings.telnet.enabled = telnetenabled.checked + settings.telnet.port = telnetportinput.value + set_all_settings() + telnetenabled.checked = settings.telnet.enabled + telnetportinput.value = settings.telnet.port + stop_loader() } settelnetsettingsbutton.addEventListener('click', () => { @@ -65,26 +35,5 @@ settelnetsettingsbutton.addEventListener('click', () => { }) const get_telnet_port = () => { - fetch(routes.telnet_port_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}"}` - }).then(data => data.json()).then(jsondata => { - telnetportinput.value = jsondata.port - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_telnet_port = () => { - start_loader() - fetch(routes.telnet_port_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","port":"${telnetportinput.value}"}` - }).then(data => data.json()).then(jsondata => { - stop_loader() - telnetportinput.value = jsondata.port - }).catch((error) => { - stop_loader() - notification(`Ошибка на сервере: ${error}`, "error") - }) + telnetportinput.value = settings.telnet.port } diff --git a/src/web/js/settings/wifi.js b/src/web/js/settings/wifi.js index d916bc3..7a5f429 100644 --- a/src/web/js/settings/wifi.js +++ b/src/web/js/settings/wifi.js @@ -13,74 +13,15 @@ const channelselect = document.querySelector('#wifichannel') let old_wifi_name = "" const get_wifi_status = () => { - fetch(routes.wifi_status_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}"}` - }).then(data => data.json()).then(jsondata => { - if (jsondata.status == "1") { - wifistatus.checked = true - wifisettingsbody.classList.add('active') - } else { - wifistatus.checked = false - } - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_wifi_status = () => { - fetch(routes.wifi_status_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","status":${wifistatus.checked}}` - }).then(data => data.json()).then(jsondata => { - if (jsondata.status === "1") { - wifistatus.checked = true - wifisettingsbody.classList.add('active') - notification("Настройки WIFI оспешно сорранены", "success") - return - } - wifistatus.checked = false - wifisettingsbody.classList.remove('active') - notification("Настройки WIFI оспешно сорранены", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + wifistatus.checked = settings.wifi.enabled + if (settings.wifi.enabled) { + wifisettingsbody.classList.add('active') + } } const get_wifi_name = () => { - fetch(routes.wifi_name_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}"}` - }).then(data => data.json()).then(jsondata => { - wifissid.value = jsondata.name - old_wifi_name = jsondata.name - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const set_wifi_name = () => { - fetch(routes.wifi_name_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","name":"${wifissid.value}"}` - }).then(data => data.json()).then(jsondata => { - wifissid.value = jsondata.name - old_wifi_name = jsondata.name - notification("Имя WIFI оспешно сорранено", "success") - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -} - -const get_wifi_password = () => { - fetch(routes.wifi_password_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}"}` - }).then(data => data.json()).then(jsondata => { - wifipassword.value = jsondata.password - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + wifissid.value = settings.wifi.name + old_wifi_name = settings.wifi.name } const set_wifi_password = () => { @@ -103,15 +44,19 @@ const set_wifi_password = () => { const set_wifi_settings = () => { if (confirm("Confirm wifi settings?")) { start_loader() - set_wifi_status() - if (wifistatus.checked) { - set_wifi_channel() - if (old_wifi_name !== wifissid.value) { - set_wifi_name() - } - if (wifipassword.value !== "" && newwifipassword.value !== "") { - set_wifi_password() - } + settings.wifi.enabled = wifistatus.checked + settings.wifi.name = wifissid.value + settings.wifi.channel = channelselect.value + set_all_settings() + wifistatus.checked = settings.wifi.enabled + if (settings.wifi.enabled) { + wifisettingsbody.classList.add('active') + } + wifissid.value = settings.wifi.name + old_wifi_name = settings.wifi.name + channelselect.value = settings.wifi.channel + if (wifipassword.value !== "" && newwifipassword.value !== "") { + set_wifi_password() } stop_loader() } @@ -166,23 +111,5 @@ wifiqr.addEventListener('click', () => { }) const get_wifi_channel = () => { - fetch(routes.wifi_channel_get(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}"}` - }).then(data => data.json()).then(jsondata => { - channelselect.value = jsondata.channel - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) + channelselect.value = settings.wifi.channel } - -const set_wifi_channel = () => { - fetch(routes.wifi_channel_set(), { - method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","channel":"${channelselect.value}"}` - }).then(data => data.json()).then(jsondata => { - channelselect.value = jsondata.channel - }).catch((error) => { - notification(`Ошибка на сервере: ${error}`, "error") - }) -}