Files
resman/backend/public/js/domains.js
T
2026-04-24 11:48:07 +02:00

418 lines
10 KiB
JavaScript

/* =========================
DNS CACHE (mit TTL)
========================= */
// =========================
// DNS CACHE (minimal + robust)
// =========================
const DNS_CACHE_TTL = 300000 // 5 min für erfolgreiche Antworten
const DNS_ERROR_TTL = 5000 // 5 sek für Fehler
const dnsCache = {}
const pending = {}
function normalizeDomain(domain) {
return domain.trim().toLowerCase()
}
async function getDNS(domain){
if(!domain) return []
const key = normalizeDomain(domain)
const now = Date.now()
const cached = dnsCache[key]
// ✅ gültiger Cache
if(cached && (now - cached.time < cached.ttl)){
return cached.ips
}
// ✅ laufende Anfrage wiederverwenden (dedupe)
if(pending[key]){
return pending[key]
}
pending[key] = (async () => {
try{
const res = await api(
API + "/dns/" + encodeURIComponent(key),
{},
{ showError: false }
)
const ips = res?.ips || []
dnsCache[key] = {
ips,
time: Date.now(),
ttl: DNS_CACHE_TTL
}
return ips
}catch(e){
// ❗ Fehler nur kurz cachen
dnsCache[key] = {
ips: [],
time: Date.now(),
ttl: DNS_ERROR_TTL
}
return []
}finally{
delete pending[key]
}
})()
return pending[key]
}
function dnsStatusMarkup(expectedIp, ips){
if(!ips.length){
return `
<span class="dns-badge off">Kein DNS</span>
`
}
const matches = expectedIp && ips.includes(expectedIp)
const badgeClass = matches ? "ok" : "warn"
const label = matches ? "DNS OK" : "DNS Abweichung"
return `
<span class="dns-badge ${badgeClass}">${label}</span>
<span class="dns-detail">${ips.join(", ")}</span>
`
}
function chipMarkup(label, muted = false){
return `<span class="meta-chip${muted ? " muted" : ""}">${label}</span>`
}
/* =========================
DOMAINS + SUBDOMAINS
========================= */
window.loadDomains = async function(){
const domains = await api(API + "/domains")
const subs = await api(API + "/subdomains")
window.domainList = domains
const resources = window.resources || []
const table = document.getElementById("domains")
table.innerHTML = ""
for(const d of domains){
const tr = document.createElement("tr")
const domainSubs = subs.filter(s => s.domain_id === d.id)
const sublist = domainSubs.length
? `
<div class="subdomain-list">
${domainSubs.map(s => `
<div class="subdomain-card">
<div class="subdomain-row">
<div>
<div class="subdomain-name">${s.subdomain}.${s.domain_name}</div>
<div class="subdomain-meta">
${s.ip_address ? chipMarkup(s.ip_address, true) : chipMarkup("Keine IP", true)}
<span id="subserver-${s.id}"></span>
</div>
</div>
<div class="domain-actions">
<button class="btn-small" onclick='openSubEdit(${JSON.stringify(s)})'>Bearbeiten</button>
<button class="btn-small btn-danger" onclick="deleteSub(${s.id})">Loeschen</button>
</div>
</div>
<div id="subdns-${s.id}" style="margin-top:8px">...</div>
</div>
`).join("")}
</div>
`
: `<div class="dns-detail">Noch keine Subdomains</div>`
tr.innerHTML = `
<td>
<button class="btn-small" onclick="moveDomain(${d.id}, 'up')">⬆</button>
<button class="btn-small" onclick="moveDomain(${d.id}, 'down')">⬇</button>
</td>
<td>
<div class="domain-name">${d.domain_name}</div>
<div class="domain-meta">
${d.provider ? chipMarkup(d.provider) : ""}
${d.ip_address ? chipMarkup(d.ip_address, true) : chipMarkup("Keine Ziel-IP", true)}
${d.resource_name ? chipMarkup(d.resource_name) : chipMarkup("Kein Server", true)}
</div>
${sublist}
</td>
<td><span class="provider">${d.provider || "?"}</span></td>
<td>${d.ip_address || ""}</td>
<td>${d.resource_name || ""}</td>
<td id="dns-${d.id}">...</td>
<td>${d.yearly_cost || ""}</td>
<td class="domain-actions">
<button class="btn-small" onclick='openDomainEdit(${JSON.stringify(d)})'>Bearbeiten</button>
<button class="btn-small" onclick="openSubCreate(${d.id})">Subdomain</button>
<button class="btn-small btn-danger" onclick="deleteDomain(${d.id})">Loeschen</button>
</td>
`
table.appendChild(tr)
/* =========================
SERVER ZUORDNUNG SUBS
========================= */
domainSubs.forEach(s => {
const server = resources.find(r =>
Array.isArray(r.ips) &&
r.ips.some(ip => ip.ip === s.ip_address)
)
if(server){
const el = document.getElementById("subserver-"+s.id)
if(el){
el.innerHTML = chipMarkup(server.name)
}
}
})
/* =========================
DNS CHECK DOMAIN
========================= */
try{
const ips = await getDNS(d.domain_name)
document.getElementById("dns-"+d.id).innerHTML = dnsStatusMarkup(d.ip_address, ips)
}catch(e){
document.getElementById("dns-"+d.id).innerHTML = `<span class="dns-badge off">Kein DNS</span>`
}
/* =========================
DNS CHECK SUBDOMAINS
========================= */
domainSubs.forEach(async s => {
const full = s.subdomain + "." + s.domain_name
try{
const ips = await getDNS(full)
document.getElementById("subdns-"+s.id).innerHTML = dnsStatusMarkup(s.ip_address, ips)
}catch(e){
document.getElementById("subdns-"+s.id).innerHTML = `<span class="dns-badge off">Kein DNS</span>`
}
})
}
}
/* =========================
DOMAIN CRUD
========================= */
window.saveDomain = async function(){
const id = document.getElementById("domain_id").value
let cost = document.getElementById("domain_cost").value
if(cost) cost = cost.replace(",", ".")
const data = {
domain_name: domain_name.value,
provider: domain_provider.value,
ip_address: domain_ip.value,
yearly_cost: cost || null,
notes: domain_notes.value
}
await runAction(
async () => {
if(id){
await api(API+"/domains/"+id,{
method:"PUT",
headers:{'Content-Type':'application/json'},
body:JSON.stringify(data)
})
}else{
await api(API+"/domains",{
method:"POST",
headers:{'Content-Type':'application/json'},
body:JSON.stringify(data)
})
}
},
async () => {
closeDomainModal()
await loadDomains()
await loadMapping()
loadCosts()
loadInfrastructure()
}
)
}
window.deleteDomain = async function(id){
if(!confirm("Domain löschen?")) return
await runAction(
async () => {
await api(API + "/domains/" + id, {
method: "DELETE"
})
},
async () => {
await loadDomains()
await loadMapping()
loadCosts()
loadInfrastructure()
}
)
}
window.moveDomain = async function(id, direction){
await runAction(
async () => {
await api(API + "/domains/" + id + "/move", {
method: "POST",
headers: {'Content-Type':'application/json'},
body: JSON.stringify({ direction })
})
},
async () => {
await loadDomains()
await loadMapping()
loadInfrastructure()
}
)
}
/* =========================
SUBDOMAIN CRUD
========================= */
window.saveSubdomain = async function(){
const id = document.getElementById("sub_id").value
const domain_id = document.getElementById("sub_domain_id").value
const subdomain = document.getElementById("sub_name").value
const ip_address = document.getElementById("sub_ip").value
const data = { domain_id, subdomain, ip_address }
await runAction(
async () => {
if(id){
await api(API+"/subdomains/"+id,{
method:"PUT",
headers:{'Content-Type':'application/json'},
body:JSON.stringify(data)
})
}else{
await api(API+"/subdomains",{
method:"POST",
headers:{'Content-Type':'application/json'},
body:JSON.stringify(data)
})
}
},
async () => {
closeSubModal()
await loadDomains()
await loadMapping()
loadInfrastructure()
}
)
}
window.deleteSub = async function(id){
if(!confirm("Subdomain löschen?")) return
await runAction(
async () => {
await api(API + "/subdomains/" + id, {
method: "DELETE"
})
},
async () => {
await loadDomains()
await loadMapping()
loadInfrastructure()
}
)
}
/* =========================
DOMAIN MAPPING
========================= */
window.loadMapping = async function(){
const data = await api(API + "/domainmap")
const table = document.getElementById("mapping")
table.innerHTML = ""
data.forEach(m => {
const tr = document.createElement("tr")
tr.innerHTML = `
<td>${m.domain_name}</td>
<td>${m.ip_address}</td>
<td>${m.server_name || ""}</td>
`
table.appendChild(tr)
})
}