Files
openrouter/src/settings/wifi/password.cpp
T
wt 782315b9f4
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
updated and added more funcs to be small
2025-04-19 17:03:56 +07:00

64 lines
2.7 KiB
C++

#include <settings/wifi/wifi.hpp>
#include <colors.h>
#include <types.hpp>
#include <json.hpp>
#include <iostream>
#include <string>
#include <syslog.h>
#include <auth/auth.hpp>
#include <helpers.hpp>
#include <config/config.hpp>
#define WIFI_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})"
namespace api
{
namespace settings
{
namespace wifi
{
void set_wifi_password(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
check_body_empty(request, response, ACCESS_REQUIRED_STRING);
nlohmann::json json_body = nlohmann::json::parse(request.body);
check_body_data(json_body, "password", response, WIFI_PASSWORD_REQUIRED_STRING);
check_body_data(json_body, "newpassword", response, WIFI_PASSWORD_REQUIRED_STRING);
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
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)
{
response.set_content(R"({"status":"reject"})", JSON_TYPE);
return;
}
std::string new_wifi_password = json_body["newpassword"];
std::string systemrequest;
systemrequest += "sed -i \"/wpa_passphrase=" \
+ old_wifi_password \
+ "/s//wpa_passphrase=" \
+ new_wifi_password \
+ "/\" /etc/hostapd/hostapd.conf";
std::cout << systemrequest << std::endl;
system(systemrequest.c_str());
system("rc restart hostapd");
std::string syslogstring = "WIFI password changed";
syslog(LOG_INFO, "%s", syslogstring.c_str());
config::config.setConfig(config::config.getConfigFileName());
nlohmann::json new_config = config::config.getConfig();
new_config["settings"]["wifi"]["password"] = new_wifi_password;
config::config.setConfig(new_config);
config::config.saveConfig();
std::string responsedata;
responsedata = R"({"success":"ok"})";
response.set_content(responsedata, JSON_TYPE);
}
}
}
}