feat: separate ip4 and ip6

This commit is contained in:
Renge 2024-04-17 20:30:54 -04:00
parent 875d7ddb42
commit 4a723d75d3

View File

@ -38,21 +38,32 @@ const spf = (req, res) => {
return res.status(err.code).send(err.message) return res.status(err.code).send(err.message)
} }
else { else {
swipl.call('assert(ip("_", "_"))'); swipl.call('assert(ip4("_", "_"))');
swipl.call('assert(nip("_", "_"))'); swipl.call('assert(nip4("_", "_"))');
swipl.call('assert(ip6("_", "_"))');
swipl.call('assert(nip6("_", "_"))');
swipl.call('assert(all("_", "_"))'); swipl.call('assert(all("_", "_"))');
swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))'); swipl.call('assert((allowed_to_send_email4(Domain, IP) :- (ip4(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip4(Domain, IP))))');
const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`); swipl.call('assert((allowed_to_send_email6(Domain, IP) :- (ip6(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip6(Domain, IP))))');
let query = new swipl.Query(`allowed_to_send_email4(${domain}, IP)`);
let ret = null; let ret = null;
let result = [] let allowedIP4 = []
while (ret = query.next()) { while (ret = query.next()) {
console.log(`Allowed IP value is: ${ret.IP}`); console.log(`Allowed IP value is: ${ret.IP}`);
result.push(`${ret.IP}`); allowedIP4.push(`${ret.IP}`);
}
query.close();
query = new swipl.Query(`allowed_to_send_email6(${domain}, IP)`);
ret = null;
let allowedIP6 = []
while (ret = query.next()) {
console.log(`Allowed IP value is: ${ret.IP}`);
allowedIP6.push(`${ret.IP}`);
} }
query.close(); query.close();
swipl.cleanup() swipl.cleanup()
swipl.initialise() swipl.initialise()
return res.status(200).send({spf: spf, allowedIP: [...new Set(result)]}) return res.status(200).send({spf: spf, allowedIP4: [...new Set(allowedIP4)], allowedIP6: [...new Set(allowedIP6)]})
} }
}) })
}) })
@ -112,17 +123,31 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
const str = spf[0] const str = spf[0]
//ip46 //ip4
if (str.match("^[+~?]?ip[46]:(.+)$")) { if (str.match("^[+~?]?ip4:(.+)$")) {
console.log("matched ip[46]!") console.log("matched ip4!")
swipl.call(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`); swipl.call(`assert(ip4(${domain}, "${str.match("^[+~?]?ip4:(.+)")[1]}"))`);
console.log(`assert(ip(${domain}, "${str.match("^[+~?]?ip[46]:(.+)")[1]}"))`); console.log(`assert(ip4(${domain}, "${str.match("^[+~?]?ip4:(.+)")[1]}"))`);
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("^-ip4:(.+)$")) {
console.log("matched -ip[46]!") console.log("matched -ip4!")
swipl.call(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`); swipl.call(`assert(nip4(${domain}, "${str.match("^-ip4:(.+)")[1]}"))`);
console.log(`assert(nip(${domain}, "${str.match("^-ip[46]:(.+)")[1]}"))`); console.log(`assert(nip4(${domain}, "${str.match("^-ip4:(.+)")[1]}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
//ip6
if (str.match("^[+~?]?ip6:(.+)$")) {
console.log("matched ip6!")
swipl.call(`assert(ip6(${domain}, "${str.match("^[+~?]?ip6:(.+)")[1]}"))`);
console.log(`assert(ip6(${domain}, "${str.match("^[+~?]?ip6:(.+)")[1]}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback);
}
else if (str.match("^-ip6:(.+)$")) {
console.log("matched -ip6!")
swipl.call(`assert(nip6(${domain}, "${str.match("^-ip6:(.+)")[1]}"))`);
console.log(`assert(nip6(${domain}, "${str.match("^-ip6:(.+)")[1]}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
@ -136,8 +161,8 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
return callback(err); return callback(err);
} }
else { else {
swipl.call(`assert(ip(${domain}, "${address}"))`); swipl.call(`assert(ip4(${domain}, "${address}"))`);
console.log(`assert(ip(${domain}, "${address}"))`); console.log(`assert(ip4(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
}); });
@ -151,8 +176,8 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
return callback(err); return callback(err);
} }
else { else {
swipl.call(`assert(nip(${domain}, "${address}"))`); swipl.call(`assert(nip4(${domain}, "${address}"))`);
console.log(`assert(nip(${domain}, "${address}"))`); console.log(`assert(nip4(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
}); });
@ -171,7 +196,7 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
return callback(err); return callback(err);
} }
else { else {
swipl.call(`assert(ip(${domain}, "${address}"))`); swipl.call(`assert(ip4(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
}); });
@ -190,7 +215,7 @@ const parseSPF = (domain, spf, swipl, curr, callback) => {
return callback(err); return callback(err);
} }
else { else {
swipl.call(`assert(nip(${domain}, "${address}"))`); swipl.call(`assert(nip4(${domain}, "${address}"))`);
return parseSPF(domain, spf.slice(1), swipl, curr, callback); return parseSPF(domain, spf.slice(1), swipl, curr, callback);
} }
}); });
@ -322,8 +347,8 @@ app.listen(PORT, (err) => {
// return res.status(err.code).send(err.message) // return res.status(err.code).send(err.message)
// } // }
// else { // else {
// swipl.call('assert(ip("_", "_"))'); // swipl.call('assert(ip4("_", "_"))');
// swipl.call('assert(nip("_", "_"))'); // swipl.call('assert(nip4("_", "_"))');
// swipl.call('assert(all("_", "_"))'); // swipl.call('assert(all("_", "_"))');
// swipl.call('assert((allowed_to_send_email(Domain, IP) :- (ip(Domain, IP)), once(\\+ all(Domain, "-"); \\+ nip(Domain, IP))))'); // 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)`); // const query = new swipl.Query(`allowed_to_send_email(${domain}, IP)`);