From bd6dd18774a0fb2409d1f00bab8dfd0f360e6cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Sun, 26 Apr 2020 21:23:51 +0200 Subject: [PATCH] [ini] --- .gitignore | 1 + inwx.js | 135 ++++++++++++++++++++++++++++++++++++++++++++++ package-lock.json | 33 ++++++++++++ readme.md | 3 ++ 4 files changed, 172 insertions(+) create mode 100644 .gitignore create mode 100644 inwx.js create mode 100644 package-lock.json create mode 100644 readme.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/inwx.js b/inwx.js new file mode 100644 index 0000000..cc64fb8 --- /dev/null +++ b/inwx.js @@ -0,0 +1,135 @@ + +/** + * @param {string} username + * @param {string} password + * @param {(exec : (category : string, action : string, input : any)=>Promise)=>Promise} core + * @return {Promise} + */ +async function callwrap( + username, + password, + core +) +{ + return ( + new Promise( + (resolve, reject) => { + require("inwx")( + {"api": "production", "user": username, "password": password}, + async (api) => { + await core( + (category, action, input) => ( + new Promise( + (resolve_, reject_) => { + api.call(category, action, input, (output) => {resolve_(output);}); + } + ) + ) + ); + resolve(undefined); + } + ); + } + ) + ); +} + + +/** + * @param {string} syntax + */ +function syntaxerror( + syntax +) +{ + console.error(`SYNTAX: inwx ${syntax}`); + process.exit(1); +} + + + +/** + * @param {string} message + */ +function log( + message +) +{ + console.info("--", message); +} + + +/** + * @param {Array} args + */ +async function main( + args +) +{ + const command = ((args.length >= 1) ? args.shift() : syntaxerror(" [ [ […]]]")); + switch (command) { + default: { + console.error("unhandled"); + break; + } + case "info": { + const syntax_ = "info "; + const username = ((args.length >= 1) ? args.shift() : syntaxerror(syntax)); + const password = ((args.length >= 1) ? args.shift() : syntaxerror(syntax)); + callwrap( + username, + password, + async (exec) => { + const response = await exec("account", "info", {}); + console.info(response); + } + ); + break; + } + case "save": { + const syntax_ = "save "; + const username = ((args.length >= 1) ? args.shift() : syntaxerror(syntax)); + const password = ((args.length >= 1) ? args.shift() : syntaxerror(syntax)); + const domain = ((args.length >= 1) ? args.shift() : syntaxerror(syntax_)); + const name = ((args.length >= 1) ? args.shift() : syntaxerror(syntax_)); + const type = ((args.length >= 1) ? args.shift() : syntaxerror(syntax_)); + const content = args.join(" "); + callwrap( + username, + password, + async (exec) => { + const response1 = await exec("nameserver", "info", {"domain": domain}); + const matching = response1["record"].filter( + (record) => ( + (record["name"] === (name + "." + domain)) + && + (record["type"] === type) + ) + ); + switch (matching.length) { + case 0: { + const result = await exec("nameserver", "createRecord", {"domain": domain, "name": name, "type": type, "content": content}); + const id = result["id"]; + log(`created record #${id}`); + break; + } + case 1: { + const id = matching[0]["id"]; + const response2 = await exec("nameserver", "updateRecord", {"id": id, "content": content}); + log(`updated record #${id}`); + break; + } + default: { + log(`found multiple records with this name and type`); + break; + } + } + } + ); + } + } +} + + +main(process.argv.slice(2)); + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9b81ee0 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,33 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "inwx": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/inwx/-/inwx-1.0.0.tgz", + "integrity": "sha1-raLy07VMrrF65TiooP8SDYrkIGg=", + "requires": { + "xmlrpc": ">=1.1.0" + } + }, + "sax": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", + "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" + }, + "xmlbuilder": { + "version": "8.2.2", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-8.2.2.tgz", + "integrity": "sha1-aSSGc0ELS6QuGmE2VR0pIjNap3M=" + }, + "xmlrpc": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/xmlrpc/-/xmlrpc-1.3.2.tgz", + "integrity": "sha1-JrLqNHhI0Ciqx+dRS1NRl23j6D0=", + "requires": { + "sax": "1.2.x", + "xmlbuilder": "8.2.x" + } + } + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..45fdd91 --- /dev/null +++ b/readme.md @@ -0,0 +1,3 @@ +- `npm install inwx` +- use `node inwx.js save ` for creating/updating a DNS-record +