This commit is contained in:
Christian Fraß 2020-04-26 21:23:51 +02:00
commit bd6dd18774
4 changed files with 172 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules/

135
inwx.js Normal file
View file

@ -0,0 +1,135 @@
/**
* @param {string} username
* @param {string} password
* @param {(exec : (category : string, action : string, input : any)=>Promise<any>)=>Promise<void>} core
* @return {Promise<void>}
*/
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<string>} args
*/
async function main(
args
)
{
const command = ((args.length >= 1) ? args.shift() : syntaxerror("<command> [<arg1> [<arg2> […]]]"));
switch (command) {
default: {
console.error("unhandled");
break;
}
case "info": {
const syntax_ = "info <username> <password>";
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 <username> <password> <domain> <name> <type> <content>";
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));

33
package-lock.json generated Normal file
View file

@ -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"
}
}
}
}

3
readme.md Normal file
View file

@ -0,0 +1,3 @@
- `npm install inwx`
- use `node inwx.js save <username> <password> <domain> <name> <type> <content>` for creating/updating a DNS-record