fully upgrade to using config file as config.json and fixed css #15

Merged
wt merged 8 commits from auth_service_config_file into main 2025-02-24 13:51:51 +07:00
26 changed files with 273 additions and 48 deletions
Showing only changes of commit 4b2d425d13 - Show all commits
+1
View File
@@ -10,6 +10,7 @@ add_executable(
src/security/cors.cpp
src/settings/settings.cpp
src/settings/backup.cpp
src/settings/telnet/enabled.cpp
src/settings/telnet/port.cpp
+1
View File
@@ -4,6 +4,7 @@ CXXFLAGS=-Wall -I src/libs -I src -std=c++20
SOURCES=src/openrouter.cpp \
src/security/cors.cpp \
src/settings/settings.cpp \
src/settings/backup.cpp \
src/settings/telnet/enabled.cpp \
src/settings/telnet/port.cpp \
src/settings/ssh/status.cpp \
+7 -3
View File
@@ -1,5 +1,9 @@
{
"auth_service": "http://127.0.0.1:43243",
"server": {
"ip": "0.0.0.0",
"port": 43244
},
"settings": {
"dns": {
"enabled": false,
@@ -31,10 +35,10 @@
},
"telnet": {
"enabled": false,
"port": "29"
"port": "23"
},
"wifi": {
"channel": "1",
"channel": "11",
"enabled": false,
"name": "home",
"password": "OpenRouterPassword"
@@ -45,4 +49,4 @@
"js": "../src/web/js",
"root": "../src/web"
}
}
}
+52
View File
@@ -0,0 +1,52 @@
{
"auth_service": "http://127.0.0.1:43243",
"server": {
"ip": "0.0.0.0",
"port": 43244
},
"settings": {
"dns": {
"enabled": false,
"localdomains": [
{
"domain": "localhost",
"ip": "127.0.0.1"
},
{
"domain": "openrouter.local.com",
"ip": "192.168.200.1"
}
],
"servers": [
"8.8.8.8"
]
},
"ftp": {
"enabled": false
},
"ssh": {
"enabled": false,
"keys": [
"key1",
"key2"
],
"port": "22",
"server": "dropbear"
},
"telnet": {
"enabled": false,
"port": "23"
},
"wifi": {
"channel": "11",
"enabled": false,
"name": "home",
"password": "OpenRouterPassword"
}
},
"static": {
"css": "../src/web/css",
"js": "../src/web/js",
"root": "../src/web"
}
}
+1
View File
@@ -6,6 +6,7 @@ executable(
'src/security/cors.cpp',
'src/settings/settings.cpp',
'src/settings/backup.cpp',
'src/settings/telnet/enabled.cpp',
'src/settings/telnet/port.cpp',
+3
View File
@@ -13,6 +13,9 @@ OpenRouter::OpenRouter()
this->srv.Post("/api/settings/get", api::settings::get);
this->srv.Post("/api/settings/set", api::settings::set);
this->srv.Get("/api/settings/backup", api::settings::backup);
this->srv.Post("/api/settings/restore", api::settings::restore);
this->srv.Post("/api/settings/restore/defaults", api::settings::restore_defaults);
this->srv.Post("/api/reboot", api::system::reboot);
-1
View File
@@ -6,7 +6,6 @@
bool authenticate(const std::string &token)
{
// config::config.setConfig(std::string("../config.json"));
httplib::Client cli(config::config.getConfig()["auth_service"]);
std::string body = R"({"token":")" + token + R"("})";
+1 -1
View File
@@ -3,4 +3,4 @@
namespace config {
static ConfigManager config(std::string("../config.json"));
}
}
+3
View File
@@ -38,6 +38,9 @@ public:
this->config = nullptr;
this->configfile.close();
}
std::string getConfigFileName() {
return this->configFileName;
}
nlohmann::json getConfig()
{
return this->config;
+2 -1
View File
@@ -1,4 +1,5 @@
#include <OpenRouter/OpenRouter.hpp>
#include <config/config.hpp>
int main()
{
@@ -6,6 +7,6 @@ int main()
#ifdef TEST
return 0;
#else
return router.serve("0.0.0.0", 43244);
return router.serve(config::config.getConfig()["server"]["ip"], config::config.getConfig()["server"]["port"]);
#endif
}
+64
View File
@@ -0,0 +1,64 @@
#include <settings/settings.hpp>
#include <config/config.hpp>
#include <types.hpp>
#include <filesystem>
#include <colors.h>
#include <helpers.hpp>
namespace api
{
namespace settings
{
void backup(const httplib::Request& request, httplib::Response& response)
{
std::ifstream backup_file(config::config.getConfigFileName());
std::stringstream backup_file_buffer;
backup_file_buffer << backup_file.rdbuf();
backup_file.close();
response.set_content(backup_file_buffer.str(), JSON_TYPE);
}
void restore(const httplib::Request& request, httplib::Response& response)
{
std::size_t size = request.files.size();
std::cout << "file size::" << size << std::endl;
bool ret = request.has_file("restore_settings");
std::cout << "file ret::" << ret << std::endl;
const httplib::MultipartFormData& file = request.get_file_value("restore_settings");
if (file.content_type == JSON_TYPE) {
nlohmann::json new_config = nlohmann::json::parse(file.content);
config::config.setConfig(new_config);
config::config.saveConfig();
set_all_settings(config::config.getConfig());
response.set_content(R"({"status":true})", JSON_TYPE);
system("rc restart auth_service");
system("rc restart openrouter");
return;
}
response.set_content(R"({"status":false})", JSON_TYPE);
}
void restore_defaults(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
if (std::filesystem::exists(config::config.getConfigFileName())) {
std::filesystem::remove(config::config.getConfigFileName());
std::filesystem::copy("../config_default.json", config::config.getConfigFileName());
usleep(300);
config::config.setConfig(config::config.getConfigFileName());
set_all_settings(config::config.getConfig());
response.set_content(R"({"status":true})", JSON_TYPE);
system("rc restart auth_service");
system("rc restart openrouter");
return;
}
response.set_content(R"({"status":false})", JSON_TYPE);
}
}
}
+45 -19
View File
@@ -13,6 +13,30 @@ namespace api
{
namespace settings
{
void set_all_settings(nlohmann::json json_body)
{
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"]);
}
void get(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
@@ -29,7 +53,7 @@ namespace api
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
config::config.setConfig(std::string("../config.json"));
config::config.setConfig(config::config.getConfigFileName());
std::string responsedata = R"({"settings":)" + config::config.getConfig()["settings"].dump() + R"(})";
response.set_content(responsedata, JSON_TYPE);
}
@@ -56,33 +80,35 @@ namespace api
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
sleep(1);
config::config.setConfig(std::string("../config.json"));
usleep(300);
config::config.setConfig(config::config.getConfigFileName());
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"]);
set_all_settings(json_body);
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::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::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::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::ftp::set_ftp(json_body["settings"]["ftp"]["enabled"]);
// 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::dns::set_dns(json_body["settings"]["dns"]["enabled"]);
api::settings::dns::set_dns_servers(json_body["settings"]["dns"]["servers"]);
api::settings::dns::set_local_domains(json_body["settings"]["dns"]["localdomains"]);
// api::settings::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);
+4
View File
@@ -11,6 +11,10 @@ namespace api
{
namespace settings
{
void set_all_settings(nlohmann::json json_body);
void backup(const httplib::Request& request, httplib::Response& response);
void restore(const httplib::Request& request, httplib::Response& response);
void restore_defaults(const httplib::Request& request, httplib::Response& response);
void get(const httplib::Request& request, httplib::Response& response);
void set(const httplib::Request& request, httplib::Response& response);
}
+6 -10
View File
@@ -4,9 +4,11 @@
#include <fstream>
#include <string>
#include <syslog.h>
#include <config/config.hpp>
#define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})"
#define SSH_PORT_PATH "/etc/openrouter/ssh/port"
#define DROPBEAR_DEFAULT_FILE "/etc/default/dropbear"
#define SSHD_CONFIG_FILE "/etc/ssh/sshd_config"
namespace api
{
@@ -16,21 +18,15 @@ namespace api
{
void set_ssh_port(int new_ssh_port)
{
std::ifstream oldsshportfile(SSH_PORT_PATH);
int old_ssh_port;
oldsshportfile >> old_ssh_port;
oldsshportfile.close();
std::ofstream sshportfileout(SSH_PORT_PATH);
sshportfileout << new_ssh_port;
sshportfileout.close();
std::string old_ssh_port = config::config.getConfig()["settings"]["ssh"]["port"];
std::stringstream systemrequest;
if (get_ssh_server() == DROPBEAR_SSH)
{
systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" /etc/default/dropbear";
systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" " << DROPBEAR_DEFAULT_FILE;
}
else if (get_ssh_server() == OPENSSH_SSH)
{
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config";
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" " << SSHD_CONFIG_FILE;
}
std::cout << systemrequest.str() << std::endl;
system(systemrequest.str().c_str());
+1 -1
View File
@@ -11,7 +11,7 @@ namespace api
{
std::string get_ssh_server()
{
config::config.setConfig(std::string("../config.json"));
config::config.setConfig(config::config.getConfigFileName());
return config::config.getConfig()["settings"]["ssh"]["server"];
}
+9
View File
@@ -1,6 +1,10 @@
#include <settings/telnet/telnet.hpp>
#include <syslog.h>
#include <iostream>
#include <sstream>
#include <config/config.hpp>
#define TELNET_PORT_FILE "/etc/default/telnet"
namespace api
{
@@ -10,6 +14,11 @@ namespace api
{
void set_telnet_port(int new_telnet_port)
{
std::string old_telnet_port = config::config.getConfig()["settings"]["telnet"]["port"];
std::stringstream systemrequest;
systemrequest << "sed -i \"s/TELNET_PORT=" << old_telnet_port << "/TELNET_PORT=" << new_telnet_port << "/g\" " << TELNET_PORT_FILE;
std::cout << systemrequest.str() << std::endl;
system(systemrequest.str().c_str());
system("rc restart telnet");
std::string syslogstring = "Telnet port changed to " + new_telnet_port;
syslog(LOG_INFO, "%s", syslogstring.c_str());
+2 -2
View File
@@ -61,8 +61,8 @@ namespace api
system("rc restart hostapd");
std::string syslogstring = "WIFI password changed";
syslog(LOG_INFO, "%s", syslogstring.c_str());
sleep(1);
config::config.setConfig(std::string("../config.json"));
usleep(500);
config::config.setConfig(config::config.getConfigFileName());
nlohmann::json new_config = config::config.getConfig();
new_config["settings"]["wifi"]["password"] = new_wifi_password;
config::config.setConfig(new_config);
+1 -1
View File
@@ -30,7 +30,7 @@ namespace api
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
config::config.setConfig(std::string("../config.json"));
config::config.setConfig(config::config.getConfigFileName());
std::string wifi_password = config::config.getConfig()["settings"]["wifi"]["password"];
std::string wifi_name = config::config.getConfig()["settings"]["wifi"]["name"];
std::stringstream toqrstring;
+7 -3
View File
@@ -197,11 +197,14 @@
<div class="settingrectheader">
<h2>Backup settings</h2>
</div>
<div class="settingrectfooter">
<a href="http://localhost:43244/config/config.json">
<button class="settingrectfooterbutton" id="backup_settings">Backup settings</button>
<div class="settingrectfooter" style="flex-direction: column;">
<a id="backup_settings" class="settingrectfooterbutton" href="" download="backup_settings.json">
<button class="settingrectfooterbutton">Backup settings</button>
</a>
<input type="file" name="" id="restore_settings_input" hidden>
<button class="settingrectfooterbutton" id="restore_settings">Restore settings</button>
<button class="settingrectfooterbutton" id="start_restore_settings">Start restore settings</button>
<button class="settingrectfooterbutton" id="restore_default_settings">Rrestore default settings</button>
</div>
</div>
<!-- backup settings -->
@@ -284,6 +287,7 @@
<script src="js/settings/ftp.js"></script>
<script src="js/settings/ssh.js"></script>
<script src="js/settings/wifi.js"></script>
<script src="js/settings/backup.js"></script>
<script src="js/reboot.js"></script>
<script src="js/syslog.js"></script>
-6
View File
@@ -45,9 +45,3 @@ infobutton.addEventListener('click', () => {
infosection.classList.add('active')
uptimeintervalid = setInterval(get_uptime, 1000)
})
// if (window.screenX > 600) {
// header.removeChild(headermenubutton)
// } else {
// header.prepend(headermenubutton)
// }
+3
View File
@@ -10,6 +10,9 @@ const routes = {
settings_get: () => [host, prefix, 'settings', 'get'].join('/'),
settings_set: () => [host, prefix, 'settings', 'set'].join('/'),
settings_backup: () => [host, prefix, 'settings', 'backup'].join('/'),
settings_restore: () => [host, prefix, 'settings', 'restore'].join('/'),
restore_defaults: () => [host, prefix, 'settings', 'restore', 'defaults'].join('/'),
reboot: () => [host, prefix, 'reboot'].join('/'),
syslog: () => [host, prefix, 'syslog'].join('/'),
+49
View File
@@ -0,0 +1,49 @@
const backup_settings_button = document.querySelector('#backup_settings')
const restore_setings_button = document.querySelector('#restore_settings')
const restore_settings_input = document.querySelector('#restore_settings_input')
const start_restore_settings = document.querySelector('#start_restore_settings')
const restore_default_settings = document.querySelector('#restore_default_settings')
backup_settings_button.href = routes.settings_backup()
restore_setings_button.addEventListener('click', () => restore_settings_input.click())
start_restore_settings.addEventListener('click', () => {
let restore_file = restore_settings_input.files[0]
console.log(restore_file)
let formData = new FormData()
formData.append('restore_settings', restore_file)
fetch(routes.settings_restore(), {method:"POST", body: formData})
.then(data => data.json())
.then(jsondata => {
console.log(jsondata)
if (jsondata.status) {
notification('Настройки применены', 'success')
get_all_settings()
} else {
notification('Некорректный файл', 'error')
}
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
})
restore_default_settings.addEventListener('click', () => {
fetch(routes.restore_defaults(), {
method: 'POST',
body: `{"token":"${localStorage.getItem("token")}"}`
}).then(data => data.json()).then(jsondata => {
if (jsondata.status) {
notification(`Настройки успешно сохранены`, "success");
get_all_settings()
} else {
notification(`Неизвестная ошибка`, "error");
}
}).catch((error) => {
notification(`Ошибка на сервере: ${error}`, "error")
})
})
// restore_settings_input.addEventListener('click', () => {
// })
+3
View File
@@ -17,6 +17,9 @@ const get_dns_enabled_status = () => {
addlocaldomainbutton.classList.add('active')
} else {
adddnsserverbutton.parentElement.classList.add('onebutton')
dnssettingbody.classList.remove('active')
adddnsserverbutton.classList.remove('active')
addlocaldomainbutton.classList.remove('active')
}
}
+4
View File
@@ -15,6 +15,10 @@ const get_ssh_status = () => {
sshsettingsbody.classList.add('active')
add_ssh_key_button.classList.add('active')
add_ssh_key_button.parentElement.classList.remove('onebutton')
} else {
sshsettingsbody.classList.remove('active')
add_ssh_key_button.classList.remove('active')
add_ssh_key_button.parentElement.classList.add('onebutton')
}
}
+2
View File
@@ -7,6 +7,8 @@ const get_telnet_enabled_status = () => {
telnetenabled.checked = settings.telnet.enabled
if (settings.telnet.enabled) {
telnetsettingbody.classList.add('active')
} else {
telnetsettingbody.classList.remove('active')
}
}
+2
View File
@@ -16,6 +16,8 @@ const get_wifi_status = () => {
wifistatus.checked = settings.wifi.enabled
if (settings.wifi.enabled) {
wifisettingsbody.classList.add('active')
} else {
wifisettingsbody.classList.remove('active')
}
}