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",
"tls_address": "0.0.0.0:8322",
"web": "./web",
"tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key",
"system_initialization": "rc",
"dnsmasq_config": "/etc/dnsmasq.conf"
"address": "0.0.0.0:8321",
"tls_address": "0.0.0.0:8322",
"web": "./web",
"tls_enabled": true,
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
"tls_key": "/home/tola/certs/localhost/localhost.key",
"system_initialization": "systemd",
"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 "cJSON.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) {
char *filename = cm_get_parameter_as_string(config, "dnsmasq_config");
if (!filename) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't read config file"));
return;
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't read config file"));
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) {
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't write file"));
return;
mg_http_reply(c, 500, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
MG_ESC("can't write file"));
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(res);
}
+120 -118
View File
@@ -1,207 +1,209 @@
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(
"#local-domains-table-body",
"#local-domains-table-body",
);
const cache_size_input = document.querySelector("#cache-size-input");
const save_settings_button = document.querySelector("#save-settings-button");
const add_dns_server_button = document.querySelector("#add_dns_server_button");
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 save_config_file_button = document.querySelector(
"#save_config_file_button",
"#save_config_file_button",
);
var settings = {};
var configFile = "";
const updateDnsServersTable = () => {
upstream_dns_servers_table_body.innerHTML = "";
for (var i = 0; i < settings.resolv.length; i++) {
upstream_dns_servers_table_body.innerHTML += `<tr>
upstream_dns_servers_table_body.innerHTML = "";
for (var i = 0; i < settings.resolv.length; i++) {
upstream_dns_servers_table_body.innerHTML += `<tr>
<th class="d-flex align-items-center justify-content-between">
${settings.resolv[i]}
<button id="${i}" class="delete-table-button btn btn-danger">Del</button>
</th>
</tr>`;
}
}
};
const updateLocalDomainsTable = () => {
local_domains_table_body.innerHTML = "";
for (const [key, value] of Object.entries(settings.hosts)) {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
local_domains_table_body.innerHTML += `<tr>
local_domains_table_body.innerHTML = "";
for (const [key, value] of Object.entries(settings.hosts)) {
if (Array.isArray(value)) {
for (var i = 0; i < value.length; i++) {
local_domains_table_body.innerHTML += `<tr>
<th>${key}</th>
<th>${value[i]}</th>
<th><button class="deldomain btn btn-danger" id="${key}|${value[i]}">Del</button>
</th>
</tr>`;
}
} else {
local_domains_table_body.innerHTML += `<tr>
}
} else {
local_domains_table_body.innerHTML += `<tr>
<th>${key}</th>
<th>${value}</th>
<th><button class="deldomain btn btn-danger" id="${key}|${value}">Del</button>
</th>
</tr>`;
}
}
}
};
const updateInterface = () => {
updateDnsServersTable();
updateLocalDomainsTable();
cache_size_input.value = settings.cache_size;
updateDnsServersTable();
updateLocalDomainsTable();
cache_size_input.value = settings.cache_size;
};
const getSettings = () => {
fetch(window.location.origin + "/api/settings")
.then((data) => data.json())
.then((jsondata) => {
settings = jsondata;
updateInterface();
})
.catch((e) => alert(e));
fetch(window.location.origin + "/api/settings")
.then((data) => data.json())
.then((jsondata) => {
settings = jsondata;
updateInterface();
})
.catch((e) => alert(e));
};
const setSettings = () => {
fetch(window.location.origin + "/api/settings", {
method: "POST",
body: JSON.stringify(settings),
fetch(window.location.origin + "/api/settings", {
method: "POST",
body: JSON.stringify(settings),
})
.then((data) => data.json())
.then((jsondata) => {
settings = jsondata;
updateInterface();
alert("Success set settings");
})
.then((data) => data.json())
.then((jsondata) => {
settings = jsondata;
updateInterface();
alert("Success set settings");
})
.catch((e) => alert(e));
.catch((e) => alert(e));
};
save_settings_button.addEventListener("click", () => {
setSettings();
setSettings();
});
const write_cache_size = (value) => {
settings.cache_size = value;
settings.cache_size = value;
};
cache_size_input.addEventListener("change", (e) => {
write_cache_size(e.target.value);
write_cache_size(e.target.value);
});
cache_size_input.addEventListener("input", (e) => {
write_cache_size(e.target.value);
write_cache_size(e.target.value);
});
getSettings();
add_dns_server_button.addEventListener("click", () => {
const dns_server = prompt("Input your DNS server:");
if (dns_server === null) return;
if (confirm("Confirm DNS settings? (add DNS server)")) {
if (Array.isArray(settings.resolv)) {
if (!settings.resolv.includes(dns_server)) {
settings.resolv.push(dns_server);
updateDnsServersTable();
}
}
const dns_server = prompt("Input your DNS server:");
if (dns_server === null) return;
if (confirm("Confirm DNS settings? (add DNS server)")) {
if (Array.isArray(settings.resolv)) {
if (!settings.resolv.includes(dns_server)) {
settings.resolv.push(dns_server);
updateDnsServersTable();
}
}
}
});
upstream_dns_servers_table_body.addEventListener("click", (e) => {
if (e.target.classList.contains("delete-table-button")) {
if (
confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)
) {
if (Array.isArray(settings.resolv)) {
settings.resolv.splice(e.target.id, 1);
updateDnsServersTable();
}
}
if (e.target.classList.contains("delete-table-button")) {
if (confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)) {
if (Array.isArray(settings.resolv)) {
settings.resolv.splice(e.target.id, 1);
updateDnsServersTable();
}
}
}
});
add_local_domain_button.addEventListener("click", () => {
const domainip = prompt("Input localdomain IP:");
if (domainip === null) return;
const domain = prompt("Input domain:");
if (domainip === null || domain === null) return;
if (
confirm(
`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`,
)
) {
for (const [key, value] of Object.entries(settings.hosts)) {
if (key === domainip) {
if (Array.isArray(value)) {
if (!value.includes(domain)) {
value.push(domain);
updateLocalDomainsTable();
return;
}
return;
}
const domainsarray = [value, domain];
settings.hosts[key] = domainsarray;
updateLocalDomainsTable();
return;
}
const domainip = prompt("Input localdomain IP:");
if (domainip === null) return;
const domain = prompt("Input domain:");
if (domainip === null || domain === null) return;
if (
confirm(`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`)
) {
for (const [key, value] of Object.entries(settings.hosts)) {
if (key === domainip) {
if (Array.isArray(value)) {
if (!value.includes(domain)) {
value.push(domain);
updateLocalDomainsTable();
return;
}
return;
}
settings.hosts[domainip] = domain;
const domainsarray = [value, domain];
settings.hosts[key] = domainsarray;
updateLocalDomainsTable();
return;
}
}
settings.hosts[domainip] = domain;
updateLocalDomainsTable();
}
});
local_domains_table_body.addEventListener("click", (e) => {
if (e.target.classList.contains("deldomain")) {
if (confirm(`Confirm DNS settings? (delete domain: ${e.target.id})`)) {
const domainip = e.target.id.split("|")[0];
const domain = e.target.id.split("|")[1];
for (const [key, value] of Object.entries(settings.hosts)) {
if (key === domainip) {
if (Array.isArray(value)) {
if (value.includes(domain)) {
value.splice(value.indexOf(domain), 1);
updateLocalDomainsTable();
return;
}
return;
}
delete settings.hosts[key];
updateLocalDomainsTable();
return;
}
if (e.target.classList.contains("deldomain")) {
if (confirm(`Confirm DNS settings? (delete domain: ${e.target.id})`)) {
const domainip = e.target.id.split("|")[0];
const domain = e.target.id.split("|")[1];
for (const [key, value] of Object.entries(settings.hosts)) {
if (key === domainip) {
if (Array.isArray(value)) {
if (value.includes(domain)) {
value.splice(value.indexOf(domain), 1);
updateLocalDomainsTable();
return;
}
return;
}
delete settings.hosts[key];
updateLocalDomainsTable();
return;
}
}
}
}
});
const getConfigFile = () => {
fetch(window.location.origin + "/api/settings/file")
.then((data) => data.text())
.then((textdata) => (edit_file_input.value = textdata))
.catch((e) => alert(e));
fetch(window.location.origin + "/api/settings/file")
.then((data) => data.text())
.then((textdata) => (edit_file_input.value = textdata))
.catch((e) => alert(e));
};
getConfigFile();
save_config_file_button.addEventListener("click", () => {
if (confirm("Save Config file?")) {
fetch(window.location.origin + "/api/settings/file", {
method: "POST",
body: edit_file_input.value,
})
.then((data) => data.text())
.then((textdata) => {
edit_file_input.value = textdata;
alert("Success write settings file");
})
.catch((e) => alert(e));
save_config_file_button.addEventListener("click", async () => {
if (confirm("Save Config file?")) {
var response = await fetch(window.location.origin + "/api/settings/file", {
method: "POST",
body: edit_file_input.value,
});
if (!response.ok) {
const result = await response.json();
alert(result.error || "Failed to save config file");
return;
}
const result = await response.json();
if (result.error) {
alert(result.error);
} else {
edit_file_input.value = result.data;
alert("Success write settings file");
}
}
});