-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (41 loc) · 1.19 KB
/
index.js
File metadata and controls
46 lines (41 loc) · 1.19 KB
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
/**
* Responds to any HTTP request that can provide a "message" field in the body.
*
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
var whois = require('whois');
const url = require('url');
exports.whoIs = function whoIs(req, res){
var url;
if(req.body.domain){
url = parseUrl(req.body.domain);
}else if(req.query && req.query.domain){
url = parseUrl(req.query.domain);
}else{
return res.status( 420 ).send( 'Missing required \'domain\' parameter.' );
}
whois.lookup(url.host, {
follow: 5
},function(err, data) {
if(err){
return res.status( 500 ).send( 'Something went wrong on our end.' );
}else{
return res.status( 200 ).send( JSON.stringify(data) );
}
});
}
function parseUrl(site){
// First, validate that site is at minimum a domain.
var urlToParse = url.parse(site);
// If host isn't defined, check path.
if(!urlToParse.host){
// It's distinctly possible that we were merely missing http...
if(site.indexOf('http') == -1){
// Found it. Try again.
return parseUrl('http://' + site);
}
// Otherwise... hm. That's not good
}
return urlToParse;
}