Files
openrouter/src/settings/dns/local_domains.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

77 lines
3.1 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_local_domains(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::stringstream responsedata;
responsedata << R"({"domains":)";
responsedata << config::config.getConfig()["settings"]["dns"]["localdomains"];
responsedata << "}";
response.set_content(responsedata.str(), JSON_TYPE);
}
void set_local_domains(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 domainsfile("/etc/dnsmasq.conf.d/hosts");
nlohmann::json domains = json_body["domains"];
for (nlohmann::json_abi_v3_11_3::json domain : domains)
{
std::string ip = domain["ip"];
std::string dom = domain["domain"];
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);
config::config.saveConfig();
system("rc restart dnsmasq");
syslog(LOG_INFO, "%s", "DNS local domains changed");
std::string responsedata = R"({"success":"true"})";
response.set_content(responsedata, JSON_TYPE);
}
}
}
}