1 parent
d1859af112
commit
3418307e84
1 file changed
+120
-96
@@ -1,29 +1,37 @@
|
|||||||
const upstream_dns_servers_table_body = document.querySelector('#upstream-dns-servers-table-body')
|
const upstream_dns_servers_table_body = document.querySelector(
|
||||||
const local_domains_table_body = document.querySelector('#local-domains-table-body')
|
"#upstream-dns-servers-table-body",
|
||||||
const cache_size_input = document.querySelector('#cache-size-input')
|
);
|
||||||
const save_settings_button = document.querySelector('#save-settings-button')
|
const local_domains_table_body = document.querySelector(
|
||||||
const add_dns_server_button = document.querySelector('#add_dns_server_button')
|
"#local-domains-table-body",
|
||||||
const add_local_domain_button = document.querySelector('#add_local_domain_button')
|
);
|
||||||
const edit_file_input = document.querySelector('#edit_file_input')
|
const cache_size_input = document.querySelector("#cache-size-input");
|
||||||
const save_config_file_button = document.querySelector('#save_config_file_button')
|
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",
|
||||||
|
);
|
||||||
|
const edit_file_input = document.querySelector("#edit_file_input");
|
||||||
|
const save_config_file_button = document.querySelector(
|
||||||
|
"#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++) {
|
||||||
@@ -32,7 +40,7 @@ const updateLocalDomainsTable = () => {
|
|||||||
<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>
|
||||||
@@ -40,144 +48,160 @@ const updateLocalDomainsTable = () => {
|
|||||||
<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));
|
||||||
|
};
|
||||||
|
|
||||||
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()
|
|
||||||
})
|
})
|
||||||
}
|
.then((data) => data.json())
|
||||||
|
.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 (confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)) {
|
if (
|
||||||
|
confirm(`Confirm DNS settings? (delete DNS server: ${e.target.id})`)
|
||||||
|
) {
|
||||||
if (Array.isArray(settings.resolv)) {
|
if (Array.isArray(settings.resolv)) {
|
||||||
settings.resolv.splice(e.target.id, 1)
|
settings.resolv.splice(e.target.id, 1);
|
||||||
updateDnsServersTable()
|
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 (confirm(`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`)) {
|
if (
|
||||||
|
confirm(
|
||||||
|
`Confirm DNS settings? (Add Local Domain: ${domainip} ${domain})`,
|
||||||
|
)
|
||||||
|
) {
|
||||||
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.push(domain)
|
value.push(domain);
|
||||||
updateLocalDomainsTable()
|
updateLocalDomainsTable();
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
const domainsarray = [value, domain]
|
const domainsarray = [value, domain];
|
||||||
settings.hosts[key] = domainsarray
|
settings.hosts[key] = domainsarray;
|
||||||
updateLocalDomainsTable()
|
updateLocalDomainsTable();
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
settings.hosts[domainip] = domain
|
settings.hosts[domainip] = domain;
|
||||||
updateLocalDomainsTable()
|
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
|
return;
|
||||||
}
|
}
|
||||||
delete settings.hosts[key]
|
delete settings.hosts[key];
|
||||||
updateLocalDomainsTable()
|
updateLocalDomainsTable();
|
||||||
return
|
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));
|
||||||
|
};
|
||||||
|
|
||||||
getConfigFile()
|
getConfigFile();
|
||||||
|
|
||||||
save_config_file_button.addEventListener('click', () => {
|
save_config_file_button.addEventListener("click", () => {
|
||||||
if (confirm("Save Config file?")) {
|
if (confirm("Save Config file?")) {
|
||||||
fetch(window.location.origin + "/api/settings/file", {
|
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())
|
})
|
||||||
.then(textdata => edit_file_input.value = textdata)
|
.then((data) => data.text())
|
||||||
|
.then((textdata) => {
|
||||||
|
edit_file_input.value = textdata;
|
||||||
|
alert("Success write settings file");
|
||||||
|
})
|
||||||
|
.catch((e) => alert(e));
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
Reference in new issue
Block a user