-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (137 loc) · 3.49 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const { VoiceResponse } = require('twilio').twiml;
const AWS = require('aws-sdk');
const dns = require('dns');
const route53 = new AWS.Route53({ apiVersion: '2013-04-01' });
const dnsPromises = dns.promises;
function topMenu() {
const twimlout = new VoiceResponse();
const gather = twimlout.gather({
action: './select',
method: 'GET',
numDigits: '1',
});
gather.say('Hello! Welcome to DNS via DTMF. Press 1 to get the current IP. Press 2 to set a new IP.');
return gather.toString();
}
function backToRoot() {
const twimlout = new VoiceResponse();
twimlout.say({
voice: 'woman',
language: 'en-US',
}, 'You have selected an invalid option.');
twimlout.pause({
length: 3,
});
twimlout.redirect({
method: 'GET',
}, './join');
return twimlout.toString();
}
async function getCurrentIP() {
let ip;
const options = {
family: 4,
hints: dns.ADDRCONFIG | dns.V4MAPPED,
};
await dnsPromises.lookup('dtmf.karlcswanson.com', options).then((result) => {
ip = result.address;
});
return ip;
}
function digitToIP(digits) {
return digits.replace(/\*/g, '.');
}
async function updateR53Record(ip) {
const params = {
ChangeBatch: {
Changes: [{
Action: 'UPSERT',
ResourceRecordSet: {
Name: process.env.DTMF_DOMAIN_NAME,
ResourceRecords: [{
Value: ip,
}],
TTL: 60,
Type: 'A',
},
}],
Comment: 'Updated via DTMF',
},
HostedZoneId: process.env.HOSTED_ZONE_ID,
};
const res = await route53.changeResourceRecordSets(params).promise();
console.log(res);
return res;
}
async function SetIP(digit) {
const ip = digitToIP(digit);
const status = await updateR53Record(ip);
const twimlout = new VoiceResponse();
twimlout.say({
voice: 'woman',
language: 'en-US',
}, 'IP has been updated!');
twimlout.pause({
length: 3,
});
twimlout.redirect({
method: 'GET',
}, './join');
return twimlout.toString();
}
async function optionOne() {
const twimlout = new VoiceResponse();
const ip = await getCurrentIP();
twimlout.say({
voice: 'woman',
language: 'en-US',
}, `You have selected option 1. The current IP is ${ip}`);
twimlout.pause({
length: 3,
});
twimlout.redirect({
method: 'GET',
}, './join');
return twimlout.toString();
}
function optionTwo() {
const twimlout = new VoiceResponse();
const gather = twimlout.gather({
action: './gather2',
method: 'GET',
finishOnKey: '#',
});
gather.say('Enter the new IP for dtmf dot karl c swanson dot com. Use the star key for dots. Press pound when complete');
return twimlout.toString();
}
function selectOption(digit) {
const Digit = parseInt(digit, 10);
switch (Digit) {
case 1: return optionOne();
case 2: return optionTwo();
default:
return backToRoot();
}
}
async function apiHandler(path, event) {
switch (path) {
case 'join': return topMenu();
case 'select': return selectOption(event.queryStringParameters.Digits);
case 'option1': return optionOne();
case 'option2': return optionTwo();
case 'getip': return getCurrentIP();
case 'gather2': return SetIP(event.queryStringParameters.Digits);
default: return 'invalid path';
}
}
exports.handler = async (event, context) => {
const output = await apiHandler(event.path.substring(1), event);
const response = {
statusCode: 200,
headers: {
'Content-Type': 'text/xml',
},
body: output,
};
return response;
};