fix: add try catch
This commit is contained in:
parent
9ffb9e334c
commit
b49cfbc816
|
@ -12,7 +12,7 @@ const spf = (req, res) => {
|
|||
let spf = ""
|
||||
dns.resolveTxt(domain, (err, address) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
console.error("ERROR", err)
|
||||
return res.status(400).send("ERROR: occur on resolving SPF")
|
||||
}
|
||||
else {
|
||||
|
@ -27,15 +27,17 @@ const spf = (req, res) => {
|
|||
}
|
||||
}
|
||||
if (!spf) {
|
||||
console.error("ERROR: not spf")
|
||||
return res.status(400).send(`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(err)
|
||||
console.error("ERROR", err)
|
||||
return res.status(err.code).send(err.message)
|
||||
}
|
||||
else {
|
||||
swipl.call('assert(ip("_", "_"))');
|
||||
swipl.call('assert(nip("_", "_"))');
|
||||
swipl.call('assert(all("_", "_"))');
|
||||
|
@ -50,7 +52,8 @@ const spf = (req, res) => {
|
|||
query.close();
|
||||
swipl.cleanup()
|
||||
swipl.initialise()
|
||||
return res.status(200).send(result)
|
||||
return res.status(200).send([...new Set(result)])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -60,6 +63,7 @@ const spf = (req, res) => {
|
|||
}
|
||||
|
||||
const parseSPF = (domain, spf, swipl, curr, callback) => {
|
||||
try {
|
||||
console.log(domain, spf, curr)
|
||||
let err = undefined
|
||||
|
||||
|
@ -88,26 +92,30 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
console.log("matched a!")
|
||||
return dns.lookup(domain, (err, address, family) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
console.error("ERROR", err)
|
||||
err = { code: 400, message: "ERROR: occur on resolving a" }
|
||||
return callback(err);
|
||||
}
|
||||
else {
|
||||
swipl.call(`assert(ip(${domain}, "${address}"))`);
|
||||
console.log(`assert(ip(${domain}, "${address}"))`);
|
||||
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(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -119,12 +127,14 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
const mx = address[i].exchange;
|
||||
dns.lookup(mx, (err, address, family) => {
|
||||
if (err) {
|
||||
console.error(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
|
@ -136,26 +146,28 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
const mx = address[i].exchange;
|
||||
dns.lookup(mx, (err, address, family) => {
|
||||
if (err) {
|
||||
console.error(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);
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//include
|
||||
else if (str.match("^include:(.+)$")) {
|
||||
next = str.match("^include:(.+)$")[1]
|
||||
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)
|
||||
console.error("ERROR", err)
|
||||
err = { code: 400, message: "ERROR: occur on resolving SPF" }
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -174,26 +186,31 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
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(err)
|
||||
return res.status(err.code).send(err.message)
|
||||
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);
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//redirect
|
||||
else if (str.match("^redirect=(.+)$")) {
|
||||
next = str.match("^redirect=(.+)$")[1]
|
||||
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)
|
||||
console.error("ERROR", err)
|
||||
err = { code: 400, message: "ERROR: occur on resolving SPF" }
|
||||
return callback(err);
|
||||
}
|
||||
|
@ -216,8 +233,9 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
console.log(spfArray)
|
||||
return parseSPF(domain, spfArray, swipl, domain, (err) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return res.status(err.code).send(err.message)
|
||||
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);
|
||||
|
@ -238,24 +256,35 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
|
|||
else {
|
||||
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
app.get('/spf', spf)
|
||||
|
||||
app.listen(PORT, (err) => {
|
||||
if (err) {
|
||||
return console.error(err);
|
||||
return console.error("ERROR", err);
|
||||
}
|
||||
else {
|
||||
return console.log(`Server is listening on ${PORT}`);
|
||||
}
|
||||
});
|
||||
|
||||
//test
|
||||
// const spfArray = "v=spf1 include:_netblocks.google.com include:_netblocks2.google.com include:_netblocks3.google.com ~all".split(" ");
|
||||
// const domain = "gmail.com"
|
||||
// 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 = "baidu.com"
|
||||
// parseSPF(domain, spfArray, swipl, domain, (err) => {
|
||||
// if (err) {
|
||||
// return res.status(err.code).send(err.message)
|
||||
// }
|
||||
// else {
|
||||
// swipl.call('assert(ip("_", "_"))');
|
||||
// swipl.call('assert(nip("_", "_"))');
|
||||
// swipl.call('assert(all("_", "_"))');
|
||||
// swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))');
|
||||
// const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`);
|
||||
// let ret = null;
|
||||
// let result = []
|
||||
|
@ -264,5 +293,7 @@ app.listen(PORT, (err) => {
|
|||
// result.push(`${ret.IP}`);
|
||||
// }
|
||||
// query.close();
|
||||
// // return res.status(200).send(result)
|
||||
// swipl.cleanup()
|
||||
// swipl.initialise()
|
||||
// }
|
||||
// });
|
Loading…
Reference in New Issue
Block a user