reuse running proxycheck / whois connections even when requesting
without cache
This commit is contained in:
parent
c1d5e437fc
commit
f49bb42578
|
@ -48,7 +48,7 @@ async function saveIPInfo(ip, whoisRet, allowed, info) {
|
||||||
* @param f proxycheck function
|
* @param f proxycheck function
|
||||||
* @param ip full ip
|
* @param ip full ip
|
||||||
* @param ipKey
|
* @param ipKey
|
||||||
* @return [ allowed, status, pcheck ]
|
* @return [ allowed, status, pcheck capromise]
|
||||||
*/
|
*/
|
||||||
async function checkPCAndLists(f, ip, ipKey) {
|
async function checkPCAndLists(f, ip, ipKey) {
|
||||||
let allowed = true;
|
let allowed = true;
|
||||||
|
@ -73,6 +73,7 @@ async function checkPCAndLists(f, ip, ipKey) {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(`Error checkAllowed for ${ip}: ${err.message}`);
|
logger.error(`Error checkAllowed for ${ip}: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const caPromise = cacheAllowed(ipKey, status);
|
const caPromise = cacheAllowed(ipKey, status);
|
||||||
return [allowed, status, pcheck, caPromise];
|
return [allowed, status, pcheck, caPromise];
|
||||||
}
|
}
|
||||||
|
@ -83,9 +84,7 @@ async function checkPCAndLists(f, ip, ipKey) {
|
||||||
* @param ip IP to check
|
* @param ip IP to check
|
||||||
* @return checkifAllowed return
|
* @return checkifAllowed return
|
||||||
*/
|
*/
|
||||||
async function withoutCache(f, ip) {
|
async function withoutCache(f, ip, ipKey) {
|
||||||
const ipKey = getIPv6Subnet(ip);
|
|
||||||
|
|
||||||
const [
|
const [
|
||||||
[allowed, status, pcheck, caPromise],
|
[allowed, status, pcheck, caPromise],
|
||||||
whoisRet,
|
whoisRet,
|
||||||
|
@ -106,40 +105,55 @@ async function withoutCache(f, ip) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* execute proxycheck with caching results
|
* Array of running ip checks
|
||||||
* do not check ip double
|
* [
|
||||||
|
* [ipKey, promise],
|
||||||
|
* [ipKey2, promise2],
|
||||||
|
* ...
|
||||||
|
* ]
|
||||||
|
*/
|
||||||
|
const checking = [];
|
||||||
|
/*
|
||||||
|
* Execute proxycheck and whois and save result into cache
|
||||||
|
* If IP is already getting checked, reuse its request
|
||||||
|
* @param ip ip to check
|
||||||
|
* @return checkIfAllowed return
|
||||||
|
*/
|
||||||
|
async function withoutCacheButReUse(f, ip, ipKey) {
|
||||||
|
const runReq = checking.find((q) => q[0] === ipKey);
|
||||||
|
if (runReq) {
|
||||||
|
return runReq[1];
|
||||||
|
}
|
||||||
|
const promise = withoutCache(f, ip, ipKey);
|
||||||
|
checking.push([ipKey, promise]);
|
||||||
|
|
||||||
|
const result = await promise;
|
||||||
|
checking.splice(
|
||||||
|
checking.findIndex((q) => q[0] === ipKey),
|
||||||
|
1,
|
||||||
|
);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* execute proxycheck, don't wait, return cache if exists or
|
||||||
|
* status -2 if currently checking
|
||||||
* @param f function for checking if proxy
|
* @param f function for checking if proxy
|
||||||
* @param ip IP to check
|
* @param ip IP to check
|
||||||
* @return Object as in checkIfAllowed
|
* @return Object as in checkIfAllowed
|
||||||
* @return true if proxy or blacklisted, false if not or whitelisted
|
* @return true if proxy or blacklisted, false if not or whitelisted
|
||||||
*/
|
*/
|
||||||
const checking = [];
|
async function withCache(f, ip, ipKey) {
|
||||||
async function withCache(f, ip) {
|
const runReq = checking.find((q) => q[0] === ipKey);
|
||||||
if (!ip || ip === '0.0.0.1') {
|
|
||||||
return {
|
if (!runReq) {
|
||||||
allowed: false,
|
|
||||||
status: 4,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
const ipKey = getIPv6Subnet(ip);
|
|
||||||
if (checking.indexOf(ipKey) === -1) {
|
|
||||||
// get from cache, if there
|
|
||||||
const cache = await getCacheAllowed(ipKey);
|
const cache = await getCacheAllowed(ipKey);
|
||||||
if (cache) {
|
if (cache) {
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
// else make asynchronous ipcheck and assume no proxy in the meantime
|
withoutCacheButReUse(f, ip, ipKey);
|
||||||
// do not check ip that currently gets checked
|
|
||||||
checking.push(ipKey);
|
|
||||||
withoutCache(f, ip)
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error('Error %s', error.message);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
const pos = checking.indexOf(ipKey);
|
|
||||||
if (~pos) checking.splice(pos, 1);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
allowed: true,
|
allowed: true,
|
||||||
status: -2,
|
status: -2,
|
||||||
|
@ -161,11 +175,19 @@ async function withCache(f, ip) {
|
||||||
* 4: invalid ip
|
* 4: invalid ip
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
function checkIfAllowed(ip, disableCache = false) {
|
export default function checkIfAllowed(ip, disableCache = false) {
|
||||||
if (disableCache) {
|
if (!ip || ip === '0.0.0.1') {
|
||||||
return withoutCache(checker, ip);
|
return {
|
||||||
|
allowed: false,
|
||||||
|
status: 4,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return withCache(checker, ip);
|
const ipKey = getIPv6Subnet(ip);
|
||||||
|
|
||||||
|
if (disableCache) {
|
||||||
|
return withoutCacheButReUse(checker, ip, ipKey);
|
||||||
|
}
|
||||||
|
return withCache(checker, ip, ipKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -179,5 +201,3 @@ function checkIfAllowed(ip, disableCache = false) {
|
||||||
export function checkIfMailDisposable(email) {
|
export function checkIfMailDisposable(email) {
|
||||||
return mailChecker(email);
|
return mailChecker(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkIfAllowed;
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user