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