fix(templates): count routing parentheses per block, keep $ in group names

Template detection counted parentheses with the scanner's file-wide
depth, so an unquoted `(` in an earlier block, as in
`log_file: /var/log/honk(.log`, carried into routing and the reader
pushed into an entry it never opened. It now counts from each routing
block's own brace.

Filling `{group}` used a replacement string, so a group name from
another file such as 'p$&' was read as a pattern. A replacer function
writes the name as it is.
This commit is contained in:
Zakk committed 2026-10-01 03:19:38 +10:00
1 parent 2939b8be2d
commit d87bf0e2d7
3 files changed
+23 -4

No files matched your search

+3 -2
View File
@@ -4,9 +4,10 @@ import {templates, type RuleTemplate} from './templates';
export const defaultGroup = 'proxy';
function routingBlock(group: string | null, rules: RuleTemplate): string[] {
// The header as written: the templates must route to the group the file already has.
// The header as written: the templates must route to the group the file already has. A replacer function keeps a `$` in
// the name literal, where a replacement string would read `$&` as a pattern.
const first = group ?? defaultGroup;
const fill = (line: string) => ' ' + line.replaceAll('{group}', first);
const fill = (line: string) => ' ' + line.replaceAll('{group}', () => first);
return ['routing {', ...templates[rules].rules.map(fill), fill(`fallback: ${templates[rules].fallback}`), '}'];
}
// The DNS split a template can bring along: mainland names resolved by a mainland resolver, the rest over DNS over TLS.
+13
View File
@@ -71,6 +71,13 @@ describe('detectTemplate', () => {
expect(detectTemplate(gfw.replaceAll('-> a', '-> direct'))).toBeNull();
});
it('counts parentheses inside routing only', () => {
// honk reads an unquoted path with a stray `(`; the scanner's count stays raised past it.
const text = `global {\n log_file: /var/log/honk(.log\n}\n\n${writeTemplate('', 'gfw', [])}`;
expect(detectTemplate(text)).toEqual({template: 'gfw', group: 'proxy'});
expect(detectTemplate(`global {\n log_file: /var/log/honk(.log\n}\n\n${existing}`)).toBeNull();
});
it('reads an include in routing as custom', () => {
const bypass = writeTemplate(existing, 'bypass', []);
expect(detectTemplate(bypass.replace(/^ fallback:/m, ' include rules.dae\n fallback:'))).toBeNull();
@@ -84,6 +91,12 @@ describe('writeTemplate', () => {
expect(templateGroups('mini', ['proxy'])).toEqual(['auto']);
});
it('writes a group another file declares with $ in its name as written', () => {
const out = writeTemplate('', 'gfw', [{name: "'p$&'", written: "'p$&'"}]);
expect(out).toContain("domain(geosite:gfw) -> 'p$&'");
expect(detectTemplate(out)).toEqual({template: 'gfw', group: "'p$&'"});
});
it('adds the default group only when no file declares any', () => {
expect(readGroupEntries(writeTemplate('', 'global', [])).map(entry => entry.name)).toEqual(['proxy']);
expect(templateGroups('global', [])).toEqual(['proxy']);
+7 -2
View File
@@ -138,14 +138,19 @@ function routingEntries(text: string): string[][] | null {
for (const block of blocks.filter(block => block.name === 'routing')) {
if (block.children.length) return null;
let line = -1;
// Parentheses count from the block's own brace: an unquoted `(` elsewhere, as in `log_file: /var/log/honk(.log`,
// leaves the scanner's count raised for the rest of the file.
let parens = 0;
for (const token of tokens) {
if (token.from <= block.open || token.from >= block.close || token.kind === 'comment') continue;
const raw = text.slice(token.from, token.to);
if (token.line !== line && token.parens === 0) entries.push([]);
if (token.line !== line && parens === 0) entries.push([]);
line = token.line;
// A match argument reads the same quoted or not; an outbound keeps its quotes, which honk keeps in group names.
// The scanner keeps `&&` and `!` in the word they touch, as in `l4proto(udp)&&dport(443)`.
entries.at(-1)!.push(...(token.kind === 'quoted' ? [token.parens ? unquote(raw) : raw] : raw.split(/(&&|\|\||!)/).filter(Boolean)));
entries.at(-1)!.push(...(token.kind === 'quoted' ? [parens ? unquote(raw) : raw] : raw.split(/(&&|\|\||!)/).filter(Boolean)));
if (token.kind === 'symbol' && raw === '(') parens++;
else if (token.kind === 'symbol' && raw === ')') parens = Math.max(0, parens - 1);
}
}
return entries;