forked from DumbWareio/DumbWhoIs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
390 lines (350 loc) · 15 KB
/
server.js
File metadata and controls
390 lines (350 loc) · 15 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const path = require('path');
const whois = require('node-whois');
const util = require('util');
const app = express();
const PORT = process.env.PORT || 3000;
// Convert whois.lookup to Promise
const lookupPromise = util.promisify(whois.lookup);
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Helper function to detect query type
function detectQueryType(query) {
// Clean up the query - remove brackets if present
const cleanQuery = query.replace(/^\[|\]$/g, '');
// ASN pattern (AS followed by numbers)
if (/^(AS|as)?\d+$/i.test(cleanQuery)) {
return 'asn';
}
// IPv6 pattern (with optional CIDR)
if (/^(?:(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9]))(?:\/\d{1,3})?$/.test(cleanQuery)) {
return 'ip';
}
// IPv4 pattern (with optional CIDR)
if (/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\/\d{1,2})?$/.test(cleanQuery)) {
return 'ip';
}
// Domain pattern (anything with a dot that's not an IP)
if (cleanQuery.includes('.')) {
return 'whois';
}
return 'unknown';
}
// Helper function to parse WHOIS data
async function parseWhoisData(data, domain) {
// Split into lines and create key-value pairs
const result = {
domainName: domain,
registrar: '',
creationDate: '',
expirationDate: '',
lastUpdated: '',
status: [],
nameservers: [],
ipAddresses: {
v4: [],
v6: []
},
raw: data
};
// Get both IPv4 and IPv6 addresses from DNS lookup
try {
const dns = require('dns').promises;
const [ipv4Addresses, ipv6Addresses] = await Promise.all([
dns.resolve4(domain).catch(() => []),
dns.resolve6(domain).catch(() => [])
]);
result.ipAddresses.v4 = ipv4Addresses;
result.ipAddresses.v6 = ipv6Addresses;
} catch (e) {
// If DNS lookup fails, keep arrays empty
}
// Regular expressions for IP addresses
const ipv4Regex = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g;
const ipv6Regex = /(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,7}:|(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}|(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}|(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}|(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:(?:(?::[0-9a-fA-F]{1,4}){1,6})|:(?:(?::[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(?::[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(?:ffff(?::0{1,4}){0,1}:){0,1}(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])|(?:[0-9a-fA-F]{1,4}:){1,4}:(?:(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(?:25[0-5]|(?:2[0-4]|1{0,1}[0-9]){0,1}[0-9])/g;
// First try to find IPs in specific fields that might contain them
const lines = data.split('\n');
for (const line of lines) {
const trimmedLine = line.trim().toLowerCase();
if (trimmedLine.includes('ip address') ||
trimmedLine.includes('a record') ||
trimmedLine.includes('aaaa record') ||
trimmedLine.includes('addresses') ||
trimmedLine.includes('host') ||
trimmedLine.includes('dns')) {
const ipv4InLine = line.match(ipv4Regex);
const ipv6InLine = line.match(ipv6Regex);
if (ipv4InLine) result.ipAddresses.v4.push(...ipv4InLine);
if (ipv6InLine) result.ipAddresses.v6.push(...ipv6InLine);
}
}
// Remove duplicates
result.ipAddresses.v4 = [...new Set(result.ipAddresses.v4)];
result.ipAddresses.v6 = [...new Set(result.ipAddresses.v6)];
// Special handling for .eu domains
if (domain.toLowerCase().endsWith('.eu')) {
const lines = data.split('\n');
let currentSection = '';
for (const line of lines) {
const trimmedLine = line.trim();
// Skip empty lines and comment lines
if (!trimmedLine || trimmedLine.startsWith('%')) continue;
// Check for section headers
if (trimmedLine.endsWith(':')) {
currentSection = trimmedLine.slice(0, -1).toLowerCase();
continue;
}
// Handle indented lines (section content)
if (line.startsWith(' ')) {
const [key, ...values] = line.trim().split(':').map(s => s.trim());
const value = values.join(':').trim();
switch (currentSection) {
case 'registrar':
if (key === 'Name') {
result.registrar = value;
}
break;
case 'name servers':
if (!key.includes(':') && key !== 'Please visit www.eurid.eu for more info.') {
result.nameservers.push(key);
}
break;
case 'technical':
if (key === 'Organisation' && !result.registrar) {
result.registrar = value;
}
break;
}
} else if (line.includes(':')) {
const [key, ...values] = line.split(':').map(s => s.trim());
const value = values.join(':').trim();
if (key === 'Domain') {
result.domainName = value;
}
}
}
// Add default status for .eu domains if none found
if (result.status.length === 0) {
result.status.push('registered');
}
} else {
// Original parsing logic for non-.eu domains
const lines = data.split('\n');
for (const line of lines) {
const [key, ...values] = line.split(':').map(s => s.trim());
const value = values.join(':').trim();
if (!key || !value) continue;
const keyLower = key.toLowerCase();
// Registrar information
if (keyLower.includes('registrar')) {
result.registrar = value;
}
// Creation date
else if (keyLower.includes('creation') || keyLower.includes('created') ||
keyLower.includes('registered')) {
result.creationDate = value;
}
// Expiration date
else if (keyLower.includes('expir')) {
result.expirationDate = value;
}
// Last updated
else if (keyLower.includes('updated') || keyLower.includes('modified')) {
result.lastUpdated = value;
}
// Status
else if (keyLower.includes('status')) {
const statuses = value.split(/[,;]/).map(s => s.trim());
result.status.push(...statuses);
}
// Nameservers
else if (keyLower.includes('name server') || keyLower.includes('nameserver')) {
const ns = value.split(/[\s,;]+/)[0];
if (ns && !result.nameservers.includes(ns)) {
result.nameservers.push(ns);
}
}
}
}
return result;
}
// IP lookup services with fallbacks
const ipLookupServices = [
{
name: 'ipapi.co',
url: (ip) => `https://ipapi.co/${ip}/json/`,
transform: (data) => ({
...data,
source: 'ipapi.co'
})
},
{
name: 'ip-api.com',
url: (ip) => `http://ip-api.com/json/${ip}`,
transform: (data) => ({
ip: data.query,
version: data.query.includes(':') ? 'IPv6' : 'IPv4',
city: data.city,
region: data.regionName,
region_code: data.region,
country_code: data.countryCode,
country_name: data.country,
postal: data.zip,
latitude: data.lat,
longitude: data.lon,
timezone: data.timezone,
org: data.org || data.isp,
asn: data.as,
source: 'ip-api.com'
})
},
{
name: 'ipwho.is',
url: (ip) => `https://ipwho.is/${ip}`,
transform: (data) => ({
ip: data.ip,
version: data.type,
city: data.city,
region: data.region,
region_code: data.region_code,
country_code: data.country_code,
country_name: data.country,
postal: data.postal,
latitude: data.latitude,
longitude: data.longitude,
timezone: data.timezone.id,
org: data.connection.org,
asn: data.connection.asn,
source: 'ipwho.is'
})
}
];
// Helper function to try IP lookup services in sequence
async function tryIpLookup(ip) {
// Remove brackets and CIDR notation for the lookup
const cleanIp = ip.replace(/^\[|\]$/g, '').replace(/\/\d+$/, '');
let lastError = null;
for (const service of ipLookupServices) {
try {
console.log(`Trying IP lookup with ${service.name}...`);
const response = await axios.get(service.url(cleanIp));
// Check if the service returned an error
if (response.data.error) {
throw new Error(response.data.message || 'Service returned error');
}
// Transform the data to our standard format
return service.transform(response.data);
} catch (error) {
console.log(`${service.name} lookup failed:`, error.message);
lastError = error;
// Continue to next service
continue;
}
}
// If we get here, all services failed
throw lastError;
}
// Universal lookup endpoint
app.get('/api/lookup/:query', async (req, res) => {
const query = req.params.query;
const queryType = detectQueryType(query);
try {
let response;
switch (queryType) {
case 'whois':
// Set specific options for WHOIS query
const options = {
follow: 3, // Follow up to 3 redirects
timeout: 10000, // 10 second timeout
};
// Add specific server for .eu domains
if (query.toLowerCase().endsWith('.eu')) {
options.server = 'whois.eu';
}
const whoisData = await lookupPromise(query, options);
const parsedData = await parseWhoisData(whoisData, query);
response = {
data: {
ldhName: parsedData.domainName,
handle: query,
status: parsedData.status,
ipAddresses: parsedData.ipAddresses,
events: [
{
eventAction: 'registration',
eventDate: parsedData.creationDate
},
{
eventAction: 'expiration',
eventDate: parsedData.expirationDate
},
{
eventAction: 'lastChanged',
eventDate: parsedData.lastUpdated
}
],
nameservers: parsedData.nameservers.map(ns => ({ ldhName: ns })),
entities: [{
roles: ['registrar'],
vcardArray: [
"vcard",
[
["version", {}, "text", "4.0"],
["fn", {}, "text", parsedData.registrar],
["email", {}, "text", ""]
]
]
}]
}
};
break;
case 'ip':
const ipData = await tryIpLookup(query);
response = { data: ipData };
break;
case 'asn':
// Remove 'AS' prefix if present
const asnNumber = query.replace(/^(AS|as)/i, '');
response = await axios.get(`https://api.bgpview.io/asn/${asnNumber}`);
break;
default:
return res.status(400).json({
error: 'Invalid input',
message: 'Please enter a valid domain name, IP address, or ASN number'
});
}
res.json({ type: queryType, data: response.data });
} catch (error) {
console.error('Error details:', error);
if (error.response) {
if (error.response.status === 429) {
res.status(429).json({
error: 'Rate limit exceeded',
message: 'All IP lookup services are currently rate limited. Please try again later.'
});
} else if (error.response.status === 404) {
res.status(404).json({ error: `${queryType.toUpperCase()} not found` });
} else {
res.status(error.response.status).json({
error: `Error fetching ${queryType.toUpperCase()} data`,
message: error.response.data?.message || error.message
});
}
} else {
res.status(500).json({
error: `Error fetching ${queryType.toUpperCase()} data`,
message: error.message
});
}
}
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});