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
81 lines
3.2 KiB
C++
81 lines
3.2 KiB
C++
#include <settings/dns/dns.hpp>
|
|
#include <colors.h>
|
|
#include <json.hpp>
|
|
#include <types.hpp>
|
|
#include <syslog.h>
|
|
#include <auth/auth.hpp>
|
|
#include <helpers.hpp>
|
|
#include <config/config.hpp>
|
|
|
|
namespace api
|
|
{
|
|
namespace settings
|
|
{
|
|
namespace dns
|
|
{
|
|
void get_dns_servers(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;
|
|
}
|
|
config::config.setConfig(std::string("../config.json"));
|
|
std::stringstream responsedata;
|
|
responsedata << R"({"servers":)";
|
|
responsedata << config::config.getConfig()["settings"]["dns"]["servers"];
|
|
responsedata << "}";
|
|
response.set_content(responsedata.str(), JSON_TYPE);
|
|
}
|
|
|
|
void set_dns_servers(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::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
|
|
nlohmann::json servers = json_body["servers"];
|
|
for (const nlohmann::json_abi_v3_11_3::json& server : servers)
|
|
{
|
|
std::string srv = server;
|
|
if (server == "")
|
|
{
|
|
continue;
|
|
}
|
|
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);
|
|
config::config.saveConfig();
|
|
system("rc restart dnsmasq");
|
|
syslog(LOG_INFO, "%s", "DNS servers changed");
|
|
std::string responsedata = R"({"success":"true"})";
|
|
response.set_content(responsedata, JSON_TYPE);
|
|
}
|
|
}
|
|
}
|
|
}
|