Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,22 @@ DNS records are stored as individual files in `/home/pi/.firewalla/config/dnsmas

```
example.home.local
├── address=/example.home.local/192.168.1.100
└── address=/example.home.local/192.168.1.101
├── host-record=example.home.local,192.168.1.100
└── host-record=example.home.local,192.168.1.101

api.home.local
└── cname=api.home.local,service.home.local
```

A records are written as `host-record` rather than `address`. dnsmasq only resolves a
`cname=` whose target is a name it already knows from `host-record`, `/etc/hosts` or a
DHCP lease — a name defined with `address=/name/ip` is not a valid CNAME target, so
CNAMEs pointing at such names fail to resolve. `host-record` also matches only the exact
name, where `address=` additionally matches every subdomain of it.

Records written by earlier versions in the `address=/name/ip` form are still read, so
external-dns can reconcile and remove them.

## Testing

1. Verify the webhook provider is running on Firewalla:
Expand Down
20 changes: 17 additions & 3 deletions src/services/dnsmasq.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,19 @@ async function getRecords() {
let recordType = 'A';

for (const line of lines) {
// Format: address=/domain/ip
// Format: host-record=domain,ip
const hostRecordMatch = line.match(/^host-record=([^,]+),(.+)$/);
if (hostRecordMatch) {
dnsName = hostRecordMatch[1];
const ip = hostRecordMatch[2];
if (validator.isValidIPv4(ip)) {
targets.push(ip);
recordType = 'A';
}
}

// Legacy format: address=/domain/ip, written before host-record was used.
// Still parsed so external-dns can see and clean up pre-existing records.
const addressMatch = line.match(/^address=\/([^\/]+)\/(.+)$/);
if (addressMatch) {
dnsName = addressMatch[1];
Expand Down Expand Up @@ -145,7 +157,7 @@ async function writeRecord(endpoint) {
let content;
if (recordType === 'A') {
// Generate dnsmasq A record format
content = targets.map(ip => `address=/${dnsName}/${ip}`).join('\n') + '\n';
content = targets.map(ip => `host-record=${dnsName},${ip}`).join('\n') + '\n';
} else if (recordType === 'TXT') {
// Generate dnsmasq TXT record format
content = targets.map(txt => `txt-record=${dnsName},"${txt}"`).join('\n') + '\n';
Expand Down Expand Up @@ -293,5 +305,7 @@ async function applyChanges(changes) {

module.exports = {
getRecords,
applyChanges
applyChanges,
// exported for tests: exercises record formatting without the service restart
writeRecord
};
107 changes: 107 additions & 0 deletions test-host-record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env node
/**
* Test script for dnsmasq record formats
*
* A records must be written as `host-record=name,ip` rather than
* `address=/name/ip`, because dnsmasq only resolves a `cname=` whose target is
* a name it already knows from host-record, /etc/hosts or a DHCP lease.
* Records written by earlier versions in the `address=` form must still be
* readable so external-dns can reconcile them.
*/

const fs = require('fs');
const os = require('os');
const path = require('path');

// Set required environment variables for testing
process.env.DOMAIN_FILTER = 'test.local';
process.env.SHARED_SECRET = 'test-secret';
process.env.DNSMASQ_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'test-dnsmasq-'));
process.env.LOG_LEVEL = 'error';

const dnsmasqDir = process.env.DNSMASQ_DIR;
const dnsmasq = require('./src/services/dnsmasq');

let failures = 0;

function check(label, actual, expected) {
const ok = actual === expected;
if (!ok) {
failures++;
}
console.log(`${ok ? 'PASS' : 'FAIL'}: ${label}`);
if (!ok) {
console.log(` expected: ${expected}`);
console.log(` actual: ${actual}`);
}
}

function removeDir(dir) {
// fs.rmSync needs Node >= 14.14; Firewalla ships Node 12.14
if (fs.rmSync) {
fs.rmSync(dir, { recursive: true, force: true });
} else {
fs.rmdirSync(dir, { recursive: true });
}
}

function readRecordFile(name) {
return fs.readFileSync(path.join(dnsmasqDir, name), 'utf8').trim();
}

async function main() {
console.log('Testing dnsmasq record formats...');

// writeRecord rather than applyChanges: applyChanges ends by restarting
// firerouter_dns, which this test has no business doing on a Firewalla.
await dnsmasq.writeRecord({ dnsName: 'a.test.local', targets: ['192.168.1.100'], recordType: 'A' });
await dnsmasq.writeRecord({ dnsName: 'multi.test.local', targets: ['192.168.1.101', '192.168.1.102'], recordType: 'A' });
await dnsmasq.writeRecord({ dnsName: 'cname.test.local', targets: ['a.test.local'], recordType: 'CNAME' });
await dnsmasq.writeRecord({ dnsName: 'txt.test.local', targets: ['heritage=external-dns'], recordType: 'TXT' });

// Written formats
check('A record uses host-record', readRecordFile('a.test.local'),
'host-record=a.test.local,192.168.1.100');

check('multiple A targets each get a host-record line', readRecordFile('multi.test.local'),
'host-record=multi.test.local,192.168.1.101\nhost-record=multi.test.local,192.168.1.102');

check('CNAME record unchanged', readRecordFile('cname.test.local'),
'cname=cname.test.local,a.test.local');

check('TXT record unchanged', readRecordFile('txt.test.local.txt'),
'txt-record=txt.test.local,"heritage=external-dns"');

// A record written by an earlier version, which must still be readable
fs.writeFileSync(path.join(dnsmasqDir, 'legacy.test.local'),
'address=/legacy.test.local/192.168.1.200\n');

const records = await dnsmasq.getRecords();
const byName = {};
for (const record of records) {
byName[record.dnsName] = record;
}

check('host-record reads back as A', `${byName['a.test.local'].recordType} ${byName['a.test.local'].targets.join(',')}`,
'A 192.168.1.100');

check('multiple host-record lines read back as one record', byName['multi.test.local'].targets.join(','),
'192.168.1.101,192.168.1.102');

check('cname reads back as CNAME', `${byName['cname.test.local'].recordType} ${byName['cname.test.local'].targets.join(',')}`,
'CNAME a.test.local');

check('legacy address= still reads back as A', `${byName['legacy.test.local'].recordType} ${byName['legacy.test.local'].targets.join(',')}`,
'A 192.168.1.200');

removeDir(dnsmasqDir);

console.log(`Record format tests completed with ${failures} failure(s).`);
process.exit(failures === 0 ? 0 : 1);
}

main().catch(err => {
console.error('Test run failed:', err);
removeDir(dnsmasqDir);
process.exit(1);
});