feat: add frontend

This commit is contained in:
Renge 2024-04-17 21:09:55 -04:00
parent 4a723d75d3
commit 74b11b8df0
2 changed files with 55 additions and 8 deletions

View File

@ -1,19 +1,21 @@
const express = require('express')
const dns = require('dns');
const swipl = require('swipl');
const path = require('path');
const app = express()
const PORT = 3000
const spf = (req, res) => {
try {
res.setHeader('Content-Type', 'application/json');
const { domain } = req.query;
console.log(domain)
let spf = ""
dns.resolveTxt(domain, (err, address) => {
if (err) {
console.error("ERROR", err)
return res.status(400).send("ERROR: occur on resolving SPF")
return res.status(400).json("ERROR: occur on resolving SPF")
}
else {
console.log(address)
@ -28,14 +30,14 @@ const spf = (req, res) => {
}
if (!spf) {
console.error("ERROR: not spf")
return res.status(400).send(`ERROR: No SPF record found in ${domain}`)
return res.status(400).json(`ERROR: No SPF record found in ${domain}`)
}
const spfArray = spf.split(" ");
console.log(spfArray)
return parseSPF(domain, spfArray, swipl, domain, (err) => {
if (err) {
console.error("ERROR", err)
return res.status(err.code).send(err.message)
return res.status(err.code).json(err.message)
}
else {
swipl.call('assert(ip4("_", "_"))');
@ -63,7 +65,7 @@ const spf = (req, res) => {
query.close();
swipl.cleanup()
swipl.initialise()
return res.status(200).send({spf: spf, allowedIP4: [...new Set(allowedIP4)], allowedIP6: [...new Set(allowedIP6)]})
return res.status(200).json({spf: spf, allowedIP4: [...new Set(allowedIP4)], allowedIP6: [...new Set(allowedIP6)]})
}
})
})
@ -75,15 +77,16 @@ const spf = (req, res) => {
const a = (req, res) => {
try {
res.setHeader('Content-Type', 'application/json');
const { domain } = req.query;
console.log(domain)
dns.resolve4(domain, (err, address) => {
if (err) {
console.log(err)
return res.status(400).send("ERROR: No a record found")
return res.status(400).json("ERROR: No a record found")
}
else {
return res.status(200).send(address)
return res.status(200).json(address)
}
})
}
@ -94,15 +97,16 @@ const a = (req, res) => {
const mx = (req, res) => {
try {
res.setHeader('Content-Type', 'application/json');
const { domain } = req.query;
console.log(domain)
dns.resolveMx(domain, (err, address) => {
if (err) {
console.log(err)
return res.status(400).send("ERROR: No mx record found")
return res.status(400).json("ERROR: No mx record found")
}
else {
return res.status(200).send(address)
return res.status(200).json(address)
}
})
}
@ -329,6 +333,7 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
app.get('/spf', spf)
app.get('/a', a)
app.get('/mx', mx)
app.use(express.static(path.join(__dirname, 'public')));
app.listen(PORT, (err) => {
if (err) {

42
Project/public/index.html Normal file
View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<label for="inputText">Enter Domain Name: </label>
<input type="text" id="inputText">
<button onclick="sendGetRequest()">Send</button>
<h3>Results: </h3>
<h5>a:</h5>
<pre id="result1"></pre>
<h5>mx: </h5>
<pre id="result2"></pre>
<h5>spf: </h5>
<pre id="result3"></pre>
<script>
async function sendGetRequest() {
const inputText = document.getElementById('inputText').value;
try {
const response1 = await fetch(`/a?domain=${encodeURIComponent(inputText)}`);
const result1 = await response1.json();
document.getElementById('result1').textContent = JSON.stringify(result1, undefined, 4);
const response2 = await fetch(`/mx?domain=${encodeURIComponent(inputText)}`);
const result2 = await response2.json();
document.getElementById('result2').textContent = JSON.stringify(result2, undefined, 4);
const response3 = await fetch(`/spf?domain=${encodeURIComponent(inputText)}`);
const result3 = await response3.json();
document.getElementById('result3').textContent = JSON.stringify(result3, undefined, 4);
} catch (error) {
console.error('ERROR:', error);
}
}
</script>
</body>
</html>