/
isitonaws.js
164 lines (148 loc) · 4.5 KB
/
isitonaws.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
157
158
159
160
161
162
163
164
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
var rangefinder = require('./lib/rangefinder.js');
var ranges = require('./lib/ranges.js');
var cleaner = require('./lib/name-cleaner.js');
var async = require('async');
const dns = require('dns');
var fs = require('fs');
var ANSWER = '<div id="answer"></div>';
exports.handler = function isItOnAWS(event, context) {
var name = null;
if (event.queryStringParameters == null ||
event.queryStringParameters == undefined ||
event.queryStringParameters.name == undefined ||
event.queryStringParameters.name == null ||
event.queryStringParameters.name == "") {
console.log("NO-OP");
// no-op, just easier this way
} else {
name = cleaner.clean(event.queryStringParameters.name);
}
var html = '';
var ip_searched = null;
async.waterfall([
// have to do this first because we need the HTML even if we’re
// going to bail out and just show the query page
function readHTML(next) {
fs.readFile('./data/index.html', next);
},
function decideWhetherToQuery(fileBody, next) {
html = fileBody.toString();
if (name) {
next(null);
} else {
next('no-query');
}
},
function lookup(next) {
console.log("Looking for " + name);
if (name.match(/^\d+\.\d+\.\d+\.\d+$/)) {
console.log("IPV4");
next(null, name, 4);
} else if (name.match(/^[0-9a-fA-F:]+$/)) {
console.log("IPV6");
next(null, name, 6);
} else {
try {
console.log("Launch DNS");
dns.lookup(name, next);
} catch (e) {
next('dns-failure');
}
}
},
function find(ip, family, next) {
console.log("Looking for IP " + ip);
ip_searched = ip;
var target = rangefinder.to32HexDigits(ip, (family == 4));
var range = searchRanges(ranges.ranges, target);
if (range) {
console.log("Yes, found it");
next(null, range);
} else {
console.log("Not found");
next(null, null);
}
},
function makeHTML(range, next) {
var newPara = '';
if (range != null) {
newPara = '<div id="answer">' +
'<p>Yes, ' + name + ' (' + ip_searched + ') is apparently on AWS.</p>' +
'<table>' +
'<tr><td align="right">Region: </td><td align="left">' + range.region + '</td></tr>' +
'<tr><td align="right">Service: </td><td align="left">' + range.service + '</td></tr>' +
'<tr><td align="right">CIDR: </td><td align="left">' + range.cidr + '</td></tr>' +
'</table>'
} else {
newPara = '<div class="answer"><p>No, ' + name + ' (' + ip_searched + ') is apparently not on AWS.</p></div>';
}
var newH = html.replace(ANSWER, newPara);
next(null, newH);
}
], function(err, result) {
if (err) {
// no-query means that we’re just displaying the page
if (err == 'no-query') {
console.log("NoQuery");
context.succeed({
"statusCode": 200,
"headers": { "Content-type": "text/html; charset=utf-8" },
"body": html
});
} else {
// something really went wrong. Apparently, the only thing
// that can cause this is a DNS botch.
newPara = '<div id="answer">' +
'<p>That doesn’t seem to be a valid hostname. ' +
'Please try again.</p>';
console.log("ouch! " + err);
context.succeed({
"statusCode": 200,
"headers": { "Content-type": "text/html; charset=utf-8" },
"body": html.replace(ANSWER, newPara)
});
}
} else {
// if we get here we have found a IP range to display in the output
context.succeed({
"statusCode": 200,
"headers": { "Content-type": "text/html; charset=utf-8" },
"body": result
});
}
});
}
function searchRanges(ranges, name) {
var low = -1;
var high = ranges.length;
while (high - low > 1) {
var probe = (high + low) >>> 1;
if (ranges[probe].range.min > name) {
high = probe;
} else {
low = probe;
}
}
if (low == -1) {
return null
}
var record = ranges[low];
if (name < record.range.min || name > record.range.max) {
return null;
}
return record;
}