mirror of
https://github.com/Lissy93/web-check.git
synced 2025-02-09 06:59:44 +01:00
Adds lambdas for getting URL redirects and TXT records
This commit is contained in:
parent
c83419cfd2
commit
3adfbb2a67
31
server/lambda/follow-redirects.js
Normal file
31
server/lambda/follow-redirects.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
exports.handler = async (event) => {
|
||||||
|
const { url } = event.queryStringParameters;
|
||||||
|
const redirects = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const got = await import('got');
|
||||||
|
await got.default(url, {
|
||||||
|
followRedirect: true,
|
||||||
|
maxRedirects: 12,
|
||||||
|
hooks: {
|
||||||
|
beforeRedirect: [
|
||||||
|
(options, response) => {
|
||||||
|
redirects.push(response.headers.location);
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: JSON.stringify({
|
||||||
|
redirects: redirects,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
statusCode: 500,
|
||||||
|
body: `Error: ${error.message}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
30
server/lambda/get-txt.js
Normal file
30
server/lambda/get-txt.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
const dns = require('dns').promises;
|
||||||
|
|
||||||
|
exports.handler = async (event) => {
|
||||||
|
const url = new URL(event.queryStringParameters.url);
|
||||||
|
try {
|
||||||
|
const txtRecords = await dns.resolveTxt(url.hostname);
|
||||||
|
|
||||||
|
// Parsing and formatting TXT records into a single object
|
||||||
|
const readableTxtRecords = txtRecords.reduce((acc, recordArray) => {
|
||||||
|
const recordObject = recordArray.reduce((recordAcc, recordString) => {
|
||||||
|
const splitRecord = recordString.split('=');
|
||||||
|
const key = splitRecord[0];
|
||||||
|
const value = splitRecord.slice(1).join('=');
|
||||||
|
return { ...recordAcc, [key]: value };
|
||||||
|
}, {});
|
||||||
|
return { ...acc, ...recordObject };
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: 200,
|
||||||
|
body: JSON.stringify(readableTxtRecords),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error:', error);
|
||||||
|
return {
|
||||||
|
statusCode: 500,
|
||||||
|
body: JSON.stringify({ message: error.message }),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user