fix: add try catch

This commit is contained in:
Renge 2024-04-17 19:50:31 -04:00
parent 9ffb9e334c
commit b49cfbc816

View File

@ -12,7 +12,7 @@ const spf = (req, res) => {
let spf = "" let spf = ""
dns.resolveTxt(domain, (err, address) => { dns.resolveTxt(domain, (err, address) => {
if (err) { if (err) {
console.error(err) console.error("ERROR", err)
return res.status(400).send("ERROR: occur on resolving SPF") return res.status(400).send("ERROR: occur on resolving SPF")
} }
else { else {
@ -27,30 +27,33 @@ const spf = (req, res) => {
} }
} }
if (!spf) { if (!spf) {
console.error("ERROR: not spf")
return res.status(400).send(`ERROR: No SPF record found in ${domain}`) return res.status(400).send(`ERROR: No SPF record found in ${domain}`)
} }
const spfArray = spf.split(" "); const spfArray = spf.split(" ");
console.log(spfArray) console.log(spfArray)
return parseSPF(domain, spfArray, swipl, domain, (err) => { return parseSPF(domain, spfArray, swipl, domain, (err) => {
if (err) { if (err) {
console.error(err) console.error("ERROR", err)
return res.status(err.code).send(err.message) return res.status(err.code).send(err.message)
} }
swipl.call('assert(ip("_", "_"))'); else {
swipl.call('assert(nip("_", "_"))'); swipl.call('assert(ip("_", "_"))');
swipl.call('assert(all("_", "_"))'); swipl.call('assert(nip("_", "_"))');
swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))'); swipl.call('assert(all("_", "_"))');
const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`); swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))');
let ret = null; const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`);
let result = [] let ret = null;
while (ret = query.next()) { let result = []
console.log(`Allowed IP value is: ${ret.IP}`); while (ret = query.next()) {
result.push(`${ret.IP}`); console.log(`Allowed IP value is: ${ret.IP}`);
result.push(`${ret.IP}`);
}
query.close();
swipl.cleanup()
swipl.initialise()
return res.status(200).send([...new Set(result)])
} }
query.close();
swipl.cleanup()
swipl.initialise()
return res.status(200).send(result)
}) })
}) })
} }
@ -60,183 +63,202 @@ const spf = (req, res) => {
} }
const parseSPF = (domain, spf, swipl, curr, callback) => { const parseSPF = (domain, spf, swipl, curr, callback) => {
console.log(domain, spf, curr) try {
let err = undefined console.log(domain, spf, curr)
let err = undefined
if (spf.length === 0) { if (spf.length === 0) {
return callback(err); return callback(err);
} }
const str = spf[0] const str = spf[0]
//ip46 //ip46
if (str.match("^[+~?]?ip[46]:(.+)$")) { if (str.match("^[+~?]?ip[46]:(.+)$")) {
console.log("matched ip[46]!") console.log("matched ip[46]!")
swipl.call(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`); swipl.call(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`);
console.log(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`); console.log(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
else if (str.match("^-ip4:(.+)$")) {
console.log("matched -ip[46]!")
swipl.call(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`);
console.log(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
//a
else if (str.match("^[+~?]?a$")) {
console.log("matched a!")
return dns.lookup(domain, (err, address, family) => {
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving a" }
return callback(err);
}
swipl.call(`assert(ip(${domain}, "${address}"))`);
console.log(`assert(ip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}); }
} else if (str.match("^-ip4:(.+)$")) {
else if (str.match("^-a$")) { console.log("matched -ip[46]!")
console.log("matched -a!") swipl.call(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`);
return dns.lookup(domain, (err, address, family) => { console.log(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`);
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving a" }
return callback(err);
}
swipl.call(`assert(nip(${domain}, "${address}"))`);
console.log(`assert(nip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}); }
}
//mx //a
else if (str.match("^[+~?]?mx$")) { else if (str.match("^[+~?]?a$")) {
console.log("matched mx!") console.log("matched a!")
return dns.resolveMx(domain, (err, address) => { return dns.lookup(domain, (err, address, family) => {
for (let i = 0; i < address.length; i++) {
const mx = address[i].exchange;
dns.lookup(mx, (err, address, family) => {
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving mx" }
return callback(err);
}
swipl.call(`assert(ip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
});
}
})
}
else if (str.match("^-mx$")) {
console.log("matched -mx!")
return dns.resolveMx(domain, (err, address) => {
for (let i = 0; i < address.length; i++) {
const mx = address[i].exchange;
dns.lookup(mx, (err, address, family) => {
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving mx" }
return callback(err);
}
swipl.call(`assert(nip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
});
}
})
}
//include
else if (str.match("^include:(.+)$")) {
next = str.match("^include:(.+)$")[1]
console.log("matched include:", next)
let txt = ""
dns.resolveTxt(next, (err, address) => {
console.log("hello")
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving SPF" }
return callback(err);
}
else {
console.log(address)
for (let i = 0; i < address.length; i++) {
const text = address[i][0];
console.log(text)
if (text.indexOf("v=spf") !== -1) {
txt = text;
break;
}
}
}
if (!txt) {
err = { code: 400, message: "ERROR: Fail to find SPF record" }
return callback(err);
}
const spfArray = txt.split(" ");
console.log(spfArray)
return parseSPF(domain, spfArray, swipl, next, (err) => {
if (err) { if (err) {
console.error(err) console.error("ERROR", err)
return res.status(err.code).send(err.message) err = { code: 400, message: "ERROR: occur on resolving a" }
} return callback(err);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
})
})
}
//redirect
else if (str.match("^redirect=(.+)$")) {
next = str.match("^redirect=(.+)$")[1]
console.log("matched redirect:", next)
let txt = ""
dns.resolveTxt(next, (err, address) => {
if (err) {
console.error(err)
err = { code: 400, message: "ERROR: occur on resolving SPF" }
return callback(err);
}
else {
console.log(address)
for (let i = 0; i < address.length; i++) {
const text = address[i][0];
console.log(text)
if (text.indexOf("v=spf") !== -1) {
txt = text;
break;
}
}
}
if (!txt) {
err = { code: 400, message: "ERROR: Fail to find SPF record" }
return callback(err);
}
const spfArray = txt.split(" ");
console.log(spfArray)
return parseSPF(domain, spfArray, swipl, domain, (err) => {
if (err) {
console.error(err)
return res.status(err.code).send(err.message)
} }
else { else {
swipl.call(`assert(ip(${domain}, "${address}"))`);
console.log(`assert(ip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
});
}
else if (str.match("^-a$")) {
console.log("matched -a!")
return dns.lookup(domain, (err, address, family) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on resolving a" }
return callback(err);
}
else {
swipl.call(`assert(nip(${domain}, "${address}"))`);
console.log(`assert(nip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
});
}
//mx
else if (str.match("^[+~?]?mx$")) {
console.log("matched mx!")
return dns.resolveMx(domain, (err, address) => {
for (let i = 0; i < address.length; i++) {
const mx = address[i].exchange;
dns.lookup(mx, (err, address, family) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on resolving mx" }
return callback(err);
}
else {
swipl.call(`assert(ip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
});
}
}) })
}) }
} else if (str.match("^-mx$")) {
console.log("matched -mx!")
return dns.resolveMx(domain, (err, address) => {
for (let i = 0; i < address.length; i++) {
const mx = address[i].exchange;
dns.lookup(mx, (err, address, family) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on resolving mx" }
return callback(err);
}
else {
swipl.call(`assert(nip(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
});
}
})
}
//all //include
else if (str.match("^-all$")) { else if (str.match("^[+~?]?include:(.+)$")) {
console.log("matched -all!") next = str.match("^[+~?]?include:(.+)$")[1]
swipl.call(`assert(all(${domain}, "-"))`); console.log("matched include:", next)
console.log(`assert(all(${domain}, "-"))`); let txt = ""
return parseSPF(domain, spf.slice(1), swipl, curr, callback); dns.resolveTxt(next, (err, address) => {
} console.log("hello")
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on resolving SPF" }
return callback(err);
}
else {
console.log(address)
for (let i = 0; i < address.length; i++) {
const text = address[i][0];
console.log(text)
if (text.indexOf("v=spf") !== -1) {
txt = text;
break;
}
}
}
if (!txt) {
err = { code: 400, message: "ERROR: Fail to find SPF record" }
return callback(err);
}
else {
const spfArray = txt.split(" ");
console.log(spfArray)
return parseSPF(domain, spfArray, swipl, next, (err) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on redirect" }
return callback(err);
}
else {
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
})
}
})
}
//others //redirect
else { else if (str.match("^[+~?]?redirect=(.+)$")) {
return parseSPF(domain, spf.slice(1), swipl, curr, callback); next = str.match("^[+~?]?redirect=(.+)$")[1]
console.log("matched redirect:", next)
let txt = ""
dns.resolveTxt(next, (err, address) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on resolving SPF" }
return callback(err);
}
else {
console.log(address)
for (let i = 0; i < address.length; i++) {
const text = address[i][0];
console.log(text)
if (text.indexOf("v=spf") !== -1) {
txt = text;
break;
}
}
}
if (!txt) {
err = { code: 400, message: "ERROR: Fail to find SPF record" }
return callback(err);
}
const spfArray = txt.split(" ");
console.log(spfArray)
return parseSPF(domain, spfArray, swipl, domain, (err) => {
if (err) {
console.error("ERROR", err)
err = { code: 400, message: "ERROR: occur on redirect" }
return callback(err);
}
else {
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
})
})
}
//all
else if (str.match("^-all$")) {
console.log("matched -all!")
swipl.call(`assert(all(${domain}, "-"))`);
console.log(`assert(all(${domain}, "-"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
//others
else {
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
}
catch (e) {
console.log(e)
} }
} }
@ -244,25 +266,34 @@ app.get('/spf', spf)
app.listen(PORT, (err) => { app.listen(PORT, (err) => {
if (err) { if (err) {
return console.error(err); return console.error("ERROR", err);
}
else {
return console.log(`Server is listening on ${PORT}`);
} }
return console.log(`Server is listening on ${PORT}`);
}); });
//test //test
// const spfArray = "v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all".split(" "); // const spfArray = "v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com include:spf4.baidu.com mx ptr -all".split(" ");
// const domain = "gmail.com" // const domain = "baidu.com"
// parseSPF(domain, spfArray, swipl, domain, (err) => { // parseSPF(domain, spfArray, swipl, domain, (err) => {
// if (err) { // if (err) {
// return res.status(err.code).send(err.message) // return res.status(err.code).send(err.message)
// } // }
// const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`); // else {
// let ret = null; // swipl.call('assert(ip("_", "_"))');
// let result = [] // swipl.call('assert(nip("_", "_"))');
// while (ret = query.next()) { // swipl.call('assert(all("_", "_"))');
// console.log(`Allowed IP value is: ${ret.IP}`); // swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))');
// result.push(`${ret.IP}`); // const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`);
// let ret = null;
// let result = []
// while (ret = query.next()) {
// console.log(`Allowed IP value is: ${ret.IP}`);
// result.push(`${ret.IP}`);
// }
// query.close();
// swipl.cleanup()
// swipl.initialise()
// } // }
// query.close();
// // return res.status(200).send(result)
// }); // });