-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
51 lines (37 loc) · 1.49 KB
/
handler.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
'use strict';
import DdnsUpdater from './ddns-updater';
import { IConfig, IHostConfig, Loader as ConfigLoader } from './config';
import { APIGatewayEvent, Callback, Context } from 'aws-lambda';
function createResponse(statusCode: number, body: string) {
return {
statusCode: statusCode,
body: body,
headers: {'Content-Type': 'text/plain'}
};
}
function index(event: APIGatewayEvent, context: Context, callback: Callback) {
callback(null, createResponse(200, event.requestContext.identity.sourceIp));
}
async function update(event: APIGatewayEvent) {
const input = event.queryStringParameters || {};
const config: IConfig = await ConfigLoader.load();
const sourceIp: string = event.requestContext.identity.sourceIp;
const ip: string = input.myip || sourceIp;
if (!input.hostname) {
return createResponse(400, 'nohost');
}
const hostname: string = input.hostname;
if (!(hostname in config.hosts)) {
return createResponse(400, 'nohost');
}
const hostConfig: IHostConfig = config.hosts[hostname];
const providerConfig = config.dns_providers[hostConfig.dns_provider || 'route53'];
const updater = new DdnsUpdater(hostname, hostConfig, providerConfig);
const currentIp: string|null = await updater.get(hostname);
if (currentIp === ip) {
return createResponse(200, `nochg ${ip}`);
}
await updater.update(hostname, ip);
return createResponse(200, `good ${ip}`);
}
export {index, update};