From d87bf0e2d77547b05c779b6cb33d2f10fe7087c4 Mon Sep 17 00:00:00 2001 From: Zakk Date: Thu, 1 Oct 2026 01:47:49 +1000 Subject: [PATCH] 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. --- src/dae/setup.ts | 5 +++-- src/dae/templates.test.ts | 13 +++++++++++++ src/dae/templates.ts | 9 +++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/dae/setup.ts b/src/dae/setup.ts index 1175a948..9671baa9 100644 --- a/src/dae/setup.ts +++ b/src/dae/setup.ts @@ -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. diff --git a/src/dae/templates.test.ts b/src/dae/templates.test.ts index 4176aac4..8a0e2ddf 100644 --- a/src/dae/templates.test.ts +++ b/src/dae/templates.test.ts @@ -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']); diff --git a/src/dae/templates.ts b/src/dae/templates.ts index b47eeb42..ecabaf07 100644 --- a/src/dae/templates.ts +++ b/src/dae/templates.ts @@ -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;