136 lines
3 KiB
JavaScript
136 lines
3 KiB
JavaScript
|
|
|
||
|
|
/**
|
||
|
|
* @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));
|
||
|
|
|