added sshkeys and ssh port

This commit is contained in:
wt committed 2024-07-02 17:28:25 +07:00
1 parent f58b62f001
commit 5fbd56ef7a
15 files changed
+528 -13

No files matched your search

+4
View File
@@ -12,6 +12,10 @@ add_executable(
src/settings/telnet/enabled.cpp
src/settings/telnet/port.cpp
src/settings/ssh/status.cpp
src/settings/ssh/keys.cpp
src/settings/ssh/port.cpp
src/settings/dns/enabled.cpp
src/settings/dns/servers.cpp
src/settings/dns/local_domains.cpp
+8
View File
@@ -2,6 +2,7 @@
#include <httplib.h>
#include <colors.h>
#include "settings/telnet/telnet.hpp"
#include "settings/ssh/ssh.hpp"
#include "settings/dns/dns.hpp"
#include "settings/ftp/ftp.hpp"
#include "reboot/reboot.hpp"
@@ -17,6 +18,13 @@ int main() {
srv.Post("/api/settings/telnet/port/get", get_telnet_port);
srv.Post("/api/settings/telnet/port/set", set_telnet_port);
srv.Post("/api/settings/ssh/status/get", get_ssh_status);
srv.Post("/api/settings/ssh/status/set", set_ssh_status);
srv.Post("/api/settings/ssh/keys/get", get_ssh_keys);
srv.Post("/api/settings/ssh/keys/set", set_ssh_keys);
srv.Post("/api/settings/ssh/port/get", get_ssh_port);
srv.Post("/api/settings/ssh/port/set", set_ssh_port);
srv.Post("/api/settings/dns/get", get_dns);
srv.Post("/api/settings/dns/set", set_dns);
srv.Post("/api/settings/dns/servers/get", get_dns_servers);
+93
View File
@@ -0,0 +1,93 @@
#include "ssh.hpp"
#include <colors.h>
#include <json.hpp>
#include "../../routes.hpp"
#include "../../types.hpp"
#define KEYS_PATH "/root/.ssh/authorized_keys"
void get_ssh_keys(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream keysfile(KEYS_PATH);
std::stringstream responsedata;
responsedata << R"({"keys":[)";
std::string key_string = "";
int iter = 0;
while (std::getline(keysfile, key_string)) {
if (key_string == "") {
continue;
}
responsedata << R"(")" << key_string << R"(",)";
key_string = "";
iter++;
}
if (iter != 0) {
responsedata.str(responsedata.str().erase(responsedata.str().size()-1, 1) + "]}");
} else {
responsedata << "]}";
}
keysfile.close();
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_ssh_keys(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ofstream keysfile(KEYS_PATH);
nlohmann::json keys = json_body["keys"];
for (std::string key : keys) {
keysfile << key << "\n";
}
keysfile.close();
system("/etc/init.d/ssh restart");
std::string responsedata = R"({"success":"true"})";
response.set_content(responsedata, JSON_TYPE);
}
}
+101
View File
@@ -0,0 +1,101 @@
#include "ssh.hpp"
#include <colors.h>
#include "../../types.hpp"
#include "../../routes.hpp"
#include <json.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
#define SSH_PORT_PATH "/etc/openrouter/ssh/port"
void get_ssh_port(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_ssh_port(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["port"] == nullptr) {
response.set_content(SSH_PORT_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream oldsshportfile(SSH_PORT_PATH);
int old_ssh_port;
oldsshportfile >> old_ssh_port;
oldsshportfile.close();
std::ofstream sshportfileout(SSH_PORT_PATH);
std::string new_ssh_port = json_body["port"];
sshportfileout << stoi(new_ssh_port);
sshportfileout.close();
std::stringstream systemrequest;
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config";
std::cout << systemrequest.str() << std::endl;
system(systemrequest.str().c_str());
system("/etc/init.d/ssh restart");
std::ifstream sshportfile(SSH_PORT_PATH);
int ssh_port;
sshportfile >> ssh_port;
sshportfile.close();
std::stringstream responsedata;
responsedata << R"({"port":")" << ssh_port << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef SSH_HPP
#define SSH_HPP
#include <httplib.h>
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);
#endif // SSH_HPP
+94
View File
@@ -0,0 +1,94 @@
#include "ssh.hpp"
#include <colors.h>
#include <json.hpp>
#include "../../routes.hpp"
#include "../../types.hpp"
#define SSH_REQUIRED_STRING R"({"required":"[token,status]"})"
void get_ssh_status(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ifstream sshstatusfile("/etc/openrouter/ssh/status");
int ssh_status;
sshstatusfile >> ssh_status;
sshstatusfile.close();
std::stringstream responsedata;
responsedata << R"({"status":")" << ssh_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
void set_ssh_status(const httplib::Request &request, httplib::Response &response) {
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["token"] == nullptr) {
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["status"] == nullptr) {
response.set_content(SSH_REQUIRED_STRING, JSON_TYPE);
return;
}
httplib::Client cli(AUTH_SERVICE);
std::stringstream body;
body << R"({"token":)" << json_body["token"] << R"(})";
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
if (res->status == httplib::OK_200) {
nlohmann::json resjson = nlohmann::json::parse(res->body);
if (resjson["access"] == "reject") {
response.set_content(resjson.dump().c_str(), JSON_TYPE);
return;
}
std::ofstream sshfileout("/etc/openrouter/ssh/status");
std::string new_ssh_status = json_body["status"];
if (new_ssh_status == "0") {
system("/etc/init.d/ssh stop");
system("rm /etc/rc.d/75ssh");
} else {
system("/etc/init.d/ssh start");
system("ln -sv /etc/init.d/ssh /etc/rc.d/75ssh");
}
sshfileout << stoi(new_ssh_status);
sshfileout.close();
std::ifstream sshfile("/etc/openrouter/ssh/status");
int ssh_status;
sshfile >> ssh_status;
sshfile.close();
std::stringstream responsedata;
responsedata << R"({"status":")" << ssh_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
+5 -1
View File
@@ -66,6 +66,7 @@
}
.settingssection {
padding: 10px;
display: flex;
/* flex-direction: column; */
gap: 10px;
@@ -93,7 +94,7 @@
.settingrectbody {
width: 100%;
max-height: 0;
overflow: hidden;
overflow: auto;
transition: 0.3s;
}
@@ -103,6 +104,7 @@
}
.settingrectbody>table {
position: relative;
width: 100%;
text-align: left;
transition: 0.3s;
@@ -178,6 +180,8 @@ tbody>tr>th {
padding: 10px;
border-radius: 10px;
width: 50%;
max-width:425px;
overflow-x: auto;
/* display: flex;
align-items: center;
justify-content: space-between; */
+28
View File
@@ -114,6 +114,33 @@
</div>
</div>
<!-- ftp settings -->
<!-- ssh settings -->
<div class="settingrect">
<div class="settingrectheader">
<h2>SSH</h2>
<div class="checkbox-wrapper-2">
<input type="checkbox" class="sc-gJwTLC ikxBAC" id="sshenable">
</div>
</div>
<div class="settingrectbody" id="sshsettingsbody">
<div style="display:flex;align-items:center;justify-content:space-between;">
<label for="sshportinput" class="forminputlabel">Port</label>
<input type="text" id="sshportinput" class="forminput">
</div>
<table class="sshkeys">
<thead>
<tr><th>SSH keys</th></tr>
</thead>
<tbody id="sshkeystablebody">
</tbody>
</table>
</div>
<div class="settingrectfooter onebutton">
<button id="add_ssh_key" class="hiddenbutton settingrectfooterbutton">Add SSH key</button>
<button class="settingrectfooterbutton" id="set_ssh_settings">Confirm</button>
</div>
</div>
<!-- ssh settings -->
</section>
<!-- settings block -->
</section>
@@ -200,6 +227,7 @@
<script src="js/settings/telnet.js"></script>
<script src="js/settings/dns.js"></script>
<script src="js/settings/ftp.js"></script>
<script src="js/settings/ssh.js"></script>
<script src="js/settings/settings.js"></script>
<script src="js/reboot.js"></script>
+3 -1
View File
@@ -1,7 +1,9 @@
const rebootbutton = document.querySelector('#rebootbutton')
rebootbutton.addEventListener('click', () => {
get_confirm_form("Confirm reboot?", reboot)
if (confirm("Confirm reboot?")) {
reboot()
}
})
const reboot = () => {
+7
View File
@@ -11,6 +11,13 @@ const routes = {
telnet_port_get: () => [host, prefix, 'settings', 'telnet', 'port', 'get'].join('/'),
telnet_port_set: () => [host, prefix, 'settings', 'telnet', 'port', 'set'].join('/'),
ssh_status_get: () => [host, prefix, 'settings', 'ssh', 'status', 'get'].join('/'),
ssh_status_set: () => [host, prefix, 'settings', 'ssh', 'status', 'set'].join('/'),
ssh_keys_get: () => [host, prefix, 'settings', 'ssh', 'keys', 'get'].join('/'),
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('/'),
dns_status: () => [host, prefix, 'settings', 'dns', 'get'].join('/'),
dns_status_set: () => [host, prefix, 'settings', 'dns', 'set'].join('/'),
dns_servers_get: () => [host, prefix, 'settings', 'dns', 'servers', 'get'].join('/'),
+18 -9
View File
@@ -65,7 +65,9 @@ const set_dns_settings = () => {
}
setdnssettingsbutton.addEventListener('click', () =>{
get_confirm_form("Confirm DNS settings?", set_dns_settings)
if (confirm("Confirm DNS settings?")) {
set_dns_settings()
}
})
dnsenabled.addEventListener('click', () => {
@@ -144,13 +146,19 @@ adddnsserverconfirmbutton.addEventListener('click', () => {
if (adddnsserverinput.value === "") {
return
}
get_confirm_form(`Confirm DNS settings? (Add DNS server: ${adddnsserverinput.value})`, set_dns_server, cancel_confirm_add_dns_server)
if (confirm(`Confirm DNS settings? (Add DNS server: ${adddnsserverinput.value})`)) {
set_dns_server()
} else {
cancel_confirm_add_dns_server
}
})
dnsserverstablebody.addEventListener('click', (e) => {
if (e.target.classList.contains('deldnsserverbutton')) {
dnsservers.splice(dnsservers.indexOf(e.target.id), 1)
set_dns_server()
if (confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)) {
set_dns_server()
}
get_dns_server()
}
})
@@ -213,21 +221,22 @@ addlocaldomaincancelbutton.addEventListener('click', () => {
addlocaldomainconfirmbutton.addEventListener('click', () => {
addlocaldomainform.classList.remove('active')
get_confirm_form(`Confirm DNS settings? (Add Local Domain: ${ipinput.value} ${domaininput.value})`, () => {
if (confirm(`Confirm DNS settings? (Add Local Domain: ${ipinput.value} ${domaininput.value})`)) {
localdomains.push({"ip": ipinput.value, "domain": domaininput.value});
set_local_domains()
ipinput.value = ""
domaininput.value = ""
}, cancel_confirm_add_localdomain)
} else {
cancel_confirm_add_localdomain()
}
})
domainstablebody.addEventListener('click', (e) => {
if (e.target.classList.contains('deldomain')) {
// const delel = localdomains.find(dom => dom.domain === e.target.id)
// delete localdomains.domains[localdomains.indexOf(delel)]
localdomains.splice(localdomains.indexOf(localdomains.find(dom => dom.domain === e.target.id)), 1)
// get_confirm_form(`Confirm DNS settings? (delete Local Domain: ${e.target.id})`, () => set_local_domains(localdomains))
set_local_domains()
if (confirm(`Confirm DNS settings? (delete Local Domain: ${e.target.id})`)) {
set_local_domains()
}
get_local_domains()
}
})
+3 -1
View File
@@ -32,5 +32,7 @@ const set_ftp_enabled_status = () => {
}
setftpsettingsbutton.addEventListener('click', () =>{
get_confirm_form("Confirm ftp settings?", set_ftp_enabled_status)
if (confirm("Confirm ftp settings?")) {
set_ftp_enabled_status()
}
})
+4
View File
@@ -7,4 +7,8 @@ const get_all_settings = () => {
get_telnet_enabled_status()
get_telnet_port()
get_ssh_status()
get_ssh_keys()
get_ssh_port()
}
+142
View File
@@ -0,0 +1,142 @@
const sshenable = document.querySelector('#sshenable')
const set_ssh_settings = document.querySelector('#set_ssh_settings')
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')
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")
})
}
const set_ssh_status = () => {
fetch(routes.ssh_status_set(), {
method: 'POST',
body: `{"token":"${localStorage.getItem("token")}","status":"${sshenable.checked?1:0}"}`
}).then(res => res.json()).then(jsondata => {
if (jsondata.status === '1') {
sshenable.checked = true
// telnetsettingbody.classList.add('active')
}
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()
}
})
sshenable.addEventListener('click', () => {
if (sshenable.checked) {
sshsettingsbody.classList.add('active')
add_ssh_key_button.classList.add('active')
add_ssh_key_button.parentElement.classList.remove('onebutton')
return
}
sshsettingsbody.classList.remove('active')
add_ssh_key_button.classList.remove('active')
add_ssh_key_button.parentElement.classList.add('onebutton')
})
// -------------------- SSH keys ---------------------------------
const get_ssh_keys = () => {
fetch(routes.ssh_keys_get(), {
method: 'POST',
body: `{"token":"${localStorage.getItem("token")}"}`
}).then(data => data.json()).then(jsondata => {
console.log(jsondata.keys)
sshkeys = jsondata.keys
sshkeystablebody.innerHTML = ""
for (const key in sshkeys) {
sshkeystablebody.innerHTML += `<tr>
<td class="one" style="flex-direction:column;align-items:start;">
${sshkeys[key]}
<button class="delsshkeybutton" id="${key}">Del</button>
</td>
</tr>`
}
}).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 ------------------------------
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()
}
}
})
add_ssh_key_button.addEventListener('click', () => {
const sshkey = prompt('Get your SSH key:')
if (confirm('Confirm SSH settings? (add SSH key)')) {
sshkeys.push(sshkey)
set_ssh_keys()
}
})
// -------------------- 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")
})
}
// ------------------- end SSH port -------------------
+3 -1
View File
@@ -59,7 +59,9 @@ const set_telnet_settings = () => {
}
settelnetsettingsbutton.addEventListener('click', () => {
get_confirm_form("Confirm telnet settings?", set_telnet_settings)
if (confirm("Confirm telnet settings?")) {
set_telnet_settings()
}
})
const get_telnet_port = () => {