-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
runner.ts
117 lines (93 loc) · 3.67 KB
/
runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { DDNSSetting, DDNSSettingGlobal, DDNSSettingService } from "./interface";
import { logger } from "./utils/logger";
import { ip } from "./services/ip";
import { AliyunService } from "./services/aliyun/service";
import { CloudflareService } from "./services/cloudflare/service";
interface IPAddress {
v4: string | null
v6: string | null
}
export class DDNSRunner {
readonly globalRRSetting: DDNSSettingGlobal;
readonly services: DDNSSettingService[] = [];
private ip: IPAddress = {
v4: null,
v6: null
};
constructor(
private settings: DDNSSetting
) {
logger.debug('生成DDNSRunner对象');
this.globalRRSetting = settings.global;
logger.debug('识别到全局设置为:');
logger.debug(this.globalRRSetting);
this.services.push(...settings.services);
this.iterateSettings();
}
static async run(settings: DDNSSetting): Promise<void> {
const ddns = new DDNSRunner(settings);
await ddns.getIP();
await ddns.registerServices();
}
/**
*
*/
async registerServices(): Promise<void> {
for (const service of this.services) {
logger.info(`处理${service.name}服务`);
switch (service.name) {
case 'aliyun': {
await AliyunService.run(service, this.globalRRSetting, this.ip);
break;
}
case 'cloudflare': {
await CloudflareService.run(service, this.globalRRSetting, this.ip);
break;
}
default:
logger.warn(`不存在这个名称的服务:${service.name}`);
break;
}
}
return Promise.resolve();
}
iterateSettings(): void {
logger.debug('遍历设置');
const [serviceCount, domainCount, RRCount] =
this.services.reduce(([serviceCount, domainCount, RRCount], service, index) => {
logger.debug(`第${index + 1}个服务 类型:${service.name}`);
const [addDomainCount, addRRCount] =
service.domains.reduce(([domainCount, RRCount], domain, index) => {
logger.debug(`\t第${index + 1}个域名:${domain.domainName}`);
const addRRCount = domain.resourceRecords.reduce((RRCount, RR, index) => {
logger.debug(`\t\t第${index + 1}个记录:${RR.resourceRecord}`);
return RRCount + 1;
}, 0);
return [domainCount + 1, RRCount + addRRCount];
}, [0, 0]);
return [serviceCount + 1, domainCount + addDomainCount, RRCount + addRRCount];
}, [0, 0, 0]);
logger.info(`共有${serviceCount}个服务、${domainCount}个域名、${RRCount}个记录可能被改变。`);
}
async getIP(): Promise<void> {
logger.info('开始获取公网IP地址');
const result4 = await ip(4);
if (result4) {
this.ip.v4 = result4;
logger.info(`本机公网IPv4地址为:${result4}`);
} else {
logger.warn('没有获取到公网IPv4地址');
}
const result6 = await ip(6);
if (result6) {
this.ip.v6 = result6;
logger.info(`本机公网IPv6地址为:[${result6}]`);
} else {
logger.warn('没有获取到公网IPv6地址');
}
if (result4 === null && result6 === null) {
logger.warn('没有获取到任何IPv4和IPv6地址,程序退出');
process.exit();
}
}
}