From 5a1a3a92a2d24f658148ae3b9c4374ea6d74cfd1 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Sat, 28 Dec 2024 01:05:01 +0700 Subject: [PATCH] added get_ssh_server and set_ssh_server --- CMakeLists.txt | 2 + meson.build | 1 + src/OpenRouter/OpenRouter.cpp | 2 + src/settings/ssh/server.cpp | 80 +++++++++++++++++++++++++++++++++ src/settings/ssh/ssh.hpp | 6 +++ src/web/index.html | 8 ++++ src/web/js/login.js | 13 +++++- src/web/js/routes.js | 5 ++- src/web/js/settings/settings.js | 1 + src/web/js/settings/ssh.js | 18 ++++++++ 10 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 src/settings/ssh/server.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 44d9782..6fb45bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ add_executable( src/settings/ssh/status.cpp src/settings/ssh/keys.cpp src/settings/ssh/port.cpp + src/settings/ssh/server.cpp + src/settings/dns/enabled.cpp src/settings/dns/servers.cpp diff --git a/meson.build b/meson.build index cceb21a..0e21c7e 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,7 @@ executable( 'src/settings/ssh/status.cpp', 'src/settings/ssh/keys.cpp', 'src/settings/ssh/port.cpp', + 'src/settings/ssh/server.cpp', 'src/settings/dns/enabled.cpp', 'src/settings/dns/servers.cpp', diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index 37063a3..cc631f7 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -18,6 +18,8 @@ OpenRouter::OpenRouter() this->srv.Post("/api/settings/ssh/keys/set", api::settings::set_ssh_keys); this->srv.Post("/api/settings/ssh/port/get", api::settings::get_ssh_port); this->srv.Post("/api/settings/ssh/port/set", api::settings::set_ssh_port); + this->srv.Post("/api/settings/ssh/server/get", api::settings::get_ssh_server_http); + this->srv.Post("/api/settings/ssh/server/set", api::settings::set_ssh_server_http); this->srv.Post("/api/settings/dns/get", api::settings::get_dns); this->srv.Post("/api/settings/dns/set", api::settings::set_dns); diff --git a/src/settings/ssh/server.cpp b/src/settings/ssh/server.cpp new file mode 100644 index 0000000..1d4601d --- /dev/null +++ b/src/settings/ssh/server.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include +#include + +#define SSH_SERVER_REQUIRED_STRING R"({"required":"[token,server]"})" + +namespace api +{ + namespace settings + { + std::string get_ssh_server() + { + std::ifstream serverfile("/etc/openrouter/ssh/server"); + std::string ssh_server = ""; + serverfile >> ssh_server; + serverfile.close(); + return ssh_server; + } + + std::string set_ssh_server(std::string &server) + { + std::ofstream serverfile("/etc/openrouter/ssh/server"); + serverfile << server; + serverfile.close(); + return get_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"]; + responsedata << R"({"server":")" << set_ssh_server(server) << R"("})"; + response.set_content(responsedata.str(), JSON_TYPE); + } + } +} \ No newline at end of file diff --git a/src/settings/ssh/ssh.hpp b/src/settings/ssh/ssh.hpp index d00ccc6..cd0ecdb 100644 --- a/src/settings/ssh/ssh.hpp +++ b/src/settings/ssh/ssh.hpp @@ -7,6 +7,12 @@ namespace api { namespace settings { + 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); diff --git a/src/web/index.html b/src/web/index.html index 3e132b6..e8a14fa 100644 --- a/src/web/index.html +++ b/src/web/index.html @@ -120,6 +120,14 @@
+
+ + + +
diff --git a/src/web/js/login.js b/src/web/js/login.js index 37e6fdd..b415053 100644 --- a/src/web/js/login.js +++ b/src/web/js/login.js @@ -6,6 +6,15 @@ const exitbutton = document.querySelector('.exitbutton') const adminpanelsection = document.querySelector('.adminpanel') const headerusername = document.querySelector('.headerusername') +const hashPassword = async (password) => { + const encoder = new TextEncoder() + const data = encoder.encode(password) + const hashBuffer = await crypto.subtle.digest('SHA-256', data) + const hashArray = Array.from(new Uint8Array(hashBuffer)) + const hashHex = hashArray.map(b=>('00' + b.toString(16)).slice(-2)).join('') + return hashHex +} + if (localStorage.getItem("token") === null) { adminpanelsection.classList.remove('active') loginform.classList.add('active') @@ -17,7 +26,9 @@ if (localStorage.getItem("token") === null) { loginformbody.addEventListener('submit', (e) => { e.preventDefault() - login(loginusername.value, loginpassword.value) + hashPassword(loginpassword.value).then(hashedpassword => { + login(loginusername.value, hashedpassword) + }) }) const login = async (username, password) => { diff --git a/src/web/js/routes.js b/src/web/js/routes.js index 58b5520..1c38016 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('/'), @@ -16,6 +16,7 @@ const routes = { ssh_keys_set: () => [host, prefix, 'settings', 'ssh', 'keys', 'set'].join('/'), ssh_port_get: () => [host, prefix, 'settings', 'ssh', 'port', 'get'].join('/'), ssh_port_set: () => [host, prefix, 'settings', 'ssh', 'port', 'set'].join('/'), + ssh_server_get: () => [host, prefix, 'settings', 'ssh', 'server', 'get'].join('/'), dns_status: () => [host, prefix, 'settings', 'dns', 'get'].join('/'), dns_status_set: () => [host, prefix, 'settings', 'dns', 'set'].join('/'), diff --git a/src/web/js/settings/settings.js b/src/web/js/settings/settings.js index 5cdc882..9f1a4db 100644 --- a/src/web/js/settings/settings.js +++ b/src/web/js/settings/settings.js @@ -11,6 +11,7 @@ const get_all_settings = () => { get_ssh_status() get_ssh_keys() get_ssh_port() + get_ssh_server() get_wifi_status() get_wifi_name() diff --git a/src/web/js/settings/ssh.js b/src/web/js/settings/ssh.js index 73f46ce..7ef21d3 100644 --- a/src/web/js/settings/ssh.js +++ b/src/web/js/settings/ssh.js @@ -4,6 +4,7 @@ const sshsettingsbody = document.querySelector('#sshsettingsbody') const sshkeystablebody = document.querySelector('#sshkeystablebody') const sshportinput = document.querySelector('#sshportinput') const add_ssh_key_button = document.querySelector('#add_ssh_key') +const sshserverinput = document.querySelector('#sshserverinput') let sshkeys = [] @@ -140,3 +141,20 @@ const set_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; + } + } + }).catch((error) => { + notification(`Ошибка на сервере: ${error}`, "error") + }) +} \ No newline at end of file