Files
openrouter/src/settings/wifi/password.cpp
T
wt 6e85d8e922
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
added full support for config file
2025-02-11 03:24:55 +07:00

99 lines
4.0 KiB
C++

#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_PASSWORD_REQUIRED_STRING R"({"required":"[token,password,newpassword]"})"
namespace api
{
namespace settings
{
namespace wifi
{
void get_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", "*");
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;
}
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);
}
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", "*");
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["password"] == nullptr)
{
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
if (json_body["newpassword"] == nullptr)
{
response.set_content(WIFI_PASSWORD_REQUIRED_STRING, JSON_TYPE);
return;
}
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::stringstream systemrequest;
systemrequest << "sed -i \"/wpa_passphrase=" << old_wifi_password << "/s//wpa_passphrase=" << new_wifi_password << "/\" /etc/hostapd/hostapd.conf";
system(systemrequest.str().c_str());
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);
}
}
}
}