first commit

This commit is contained in:
Flummi 2021-11-28 04:13:28 +00:00
commit ef05b04d91
5 changed files with 106 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
config/config.json

28
package-lock.json generated Normal file
View File

@ -0,0 +1,28 @@
{
"name": "bindd",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "bindd",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"flumm-fetch": "^1.0.1"
}
},
"node_modules/flumm-fetch": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/flumm-fetch/-/flumm-fetch-1.0.1.tgz",
"integrity": "sha512-pZ5U0hheCSW43vfGZQMunr03U7rUOX+iy2y13Tu4nc3iRL+E/Qfeo5nZ2B2JMYKOGIx1A1anUYOz+ulyhouyjg=="
}
},
"dependencies": {
"flumm-fetch": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/flumm-fetch/-/flumm-fetch-1.0.1.tgz",
"integrity": "sha512-pZ5U0hheCSW43vfGZQMunr03U7rUOX+iy2y13Tu4nc3iRL+E/Qfeo5nZ2B2JMYKOGIx1A1anUYOz+ulyhouyjg=="
}
}
}

14
package.json Normal file
View File

@ -0,0 +1,14 @@
{
"name": "bindd",
"version": "1.0.0",
"description": "",
"main": "src/index.mjs",
"scripts": {
"start": "node --experimental-json-modules src/index.mjs"
},
"author": "Flummi",
"license": "ISC",
"dependencies": {
"flumm-fetch": "^1.0.1"
}
}

60
src/index.mjs Normal file
View File

@ -0,0 +1,60 @@
import cfg from "../config/config.json" assert { type: "json" };
import fetch from "flumm-fetch";
import { promises as fs } from "fs";
import { execSync as exec } from "child_process";
class updateBind {
bindTpl = zone => `zone "${zone}" {\n type slave;\n file "/etc/bind/keyhelp_domains/${zone}";\n masters { 135.181.244.181; };\n};\n`;
fetchApi = async path => (await fetch(`https://${cfg.api.host}/${path}`, { headers: { "X-API-Key": cfg.api.key } })).json();
constructor() {
return (async () => {
await this.init();
return this;
})();
};
async init() {
try {
this.domains = (await fs.readFile(cfg.bind.config, "utf-8"))
.match(/zone \"(.*?)\"/g)
.map(z => z.replace(/zone \"(.*?)\"/, "$1"));
} catch {
this.domains = [];
}
};
async generateConfig() {
const dnslist = (await Promise.all((await this.fetchApi("domains?subdomains=false")).map(async domain => {
const dns = await this.fetchApi(`dns/${domain.id}`);
if(dns.is_dns_disabled)
return false;
return {
id: domain.id,
name: domain.domain,
dkim: dns.dkim_txt_record,
dns: dns.records
};
}))).filter(Boolean);
if(dnslist.length > 0) {
const files = await fs.readdir(cfg.bind.domainFiles);
await Promise.all(files.map(async file => await fs.unlink(`${cfg.bind.domainFiles}/${file}`)));
}
let bindconfig = [];
for(const domain of dnslist)
bindconfig.push(this.bindTpl(domain.name));
await fs.writeFile(cfg.bind.config, bindconfig);
};
reloadBind() {
return exec("systemctl restart named");
};
};
const bind = await new updateBind();
await bind.generateConfig();
bind.reloadBind();

2
start.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
node --experimental-json-modules src/index.mjs