Punycode & TLD handling in search

parent d24aa129
......@@ -3,6 +3,11 @@ import isIp from "is-ip";
import domainNameRegex from "domain-name-regex";
import isAsn from "is-asn";
import isCidr from "is-cidr";
import punycode from "punycode";
import isDomainName from "is-domain-name";
const { Resolver } = require('dns').promises;
const resolver = new Resolver();
const router: Router = Router();
......@@ -19,10 +24,46 @@ router.get("/", (req: Request, res: Response, next: NextFunction) => {
res.redirect(`/${query}`);
} else if(isCidr(query)) {
res.redirect(`/net/${query}`);
} else if(domainNameRegex.test(query)) {
res.redirect(`/dns/${query}`);
} else if(domainNameRegex.test(punycode.toASCII(query))) {
res.redirect(`/dns/${punycode.toASCII(query)}`);
} else {
res.send("Query: " + query);
/**
* TLD check
*/
if(isDomainName(punycode.toASCII(query))) {
const tld = punycode.toASCII(query);
if(tld.toLowerCase() !== tld && tld.toUpperCase() !== tld) {
// If query is partially upper case, do not continue to TLD resolve.
//
// google -> TLD
// Google -> continue_search
// GOOGLE -> TLD
continue_search(query);
} else {
resolver.resolveNs(tld)
.then(addresses => {
if(addresses.length > 2) {
res.redirect(`/tld/${tld}`)
}
})
.catch(err => {
if(err.errno === "ENOTFOUND") {
// TLD not found
continue_search(query);
} else {
// DNS errors
next(err);
}
})
}
} else {
continue_search(query);
}
function continue_search(query) {
res.send("Query: " + query);
}
}
} else {
next();
......
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