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) { async function findMaintainerEmail(host) {
const resolve = await axios(`https://dns.google/resolve?name=${encodeURIComponent(host)}&type=TXT`); const resolve = await axios(`https://dns.google/resolve?name=${encodeURIComponent(host)}&type=TXT`);
if (resolve.data.Answer) {
for (const head of resolve.data.Answer) { for (const head of resolve.data.Answer) {
return head.data.slice(record_email_prefix.length); return head.data.slice(record_email_prefix.length);
} }
}
throw new Error(record_email_prefix + ' TXT is missing'); throw new Error(record_email_prefix + ' TXT is missing');
} }

View file

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