-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
126 lines (116 loc) · 3.91 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
const https = require('https');
// Configuration. Get some one or more api keys at https://www.whoisxmlapi.com/
const domain= 'hotsprings.io';
const currentOrganization = 'Tobias Meisen - EDV-Beratung und Dienstleistungen';
const apiKeys = ['abc'];
/**
* Fetches WHOIS data using a randomly chosen API key
*/
function fetchWhoisData() {
const apiKey = apiKeys[Math.floor(Math.random() * apiKeys.length)];
const url = `https://www.whoisxmlapi.com/whoisserver/WhoisService?outputFormat=JSON&apiKey=${ apiKey }&domainName=${ domain }`;
return new Promise((resolve, reject) => {
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => data += chunk);
resp.on('end', () => resolve(JSON.parse(data)));
}).on("error", (err) => reject(err));
});
}
/**
* Fetches the WHOIS data and caches it as long as the lambda is warm.
* We are not expecting our lambda to be warm for more than five minutes.
* Caching saves our maximum allowed WHOIS calls.
*/
let cachedWhoisData;
async function getWhoisData() {
if (!cachedWhoisData) {
console.log('Fetching WHOIS data');
cachedWhoisData = await fetchWhoisData();
} else {
console.log('Using cached WHOIS data');
}
console.log(cachedWhoisData);
return cachedWhoisData;
};
/**
* Attempts to read the WHOIS data and returns a human readable headline and
* text that explains the outcome. Also returns a color for the page background.
*/
async function getWasTransferred() {
console.log('Checking if domain was transferred');
let organization;
try {
const whoisData = await getWhoisData();
organization = whoisData.WhoisRecord.registryData.registrant.organization;
} catch (e) {
console.error('Failed to interpret WHOIS data', e);
return {
headline: 'Keine Ahnung!',
text: `Beim Laden der Daten ging anscheinend etwas schief: "${ e.message }"`,
color: '#666666',
}
}
if (organization !== currentOrganization) {
console.log(`Organization changed to ${ organization }`);
return {
headline: 'Anscheinend schon!',
text: `Die registrierte Organisation hat sich verändert und lautet nun "${ organization }"`,
color: '#19ad6e',
}
}
return {
headline: 'Nein!',
text: `Die registrierte Organisation ist weiterhin "${ organization }"`,
color: '#d40115',
}
};
/**
* Builds a html page that prints a nice text with the result
*/
async function getHtml(result) {
console.log('Building HTML');
return `
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wurde ${ domain } schon umgezogen?</title>
<style type="text/css">
body {
background-color: ${ result.color };
font-family: Arial, sans-serif;
text-align: center;
color: #ffffff;
padding: 100px;
}
.how-does-this-work {
margin-top: 100px;
opacity: 0.5;
}
.how-does-this-work a {
color: #ffffff;
text-decoration: none;
}
.how-does-this-work a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>${ result.headline }</h1>
<p>${ result.text }</p>
<p class="how-does-this-work">
<a href="https://github.com/FabianScheidt/WasSomeDomainTransferred" target="_blank">Wie funktioniert das?</a>
</p>
</body>
</html>`;
}
/**
* Since we are expecting high demand for this page, we run it in AWS Lamda!
*/
exports.handler = async () => {
const result = await getWasTransferred();
return await getHtml(result);
};