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
89 lines
3.4 KiB
C++
89 lines
3.4 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>
|
|
|
|
#define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})"
|
|
|
|
namespace api
|
|
{
|
|
namespace settings
|
|
{
|
|
namespace dns
|
|
{
|
|
void get_dns(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"));
|
|
int dns_enabled = config::config.getConfig()["settings"]["dns"]["enabled"];
|
|
std::stringstream responsedata;
|
|
responsedata << R"({"enabled":")" << dns_enabled << R"("})";
|
|
response.set_content(responsedata.str(), JSON_TYPE);
|
|
}
|
|
|
|
void set_dns(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["enabled"] == nullptr)
|
|
{
|
|
response.set_content(DNS_REQUIRED_STRING, JSON_TYPE);
|
|
return;
|
|
}
|
|
|
|
if (!authenticate(json_body["token"]))
|
|
{
|
|
response.set_content(R"({"access":"reject"})", JSON_TYPE);
|
|
return;
|
|
}
|
|
bool new_dns_enabled = json_body["enabled"];
|
|
if (!new_dns_enabled)
|
|
{
|
|
system("rc stop dnsmasq");
|
|
system("rc del dnsmasq");
|
|
syslog(LOG_INFO, "%s", "DNS is disabled");
|
|
}
|
|
else
|
|
{
|
|
system("rc restart dnsmasq");
|
|
system("rc add dnsmasq");
|
|
syslog(LOG_INFO, "%s", "DNS is enabled");
|
|
}
|
|
config::config.setConfig(std::string("../config.json"));
|
|
nlohmann::json new_config = config::config.getConfig();
|
|
new_config["settings"]["dns"]["enabled"] = new_dns_enabled;
|
|
config::config.setConfig(new_config);
|
|
config::config.saveConfig();
|
|
int dns_enabled = config::config.getConfig()["settings"]["dns"]["enabled"];
|
|
std::stringstream responsedata;
|
|
responsedata << R"({"enabled":")" << dns_enabled << R"("})";
|
|
response.set_content(responsedata.str(), JSON_TYPE);
|
|
}
|
|
}
|
|
}
|
|
}
|