fix(dcrouter): improve domain pattern matching to support routing-glob and wildcard patterns and use matching logic when resolving routes

This commit is contained in:
2026-02-13 23:16:25 +00:00
parent c33ecdc26f
commit 2638990667
4 changed files with 27 additions and 16 deletions

View File

@@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@serve.zone/dcrouter',
version: '5.4.1',
version: '5.4.2',
description: 'A multifaceted routing service handling mail and SMS delivery functions.'
}

View File

@@ -674,24 +674,25 @@ export class DcRouter {
* @returns Whether the domain matches the pattern
*/
private isDomainMatch(domain: string, pattern: string): boolean {
// Normalize inputs
domain = domain.toLowerCase();
pattern = pattern.toLowerCase();
// Check for exact match
if (domain === pattern) {
return true;
if (domain === pattern) return true;
// Routing-glob: *example.com matches example.com, sub.example.com, *.example.com
if (pattern.startsWith('*') && !pattern.startsWith('*.')) {
const baseDomain = pattern.slice(1); // *nevermind.cloud → nevermind.cloud
if (domain === baseDomain || domain === `*.${baseDomain}`) return true;
if (domain.endsWith(baseDomain) && domain.length > baseDomain.length) return true;
}
// Check for wildcard match (*.example.com)
// Standard wildcard: *.example.com matches sub.example.com and example.com
if (pattern.startsWith('*.')) {
const patternSuffix = pattern.slice(2); // Remove the "*." prefix
// Check if domain ends with the pattern suffix and has at least one character before it
return domain.endsWith(patternSuffix) && domain.length > patternSuffix.length;
const suffix = pattern.slice(2);
if (domain === suffix) return true;
return domain.endsWith(suffix) && domain.length > suffix.length;
}
// No match
return false;
}
@@ -705,7 +706,9 @@ export class DcRouter {
const routeDomains = Array.isArray(route.match.domains)
? route.match.domains
: [route.match.domains];
if (routeDomains.includes(domain)) return route.name;
for (const pattern of routeDomains) {
if (this.isDomainMatch(domain, pattern)) return route.name;
}
}
return undefined;
}