-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add maradns config parser (#5)
* docs(nsd): tidy up and add comments * split ns implementations into separate files * nsd: strip double quotes off values
- Loading branch information
Showing
17 changed files
with
533 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,26 @@ | ||
|
||
const os = require('os') | ||
|
||
const nearley = require('nearley') | ||
|
||
exports.parseBindConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('./dist/bind.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.length > 1) { | ||
console.error(`ERROR: ambigious parser rule`) | ||
} | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
return parser.results[0] | ||
.filter(z => z[0] && z[0].type) // weed out nulls | ||
.map(z => z[0]) // remove array nesting | ||
} | ||
|
||
exports.parseKnotConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('./dist/knot.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
const r = {} | ||
|
||
const sections = [ 'acl', 'key', 'log', 'policy', 'remote', 'server', 'template', 'zone' ] | ||
|
||
parser.results[0][0] | ||
.filter(z => z !== null) | ||
.map(z => { | ||
for (const s of sections) { | ||
if (z[s]) { | ||
if (!r[s]) r[s] = [] | ||
r[s].push(...z[s]) | ||
} | ||
} | ||
}) | ||
|
||
return r | ||
} | ||
|
||
exports.parseNsdConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('./dist/nsd.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str + os.EOL) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
const r = {} | ||
|
||
const sections = [ 'key', 'pattern', 'remote-control', 'server', 'tls-auth', 'zone' ] | ||
exports.valueCleanup = function (str) { | ||
// strip double quotes | ||
if (str.charAt(0) === '"' && str.charAt(str.length -1) === '"') { | ||
str = str.substr(1,str.length -2) | ||
} | ||
|
||
parser.results[0] | ||
.filter(z => z !== null) | ||
.map(z => { | ||
for (const s of sections) { | ||
if (z[s]) { | ||
if (!r[s]) r[s] = [] | ||
r[s].push(z[s]) | ||
} | ||
} | ||
}) | ||
if (/^[0-9.]+$/.test(str) && Number(str).toString() === str) { | ||
return Number(str) | ||
} | ||
|
||
return r | ||
return str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
const os = require('os') | ||
|
||
const nearley = require('nearley') | ||
|
||
exports.parseConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('../dist/bind.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.length > 1) { | ||
console.error(`ERROR: ambigious parser rule`) | ||
} | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
return parser.results[0] | ||
.filter(z => z[0] && z[0].type) // weed out nulls | ||
.map(z => z[0]) // remove array nesting | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
const os = require('os') | ||
|
||
const nearley = require('nearley') | ||
|
||
exports.parseConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('../dist/knot.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
const r = {} | ||
|
||
const sections = [ 'acl', 'key', 'log', 'policy', 'remote', 'server', 'template', 'zone' ] | ||
|
||
parser.results[0][0] | ||
.filter(z => z !== null) | ||
.map(z => { | ||
for (const s of sections) { | ||
if (z[s]) { | ||
if (!r[s]) r[s] = [] | ||
r[s].push(...z[s]) | ||
} | ||
} | ||
}) | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
const ns = require('../index') | ||
|
||
exports.parseConfig = async str => { | ||
// https://maradns.samiam.org/tutorial/man.mararc.html | ||
|
||
const re = { | ||
blank : /^\s*?$/, | ||
comment : /^\s*(?:#)[^\r\n]*?$/, | ||
variable: /^([^ \t]*)\s*=\s*([^\r\n]*)$/, | ||
} | ||
|
||
const res = {} | ||
|
||
for (const line of str.split(/[\r\n]/)) { | ||
|
||
if (re.blank.test(line)) continue | ||
if (re.comment.test(line)) continue | ||
|
||
const match = line.match(re.variable) | ||
if (match) { | ||
res[match[1]] = ns.valueCleanup(match[2]) | ||
} | ||
else { | ||
console.log(line) | ||
throw ('parser failed') | ||
} | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
const os = require('os') | ||
|
||
const nearley = require('nearley') | ||
|
||
const ns = require('../index') | ||
|
||
exports.parseConfig = async str => { | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const grammar = nearley.Grammar.fromCompiled(require('../dist/nsd.js')) | ||
grammar.start = 'main' | ||
|
||
const parser = new nearley.Parser(grammar) | ||
parser.feed(str + os.EOL) | ||
if (!str.endsWith(os.EOL)) parser.feed(os.EOL) | ||
|
||
if (parser.results.length === 0) return [] | ||
|
||
const r = {} | ||
|
||
const sections = [ 'key', 'pattern', 'remote-control', 'server', 'tls-auth', 'zone' ] | ||
|
||
parser.results[0] | ||
.filter(z => z !== null) | ||
.map(z => { | ||
for (const s of sections) { | ||
if (z[s]) { | ||
if (!r[s]) r[s] = [] | ||
r[s].push(z[s]) | ||
} | ||
} | ||
}) | ||
|
||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@lexer lexer | ||
|
||
main -> ( comment {% id %} | ||
| blank {% id %} | ||
| variable {% id %} | ||
):* {% id %} | ||
|
||
blank -> %blank {% asNull %} | ||
comment -> %comment {% asNull %} | ||
variable -> %variable {% asVariable %} | ||
|
||
@{% | ||
const moo = require("moo"); | ||
|
||
const lexer = moo.compile({ | ||
variable: /^[^ \t]*\s*=\s*[^\r\n]*?[\r\n]/, | ||
blank : /^\s*?$[\r\n]/, | ||
comment: /^\s*(?:#)[^\r\n]*?[\r\n]/, | ||
}); | ||
|
||
function asNull (d) { return null } | ||
|
||
const varRe = /^([^ \t]*)\s*=\s*([^\r\n]*?)[\r\n]/ | ||
|
||
function asVariable (d) { | ||
const match = d[0].value.match(varRe) | ||
return { [match[1]]: match[2].trim().replace(/^"(.*)"$/, '$1') } | ||
} | ||
|
||
%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
const assert = require('assert') | ||
const fs = require('fs').promises | ||
|
||
const ns = require('../lib/bind.js') | ||
|
||
|
||
describe('bind', function () { | ||
|
||
describe('parseConfig', function () { | ||
|
||
it('parses named.conf file', async () => { | ||
const file = './test/fixtures/bind/named.conf-ztrax-master' | ||
const buf = await fs.readFile(file) | ||
|
||
const r = await ns.parseConfig(buf.toString()) | ||
// console.dir(r, { depth: null }) | ||
assert.equal(r.length, 5) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
csv2 = {} | ||
csv2["example.com."] = "db.example.com" | ||
csv2["example2.com."] = "db.example2.com" | ||
|
||
ipv4_bind_addresses = "10.1.2.3,10.1.2.4,127.0.0.1" | ||
|
||
chroot_dir = "/etc/maradns" | ||
|
||
maradns_uid = 99 | ||
maradns_gid = 99 | ||
|
||
no_fingerprint = 0 | ||
|
||
max_chain = 8 | ||
max_ar_chain = 1 | ||
max_total = 20 | ||
|
||
verbose_level = 3 | ||
zone_transfer_acl = "10.1.1.1/24, 10.100.100.100/255.255.255.224" |
Oops, something went wrong.