ddnsman
Version:
DDNS Management
66 lines (65 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/* tslint:disable no-console */
const ClientStatus_1 = require("../constants/ClientStatus");
const DnsPodClient_1 = require("./DnsPodClient");
class Worker {
constructor() {
this.dnsClientStore = new Map();
}
onMessage(msg) {
switch (msg.action) {
case 'start': {
if (this.dnsClientStore.has(msg.name)) {
throw new Error(`${msg.name} already exists.`);
}
else {
const client = new DnsPodClient_1.DnsPodClient(msg.name, msg.subdomain, msg.domain, msg.loginToken);
this.dnsClientStore.set(msg.name, client);
}
return 'SUCCESS';
}
case 'stop': {
if (msg.name === 'all') {
this.dnsClientStore.clear();
}
else {
if (this.dnsClientStore.has(msg.name)) {
this.dnsClientStore.delete(msg.name);
}
else {
throw new Error(`${msg.name} doesn't exist.`);
}
}
return 'SUCCESS';
}
case 'list': {
const domains = [];
for (const item of this.dnsClientStore.values()) {
domains.push([item.name, item.status, item.subdomain, item.domain, item.ip || '']);
}
return domains;
}
}
}
async sync(ip) {
for (const item of this.dnsClientStore.values()) {
try {
await item.sync(ip);
}
catch (err) {
console.error(err);
}
}
}
getActiveDomainCount() {
let count = 0;
for (const client of this.dnsClientStore.values()) {
if (client.status !== ClientStatus_1.ClientStatus.STOPPED) {
count += 1;
}
}
return count;
}
}
exports.Worker = Worker;