Sync DNS2 production changes - removed obsolete test suite and refactored structure
This commit is contained in:
@@ -25,7 +25,7 @@ module.exports = function(ctx) {
|
||||
const response = await ctx.fetchT(`${ctx.caddy.adminUrl}/load`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': CADDY.CONTENT_TYPE },
|
||||
body: caddyfileContent,
|
||||
body: caddyfileContent
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -39,80 +39,80 @@ module.exports = function(ctx) {
|
||||
|
||||
// Get Certificate Authorities from Caddyfile
|
||||
router.get('/caddy/cas', ctx.asyncHandler(async (req, res) => {
|
||||
const content = await ctx.caddy.read();
|
||||
const cas = [];
|
||||
const content = await ctx.caddy.read();
|
||||
const cas = [];
|
||||
|
||||
const pkiRegex = /pki\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs;
|
||||
let pkiMatch;
|
||||
while ((pkiMatch = pkiRegex.exec(content)) !== null) {
|
||||
const pkiBlock = pkiMatch[1];
|
||||
let caMatch;
|
||||
const caBlockRegex = /ca\s+(\S+)\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs;
|
||||
while ((caMatch = caBlockRegex.exec(pkiBlock)) !== null) {
|
||||
const caName = caMatch[1];
|
||||
const caBlock = caMatch[2];
|
||||
const ca = { id: caName, name: caName, root: {}, intermediate: {} };
|
||||
const pkiRegex = /pki\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs;
|
||||
let pkiMatch;
|
||||
while ((pkiMatch = pkiRegex.exec(content)) !== null) {
|
||||
const pkiBlock = pkiMatch[1];
|
||||
let caMatch;
|
||||
const caBlockRegex = /ca\s+(\S+)\s*\{([^}]*(?:\{[^}]*\}[^}]*)*)\}/gs;
|
||||
while ((caMatch = caBlockRegex.exec(pkiBlock)) !== null) {
|
||||
const caName = caMatch[1];
|
||||
const caBlock = caMatch[2];
|
||||
const ca = { id: caName, name: caName, root: {}, intermediate: {} };
|
||||
|
||||
const nameMatch = /name\s+"([^"]+)"/.exec(caBlock);
|
||||
if (nameMatch) ca.name = nameMatch[1];
|
||||
const nameMatch = /name\s+"([^"]+)"/.exec(caBlock);
|
||||
if (nameMatch) ca.name = nameMatch[1];
|
||||
|
||||
const rootCnMatch = /root_cn\s+"([^"]+)"/.exec(caBlock);
|
||||
const intCnMatch = /intermediate_cn\s+"([^"]+)"/.exec(caBlock);
|
||||
if (rootCnMatch) ca.root_cn = rootCnMatch[1];
|
||||
if (intCnMatch) ca.intermediate_cn = intCnMatch[1];
|
||||
const rootCnMatch = /root_cn\s+"([^"]+)"/.exec(caBlock);
|
||||
const intCnMatch = /intermediate_cn\s+"([^"]+)"/.exec(caBlock);
|
||||
if (rootCnMatch) ca.root_cn = rootCnMatch[1];
|
||||
if (intCnMatch) ca.intermediate_cn = intCnMatch[1];
|
||||
|
||||
const rootMatch = /root\s*\{([^}]*)\}/s.exec(caBlock);
|
||||
if (rootMatch) {
|
||||
const rootBlock = rootMatch[1];
|
||||
const certMatch = /cert\s+(\S+)/.exec(rootBlock);
|
||||
const keyMatch = /key\s+(\S+)/.exec(rootBlock);
|
||||
if (certMatch) ca.root.cert = certMatch[1];
|
||||
if (keyMatch) ca.root.key = keyMatch[1];
|
||||
const rootMatch = /root\s*\{([^}]*)\}/s.exec(caBlock);
|
||||
if (rootMatch) {
|
||||
const rootBlock = rootMatch[1];
|
||||
const certMatch = /cert\s+(\S+)/.exec(rootBlock);
|
||||
const keyMatch = /key\s+(\S+)/.exec(rootBlock);
|
||||
if (certMatch) ca.root.cert = certMatch[1];
|
||||
if (keyMatch) ca.root.key = keyMatch[1];
|
||||
}
|
||||
|
||||
const intMatch = /intermediate\s*\{([^}]*)\}/s.exec(caBlock);
|
||||
if (intMatch) {
|
||||
const intBlock = intMatch[1];
|
||||
const certMatch = /cert\s+(\S+)/.exec(intBlock);
|
||||
const keyMatch = /key\s+(\S+)/.exec(intBlock);
|
||||
if (certMatch) ca.intermediate.cert = certMatch[1];
|
||||
if (keyMatch) ca.intermediate.key = keyMatch[1];
|
||||
}
|
||||
|
||||
cas.push(ca);
|
||||
}
|
||||
}
|
||||
|
||||
const intMatch = /intermediate\s*\{([^}]*)\}/s.exec(caBlock);
|
||||
if (intMatch) {
|
||||
const intBlock = intMatch[1];
|
||||
const certMatch = /cert\s+(\S+)/.exec(intBlock);
|
||||
const keyMatch = /key\s+(\S+)/.exec(intBlock);
|
||||
if (certMatch) ca.intermediate.cert = certMatch[1];
|
||||
if (keyMatch) ca.intermediate.key = keyMatch[1];
|
||||
const tlsGlobalRegex = /\{\s*acme_ca\s+(\S+)/g;
|
||||
let tlsMatch;
|
||||
while ((tlsMatch = tlsGlobalRegex.exec(content)) !== null) {
|
||||
cas.push({ name: 'acme', url: tlsMatch[1], type: 'acme' });
|
||||
}
|
||||
|
||||
const siteBlocks = content.match(/[\w.-]+\s*\{[^}]*tls\s+[^}]*\}/gs) || [];
|
||||
const tlsInternalCAs = new Set();
|
||||
for (const block of siteBlocks) {
|
||||
const tlsInternalMatch = /tls\s+internal\s*\{[^}]*ca\s+(\S+)/s.exec(block);
|
||||
if (tlsInternalMatch) tlsInternalCAs.add(tlsInternalMatch[1]);
|
||||
if (/tls\s+internal(?:\s|$)/.test(block) && !/tls\s+internal\s*\{/.test(block)) {
|
||||
tlsInternalCAs.add('local');
|
||||
}
|
||||
|
||||
cas.push(ca);
|
||||
}
|
||||
}
|
||||
|
||||
const tlsGlobalRegex = /\{\s*acme_ca\s+(\S+)/g;
|
||||
let tlsMatch;
|
||||
while ((tlsMatch = tlsGlobalRegex.exec(content)) !== null) {
|
||||
cas.push({ name: 'acme', url: tlsMatch[1], type: 'acme' });
|
||||
}
|
||||
|
||||
const siteBlocks = content.match(/[\w.-]+\s*\{[^}]*tls\s+[^}]*\}/gs) || [];
|
||||
const tlsInternalCAs = new Set();
|
||||
for (const block of siteBlocks) {
|
||||
const tlsInternalMatch = /tls\s+internal\s*\{[^}]*ca\s+(\S+)/s.exec(block);
|
||||
if (tlsInternalMatch) tlsInternalCAs.add(tlsInternalMatch[1]);
|
||||
if (/tls\s+internal(?:\s|$)/.test(block) && !/tls\s+internal\s*\{/.test(block)) {
|
||||
tlsInternalCAs.add('local');
|
||||
for (const caName of tlsInternalCAs) {
|
||||
if (!cas.find(c => c.name === caName)) {
|
||||
cas.push({ name: caName, type: 'internal', note: 'Referenced in tls directive' });
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const caName of tlsInternalCAs) {
|
||||
if (!cas.find(c => c.name === caName)) {
|
||||
cas.push({ name: caName, type: 'internal', note: 'Referenced in tls directive' });
|
||||
if (cas.length === 0 && /tls\s+internal/.test(content)) {
|
||||
cas.push({ name: 'local', type: 'internal', note: 'Default Caddy internal CA' });
|
||||
}
|
||||
}
|
||||
if (cas.length === 0 && /tls\s+internal/.test(content)) {
|
||||
cas.push({ name: 'local', type: 'internal', note: 'Default Caddy internal CA' });
|
||||
}
|
||||
|
||||
const caList = cas.map(ca => ({
|
||||
id: ca.id || ca.name,
|
||||
name: ca.name,
|
||||
displayName: ca.name !== (ca.id || ca.name) ? `${ca.name} (${ca.id || ca.name})` : ca.name,
|
||||
}));
|
||||
res.json({ status: 'success', data: { cas: caList } });
|
||||
const caList = cas.map(ca => ({
|
||||
id: ca.id || ca.name,
|
||||
name: ca.name,
|
||||
displayName: ca.name !== (ca.id || ca.name) ? `${ca.name} (${ca.id || ca.name})` : ca.name
|
||||
}));
|
||||
res.json({ status: 'success', data: { cas: caList } });
|
||||
}, 'caddy-get-cas'));
|
||||
|
||||
// Remove a site from Caddyfile
|
||||
@@ -123,7 +123,7 @@ module.exports = function(ctx) {
|
||||
const result = await ctx.caddy.modify((content) => {
|
||||
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const siteBlockRegex = new RegExp(
|
||||
`\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g',
|
||||
`\\n?${escapedDomain}\\s*\\{[^{}]*(?:\\{[^{}]*(?:\\{[^{}]*\\}[^{}]*)*\\}[^{}]*)*\\}\\s*`, 'g'
|
||||
);
|
||||
const modified = content.replace(siteBlockRegex, '\n');
|
||||
if (modified.length === content.length) return null;
|
||||
@@ -149,7 +149,7 @@ module.exports = function(ctx) {
|
||||
const upstreamRegex = /^[a-z0-9.-]+:\d{1,5}$/i;
|
||||
if (!upstreamRegex.test(upstream)) return ctx.errorResponse(res, 400, 'Invalid upstream format. Use host:port');
|
||||
|
||||
const content = await ctx.caddy.read();
|
||||
let content = await ctx.caddy.read();
|
||||
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const siteBlockRegex = new RegExp(`\\n?${escapedDomain}\\s*\\{`, 'g');
|
||||
if (siteBlockRegex.test(content)) {
|
||||
@@ -200,7 +200,7 @@ module.exports = function(ctx) {
|
||||
}
|
||||
|
||||
const sslConfig = sslType === 'letsencrypt' ? '' : 'tls internal';
|
||||
const hostHeader = preserveHost ? '\n header_up Host {upstream_hostport}' : '';
|
||||
const hostHeader = preserveHost ? `\n header_up Host {upstream_hostport}` : '';
|
||||
|
||||
const urlObj = new URL(externalUrl);
|
||||
|
||||
@@ -238,7 +238,7 @@ module.exports = function(ctx) {
|
||||
await ctx.addServiceToConfig({
|
||||
id: subdomain, name: serviceName, logo,
|
||||
isExternal: true, externalUrl,
|
||||
deployedAt: new Date().toISOString(),
|
||||
deployedAt: new Date().toISOString()
|
||||
});
|
||||
ctx.log.info('deploy', 'Service added to dashboard', { subdomain });
|
||||
} catch (serviceError) {
|
||||
@@ -248,7 +248,7 @@ module.exports = function(ctx) {
|
||||
|
||||
const response = {
|
||||
success: true,
|
||||
message: `External service proxy for ${domain} -> ${externalUrl} created${shouldReload ? ' and Caddy reloaded' : ''}`,
|
||||
message: `External service proxy for ${domain} -> ${externalUrl} created${shouldReload ? ' and Caddy reloaded' : ''}`
|
||||
};
|
||||
if (dnsWarning) response.warning = dnsWarning;
|
||||
res.json(response);
|
||||
|
||||
Reference in New Issue
Block a user