update 2024-06-28
This commit is contained in:
1 parent
defd2f551d
commit
a7ff04b7c2
13 files changed
+248
-40
No files matched your search
@@ -6,5 +6,6 @@
|
||||
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);
|
||||
|
||||
#endif // DNS_HPP
|
||||
@@ -46,3 +46,42 @@ void get_dns_servers(const httplib::Request &request, httplib::Response &respons
|
||||
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 (request.body.empty()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
httplib::Client cli(AUTH_SERVICE);
|
||||
|
||||
std::stringstream body;
|
||||
body << R"({"token":)" << json_body["token"] << R"(})";
|
||||
|
||||
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
|
||||
if (res->status == httplib::OK_200) {
|
||||
nlohmann::json resjson = nlohmann::json::parse(res->body);
|
||||
if (resjson["access"] == "reject") {
|
||||
response.set_content(resjson.dump().c_str(), JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
|
||||
nlohmann::json servers = json_body["servers"];
|
||||
for (nlohmann::json_abi_v3_11_3::json server : servers) {
|
||||
std::string srv = server;
|
||||
dnsserversfile << "nameserver " << srv << "\n";
|
||||
}
|
||||
dnsserversfile.close();
|
||||
system("/etc/init.d/dnsmasq restart");
|
||||
std::string responsedata = R"({"success":"true"})";
|
||||
response.set_content(responsedata, JSON_TYPE);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user