UNPKG

aliyun-dns-ddns

Version:

阿里云动态DNS自动更新库

149 lines (148 loc) 6.37 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AliyunDDNS = void 0; const axios_1 = __importDefault(require("axios")); const alidns20150109_1 = __importStar(require("@alicloud/alidns20150109")), $Alidns20150109 = alidns20150109_1; const $OpenApi = __importStar(require("@alicloud/openapi-client")); const node_schedule_1 = __importDefault(require("node-schedule")); // 导出一个类,封装DDNS功能,方便作为库调用 class AliyunDDNS { constructor(accessKeyId, accessKeySecret, domainName, recordType = 'A', subDomain = '@', regionId = 'cn-hangzhou') { this.accessKeyId = accessKeyId; this.accessKeySecret = accessKeySecret; this.domainName = domainName; this.recordType = recordType; this.subDomain = subDomain; this.regionId = regionId; this.lastIp = null; const config = new $OpenApi.Config({ accessKeyId: this.accessKeyId, accessKeySecret: this.accessKeySecret, }); config.endpoint = `alidns.${this.regionId}.aliyuncs.com`; this.client = new alidns20150109_1.default(config); } async getCurrentIp() { try { const response = await axios_1.default.get('https://myip.ipip.net'); const ipMatch = response.data.match(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/); if (ipMatch) { return ipMatch[0]; } console.error('从返回数据中未能匹配到IP地址:', response.data); return null; } catch (error) { console.error('获取公网IP失败:', error); return null; } } async getRecordId() { var _a, _b, _c, _d; try { const request = new $Alidns20150109.DescribeDomainRecordsRequest({ domainName: this.domainName, pageSize: 10, type: this.recordType, RRKeyWord: this.subDomain, }); const response = await this.client.describeDomainRecords(request); if ((_c = (_b = (_a = response.body) === null || _a === void 0 ? void 0 : _a.domainRecords) === null || _b === void 0 ? void 0 : _b.record) === null || _c === void 0 ? void 0 : _c.length) { const specificRecord = response.body.domainRecords.record.find(rec => rec.RR === this.subDomain); return (_d = specificRecord === null || specificRecord === void 0 ? void 0 : specificRecord.recordId) !== null && _d !== void 0 ? _d : null; } return null; } catch (error) { console.error('获取解析记录ID失败:', error); return null; } } async updateRecord(recordId, ip) { try { const request = new $Alidns20150109.UpdateDomainRecordRequest({ recordId, RR: this.subDomain, type: this.recordType, value: ip, }); await this.client.updateDomainRecord(request); const fullDomain = this.subDomain === '@' ? this.domainName : `${this.subDomain}.${this.domainName}`; console.log(`成功更新解析记录: ${fullDomain} -> ${ip}`); } catch (error) { console.error('更新解析记录失败:', error); } } async performUpdateCheck() { console.log(`[${new Date().toLocaleString()}] 开始执行IP检查...`); const currentIp = await this.getCurrentIp(); console.log(`当前公网IP: ${currentIp !== null && currentIp !== void 0 ? currentIp : '获取失败'}`); if (currentIp) { if (currentIp !== this.lastIp) { console.log('IP发生变化,开始更新DNS记录...'); const recordId = await this.getRecordId(); if (recordId) { await this.updateRecord(recordId, currentIp); this.lastIp = currentIp; } else { const fullDomain = this.subDomain === '@' ? this.domainName : `${this.subDomain}.${this.domainName}`; console.log(`未找到域名 ${fullDomain} 的解析记录,请先在阿里云DNS控制台添加。`); } } else { console.log('IP未变化,无需更新。'); } } else { console.log('无法获取当前IP。'); } } startSchedule(cronExpression = '0 * * * *') { console.log('DDNS服务启动,开始定时任务...'); // 立即执行一次 this.performUpdateCheck(); // 定时执行 node_schedule_1.default.scheduleJob(cronExpression, () => { this.performUpdateCheck(); }); } } exports.AliyunDDNS = AliyunDDNS;