diff --git a/config.json b/config.json index f79a114..0c05b20 100644 --- a/config.json +++ b/config.json @@ -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" } diff --git a/src/restart_dnsmasq.c b/src/restart_dnsmasq.c new file mode 100644 index 0000000..83990fd --- /dev/null +++ b/src/restart_dnsmasq.c @@ -0,0 +1,32 @@ +#include "restart_dnsmasq.h" +#include "ConfigManager.h" +#include "config.h" +#include +#include + +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; +} diff --git a/src/restart_dnsmasq.h b/src/restart_dnsmasq.h new file mode 100644 index 0000000..d434892 --- /dev/null +++ b/src/restart_dnsmasq.h @@ -0,0 +1,3 @@ +#pragma once + +char *restart_dnsmasq(); diff --git a/src/set_settings_file.c b/src/set_settings_file.c index c13bb0c..75b8d0a 100644 --- a/src/set_settings_file.c +++ b/src/set_settings_file.c @@ -1,21 +1,52 @@ -#include "settings_file.h" #include +#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); } diff --git a/web/js/settings/settings.js b/web/js/settings/settings.js index e45e713..a8ba708 100644 --- a/web/js/settings/settings.js +++ b/web/js/settings/settings.js @@ -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 += ` + upstream_dns_servers_table_body.innerHTML = ""; + for (var i = 0; i < settings.resolv.length; i++) { + upstream_dns_servers_table_body.innerHTML += ` ${settings.resolv[i]} `; - } + } }; 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 += ` + 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 += ` ${key} ${value[i]} `; - } - } else { - local_domains_table_body.innerHTML += ` + } + } else { + local_domains_table_body.innerHTML += ` ${key} ${value} `; - } } + } }; 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"); + } + } });