-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIonosUpdater.ts
87 lines (73 loc) · 1.95 KB
/
IonosUpdater.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
import { Updater } from "./Updater.interface";
import http from "http";
import https from "https";
import { assertEnvironment, getEnv } from "../EnvironmentReader.js";
/**
* An Updater to set the current IP addresses via
* the IONOS' DDNS service.
*
* Configuration (environment variables):
* - `IONOS_API_KEY`
*/
export class IonosUpdater implements Updater {
private static readonly IONOS_UPDATE_HOST = "api.hosting.ionos.com";
public init(): void {
assertEnvironment("IONOS_API_KEY");
}
public async update(): Promise<void> {
const basicOptions: https.RequestOptions = {
hostname: IonosUpdater.IONOS_UPDATE_HOST,
port: 443,
path: `/dns/v1/dyndns?q=${getEnv("IONOS_API_KEY")}`,
method: "GET",
};
const ipv4Options = {
...basicOptions,
family: 4,
};
await IonosUpdater.callUpdateHook(ipv4Options);
const ipv6Options = {
...basicOptions,
family: 6,
};
await IonosUpdater.callUpdateHook(ipv6Options);
}
private static async callUpdateHook(
options: https.RequestOptions
): Promise<void> {
return new Promise<void>((resolve, reject) => {
const req = https.request(options, (res) => {
IonosUpdater.evaluateResult(res, resolve, reject);
});
req.on("error", (error: Error) => {
reject(error);
});
req.end();
});
}
private static evaluateResult(
res: http.IncomingMessage,
resolve: Function,
reject: Function
) {
if (res.statusCode == 200) {
resolve();
} else {
IonosUpdater.withCollectedData(res, (data: string) => {
reject(Error(`Server returned error code ${res.statusCode}: ${data}`));
});
}
}
private static withCollectedData(
message: http.IncomingMessage,
action: (data: string) => void
) {
let data = "";
message.on("data", (chunk: string) => {
data += chunk;
});
message.on("end", () => {
action(data);
});
}
}