making easy
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Waiting to run
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Waiting to run

This commit is contained in:
wt committed 2025-02-20 18:07:59 +07:00
1 parent c376868513
commit cedc9cd995
38 files changed
+313 -1364

No files matched your search

+1 -75
View File
@@ -1,15 +1,8 @@
#include <settings/wifi/wifi.hpp>
#include <colors.h>
#include <types.hpp>
#include <json.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <syslog.h>
#include <auth/auth.hpp>
#include <helpers.hpp>
#include <config/config.hpp>
#define WIFI_NAME_REQUIRED_STRING R"({"required":"[token,name]"})"
@@ -19,51 +12,8 @@ namespace api
{
namespace wifi
{
void get_wifi_name(const httplib::Request& request, httplib::Response& response)
void set_wifi_name(std::string new_wifi_name)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
config::config.setConfig(std::string("../config.json"));
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);
}
void set_wifi_name(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["name"] == nullptr)
{
response.set_content(WIFI_NAME_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
std::string old_wifi_name;
std::string wifisettingsbuffer;
@@ -77,7 +27,6 @@ namespace api
}
}
wifisettingsfile.close();
std::string new_wifi_name = json_body["name"];
std::stringstream systemrequest;
systemrequest << "sed -i \"/ssid=" << old_wifi_name << "/s//ssid=" << new_wifi_name << "/\" /etc/hostapd/hostapd.conf";
std::cout << systemrequest.str() << std::endl;
@@ -85,29 +34,6 @@ namespace api
system("rc restart hostapd");
std::string syslogstring = "WIFI name changed to " + new_wifi_name;
syslog(LOG_INFO, "%s", syslogstring.c_str());
std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH);
std::string wifi_name;
std::string changedwifisettingsbuffer;
while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer))
{
if (changedwifisettingsbuffer.find("ssid=") == 0)
{
wifi_name = changedwifisettingsbuffer;
wifi_name.erase(0, 5);
break;
}
}
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);
}
}
}