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/keys.cpp b/src/settings/ssh/keys.cpp index 3724f76..c183e6d 100644 --- a/src/settings/ssh/keys.cpp +++ b/src/settings/ssh/keys.cpp @@ -6,7 +6,7 @@ #include #include -#define KEYS_PATH "/root/.ssh/authorized_keys" +#define KEYS_PATH "~/.ssh/authorized_keys" namespace api { diff --git a/src/settings/ssh/port.cpp b/src/settings/ssh/port.cpp index b8c7a93..f51ca8b 100644 --- a/src/settings/ssh/port.cpp +++ b/src/settings/ssh/port.cpp @@ -73,10 +73,22 @@ namespace api sshportfileout << stoi(new_ssh_port); sshportfileout.close(); std::stringstream systemrequest; - systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config"; + if (get_ssh_server() == DROPBEAR_SSH) + { + systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" /etc/default/dropbear"; + } else if (get_ssh_server() == OPENSSH_SSH) + { + systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config"; + } std::cout << systemrequest.str() << std::endl; system(systemrequest.str().c_str()); - system("rc restart ssh"); + if (get_ssh_server() == OPENSSH_SSH) + { + system("rc restart ssh"); + } else if (get_ssh_server() == DROPBEAR_SSH) + { + system("rc restart dropbear"); + } std::string syslogstring = "SSH port changed to " + new_ssh_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); std::ifstream sshportfile(SSH_PORT_PATH); diff --git a/src/settings/ssh/server.cpp b/src/settings/ssh/server.cpp new file mode 100644 index 0000000..ff48465 --- /dev/null +++ b/src/settings/ssh/server.cpp @@ -0,0 +1,93 @@ +#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"]; + std::string new_server = set_ssh_server(server); + if (new_server == DROPBEAR_SSH) + { + system("rc stop ssh"); + system("rc del ssh"); + system("rc start dropbear"); + system("rc add dropbear"); + } else if (new_server == OPENSSH_SSH) { + system("rc stop dropbear"); + system("rc del dropbear"); + system("rc start ssh"); + system("rc add ssh"); + } + responsedata << R"({"server":")" << new_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..f79b04f 100644 --- a/src/settings/ssh/ssh.hpp +++ b/src/settings/ssh/ssh.hpp @@ -3,10 +3,19 @@ #include +#define DROPBEAR_SSH "dropbear" +#define OPENSSH_SSH "openssh" + 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/settings/ssh/status.cpp b/src/settings/ssh/status.cpp index a54a71a..8f2cd48 100644 --- a/src/settings/ssh/status.cpp +++ b/src/settings/ssh/status.cpp @@ -63,14 +63,26 @@ namespace api std::string new_ssh_status = json_body["status"]; if (new_ssh_status == "0") { - system("rc stop ssh"); - system("rc del ssh"); + if (get_ssh_server() == DROPBEAR_SSH) + { + system("rc stop dropbear"); + system("rc del dropbear"); + } else if (get_ssh_server() == OPENSSH_SSH) { + system("rc stop ssh"); + system("rc del ssh"); + } syslog(LOG_INFO, "%s", "SSH is disabled"); } else { - system("rc start ssh"); - system("rc add ssh"); + if (get_ssh_server() == DROPBEAR_SSH) + { + system("rc start dropbear"); + system("rc add dropbear"); + } else if (get_ssh_server() == OPENSSH_SSH) { + system("rc start ssh"); + system("rc add ssh"); + } syslog(LOG_INFO, "%s", "SSH is enabled"); } sshfileout << stoi(new_ssh_status); 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..0492da9 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,8 @@ 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('/'), + ssh_server_set: () => [host, prefix, 'settings', 'ssh', 'server', 'set'].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..543a497 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 = [] @@ -44,6 +45,7 @@ set_ssh_settings.addEventListener('click', () => { if (confirm("Confirm SSH settings?")) { set_ssh_status() set_ssh_port() + set_ssh_server() } }) @@ -140,3 +142,36 @@ 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") + }) +} + +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") + }) +}