added get_dns_servers_table and fix some errors
This commit is contained in:
1 parent
0a87be4d59
commit
defd2f551d
12 files changed
+107
-10
No files matched your search
@@ -5,5 +5,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);
|
||||
|
||||
#endif // DNS_HPP
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "dns.hpp"
|
||||
#include <colors.h>
|
||||
#include <json.hpp>
|
||||
#include "../../routes.hpp"
|
||||
#include "../../types.hpp"
|
||||
|
||||
// #define DNS_REQUIRED_STRING R"({"required":"[token,enabled]"})"
|
||||
|
||||
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 (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::ifstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
|
||||
std::stringstream responsedata;
|
||||
responsedata << R"({"servers":[")";
|
||||
std::string dns_server_string = "";
|
||||
while (std::getline(dnsserversfile, dns_server_string)) {
|
||||
dns_server_string.erase(0, 11);
|
||||
responsedata << dns_server_string << R"(",")";
|
||||
dns_server_string = "";
|
||||
}
|
||||
responsedata.str(responsedata.str().erase(responsedata.str().size()-2, 2) + "]}");
|
||||
dnsserversfile.close();
|
||||
response.set_content(responsedata.str(), JSON_TYPE);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user