making easy
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

This commit is contained in:
wt committed 2025-02-20 18:07:59 +07:00
1 parent c376868513
commit cedc9cd995
38 files changed
+313 -1364

No files matched your search

+4 -9
View File
@@ -1,7 +1,7 @@
#ifndef DNS_HPP
#define DNS_HPP
#include <httplib.h>
#include <json.hpp>
namespace api
{
@@ -9,14 +9,9 @@ namespace api
{
namespace dns
{
void get_dns(const httplib::Request& request, httplib::Response& response);
void set_dns(const httplib::Request& request, httplib::Response& response);
void get_dns_servers(const httplib::Request& request, httplib::Response& response);
void set_dns_servers(const httplib::Request& request, httplib::Response& response);
void get_local_domains(const httplib::Request& request, httplib::Response& response);
void set_local_domains(const httplib::Request& request, httplib::Response& response);
void set_dns(bool new_dns_enabled);
void set_dns_servers(nlohmann::json servers);
void set_local_domains(nlohmann::json domains);
}
}
}
+1 -62
View File
@@ -1,13 +1,5 @@
#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
{
@@ -15,52 +7,8 @@ namespace api
{
namespace dns
{
void get_dns(const httplib::Request& request, httplib::Response& response)
void set_dns(bool new_dns_enabled)
{
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");
@@ -73,15 +21,6 @@ namespace api
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);
}
}
}
+2 -53
View File
@@ -1,11 +1,6 @@
#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>
#include <fstream>
namespace api
{
@@ -13,48 +8,9 @@ namespace api
{
namespace dns
{
void get_local_domains(const httplib::Request& request, httplib::Response& response)
void set_local_domains(nlohmann::json domains)
{
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"({"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"];
@@ -62,15 +18,8 @@ namespace api
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);
}
}
}
+2 -53
View File
@@ -1,11 +1,6 @@
#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>
#include <fstream>
namespace api
{
@@ -13,48 +8,9 @@ namespace api
{
namespace dns
{
void get_dns_servers(const httplib::Request& request, httplib::Response& response)
void set_dns_servers(nlohmann::json servers)
{
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;
@@ -65,15 +21,8 @@ namespace api
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);
}
}
}