added dns settings
This commit is contained in:
@@ -8,6 +8,9 @@ add_executable(
|
||||
openrouter
|
||||
src/openrouter.cpp
|
||||
src/security/cors.cpp
|
||||
|
||||
src/settings/telnet/telnet.cpp
|
||||
src/settings/dns/dns.cpp
|
||||
|
||||
src/reboot/reboot.cpp
|
||||
)
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef HELPERS_HPP
|
||||
#define HELPERS_HPP
|
||||
|
||||
|
||||
|
||||
#endif // HELPERS_HPP
|
||||
+5
-1
@@ -2,6 +2,7 @@
|
||||
#include <httplib.h>
|
||||
#include <colors.h>
|
||||
#include "settings/telnet/telnet.hpp"
|
||||
#include "settings/dns/dns.hpp"
|
||||
#include "reboot/reboot.hpp"
|
||||
|
||||
#define CORS_ENABLE
|
||||
@@ -12,6 +13,9 @@ int main() {
|
||||
|
||||
srv.Post("/api/settings/telnet/get", get_telnet);
|
||||
srv.Post("/api/settings/telnet/set", set_telnet);
|
||||
srv.Post("/api/settings/dns/get", get_dns);
|
||||
srv.Post("/api/settings/dns/set", set_dns);
|
||||
|
||||
srv.Post("/api/reboot", reboot);
|
||||
|
||||
srv.Options("/(.*)", set_cors_headers);
|
||||
@@ -23,6 +27,6 @@ int main() {
|
||||
std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW
|
||||
<< "0.0.0.0" << RESET << ":" << CYAN << "43244" << RESET << " ]" << std::endl;
|
||||
|
||||
srv.listen("0.0.0.0", 43244);
|
||||
srv.listen("0.0.0.0", 80);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
#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(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 dnsfile("/etc/openrouter/dns");
|
||||
int dns_enabled;
|
||||
dnsfile >> dns_enabled;
|
||||
dnsfile.close();
|
||||
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 (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;
|
||||
}
|
||||
|
||||
if (json_body["enabled"] == nullptr) {
|
||||
response.set_content(DNS_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 dnsfileout("/etc/openrouter/dns");
|
||||
std::string new_dns_enabled = json_body["enabled"];
|
||||
if (new_dns_enabled == "0") {
|
||||
system("/etc/init.d/dnsmasq stop");
|
||||
system("rm /etc/rc.d/60dnsmasq");
|
||||
} else {
|
||||
system("/etc/init.d/dnsmasq restart");
|
||||
system("ln -sv /etc/init.d/dnsmasq /etc/rc.d/60dnsmasq");
|
||||
}
|
||||
dnsfileout << stoi(new_dns_enabled);
|
||||
dnsfileout.close();
|
||||
std::ifstream dnsfile("/etc/openrouter/dns");
|
||||
int dns_enabled;
|
||||
dnsfile >> dns_enabled;
|
||||
dnsfile.close();
|
||||
std::stringstream responsedata;
|
||||
responsedata << R"({"enabled":")" << dns_enabled << R"("})";
|
||||
response.set_content(responsedata.str(), JSON_TYPE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#ifndef DNS_HPP
|
||||
#define DNS_HPP
|
||||
|
||||
#include <httplib.h>
|
||||
|
||||
void get_dns(const httplib::Request &request, httplib::Response &response);
|
||||
void set_dns(const httplib::Request &request, httplib::Response &response);
|
||||
|
||||
#endif // DNS_HPP
|
||||
@@ -65,6 +65,12 @@
|
||||
background-color: hsl(from green h s 20%);
|
||||
}
|
||||
|
||||
.settingssection {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.settingrect {
|
||||
width: 25%;
|
||||
height: max-content;
|
||||
|
||||
@@ -56,6 +56,18 @@
|
||||
<button id="set_telnet_settings">Confirm</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="settingrect">
|
||||
<div class="settingrectheader">
|
||||
<h2>Dns</h2>
|
||||
<div class="checkbox-wrapper-2">
|
||||
<input type="checkbox" class="sc-gJwTLC ikxBAC" id="dnsenable">
|
||||
</div>
|
||||
</div>
|
||||
<div class="settingrectfooter">
|
||||
<button>Add dns server</button>
|
||||
<button id="set_dns_settings">Confirm</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</section>
|
||||
|
||||
@@ -92,6 +104,26 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form confirmtelnetform">
|
||||
<div class="formbody confirmtelnetformbody">
|
||||
<h1 class="formheader">Confirm telnet settings?</h1>
|
||||
<div class="forminputbody confirmform">
|
||||
<button class="formbutton" id="canceltelnet">Cancel</button>
|
||||
<button class="formbutton" id="confirmtelnet">Confirm</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form confirmdnsform">
|
||||
<div class="formbody confirmdnsformbody">
|
||||
<h1 class="formheader">Confirm dns settings?</h1>
|
||||
<div class="forminputbody confirmform">
|
||||
<button class="formbutton" id="canceldns">Cancel</button>
|
||||
<button class="formbutton" id="confirmdns">Confirm</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="loader">
|
||||
<h1>Reboting</h1>
|
||||
<div class="loadertip">
|
||||
@@ -103,6 +135,7 @@
|
||||
<script src="js/notification.js"></script>
|
||||
<script src="js/login.js"></script>
|
||||
<script src="js/settings/telnet.js"></script>
|
||||
<script src="js/settings/dns.js"></script>
|
||||
<script src="js/reboot.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,5 +1,5 @@
|
||||
const authhost = 'http://192.168.122.76:43243'
|
||||
const host = 'http://192.168.122.76:43244'
|
||||
const host = window.location.origin
|
||||
const prefix = 'api'
|
||||
|
||||
const routes = {
|
||||
@@ -7,6 +7,8 @@ const routes = {
|
||||
user: () => [authhost, prefix, 'users', 'user'].join('/'),
|
||||
telnet_status: () => [host, prefix, 'settings', 'telnet', 'get'].join('/'),
|
||||
telnet_status_set: () => [host, prefix, 'settings', 'telnet', 'set'].join('/'),
|
||||
dns_status: () => [host, prefix, 'settings', 'dns', 'get'].join('/'),
|
||||
dns_status_set: () => [host, prefix, 'settings', 'dns', 'set'].join('/'),
|
||||
reboot: () => [host, prefix, 'reboot'].join('/')
|
||||
// all_users: () => [host, prefix, 'users', 'all'].join('/'),
|
||||
// access: () => [host, prefix, 'access'].join('/'),
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
const dnsenabled = document.querySelector('#dnsenable')
|
||||
const setdnssettingsbutton = document.querySelector('#set_dns_settings')
|
||||
const confirmdnsform = document.querySelector('.confirmdnsform')
|
||||
const canceldnsbutton = document.querySelector('#canceldns')
|
||||
const confirmdnsbutton = document.querySelector('#confirmdns')
|
||||
|
||||
const get_dns_enabled_status = () => {
|
||||
fetch(routes.dns_status(), {
|
||||
method: 'POST',
|
||||
body: `{"token":"${localStorage.getItem("token")}"}`
|
||||
}).then(res => res.json()).then(jsondata => {
|
||||
if (jsondata.enabled === '1') {
|
||||
dnsenabled.checked = true
|
||||
// dnssettingbody.classList.add('active')
|
||||
return;
|
||||
}
|
||||
}).catch((error) => {
|
||||
notification(`Ошибка на сервере: ${error}`, "error")
|
||||
})
|
||||
}
|
||||
|
||||
get_dns_enabled_status()
|
||||
|
||||
const set_dns_enabled_status = (status) => {
|
||||
fetch(routes.dns_status_set(), {
|
||||
method: 'POST',
|
||||
body: `{"token":"${localStorage.getItem("token")}","enabled":"${status?1:0}"}`
|
||||
}).then(res => res.json()).then(jsondata => {
|
||||
if (jsondata.enabled === '1') {
|
||||
dnsenabled.checked = true
|
||||
// dnssettingbody.classList.add('active')
|
||||
}
|
||||
notification("Настройки dns сохранены", "success")
|
||||
}).catch((error) => {
|
||||
notification(`Ошибка на сервере: ${error}`, "error")
|
||||
})
|
||||
}
|
||||
|
||||
setdnssettingsbutton.addEventListener('click', () =>{
|
||||
confirmdnsform.classList.add('active')
|
||||
})
|
||||
canceldnsbutton.addEventListener('click', () => {
|
||||
confirmdnsform.classList.remove('active')
|
||||
})
|
||||
|
||||
confirmdnsbutton.addEventListener('click', () => {
|
||||
set_dns_enabled_status(dnsenabled.checked)
|
||||
confirmdnsform.classList.remove('active')
|
||||
})
|
||||
@@ -1,6 +1,9 @@
|
||||
const telnetenabled = document.querySelector('#telnetenable')
|
||||
const telnetsettingbody = document.querySelector('.telnetsettingbody')
|
||||
const settelnetsettingsbutton = document.querySelector('#set_telnet_settings')
|
||||
const confirmtelnetform = document.querySelector('.confirmtelnetform')
|
||||
const canceltelnetbutton = document.querySelector('#canceltelnet')
|
||||
const confirmtelnetbutton = document.querySelector('#confirmtelnet')
|
||||
|
||||
const get_telnet_enabled_status = () => {
|
||||
fetch(routes.telnet_status(), {
|
||||
@@ -43,5 +46,14 @@ telnetenabled.addEventListener('click', () => {
|
||||
})
|
||||
|
||||
settelnetsettingsbutton.addEventListener('click', () => {
|
||||
confirmtelnetform.classList.add('active')
|
||||
})
|
||||
|
||||
canceltelnetbutton.addEventListener('click', () => {
|
||||
confirmtelnetform.classList.remove('active')
|
||||
})
|
||||
|
||||
confirmtelnetbutton.addEventListener('click', () => {
|
||||
set_telnet_enabled_status(telnetenabled.checked)
|
||||
confirmtelnetform.classList.remove('active')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user