resman/backend/public/js/domains.js
2026-04-06 18:21:01 +02:00

371 lines
8.3 KiB
JavaScript

/* =========================
DNS CACHE (mit TTL)
========================= */
const dnsCache = {}
async function getDNS(domain){
const now = Date.now()
const TTL = 30000 // 30 Sekunden
if(dnsCache[domain] && (now - dnsCache[domain].time < TTL)){
return dnsCache[domain].ips
}
try{
const res = await api(API + "/dns/" + domain, {}, { showError: false })
const ips = res.ips || []
dnsCache[domain] = {
ips: ips,
time: now
}
return ips
}catch(e){
dnsCache[domain] = {
ips: [],
time: now
}
return []
}
}
/* =========================
DOMAINS + SUBDOMAINS
========================= */
window.loadDomains = async function(){
const domains = await api(API + "/domains")
const subs = await api(API + "/subdomains")
window.domainList = domains
const table = document.getElementById("domains")
table.innerHTML = ""
for(const d of domains){
const tr = document.createElement("tr")
// Subdomains Liste
const sublist = subs
.filter(s => s.domain_id === d.id)
.map(s => `
<div class="subdomain">
${s.subdomain}.${s.domain_name}
<span id="subdns-${s.id}">...</span>
<span id="subserver-${s.id}" class="serverlink"></span>
<button onclick='openSubEdit(${JSON.stringify(s)})'>Edit</button>
<button onclick="deleteSub(${s.id})">Delete</button>
</div>
`).join("")
tr.innerHTML = `
<td>
<button onclick="moveDomain(${d.id}, 'up')">⬆</button>
<button onclick="moveDomain(${d.id}, 'down')">⬇</button>
</td>
<td>
${d.domain_name}
${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>
<button onclick='openDomainEdit(${JSON.stringify(d)})'>Edit</button>
<button onclick="deleteDomain(${d.id})">Delete</button>
<button onclick="openSubCreate(${d.id})">+ Subdomain</button>
</td>
`
table.appendChild(tr)
/* =========================
SERVER ZUORDNUNG SUBS
========================= */
const resources = window.resources || []
subs.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.innerText = " → " + server.name
}
}
})
/* =========================
DNS CHECK DOMAIN
========================= */
try{
const ips = await getDNS(d.domain_name)
let status = "❌"
if(ips.length){
if(d.ip_address && ips.includes(d.ip_address)){
status = "✅ " + ips.join(", ")
}else{
status = "⚠ " + ips.join(", ")
}
}
document.getElementById("dns-"+d.id).innerHTML = status
}catch(e){
document.getElementById("dns-"+d.id).innerHTML = "❌"
}
/* =========================
DNS CHECK SUBDOMAINS
========================= */
subs
.filter(s => s.domain_id === d.id)
.forEach(async s => {
const full = s.subdomain + "." + s.domain_name
try{
const ips = await getDNS(full)
let status = "❌"
if(ips.length){
if(s.ip_address && ips.includes(s.ip_address)){
status = "✅ " + ips.join(", ")
}else{
status = "⚠ " + ips.join(", ")
}
}
document.getElementById("subdns-"+s.id).innerHTML = status
}catch(e){
document.getElementById("subdns-"+s.id).innerHTML = "❌"
}
})
}
}
/* =========================
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)
})
}