ddnsman
Version:
DDNS Management
130 lines (129 loc) • 4.49 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug_1 = __importDefault(require("debug"));
const https_1 = require("https");
const querystring_1 = __importDefault(require("querystring"));
const ClientStatus_1 = require("../constants/ClientStatus");
const dnspod_1 = require("../constants/dnspod");
const DnsClient_1 = require("./DnsClient");
const debug = debug_1.default('ddnsman:DnsPodClient');
const isResponseValid = (res) => {
return res.status.code === '1';
};
class DnsPodClient extends DnsClient_1.DnsClient {
constructor(name, subdomain, domain, loginToken) {
super(name, subdomain, domain);
this.loginToken = loginToken;
}
get ip() {
return this.record ? this.record.value : undefined;
}
async getRecord() {
debug('getRecord:' + this.domain);
const opt = { domain: this.domain, sub_domain: this.subdomain };
try {
const res = await this._request(dnspod_1.ApiUrl.record.list, opt);
const records = res.records;
const record = records.find((item) => item.type === 'A' && item.name === this.subdomain);
if (record) {
return record;
}
}
catch (err) {
if (err.code === '10') {
// code 10 means the record doesn't exist.
return;
}
else {
throw err;
}
}
}
async createRecord(ip) {
const opt = {
sub_domain: this.subdomain,
domain: this.domain,
ttl: 600,
record_type: 'A',
record_line: '默认',
value: ip
};
const res = await this._request(dnspod_1.ApiUrl.record.create, opt);
return res.record;
}
async updateDdns(publicIp, recordId, recordLine = '默认') {
const opt = {
domain: this.domain,
record_id: recordId,
value: publicIp,
sub_domain: this.subdomain,
record_line: recordLine
};
const res = await this._request(dnspod_1.ApiUrl.record.ddns, opt);
this.status = ClientStatus_1.ClientStatus.SYNCING;
return res.record;
}
async sync(ip) {
const subdomain = this.subdomain;
const domain = this.domain;
debug('sync %s with %s', [subdomain, domain].join('.'), ip);
if (!this.record) {
this.record = await this.getRecord();
}
if (this.record) {
if (this.record.value !== ip) {
this.record = await this.updateDdns(ip, this.record.id);
}
else {
this.status = 'sync';
}
}
else {
this.record = await this.createRecord(ip);
this.record = await this.updateDdns(ip, this.record.id);
}
return this.record;
}
async _request(path, options) {
return new Promise((resolve, reject) => {
const body = querystring_1.default.stringify(Object.assign({ login_token: this.loginToken, format: 'json' }, options));
const req = https_1.request({
method: 'POST',
host: 'dnsapi.cn',
path,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'ddnsman/1.0.0 (wenwei1202@gmail.com)',
Accept: 'application/json'
}
}, (res) => {
const data = [];
res
.on('data', (chunk) => {
data.push(chunk);
})
.on('end', () => {
const result = JSON.parse(Buffer.concat(data).toString());
if (isResponseValid(result)) {
resolve(result);
}
else {
reject(result);
}
})
.on('error', (err) => {
reject(err);
});
});
req.on('error', (err) => {
reject(err);
});
req.write(body);
req.end();
});
}
}
exports.DnsPodClient = DnsPodClient;