added restart_dnsmasq and change set_settings_file
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 6s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 8s

This commit is contained in:
wt committed 2025-09-09 14:02:21 +07:00
1 parent 4473b90a2a
commit 6e7e8b19eb
5 files changed
+203 -135

No files matched your search

+8 -8
View File
@@ -1,10 +1,10 @@
{ {
"address": "0.0.0.0:8321", "address": "0.0.0.0:8321",
"tls_address": "0.0.0.0:8322", "tls_address": "0.0.0.0:8322",
"web": "./web", "web": "./web",
"tls_enabled": true, "tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt", "tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key", "tls_key": "/home/tola/certs/localhost/localhost.key",
"system_initialization": "rc", "system_initialization": "systemd",
"dnsmasq_config": "/etc/dnsmasq.conf" "dnsmasq_config": "/etc/dnsmasq.conf"
} }
+32
View File
@@ -0,0 +1,32 @@
#include "restart_dnsmasq.h"
#include "ConfigManager.h"
#include "config.h"
#include <string.h>
#include <stdlib.h>
char *restart_dnsmasq() {
int res = -2;
char *system_initialization = cm_get_parameter_as_string(config, "system_initialization");
if (!system_initialization) {
res = -1;
}
if (!strcmp(system_initialization, "rc")) {
res = system("rc restart dnsmasq");
}
if (!strcmp(system_initialization, "openrc")) {
res = system("rc restart dnsmasq");
}
if (!strcmp(system_initialization, "systemd")) {
res = system("systemctl restart dnsmasq");
}
if (!strcmp(system_initialization, "finit")) {
res = system("initctl restart dnsmasq");
}
if (!strcmp(system_initialization, "init")) {
res = system("/etc/init.d/dnsmasq restart");
}
if (res != 0) {
return strdup("Failed to restart dnsmasq");
}
return nullptr;
}
+3
View File
@@ -0,0 +1,3 @@
#pragma once
char *restart_dnsmasq();
+40 -9
View File
@@ -1,21 +1,52 @@
#include "settings_file.h"
#include <file.h> #include <file.h>
#include "cJSON.h"
#include "config.h" #include "config.h"
#include "restart_dnsmasq.h"
#include "settings_file.h"
void set_settings_file(struct mg_connection *c, struct mg_http_message *hm) { void set_settings_file(struct mg_connection *c, struct mg_http_message *hm) {
char *filename = cm_get_parameter_as_string(config, "dnsmasq_config"); char *filename = cm_get_parameter_as_string(config, "dnsmasq_config");
if (!filename) { if (!filename) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't read config file")); MG_ESC("can't read config file"));
return; return;
} }
char *res = write_file(cm_get_parameter_as_string(config, "dnsmasq_config"), hm->body.buf); char *res = write_file(cm_get_parameter_as_string(config, "dnsmasq_config"),
hm->body.buf);
if (!res) { if (!res) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"), mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't write file")); MG_ESC("can't write file"));
return; return;
} }
mg_http_reply(c, 200, "Content-Type: text/plain\r\nAccess-Control-Allow-Origin: *\r\n", res); char *error = restart_dnsmasq();
if (error) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC(error));
free(error);
return;
}
cJSON *response = cJSON_CreateObject();
if (!response) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't create response"));
free(filename);
free(res);
return;
}
if (!cJSON_AddStringToObject(response, "data", res)) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't add data to response"));
cJSON_Delete(response);
free(filename);
free(res);
return;
}
char *response_str = cJSON_PrintUnformatted(response);
mg_http_reply(
c, 200, "Content-Type: text/plain\r\nAccess-Control-Allow-Origin: *\r\n",
response_str);
cJSON_Delete(response);
free(response_str);
free(filename); free(filename);
free(res); free(res);
} }
+120 -118
View File
@@ -1,207 +1,209 @@
const upstream_dns_servers_table_body = document.querySelector( const upstream_dns_servers_table_body = document.querySelector(
"#upstream-dns-servers-table-body", "#upstream-dns-servers-table-body",
); );
const local_domains_table_body = document.querySelector( const local_domains_table_body = document.querySelector(
"#local-domains-table-body", "#local-domains-table-body",
); );
const cache_size_input = document.querySelector("#cache-size-input"); const cache_size_input = document.querySelector("#cache-size-input");
const save_settings_button = document.querySelector("#save-settings-button"); const save_settings_button = document.querySelector("#save-settings-button");
const add_dns_server_button = document.querySelector("#add_dns_server_button"); const add_dns_server_button = document.querySelector("#add_dns_server_button");
const add_local_domain_button = document.querySelector( const add_local_domain_button = document.querySelector(
"#add_local_domain_button", "#add_local_domain_button",
); );
const edit_file_input = document.querySelector("#edit_file_input"); const edit_file_input = document.querySelector("#edit_file_input");
const save_config_file_button = document.querySelector( const save_config_file_button = document.querySelector(
"#save_config_file_button", "#save_config_file_button",
); );
var settings = {}; var settings = {};
var configFile = ""; var configFile = "";
const updateDnsServersTable = () => { const updateDnsServersTable = () => {
upstream_dns_servers_table_body.innerHTML = ""; upstream_dns_servers_table_body.innerHTML = "";
for (var i = 0; i < settings.resolv.length; i++) { for (var i = 0; i < settings.resolv.length; i++) {
upstream_dns_servers_table_body.innerHTML += `<tr> upstream_dns_servers_table_body.innerHTML += `<tr>
<th class="d-flex align-items-center justify-content-between"> <th class="d-flex align-items-center justify-content-between">
${settings.resolv[i]} ${settings.resolv[i]}
<button id="${i}" class="delete-table-button btn btn-danger">Del</button> <button id="${i}" class="delete-table-button btn btn-danger">Del</button>
</th> </th>
</tr>`; </tr>`;
} }
}; };
const updateLocalDomainsTable = () => { const updateLocalDomainsTable = () => {
local_domains_table_body.innerHTML = ""; local_domains_table_body.innerHTML = "";
for (const [key, value] of Object.entries(settings.hosts)) { for (const [key, value] of Object.entries(settings.hosts)) {
if (Array.isArray(value)) { if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) { for (var i = 0; i < value.length; i++) {
local_domains_table_body.innerHTML += `<tr> local_domains_table_body.innerHTML += `<tr>
<th>${key}</th> <th>${key}</th>
<th>${value[i]}</th> <th>${value[i]}</th>
<th><button class="deldomain btn btn-danger" id="${key}|${value[i]}">Del</button> <th><button class="deldomain btn btn-danger" id="${key}|${value[i]}">Del</button>
</th> </th>
</tr>`; </tr>`;
} }
} else { } else {
local_domains_table_body.innerHTML += `<tr> local_domains_table_body.innerHTML += `<tr>
<th>${key}</th> <th>${key}</th>
<th>${value}</th> <th>${value}</th>
<th><button class="deldomain btn btn-danger" id="${key}|${value}">Del</button> <th><button class="deldomain btn btn-danger" id="${key}|${value}">Del</button>
</th> </th>
</tr>`; </tr>`;
}
} }
}
}; };
const updateInterface = () => { const updateInterface = () => {
updateDnsServersTable(); updateDnsServersTable();
updateLocalDomainsTable(); updateLocalDomainsTable();
cache_size_input.value = settings.cache_size; cache_size_input.value = settings.cache_size;
}; };
const getSettings = () => { const getSettings = () => {
fetch(window.location.origin + "/api/settings") fetch(window.location.origin + "/api/settings")
.then((data) => data.json()) .then((data) => data.json())
.then((jsondata) => { .then((jsondata) => {
settings = jsondata; settings = jsondata;
updateInterface(); updateInterface();
}) })
.catch((e) => alert(e)); .catch((e) => alert(e));
}; };
const setSettings = () => { const setSettings = () => {
fetch(window.location.origin + "/api/settings", { fetch(window.location.origin + "/api/settings", {
method: "POST", method: "POST",
body: JSON.stringify(settings), body: JSON.stringify(settings),
})
.then((data) => data.json())
.then((jsondata) => {
settings = jsondata;
updateInterface();
alert("Success set settings");
}) })
.then((data) => data.json()) .catch((e) => alert(e));
.then((jsondata) => {
settings = jsondata;
updateInterface();
alert("Success set settings");
})
.catch((e) => alert(e));
}; };
save_settings_button.addEventListener("click", () => { save_settings_button.addEventListener("click", () => {
setSettings(); setSettings();
}); });
const write_cache_size = (value) => { const write_cache_size = (value) => {
settings.cache_size = value; settings.cache_size = value;
}; };
cache_size_input.addEventListener("change", (e) => { cache_size_input.addEventListener("change", (e) => {
write_cache_size(e.target.value); write_cache_size(e.target.value);
}); });
cache_size_input.addEventListener("input", (e) => { cache_size_input.addEventListener("input", (e) => {
write_cache_size(e.target.value); write_cache_size(e.target.value);
}); });
getSettings(); getSettings();
add_dns_server_button.addEventListener("click", () => { add_dns_server_button.addEventListener("click", () => {
const dns_server = prompt("Input your DNS server:"); const dns_server = prompt("Input your DNS server:");
if (dns_server === null) return; if (dns_server === null) return;
if (confirm("Confirm DNS settings? (add DNS server)")) { if (confirm("Confirm DNS settings? (add DNS server)")) {
if (Array.isArray(settings.resolv)) { if (Array.isArray(settings.resolv)) {
if (!settings.resolv.includes(dns_server)) { if (!settings.resolv.includes(dns_server)) {
settings.resolv.push(dns_server); settings.resolv.push(dns_server);
updateDnsServersTable(); updateDnsServersTable();
} }
}
} }
}
}); });
upstream_dns_servers_table_body.addEventListener("click", (e) => { upstream_dns_servers_table_body.addEventListener("click", (e) => {
if (e.target.classList.contains("delete-table-button")) { if (e.target.classList.contains("delete-table-button")) {
if ( if (confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)) {
confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`) if (Array.isArray(settings.resolv)) {
) { settings.resolv.splice(e.target.id, 1);
if (Array.isArray(settings.resolv)) { updateDnsServersTable();
settings.resolv.splice(e.target.id, 1); }
updateDnsServersTable();
}
}
} }
}
}); });
add_local_domain_button.addEventListener("click", () => { add_local_domain_button.addEventListener("click", () => {
const domainip = prompt("Input localdomain IP:"); const domainip = prompt("Input localdomain IP:");
if (domainip === null) return; if (domainip === null) return;
const domain = prompt("Input domain:"); const domain = prompt("Input domain:");
if (domainip === null || domain === null) return; if (domainip === null || domain === null) return;
if ( if (
confirm( confirm(`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`)
`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`, ) {
) for (const [key, value] of Object.entries(settings.hosts)) {
) { if (key === domainip) {
for (const [key, value] of Object.entries(settings.hosts)) { if (Array.isArray(value)) {
if (key === domainip) { if (!value.includes(domain)) {
if (Array.isArray(value)) { value.push(domain);
if (!value.includes(domain)) { updateLocalDomainsTable();
value.push(domain); return;
updateLocalDomainsTable(); }
return; return;
}
return;
}
const domainsarray = [value, domain];
settings.hosts[key] = domainsarray;
updateLocalDomainsTable();
return;
}
} }
settings.hosts[domainip] = domain; const domainsarray = [value, domain];
settings.hosts[key] = domainsarray;
updateLocalDomainsTable(); updateLocalDomainsTable();
return;
}
} }
settings.hosts[domainip] = domain;
updateLocalDomainsTable();
}
}); });
local_domains_table_body.addEventListener("click", (e) => { local_domains_table_body.addEventListener("click", (e) => {
if (e.target.classList.contains("deldomain")) { if (e.target.classList.contains("deldomain")) {
if (confirm(`Confirm DNS settings? (delete domain: ${e.target.id})`)) { if (confirm(`Confirm DNS settings? (delete domain: ${e.target.id})`)) {
const domainip = e.target.id.split("|")[0]; const domainip = e.target.id.split("|")[0];
const domain = e.target.id.split("|")[1]; const domain = e.target.id.split("|")[1];
for (const [key, value] of Object.entries(settings.hosts)) { for (const [key, value] of Object.entries(settings.hosts)) {
if (key === domainip) { if (key === domainip) {
if (Array.isArray(value)) { if (Array.isArray(value)) {
if (value.includes(domain)) { if (value.includes(domain)) {
value.splice(value.indexOf(domain), 1); value.splice(value.indexOf(domain), 1);
updateLocalDomainsTable(); updateLocalDomainsTable();
return; return;
}
return;
}
delete settings.hosts[key];
updateLocalDomainsTable();
return;
}
} }
return;
}
delete settings.hosts[key];
updateLocalDomainsTable();
return;
} }
}
} }
}
}); });
const getConfigFile = () => { const getConfigFile = () => {
fetch(window.location.origin + "/api/settings/file") fetch(window.location.origin + "/api/settings/file")
.then((data) => data.text()) .then((data) => data.text())
.then((textdata) => (edit_file_input.value = textdata)) .then((textdata) => (edit_file_input.value = textdata))
.catch((e) => alert(e)); .catch((e) => alert(e));
}; };
getConfigFile(); getConfigFile();
save_config_file_button.addEventListener("click", () => { save_config_file_button.addEventListener("click", async () => {
if (confirm("Save Config file?")) { if (confirm("Save Config file?")) {
fetch(window.location.origin + "/api/settings/file", { var response = await fetch(window.location.origin + "/api/settings/file", {
method: "POST", method: "POST",
body: edit_file_input.value, body: edit_file_input.value,
}) });
.then((data) => data.text()) if (!response.ok) {
.then((textdata) => { const result = await response.json();
edit_file_input.value = textdata; alert(result.error || "Failed to save config file");
alert("Success write settings file"); return;
})
.catch((e) => alert(e));
} }
const result = await response.json();
if (result.error) {
alert(result.error);
} else {
edit_file_input.value = result.data;
alert("Success write settings file");
}
}
}); });