Improve TLD page

parent e396fc82
......@@ -2,6 +2,9 @@ import { NextFunction, Request, Response, Router } from "express";
import axios from "axios";
import isDomainName from "is-domain-name";
const { Resolver } = require('dns').promises;
const resolver = new Resolver();
const router: Router = Router();
/**
......@@ -21,6 +24,53 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
})
});
function catcher(x) {
return;
}
function getRecords(queryArray: string[]): Promise<{}> {
return new Promise((resolve, reject) => {
const promises = [];
queryArray.forEach((hostname => {
promises.push(
axios.get(`https://api.telcodb.net/v1/resolve?format=json&q=${hostname}`)
.then(x => x.data)
.then(data => {
data._hostname = hostname;
return data;
})
.catch(catcher)
);
}));
/**
* Remove a key from dictionary
*
* @param dict Dictionary
* @param key Key to remove
*/
const deleteKeyFromDict = (dict, key) => {
delete dict[key];
return dict;
};
Promise.all(promises)
.then(dataArray => {
const returnedData = {};
dataArray.forEach(response => {
returnedData[`${response._hostname}`] = deleteKeyFromDict(response, "_hostname");
});
resolve(returnedData);
})
.catch(reject);
});
}
router.get("/:tld", (req: Request, res: Response, next: NextFunction) => {
const tld = req.params.tld;
......@@ -28,20 +78,30 @@ router.get("/:tld", (req: Request, res: Response, next: NextFunction) => {
axios.get(`https://api.telcodb.net/v1/resolve?format=json&q=${tld}`)
.then(x => x.data)
.then(data => data.ns)
.then(nameservers => {
if(nameservers === undefined) {
.then(ns => {
if(ns === undefined) {
next();
return;
}
res.render("tld/show.twig", {
"canonical_url": `/tld/${tld}`,
"title": `Information about .${tld}`,
"tld": tld,
// Alphabetical order
return ns.sort();
})
.then(nameservers => {
getRecords(nameservers)
.then(nameserverAddresses => {
res.render("tld/show.twig", {
"canonical_url": `/tld/${tld}`,
"title": `Information about .${tld}`,
"tld": tld,
"nameservers": nameservers,
});
"nameservers": nameservers,
"nsAddr": nameserverAddresses,
});
})
.catch(catcher)
})
.catch(next);
} else {
......
......@@ -4,11 +4,32 @@
<h1>{{ title }}</h1>
<h2>Nameservers</h2>
<ul class="pl-3">
{% for server in nameservers %}
<li><a href="/dns/{{ server }}">{{ server }}</a></li>
{% endfor %}
</ul>
<table class="table w-auto">
<thead>
<tr>
<th scope="col">Nameserver</th>
<th scope="col">A</th>
<th scope="col">AAAA</th>
</tr>
</thead>
<tbody>
{% for server in nameservers %}
<tr>
<td><a href="/dns/{{ server }}">{{ server }}</a></td>
<td class="ipRecord add-parenthesis" data-ip="{{ nsAddr[server].a }}">
<a href="/ip/{{ nsAddr[server].a }}">{{ nsAddr[server].a }}</a>
</td>
<td class="ipRecord add-parenthesis" data-ip="{{ nsAddr[server].aaaa }}">
<a href="/ip/{{ nsAddr[server].aaaa }}">{{ nsAddr[server].aaaa }}</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<p>More detailed DNS data can be found from <a href="/dns/{{ tld }}">here</a>.</p>
{% endblock %}
{% block javascripts %}
{# <script src="/assets/js/pages/dns/show/reverse-fetch.js" async defer></script> #}
{% endblock %}
\ No newline at end of file
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