Fix redirect

This commit is contained in:
Wildan M 2021-08-22 08:09:30 +07:00
commit 00feecd9ff
2 changed files with 21 additions and 16 deletions

2
app.js
View file

@ -29,9 +29,11 @@ function getCertCachePath(host) {
}
async function findMaintainerEmail(host) {
const resolve = await axios(`https://dns.google/resolve?name=${encodeURIComponent(host)}&type=TXT`);
if (resolve.data.Answer) {
for (const head of resolve.data.Answer) {
return head.data.slice(record_email_prefix.length);
}
}
throw new Error(record_email_prefix + ' TXT is missing');
}

View file

@ -15,6 +15,7 @@ const resolveCache = {};
async function buildCache(host) {
const resolve = await axios(`https://dns.google/resolve?name=${encodeURIComponent(host)}&type=TXT`);
if (resolve.data.Answer) {
for (const head of resolve.data.Answer) {
let url = head.data.slice(record_prefix.length);
let expand = false;
@ -28,6 +29,7 @@ async function buildCache(host) {
expire: Date.now() + Math.max(head.TTL, 86400) * 1000,
};
}
}
throw new Error(record_prefix + ' TXT is missing');
}
@ -38,11 +40,12 @@ const server = http.createServer(async function (req, res) {
cache = await buildCache(req.headers.host);
resolveCache[req.headers.host] = cache;
}
req.statusCode = 301;
req.headers.location = cache.expand ? cache.url + req.url : cache.url;
res.writeHead(301, {
'Location': cache.expand ? cache.url + req.url : cache.url,
});
return;
} catch (error) {
res.statusCode = 400;
res.writeHead(400);
res.write(error.message || 'Unknown error');
} finally {
res.end();