Compress IPv6 addresses

parent 91142170
......@@ -2,6 +2,7 @@ import { NextFunction, Request, Response, Router } from "express";
import isIp from "is-ip";
import { promises } from "dns";
import axios from "axios";
import compressIp from "../functions/compressIp";
const router: Router = Router();
......@@ -13,59 +14,66 @@ function catcher(x) {
* Index
*/
router.get("/:ip", (req: Request, res: Response, next: NextFunction) => {
if(isIp(req.params.ip)) {
Promise.all([
promises.reverse(req.params.ip).catch(catcher),
axios.get(`https://api.telcodb.net/v1/bgpann?format=json&q=${req.params.ip}`).catch(catcher),
])
.then(result => {
// Override, as it cries as it may return void (see catcher)
const reverses: any = result[0]
// any type is just to avoid errors with Twig
// Error message being "Property 'data' does not exist on type 'void'."
let announcements: any = result[1];
const ipAddress = req.params.ip;
const compressedIp = compressIp(ipAddress);
let announcementsData;
try {
announcementsData = announcements.data;
} catch {}
const ipVersion = isIp.version(req.params.ip);
if(isIp(ipAddress)) {
if(compressedIp != ipAddress) {
res.redirect(301, `/ip/${compressedIp}`);
} else {
Promise.all([
promises.reverse(ipAddress).catch(catcher),
axios.get(`https://api.telcodb.net/v1/bgpann?format=json&q=${ipAddress}`).catch(catcher),
])
.then(result => {
// Override, as it cries as it may return void (see catcher)
const reverses: any = result[0]
// any type is just to avoid errors with Twig
// Error message being "Property 'data' does not exist on type 'void'."
let announcements: any = result[1];
let reverse;
try {
if(reverses.length !== 0) {
reverse = reverses[0];
}
} catch {}
let title = `${req.params.ip}`;
title += reverse ? ` - ${reverse}` : "";
title += " - IP address geolocation";
let announcementsData;
try {
announcementsData = announcements.data;
} catch {}
const ipVersion = isIp.version(ipAddress);
let description = `${req.params.ip} `;
description += reverse ? `or ${reverse} ` : "";
description += `is an IPv${ipVersion} address.`;
// description += ` owned by ${ispName} and located in ${placeName} (see map)`;
let reverse;
try {
if(reverses.length !== 0) {
reverse = reverses[0];
}
} catch {}
res.render("ip/show.twig", {
// "title": `${req.params.ip} - ${reverse} - ${country} - ${isp} - IP address geolocation`,
"title": title,
"disable_title_prefix": true,
"description": description,
let title = `${ipAddress}`;
title += reverse ? ` - ${reverse}` : "";
title += " - IP address geolocation";
let description = `${ipAddress} `;
description += reverse ? `or ${reverse} ` : "";
description += `is an IPv${ipVersion} address.`;
// description += ` owned by ${ispName} and located in ${placeName} (see map)`;
res.render("ip/show.twig", {
// "title": `${ipAddress} - ${reverse} - ${country} - ${isp} - IP address geolocation`,
"title": title,
"disable_title_prefix": true,
"description": description,
"canonical_url": `/ip/${req.params.ip}`,
"canonical_url": `/ip/${ipAddress}`,
"address": req.params.ip,
"version": ipVersion,
"address": ipAddress,
"version": ipVersion,
"reverse": reverse,
"reverses": reverses,
"reverse": reverse,
"reverses": reverses,
"announcements": announcementsData,
});
})
.catch(next);
"announcements": announcementsData,
});
})
.catch(next);
}
} else {
next();
}
......
......@@ -6,6 +6,7 @@ import isCidr from "is-cidr";
import punycode from "punycode";
import isDomainName from "is-domain-name";
import { URL } from "url";
import compressIp from "../functions/compressIp";
const { Resolver } = require('dns').promises;
const resolver = new Resolver();
......@@ -31,7 +32,7 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
if(query) {
if(isIp(query)) {
res.redirect(`/ip/${query}`);
res.redirect(`/ip/${compressIp(query)}`);
} else if(isAsn(query.toUpperCase())) {
res.redirect(`/${query}`);
} else if(isCidr(query)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment