2026-05-14 11:15:30 -04:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
const domainValidator = require('../validators/domain');
|
|
|
|
|
const authMw = require('../session');
|
|
|
|
|
const database = require('../database.js');
|
|
|
|
|
const dbConnection = database.db;
|
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
const pageTitle = 'Domain Manager | Edit DNS';
|
2026-05-14 16:00:59 -04:00
|
|
|
const { GetAllRecords, CoreDNSToDomain } = require('../coredns_bridge.js');
|
2026-05-14 16:43:54 -04:00
|
|
|
const { capitalizeFirstLetter } = require('../helpers.js');
|
2026-05-14 11:15:30 -04:00
|
|
|
|
2026-05-14 17:09:26 -04:00
|
|
|
const recordTypeMap = {
|
2026-05-14 17:51:43 -04:00
|
|
|
"host": "A",
|
|
|
|
|
"cname": "CNAME",
|
|
|
|
|
"txt": "TXT",
|
|
|
|
|
"mx": "MX",
|
|
|
|
|
"srv": "SRV"
|
2026-05-14 17:09:26 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 18:33:29 -04:00
|
|
|
const supportedRecordTypes = [
|
|
|
|
|
"A",
|
|
|
|
|
"CNAME",
|
|
|
|
|
"TXT",
|
|
|
|
|
"MX"
|
|
|
|
|
];
|
|
|
|
|
|
2026-05-14 11:15:30 -04:00
|
|
|
// Manage domains
|
|
|
|
|
router.get('/dns/edit/:domainId', authMw.AllowIfAuthenticated, async (req, res, next) => {
|
|
|
|
|
const result = await dbConnection.transaction(async(t) => {
|
|
|
|
|
const ownedDomain = await database.models.RegisteredDomain.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: req.params.domainId,
|
|
|
|
|
owner: req.session.userId
|
|
|
|
|
}
|
|
|
|
|
}, {transaction: t});
|
|
|
|
|
|
|
|
|
|
return ownedDomain;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
|
return next();
|
|
|
|
|
|
2026-05-14 14:45:14 -04:00
|
|
|
const fullDomain = result.domain + "." + result.tld;
|
2026-05-14 15:49:03 -04:00
|
|
|
const rawRecords = await GetAllRecords(fullDomain);
|
|
|
|
|
let records = [];
|
2026-05-14 17:41:32 -04:00
|
|
|
let recordHeaders = ["Type", "Name"];
|
2026-05-14 14:45:14 -04:00
|
|
|
|
2026-05-14 15:49:03 -04:00
|
|
|
for(const [key, value] of Object.entries(rawRecords)) {
|
2026-05-14 16:34:50 -04:00
|
|
|
let parsedVal = JSON.parse(value);
|
2026-05-14 18:14:23 -04:00
|
|
|
let finalRecord = {_key: key};
|
2026-05-14 17:09:26 -04:00
|
|
|
let recordType = undefined;
|
|
|
|
|
|
|
|
|
|
for(const [key, value] of Object.entries(parsedVal)) {
|
|
|
|
|
if(recordTypeMap[key] !== undefined) {
|
|
|
|
|
finalRecord["Type"] = recordTypeMap[key];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 17:41:32 -04:00
|
|
|
finalRecord["Name"] = CoreDNSToDomain(key);
|
|
|
|
|
|
2026-05-14 17:09:26 -04:00
|
|
|
if(recordType === undefined) {
|
|
|
|
|
console.warn("Unknown record type!");
|
|
|
|
|
}
|
2026-05-14 16:34:50 -04:00
|
|
|
|
|
|
|
|
for(const [key, value] of Object.entries(parsedVal)) {
|
2026-05-14 18:14:23 -04:00
|
|
|
if(!recordHeaders.includes(key) && key !== '_key') {
|
2026-05-14 16:43:54 -04:00
|
|
|
recordHeaders.push(capitalizeFirstLetter(key));
|
2026-05-14 16:34:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalRecord[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
records.push(finalRecord);
|
2026-05-14 15:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
2026-05-14 16:34:50 -04:00
|
|
|
res.render('dns', {title: pageTitle, domain: result, dnsRecords: records, dnsRecordsHeaders: recordHeaders});
|
2026-05-14 11:15:30 -04:00
|
|
|
});
|
|
|
|
|
|
2026-05-14 18:33:29 -04:00
|
|
|
// Add new DNS record
|
|
|
|
|
router.get('/dns/new/:domainId', authMw.AllowIfAuthenticated, async (req, res, next) => {
|
|
|
|
|
const result = await dbConnection.transaction(async(t) => {
|
|
|
|
|
const ownedDomain = await database.models.RegisteredDomain.findOne({
|
|
|
|
|
where: {
|
|
|
|
|
id: req.params.domainId,
|
|
|
|
|
owner: req.session.userId
|
|
|
|
|
}
|
|
|
|
|
}, {transaction: t});
|
|
|
|
|
|
|
|
|
|
return ownedDomain;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
|
return next();
|
|
|
|
|
|
|
|
|
|
const fullDomain = result.domain + "." + result.tld;
|
|
|
|
|
|
|
|
|
|
res.render('newdns', {title: pageTitle, domain: result, supportedRecordTypes: supportedRecordTypes});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-14 11:15:30 -04:00
|
|
|
module.exports = router;
|