From ee517632e4bb76bf5bfd092a3db22be777b762f6 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 10 Feb 2025 14:22:49 +0700 Subject: [PATCH 1/4] added config.json as default config file --- config.json | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..50dc10f --- /dev/null +++ b/config.json @@ -0,0 +1,33 @@ +{ + "settings": { + "dns": { + "enabled": true, + "localdomains": [ + {"ip": "127.0.0.1", "domain": "localhost"}, + {"ip": "192.168.200.1", "domain": "openrouter.local.com"} + ], + "servers": [ + "8.8.8.8" + ] + }, + "ftp": { + "enabled": true + }, + "ssh": { + "enabled": true, + "port": 22, + "server": "dropbear", + "keys": [] + }, + "telnet": { + "enabled": true, + "port": 23 + }, + "wifi": { + "enabled": true, + "name": "OpenRouterWifi", + "password": "OpenRouterPassword", + "channel": 6 + } + } +} \ No newline at end of file From 0c4183843f3138fc21eed59e1c2953b25ce0e9c9 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Mon, 10 Feb 2025 18:13:51 +0700 Subject: [PATCH 2/4] added config mangager --- config.json | 21 +++++++--- src/OpenRouter/OpenRouter.cpp | 2 + src/config/config.hpp | 6 +++ src/settings/dns/enabled.cpp | 34 ++++++++++------ src/settings/dns/local_domains.cpp | 63 +++++++++++++++++------------- src/settings/dns/servers.cpp | 42 +++++++++++--------- src/settings/ftp/ftp.cpp | 33 ++++++++++------ src/settings/ssh/keys.cpp | 48 ++++++++++++----------- src/settings/ssh/port.cpp | 10 +++-- src/settings/ssh/server.cpp | 11 +++--- src/settings/ssh/status.cpp | 10 +++-- src/settings/telnet/enabled.cpp | 33 ++++++++++------ src/settings/telnet/port.cpp | 29 ++++++++------ src/settings/wifi/channel.cpp | 28 +++++++------ src/settings/wifi/name.cpp | 28 +++++++------ src/settings/wifi/password.cpp | 28 +++++++------ src/settings/wifi/qr.cpp | 43 ++++++++++---------- src/settings/wifi/status.cpp | 10 +++-- src/web/js/settings/dns.js | 2 +- src/web/js/settings/ftp.js | 2 +- src/web/js/settings/telnet.js | 2 +- 21 files changed, 282 insertions(+), 203 deletions(-) create mode 100644 src/config/config.hpp diff --git a/config.json b/config.json index 50dc10f..79cc838 100644 --- a/config.json +++ b/config.json @@ -3,8 +3,14 @@ "dns": { "enabled": true, "localdomains": [ - {"ip": "127.0.0.1", "domain": "localhost"}, - {"ip": "192.168.200.1", "domain": "openrouter.local.com"} + { + "domain": "localhost", + "ip": "127.0.0.1" + }, + { + "domain": "openrouter.local.com", + "ip": "192.168.200.1" + } ], "servers": [ "8.8.8.8" @@ -15,19 +21,22 @@ }, "ssh": { "enabled": true, + "keys": [ + "key1", + "key2" + ], "port": 22, - "server": "dropbear", - "keys": [] + "server": "openssh" }, "telnet": { "enabled": true, "port": 23 }, "wifi": { + "channel": 6, "enabled": true, "name": "OpenRouterWifi", - "password": "OpenRouterPassword", - "channel": 6 + "password": "OpenRouterPassword" } } } \ No newline at end of file diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index 2cf5327..98055f3 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -4,9 +4,11 @@ #include #include #include +#include OpenRouter::OpenRouter() { + std::cout << config::config.getConfig() << std::endl; 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); diff --git a/src/config/config.hpp b/src/config/config.hpp new file mode 100644 index 0000000..dd29eea --- /dev/null +++ b/src/config/config.hpp @@ -0,0 +1,6 @@ +#pragma once +#include + +namespace config { + static ConfigManager config(std::string("../config.json")); +} \ No newline at end of file diff --git a/src/settings/dns/enabled.cpp b/src/settings/dns/enabled.cpp index 934416b..e42564d 100644 --- a/src/settings/dns/enabled.cpp +++ b/src/settings/dns/enabled.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -30,10 +31,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream dnsfile("/etc/openrouter/dns"); - int dns_enabled; - dnsfile >> dns_enabled; - dnsfile.close(); + // std::ifstream dnsfile("/etc/openrouter/dns"); + // int dns_enabled; + // dnsfile >> dns_enabled; + // dnsfile.close(); + 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); @@ -61,9 +63,9 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream dnsfileout("/etc/openrouter/dns"); - std::string new_dns_enabled = json_body["enabled"]; - if (new_dns_enabled == "0") + // std::ofstream dnsfileout("/etc/openrouter/dns"); + bool new_dns_enabled = json_body["enabled"]; + if (!new_dns_enabled) { system("rc stop dnsmasq"); system("rc del dnsmasq"); @@ -75,12 +77,18 @@ namespace api system("rc add dnsmasq"); syslog(LOG_INFO, "%s", "DNS is enabled"); } - dnsfileout << stoi(new_dns_enabled); - dnsfileout.close(); - std::ifstream dnsfile("/etc/openrouter/dns"); - int dns_enabled; - dnsfile >> dns_enabled; - dnsfile.close(); + 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"]; + // dnsfileout << stoi(new_dns_enabled); + // dnsfileout.close(); + // std::ifstream dnsfile("/etc/openrouter/dns"); + // int dns_enabled; + // dnsfile >> dns_enabled; + // dnsfile.close(); + 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 45d9a98..5c813b6 100644 --- a/src/settings/dns/local_domains.cpp +++ b/src/settings/dns/local_domains.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace api { @@ -28,34 +29,38 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); + // std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); std::stringstream responsedata; - responsedata << R"({"domains":[)"; - std::string ip_string = ""; - std::string domain_string = ""; - int iter = 0; - while (!domainsfile.eof()) - { - domainsfile >> ip_string; - domainsfile >> domain_string; - if (domain_string.empty()) - { - continue; - } - responsedata << R"({"ip":")" << ip_string << R"(","domain":")" << domain_string << R"("},)"; - ip_string = ""; - domain_string = ""; - iter++; - } - if (iter != 0) - { - responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); - } - else - { - responsedata << "]}"; - } - domainsfile.close(); + responsedata << R"({"domains":)"; + // std::string ip_string = ""; + // std::string domain_string = ""; + // int iter = 0; + responsedata << config::config.getConfig()["settings"]["dns"]["localdomains"]; + // for (nlohmann::json domains : config::config.getConfig()["setttings"]["dns"]["localdomains"]) { + // responsedata << + // } + // while (!domainsfile.eof()) + // { + // domainsfile >> ip_string; + // domainsfile >> domain_string; + // if (domain_string.empty()) + // { + // continue; + // } + // responsedata << R"({"ip":")" << ip_string << R"(","domain":")" << domain_string << R"("},)"; + // ip_string = ""; + // domain_string = ""; + // iter++; + // } + // if (iter != 0) + // { + // responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); + // } + // else + // { + responsedata << "}"; + // } + // domainsfile.close(); response.set_content(responsedata.str(), JSON_TYPE); } @@ -84,6 +89,10 @@ namespace api domainsfile << ip << " " << dom << "\n"; } domainsfile.close(); + 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"})"; diff --git a/src/settings/dns/servers.cpp b/src/settings/dns/servers.cpp index f6c03e6..2c7148f 100644 --- a/src/settings/dns/servers.cpp +++ b/src/settings/dns/servers.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace api { @@ -30,24 +31,25 @@ namespace api } std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); std::stringstream responsedata; - responsedata << R"({"servers":[)"; - std::string dns_server_string = ""; - int iter = 0; - while (std::getline(dnsserversfile, dns_server_string)) - { - dns_server_string.erase(0, 11); - responsedata << "\"" << dns_server_string << R"(",)"; - dns_server_string = ""; - iter++; - } - if (iter != 0) - { - responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); - } - else - { - responsedata << "]}"; - } + responsedata << R"({"servers":)"; + responsedata << config::config.getConfig()["settings"]["dns"]["servers"]; + // std::string dns_server_string = ""; + // int iter = 0; + // while (std::getline(dnsserversfile, dns_server_string)) + // { + // dns_server_string.erase(0, 11); + // responsedata << "\"" << dns_server_string << R"(",)"; + // dns_server_string = ""; + // iter++; + // } + // if (iter != 0) + // { + // responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); + // } + // else + // { + responsedata << "}"; + // } dnsserversfile.close(); response.set_content(responsedata.str(), JSON_TYPE); } @@ -80,6 +82,10 @@ namespace api dnsserversfile << "nameserver " << srv << "\n"; } dnsserversfile.close(); + 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"})"; diff --git a/src/settings/ftp/ftp.cpp b/src/settings/ftp/ftp.cpp index b3c326b..5f7dce5 100644 --- a/src/settings/ftp/ftp.cpp +++ b/src/settings/ftp/ftp.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define FTP_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -30,10 +31,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream ftpfile("/etc/openrouter/ftp"); - int ftp_enabled; - ftpfile >> ftp_enabled; - ftpfile.close(); + // std::ifstream ftpfile("/etc/openrouter/ftp"); + // int ftp_enabled; + // ftpfile >> ftp_enabled; + // ftpfile.close(); + 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); @@ -61,9 +63,9 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream ftpfileout("/etc/openrouter/ftp"); - std::string new_ftp_enabled = json_body["enabled"]; - if (new_ftp_enabled == "0") + // std::ofstream ftpfileout("/etc/openrouter/ftp"); + bool new_ftp_enabled = json_body["enabled"]; + if (!new_ftp_enabled) { system("rc stop ftp"); system("rc del ftp"); @@ -75,12 +77,17 @@ namespace api system("rc add ftp"); syslog(LOG_INFO, "%s", "Ftp is enabled"); } - ftpfileout << stoi(new_ftp_enabled); - ftpfileout.close(); - std::ifstream ftpfile("/etc/openrouter/ftp"); - int ftp_enabled; - ftpfile >> ftp_enabled; - ftpfile.close(); + // ftpfileout << stoi(new_ftp_enabled); + // ftpfileout.close(); + // std::ifstream ftpfile("/etc/openrouter/ftp"); + // int ftp_enabled; + // ftpfile >> ftp_enabled; + // ftpfile.close(); + 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/ssh/keys.cpp b/src/settings/ssh/keys.cpp index 13d7201..00fcb0a 100644 --- a/src/settings/ssh/keys.cpp +++ b/src/settings/ssh/keys.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define KEYS_PATH "~/.ssh/authorized_keys" @@ -30,30 +31,31 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream keysfile(KEYS_PATH); + // 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.empty()) - { - 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(); + responsedata << R"({"keys":)"; + responsedata << config::config.getConfig()["settings"]["ssh"]["keys"]; + // std::string key_string = ""; + // int iter = 0; + // while (std::getline(keysfile, key_string)) + // { + // if (key_string.empty()) + // { + // 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); } diff --git a/src/settings/ssh/port.cpp b/src/settings/ssh/port.cpp index 9723b9e..9e94cff 100644 --- a/src/settings/ssh/port.cpp +++ b/src/settings/ssh/port.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define SSH_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" #define SSH_PORT_PATH "/etc/openrouter/ssh/port" @@ -35,10 +36,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream sshportfile(SSH_PORT_PATH); - int ssh_port; - sshportfile >> ssh_port; - sshportfile.close(); + // std::ifstream sshportfile(SSH_PORT_PATH); + // int ssh_port; + // sshportfile >> ssh_port; + // sshportfile.close(); + 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 35275e5..6969a2b 100644 --- a/src/settings/ssh/server.cpp +++ b/src/settings/ssh/server.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define SSH_SERVER_REQUIRED_STRING R"({"required":"[token,server]"})" @@ -16,11 +17,11 @@ namespace api { 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::ifstream serverfile("/etc/openrouter/ssh/server"); + // std::string ssh_server = ""; + // serverfile >> ssh_server; + // serverfile.close(); + return config::config.getConfig()["settings"]["ssh"]["server"]; } std::string set_ssh_server(std::string &server) diff --git a/src/settings/ssh/status.cpp b/src/settings/ssh/status.cpp index a284a07..0a4a2c6 100644 --- a/src/settings/ssh/status.cpp +++ b/src/settings/ssh/status.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define SSH_REQUIRED_STRING R"({"required":"[token,status]"})" @@ -30,10 +31,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); - int ssh_status; - sshstatusfile >> ssh_status; - sshstatusfile.close(); + // std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); + // int ssh_status; + // sshstatusfile >> ssh_status; + // sshstatusfile.close(); + 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 53698e6..98e7d55 100644 --- a/src/settings/telnet/enabled.cpp +++ b/src/settings/telnet/enabled.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define TELNET_REQUIRED_STRING R"({"required":"[token,enabled]"})" @@ -34,10 +35,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - int telnet_enabled; - telnetfile >> telnet_enabled; - telnetfile.close(); + // std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); + // int telnet_enabled; + // telnetfile >> telnet_enabled; + // telnetfile.close(); + 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); @@ -65,9 +67,9 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); - std::string new_telnet_enabled = json_body["enabled"]; - if (new_telnet_enabled == "0") + // std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); + bool new_telnet_enabled = json_body["enabled"]; + if (!new_telnet_enabled) { system("rc stop telnet"); system("rc del telnet"); @@ -79,12 +81,17 @@ namespace api system("rc add telnet"); syslog(LOG_INFO, "%s", "Telnet is enabled"); } - telnetfileout << stoi(new_telnet_enabled); - telnetfileout.close(); - std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - int telnet_enabled; - telnetfile >> telnet_enabled; - telnetfile.close(); + 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"]; + // telnetfileout << stoi(new_telnet_enabled); + // telnetfileout.close(); + // std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); + // int telnet_enabled; + // telnetfile >> telnet_enabled; + // telnetfile.close(); 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 d1816a2..9283ea1 100644 --- a/src/settings/telnet/port.cpp +++ b/src/settings/telnet/port.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define TELNET_PORT_REQUIRED_STRING R"({"required":"[token,port]"})" @@ -34,10 +35,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - int telnet_port; - telnetportfile >> telnet_port; - telnetportfile.close(); + // std::ifstream telnetportfile("/etc/openrouter/telnet/port"); + // int telnet_port; + // telnetportfile >> telnet_port; + // telnetportfile.close(); + 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); @@ -65,18 +67,23 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); + // std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); std::string new_telnet_port = json_body["port"]; - telnetportfileout << stoi(new_telnet_port); - telnetportfileout.close(); + // telnetportfileout << stoi(new_telnet_port); + // telnetportfileout.close(); system("rc restart telnet"); std::string syslogstring = "Telnet port changed to " + new_telnet_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); - std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - int telnet_port; - telnetportfile >> telnet_port; - telnetportfile.close(); + // std::ifstream telnetportfile("/etc/openrouter/telnet/port"); + // int telnet_port; + // telnetportfile >> telnet_port; + // telnetportfile.close(); std::stringstream responsedata; + 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/wifi/channel.cpp b/src/settings/wifi/channel.cpp index b3663bb..933da25 100644 --- a/src/settings/wifi/channel.cpp +++ b/src/settings/wifi/channel.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})" @@ -34,19 +35,20 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_channel; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) - { - if (wifisettingsbuffer.find("channel=") == 0) - { - wifi_channel = wifisettingsbuffer; - wifi_channel.erase(0, 8); - break; - } - } - wifisettingsfile.close(); + // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + // std::string wifi_channel; + // std::string wifisettingsbuffer; + // while (std::getline(wifisettingsfile, wifisettingsbuffer)) + // { + // if (wifisettingsbuffer.find("channel=") == 0) + // { + // wifi_channel = wifisettingsbuffer; + // wifi_channel.erase(0, 8); + // break; + // } + // } + // wifisettingsfile.close(); + 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 245bca6..e894269 100644 --- a/src/settings/wifi/name.cpp +++ b/src/settings/wifi/name.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})" @@ -34,19 +35,20 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_name; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) - { - if (wifisettingsbuffer.find("ssid=") == 0) - { - wifi_name = wifisettingsbuffer; - wifi_name.erase(0, 5); - break; - } - } - wifisettingsfile.close(); + // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + // std::string wifi_name; + // std::string wifisettingsbuffer; + // while (std::getline(wifisettingsfile, wifisettingsbuffer)) + // { + // if (wifisettingsbuffer.find("ssid=") == 0) + // { + // wifi_name = wifisettingsbuffer; + // wifi_name.erase(0, 5); + // break; + // } + // } + // wifisettingsfile.close(); + 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); diff --git a/src/settings/wifi/password.cpp b/src/settings/wifi/password.cpp index 2cdd928..8c9d66d 100644 --- a/src/settings/wifi/password.cpp +++ b/src/settings/wifi/password.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})" @@ -34,19 +35,20 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) - { - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) - { - wifi_password = wifisettingsbuffer; - wifi_password.erase(0, 15); - break; - } - } - wifisettingsfile.close(); + // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + // std::string wifi_password; + // std::string wifisettingsbuffer; + // while (std::getline(wifisettingsfile, wifisettingsbuffer)) + // { + // if (wifisettingsbuffer.find("wpa_passphrase=") == 0) + // { + // wifi_password = wifisettingsbuffer; + // wifi_password.erase(0, 15); + // break; + // } + // } + // wifisettingsfile.close(); + 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); diff --git a/src/settings/wifi/qr.cpp b/src/settings/wifi/qr.cpp index 1901747..d168c27 100644 --- a/src/settings/wifi/qr.cpp +++ b/src/settings/wifi/qr.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace api { @@ -29,26 +30,28 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_name; - std::string wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) - { - if (wifisettingsbuffer.find("ssid=") == 0) - { - wifi_name = wifisettingsbuffer; - wifi_name.erase(0, 5); - } - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) - { - wifi_password = wifisettingsbuffer; - wifi_password.erase(0, 15); - break; - } - } - wifisettingsfile.close(); - + // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); + // std::string wifi_name; + // std::string wifi_password; + // std::string wifisettingsbuffer; + // while (std::getline(wifisettingsfile, wifisettingsbuffer)) + // { + // if (wifisettingsbuffer.find("ssid=") == 0) + // { + // wifi_name = wifisettingsbuffer; + // wifi_name.erase(0, 5); + // } + // if (wifisettingsbuffer.find("wpa_passphrase=") == 0) + // { + // wifi_password = wifisettingsbuffer; + // wifi_password.erase(0, 15); + // break; + // } + // } + // wifisettingsfile.close(); + + std::string wifi_password = config::config.getConfig()["settings"]["wifi"]["password"]; + std::string wifi_name = config::config.getConfig()["settings"]["wifi"]["name"]; std::stringstream toqrstring; toqrstring << "WIFI:T:WPA;P:" << wifi_password << ";S:" << wifi_name << ";"; diff --git a/src/settings/wifi/status.cpp b/src/settings/wifi/status.cpp index fe0a8b6..05e046f 100644 --- a/src/settings/wifi/status.cpp +++ b/src/settings/wifi/status.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})" @@ -30,10 +31,11 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); - int wifi_status; - wifistatusfile >> wifi_status; - wifistatusfile.close(); + // std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); + // int wifi_status; + // wifistatusfile >> wifi_status; + // wifistatusfile.close(); + 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/web/js/settings/dns.js b/src/web/js/settings/dns.js index c3c0650..a202fab 100644 --- a/src/web/js/settings/dns.js +++ b/src/web/js/settings/dns.js @@ -33,7 +33,7 @@ const get_dns_enabled_status = () => { const set_dns_enabled_status = () => { fetch(routes.dns_status_set(), { method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":"${dnsenabled.checked ? 1 : 0}"}` + body: `{"token":"${localStorage.getItem("token")}","enabled":${dnsenabled.checked}}` }).then(res => res.json()).then(jsondata => { if (jsondata.enabled === '1') { dnsenabled.checked = true diff --git a/src/web/js/settings/ftp.js b/src/web/js/settings/ftp.js index 1386adc..61de91e 100644 --- a/src/web/js/settings/ftp.js +++ b/src/web/js/settings/ftp.js @@ -19,7 +19,7 @@ const get_ftp_enabled_status = () => { const set_ftp_enabled_status = () => { fetch(routes.ftp_status_set(), { method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":"${ftpenabled.checked ? 1 : 0}"}` + body: `{"token":"${localStorage.getItem("token")}","enabled":${ftpenabled.checked}}` }).then(res => res.json()).then(jsondata => { if (jsondata.enabled === '1') { ftpenabled.checked = true diff --git a/src/web/js/settings/telnet.js b/src/web/js/settings/telnet.js index 096867b..1bf8ff8 100644 --- a/src/web/js/settings/telnet.js +++ b/src/web/js/settings/telnet.js @@ -27,7 +27,7 @@ const get_telnet_enabled_status = () => { const set_telnet_enabled_status = () => { fetch(routes.telnet_status_set(), { method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","enabled":"${telnetenabled.checked ? 1 : 0}"}` + body: `{"token":"${localStorage.getItem("token")}","enabled":${telnetenabled.checked}}` }).then(res => res.json()).then(jsondata => { if (jsondata.enabled === '1') { telnetenabled.checked = true From 6e85d8e9224fa383f98977d6f1d2af77fd2cd534 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Tue, 11 Feb 2025 03:24:55 +0700 Subject: [PATCH 3/4] added full support for config file --- config.json | 23 +++-- src/OpenRouter/OpenRouter.cpp | 1 - src/libs/ConfigManager.hpp | 133 +++++++++++++++++++++++++++++ src/openrouter.cpp | 2 +- src/settings/dns/enabled.cpp | 13 +-- src/settings/dns/local_domains.cpp | 31 +------ src/settings/dns/servers.cpp | 19 +---- src/settings/ftp/ftp.cpp | 12 +-- src/settings/ssh/keys.cpp | 26 ++---- src/settings/ssh/port.cpp | 14 ++- src/settings/ssh/server.cpp | 14 ++- src/settings/ssh/status.cpp | 21 ++--- src/settings/telnet/enabled.cpp | 12 +-- src/settings/telnet/port.cpp | 12 +-- src/settings/wifi/channel.cpp | 33 ++----- src/settings/wifi/name.cpp | 19 ++--- src/settings/wifi/password.cpp | 33 ++----- src/settings/wifi/qr.cpp | 20 ----- src/settings/wifi/status.cpp | 21 ++--- src/web/js/settings/ssh.js | 2 +- src/web/js/settings/wifi.js | 2 +- 21 files changed, 209 insertions(+), 254 deletions(-) create mode 100644 src/libs/ConfigManager.hpp diff --git a/config.json b/config.json index 79cc838..db3769a 100644 --- a/config.json +++ b/config.json @@ -1,7 +1,7 @@ { "settings": { "dns": { - "enabled": true, + "enabled": false, "localdomains": [ { "domain": "localhost", @@ -10,33 +10,38 @@ { "domain": "openrouter.local.com", "ip": "192.168.200.1" + }, + { + "domain": "asdf", + "ip": "1.2.3.4" } ], "servers": [ - "8.8.8.8" + "8.8.8.8", + "1.2.3.4" ] }, "ftp": { - "enabled": true + "enabled": false }, "ssh": { - "enabled": true, + "enabled": false, "keys": [ "key1", "key2" ], "port": 22, - "server": "openssh" + "server": "dropbear" }, "telnet": { - "enabled": true, - "port": 23 + "enabled": false, + "port": 25 }, "wifi": { "channel": 6, - "enabled": true, + "enabled": false, "name": "OpenRouterWifi", - "password": "OpenRouterPassword" + "password": "Simba28082020" } } } \ No newline at end of file diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index 98055f3..cb6e6e1 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -8,7 +8,6 @@ OpenRouter::OpenRouter() { - std::cout << config::config.getConfig() << std::endl; 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); diff --git a/src/libs/ConfigManager.hpp b/src/libs/ConfigManager.hpp new file mode 100644 index 0000000..362b5e9 --- /dev/null +++ b/src/libs/ConfigManager.hpp @@ -0,0 +1,133 @@ +#ifndef CONFIG_MANAGER_HPP +#define CONFIG_MANAGER_HPP + +#include +#include +#include +#include + +class ConfigManager +{ +private: + nlohmann::json config; + std::fstream configfile; + std::string configFileName; +public: + ConfigManager() + { + this->config = nullptr; + } + ConfigManager(nlohmann::json object) + { + this->config = object; + } + ConfigManager(std::string filename) + { + this->configfile.open(filename); + this->config = nlohmann::json::parse(this->configfile); + this->configfile.close(); + this->configFileName = filename; + } + ConfigManager(nlohmann::json object, std::string filename) + { + this->config = object; + this->configFileName = filename; + } + ~ConfigManager() + { + this->config = nullptr; + this->configfile.close(); + } + nlohmann::json getConfig() + { + return this->config; + } + int setConfig(nlohmann::json object) + { + this->config = object; + return 0; + } + int setConfig(std::string filename) + { + this->configfile.open(filename); + this->config = nlohmann::json::parse(this->configfile); + this->configFileName = filename; + this->configfile.close(); + return 0; + } + int setConfigFile(std::string filename) + { + this->configFileName = filename; + return 0; + } + std::string getParameterAsString(std::string paramName) + { + return this->config[paramName]; + } + int getParameterAsInt(std::string paramName) + { + return this->config[paramName]; + } + double getParameterAsDouble(std::string paramName) + { + return this->config[paramName]; + } + float getParameterAsFloat(std::string paramName) + { + return this->config[paramName]; + } + bool getParameterAsBool(std::string paramName) + { + return this->config[paramName]; + } + int setParameter(std::string name, int value) + { + this->config[name] = value; + return 0; + } + int setParameter(std::string name, double value) + { + this->config[name] = value; + return 0; + } + int setParameter(std::string name, float value) + { + this->config[name] = value; + return 0; + } + int setParameterAsNull(std::string name) + { + this->config[name] = nullptr; + return 0; + } + int setParameter(std::string name, std::string value) + { + this->config[name] = value; + return 0; + } + int setParameter(std::string name, bool value) + { + this->config[name] = value; + return 0; + } + int saveConfig() + { + this->configfile.open(this->configFileName, std::ios::trunc | std::ios::out | std::ios::in); + this->configfile << this->config.dump(4); + this->configfile.close(); + return 0; + } + int saveConfig(std::string filename) + { + this->configfile.open(filename, std::ios::trunc | std::ios::out | std::ios::in); + this->configfile << this->config.dump(4); + this->configfile.close(); + return 0; + } + friend std::ostream& operator<<(std::ostream& out, const ConfigManager& manager) + { + return out << manager.config; + } +}; + +#endif // CONFIG_MANAGER_HPP \ No newline at end of file diff --git a/src/openrouter.cpp b/src/openrouter.cpp index 0cc8c3c..c47ec2c 100644 --- a/src/openrouter.cpp +++ b/src/openrouter.cpp @@ -6,6 +6,6 @@ int main() #ifdef TEST return 0; #else - return router.serve("127.0.0.1", 43244); + return router.serve("0.0.0.0", 43244); #endif } diff --git a/src/settings/dns/enabled.cpp b/src/settings/dns/enabled.cpp index e42564d..c0413cc 100644 --- a/src/settings/dns/enabled.cpp +++ b/src/settings/dns/enabled.cpp @@ -31,10 +31,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream dnsfile("/etc/openrouter/dns"); - // int dns_enabled; - // dnsfile >> dns_enabled; - // dnsfile.close(); int dns_enabled = config::config.getConfig()["settings"]["dns"]["enabled"]; std::stringstream responsedata; responsedata << R"({"enabled":")" << dns_enabled << R"("})"; @@ -63,7 +59,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ofstream dnsfileout("/etc/openrouter/dns"); bool new_dns_enabled = json_body["enabled"]; if (!new_dns_enabled) { @@ -77,18 +72,12 @@ 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"]; - // dnsfileout << stoi(new_dns_enabled); - // dnsfileout.close(); - // std::ifstream dnsfile("/etc/openrouter/dns"); - // int dns_enabled; - // dnsfile >> dns_enabled; - // dnsfile.close(); - 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 5c813b6..448b7d7 100644 --- a/src/settings/dns/local_domains.cpp +++ b/src/settings/dns/local_domains.cpp @@ -29,38 +29,10 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream domainsfile("/etc/dnsmasq.conf.d/hosts"); std::stringstream responsedata; responsedata << R"({"domains":)"; - // std::string ip_string = ""; - // std::string domain_string = ""; - // int iter = 0; responsedata << config::config.getConfig()["settings"]["dns"]["localdomains"]; - // for (nlohmann::json domains : config::config.getConfig()["setttings"]["dns"]["localdomains"]) { - // responsedata << - // } - // while (!domainsfile.eof()) - // { - // domainsfile >> ip_string; - // domainsfile >> domain_string; - // if (domain_string.empty()) - // { - // continue; - // } - // responsedata << R"({"ip":")" << ip_string << R"(","domain":")" << domain_string << R"("},)"; - // ip_string = ""; - // domain_string = ""; - // iter++; - // } - // if (iter != 0) - // { - // responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); - // } - // else - // { - responsedata << "}"; - // } - // domainsfile.close(); + responsedata << "}"; response.set_content(responsedata.str(), JSON_TYPE); } @@ -89,6 +61,7 @@ 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); diff --git a/src/settings/dns/servers.cpp b/src/settings/dns/servers.cpp index 2c7148f..5017771 100644 --- a/src/settings/dns/servers.cpp +++ b/src/settings/dns/servers.cpp @@ -29,28 +29,10 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf"); std::stringstream responsedata; responsedata << R"({"servers":)"; responsedata << config::config.getConfig()["settings"]["dns"]["servers"]; - // std::string dns_server_string = ""; - // int iter = 0; - // while (std::getline(dnsserversfile, dns_server_string)) - // { - // dns_server_string.erase(0, 11); - // responsedata << "\"" << dns_server_string << R"(",)"; - // dns_server_string = ""; - // iter++; - // } - // if (iter != 0) - // { - // responsedata.str(responsedata.str().erase(responsedata.str().size() - 1, 1) + "]}"); - // } - // else - // { responsedata << "}"; - // } - dnsserversfile.close(); response.set_content(responsedata.str(), JSON_TYPE); } @@ -82,6 +64,7 @@ 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); diff --git a/src/settings/ftp/ftp.cpp b/src/settings/ftp/ftp.cpp index 5f7dce5..ed1109f 100644 --- a/src/settings/ftp/ftp.cpp +++ b/src/settings/ftp/ftp.cpp @@ -31,10 +31,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream ftpfile("/etc/openrouter/ftp"); - // int ftp_enabled; - // ftpfile >> ftp_enabled; - // ftpfile.close(); int ftp_enabled = config::config.getConfig()["settings"]["ftp"]["enabled"]; std::stringstream responsedata; responsedata << R"({"enabled":")" << ftp_enabled << R"("})"; @@ -63,7 +59,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ofstream ftpfileout("/etc/openrouter/ftp"); bool new_ftp_enabled = json_body["enabled"]; if (!new_ftp_enabled) { @@ -77,12 +72,7 @@ namespace api system("rc add ftp"); syslog(LOG_INFO, "%s", "Ftp is enabled"); } - // ftpfileout << stoi(new_ftp_enabled); - // ftpfileout.close(); - // std::ifstream ftpfile("/etc/openrouter/ftp"); - // int ftp_enabled; - // ftpfile >> ftp_enabled; - // ftpfile.close(); + 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); diff --git a/src/settings/ssh/keys.cpp b/src/settings/ssh/keys.cpp index 00fcb0a..21a2fc3 100644 --- a/src/settings/ssh/keys.cpp +++ b/src/settings/ssh/keys.cpp @@ -31,31 +31,10 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream keysfile(KEYS_PATH); std::stringstream responsedata; responsedata << R"({"keys":)"; responsedata << config::config.getConfig()["settings"]["ssh"]["keys"]; - // std::string key_string = ""; - // int iter = 0; - // while (std::getline(keysfile, key_string)) - // { - // if (key_string.empty()) - // { - // 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); } @@ -82,6 +61,11 @@ namespace api 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"})"; diff --git a/src/settings/ssh/port.cpp b/src/settings/ssh/port.cpp index 9e94cff..bf81340 100644 --- a/src/settings/ssh/port.cpp +++ b/src/settings/ssh/port.cpp @@ -36,10 +36,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream sshportfile(SSH_PORT_PATH); - // int ssh_port; - // sshportfile >> ssh_port; - // sshportfile.close(); int ssh_port = config::config.getConfig()["settings"]["ssh"]["port"]; std::stringstream responsedata; responsedata << R"({"port":")" << ssh_port << R"("})"; @@ -95,10 +91,12 @@ namespace api } std::string syslogstring = "SSH port changed to " + new_ssh_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); - std::ifstream sshportfile(SSH_PORT_PATH); - int ssh_port; - sshportfile >> ssh_port; - sshportfile.close(); + 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 6969a2b..015c064 100644 --- a/src/settings/ssh/server.cpp +++ b/src/settings/ssh/server.cpp @@ -17,19 +17,17 @@ namespace api { std::string get_ssh_server() { - // std::ifstream serverfile("/etc/openrouter/ssh/server"); - // std::string ssh_server = ""; - // serverfile >> ssh_server; - // serverfile.close(); return config::config.getConfig()["settings"]["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(); + 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) diff --git a/src/settings/ssh/status.cpp b/src/settings/ssh/status.cpp index 0a4a2c6..ef73b45 100644 --- a/src/settings/ssh/status.cpp +++ b/src/settings/ssh/status.cpp @@ -31,10 +31,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream sshstatusfile("/etc/openrouter/ssh/status"); - // int ssh_status; - // sshstatusfile >> ssh_status; - // sshstatusfile.close(); int ssh_status = config::config.getConfig()["settings"]["ssh"]["enabled"]; std::stringstream responsedata; responsedata << R"({"status":")" << ssh_status << R"("})"; @@ -63,9 +59,8 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream sshfileout("/etc/openrouter/ssh/status"); - std::string new_ssh_status = json_body["status"]; - if (new_ssh_status == "0") + bool new_ssh_status = json_body["status"]; + if (!new_ssh_status) { if (get_ssh_server() == DROPBEAR_SSH) { @@ -89,12 +84,12 @@ namespace api } syslog(LOG_INFO, "%s", "SSH is enabled"); } - sshfileout << stoi(new_ssh_status); - sshfileout.close(); - std::ifstream sshfile("/etc/openrouter/ssh/status"); - int ssh_status; - sshfile >> ssh_status; - sshfile.close(); + 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 98e7d55..6751fc2 100644 --- a/src/settings/telnet/enabled.cpp +++ b/src/settings/telnet/enabled.cpp @@ -35,10 +35,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - // int telnet_enabled; - // telnetfile >> telnet_enabled; - // telnetfile.close(); int telnet_enabled = config::config.getConfig()["settings"]["telnet"]["enabled"]; std::stringstream responsedata; responsedata << R"({"enabled":")" << telnet_enabled << R"("})"; @@ -67,7 +63,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ofstream telnetfileout("/etc/openrouter/telnet/enabled"); bool new_telnet_enabled = json_body["enabled"]; if (!new_telnet_enabled) { @@ -81,17 +76,12 @@ 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"]; - // telnetfileout << stoi(new_telnet_enabled); - // telnetfileout.close(); - // std::ifstream telnetfile("/etc/openrouter/telnet/enabled"); - // int telnet_enabled; - // telnetfile >> telnet_enabled; - // telnetfile.close(); 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 9283ea1..132ffcb 100644 --- a/src/settings/telnet/port.cpp +++ b/src/settings/telnet/port.cpp @@ -35,10 +35,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - // int telnet_port; - // telnetportfile >> telnet_port; - // telnetportfile.close(); int telnet_port = config::config.getConfig()["settings"]["telnet"]["port"]; std::stringstream responsedata; responsedata << R"({"port":")" << telnet_port << R"("})"; @@ -67,18 +63,12 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ofstream telnetportfileout("/etc/openrouter/telnet/port"); std::string new_telnet_port = json_body["port"]; - // telnetportfileout << stoi(new_telnet_port); - // telnetportfileout.close(); system("rc restart telnet"); std::string syslogstring = "Telnet port changed to " + new_telnet_port; syslog(LOG_INFO, "%s", syslogstring.c_str()); - // std::ifstream telnetportfile("/etc/openrouter/telnet/port"); - // int telnet_port; - // telnetportfile >> telnet_port; - // telnetportfile.close(); 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); diff --git a/src/settings/wifi/channel.cpp b/src/settings/wifi/channel.cpp index 933da25..e2b9a36 100644 --- a/src/settings/wifi/channel.cpp +++ b/src/settings/wifi/channel.cpp @@ -35,19 +35,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - // std::string wifi_channel; - // std::string wifisettingsbuffer; - // while (std::getline(wifisettingsfile, wifisettingsbuffer)) - // { - // if (wifisettingsbuffer.find("channel=") == 0) - // { - // wifi_channel = wifisettingsbuffer; - // wifi_channel.erase(0, 8); - // break; - // } - // } - // wifisettingsfile.close(); int wifi_channel = config::config.getConfig()["settings"]["wifi"]["channel"]; std::stringstream responsedata; responsedata << R"({"channel":")" << wifi_channel << R"("})"; @@ -97,20 +84,12 @@ namespace api system("rc restart hostapd"); std::string syslogstring = "WIFI channel changed to " + new_channel; syslog(LOG_INFO, "%s", syslogstring.c_str()); - - std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH); - std::string wifi_channel; - std::string changedwifisettingsbuffer; - while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) - { - if (changedwifisettingsbuffer.find("channel=") == 0) - { - wifi_channel = changedwifisettingsbuffer; - wifi_channel.erase(0, 8); - break; - } - } - changedwifisettingsfile.close(); + 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 e894269..20cde2e 100644 --- a/src/settings/wifi/name.cpp +++ b/src/settings/wifi/name.cpp @@ -35,19 +35,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - // std::string wifi_name; - // std::string wifisettingsbuffer; - // while (std::getline(wifisettingsfile, wifisettingsbuffer)) - // { - // if (wifisettingsbuffer.find("ssid=") == 0) - // { - // wifi_name = wifisettingsbuffer; - // wifi_name.erase(0, 5); - // break; - // } - // } - // wifisettingsfile.close(); std::string wifi_name = config::config.getConfig()["settings"]["wifi"]["name"]; std::stringstream responsedata; responsedata << R"({"name":")" << wifi_name << R"("})"; @@ -111,6 +98,12 @@ namespace api } } 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 8c9d66d..dc3a713 100644 --- a/src/settings/wifi/password.cpp +++ b/src/settings/wifi/password.cpp @@ -35,19 +35,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - // std::string wifi_password; - // std::string wifisettingsbuffer; - // while (std::getline(wifisettingsfile, wifisettingsbuffer)) - // { - // if (wifisettingsbuffer.find("wpa_passphrase=") == 0) - // { - // wifi_password = wifisettingsbuffer; - // wifi_password.erase(0, 15); - // break; - // } - // } - // wifisettingsfile.close(); std::string wifi_password = config::config.getConfig()["settings"]["wifi"]["password"]; std::stringstream responsedata; responsedata << R"({"password":")" << wifi_password << R"("})"; @@ -82,19 +69,7 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - std::string old_wifi_password; - std::string wifisettingsbuffer; - while (std::getline(wifisettingsfile, wifisettingsbuffer)) - { - if (wifisettingsbuffer.find("wpa_passphrase=") == 0) - { - old_wifi_password = wifisettingsbuffer; - old_wifi_password.erase(0, 15); - break; - } - } - wifisettingsfile.close(); + std::string old_wifi_password = config::config.getConfig()["settings"]["wifi"]["password"]; std::string req_old_password = json_body["password"]; if (old_wifi_password != req_old_password) { @@ -108,6 +83,12 @@ 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")); + nlohmann::json new_config = config::config.getConfig(); + new_config["settings"]["wifi"]["password"] = new_wifi_password; + config::config.setConfig(new_config); + config::config.saveConfig(); std::stringstream responsedata; responsedata << R"({"success":"ok"})"; response.set_content(responsedata.str(), JSON_TYPE); diff --git a/src/settings/wifi/qr.cpp b/src/settings/wifi/qr.cpp index d168c27..4af721e 100644 --- a/src/settings/wifi/qr.cpp +++ b/src/settings/wifi/qr.cpp @@ -30,26 +30,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH); - // std::string wifi_name; - // std::string wifi_password; - // std::string wifisettingsbuffer; - // while (std::getline(wifisettingsfile, wifisettingsbuffer)) - // { - // if (wifisettingsbuffer.find("ssid=") == 0) - // { - // wifi_name = wifisettingsbuffer; - // wifi_name.erase(0, 5); - // } - // if (wifisettingsbuffer.find("wpa_passphrase=") == 0) - // { - // wifi_password = wifisettingsbuffer; - // wifi_password.erase(0, 15); - // break; - // } - // } - // wifisettingsfile.close(); - std::string wifi_password = config::config.getConfig()["settings"]["wifi"]["password"]; std::string wifi_name = config::config.getConfig()["settings"]["wifi"]["name"]; std::stringstream toqrstring; diff --git a/src/settings/wifi/status.cpp b/src/settings/wifi/status.cpp index 05e046f..78bea0d 100644 --- a/src/settings/wifi/status.cpp +++ b/src/settings/wifi/status.cpp @@ -31,10 +31,6 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - // std::ifstream wifistatusfile("/etc/openrouter/wifi/status"); - // int wifi_status; - // wifistatusfile >> wifi_status; - // wifistatusfile.close(); int wifi_status = config::config.getConfig()["settings"]["wifi"]["enabled"]; std::stringstream responsedata; responsedata << R"({"status":")" << wifi_status << R"("})"; @@ -63,9 +59,8 @@ namespace api response.set_content(R"({"access":"reject"})", JSON_TYPE); return; } - std::ofstream wififileout("/etc/openrouter/wifi/status"); - std::string new_wifi_status = json_body["status"]; - if (new_wifi_status == "0") + bool new_wifi_status = json_body["status"]; + if (!new_wifi_status) { system("rc stop hostapd"); system("rc del hostapd"); @@ -77,12 +72,12 @@ namespace api system("rc add hostapd"); syslog(LOG_INFO, "%s", "WIFI is enabled"); } - wififileout << stoi(new_wifi_status); - wififileout.close(); - std::ifstream wififile("/etc/openrouter/wifi/status"); - int wifi_status; - wififile >> wifi_status; - wififile.close(); + 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/web/js/settings/ssh.js b/src/web/js/settings/ssh.js index 543a497..fbb93cc 100644 --- a/src/web/js/settings/ssh.js +++ b/src/web/js/settings/ssh.js @@ -28,7 +28,7 @@ const get_ssh_status = () => { const set_ssh_status = () => { fetch(routes.ssh_status_set(), { method: 'POST', - body: `{"token":"${localStorage.getItem("token")}","status":"${sshenable.checked ? 1 : 0}"}` + body: `{"token":"${localStorage.getItem("token")}","status":${sshenable.checked}}` }).then(res => res.json()).then(jsondata => { if (jsondata.status === '1') { sshenable.checked = true diff --git a/src/web/js/settings/wifi.js b/src/web/js/settings/wifi.js index 58afb25..d916bc3 100644 --- a/src/web/js/settings/wifi.js +++ b/src/web/js/settings/wifi.js @@ -31,7 +31,7 @@ const get_wifi_status = () => { const set_wifi_status = () => { fetch(routes.wifi_status_set(), { method: 'POST', - body: `{"token":"${localStorage.getItem('token')}","status":"${wifistatus.checked ? "1" : "0"}"}` + body: `{"token":"${localStorage.getItem('token')}","status":${wifistatus.checked}}` }).then(data => data.json()).then(jsondata => { if (jsondata.status === "1") { wifistatus.checked = true From 8e04c872d492745a7c0d4940ea9dc45d4d599ae6 Mon Sep 17 00:00:00 2001 From: TolaMironcenko Date: Tue, 11 Feb 2025 09:33:56 +0700 Subject: [PATCH 4/4] added static files paths in config file --- config.json | 16 ++++++++-------- src/OpenRouter/OpenRouter.cpp | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/config.json b/config.json index db3769a..2774c6d 100644 --- a/config.json +++ b/config.json @@ -10,15 +10,10 @@ { "domain": "openrouter.local.com", "ip": "192.168.200.1" - }, - { - "domain": "asdf", - "ip": "1.2.3.4" } ], "servers": [ - "8.8.8.8", - "1.2.3.4" + "8.8.8.8" ] }, "ftp": { @@ -35,13 +30,18 @@ }, "telnet": { "enabled": false, - "port": 25 + "port": 23 }, "wifi": { "channel": 6, "enabled": false, "name": "OpenRouterWifi", - "password": "Simba28082020" + "password": "OpenRouterPassword" } + }, + "static": { + "css": "../src/web/css", + "js": "../src/web/js", + "root": "../src/web" } } \ No newline at end of file diff --git a/src/OpenRouter/OpenRouter.cpp b/src/OpenRouter/OpenRouter.cpp index cb6e6e1..783f891 100644 --- a/src/OpenRouter/OpenRouter.cpp +++ b/src/OpenRouter/OpenRouter.cpp @@ -51,9 +51,9 @@ OpenRouter::OpenRouter() this->srv.Options("/(.*)", api::security::set_cors_headers); - this->srv.set_mount_point("/", "../src/web"); - this->srv.set_mount_point("/js", "../src/web/js"); - this->srv.set_mount_point("/css", "../src/web/css"); + this->srv.set_mount_point("/", config::config.getConfig()["static"]["root"]); + this->srv.set_mount_point("/js", config::config.getConfig()["static"]["js"]); + this->srv.set_mount_point("/css", config::config.getConfig()["static"]["css"]); } OpenRouter::~OpenRouter() = default;