[addon] refreshes and blacklists failed proxies

This commit is contained in:
TheBeastLT
2020-06-06 16:18:41 +02:00
parent 0361dc8096
commit 6496d7785e
3 changed files with 31 additions and 11 deletions

View File

@@ -68,5 +68,9 @@ function cacheUserAgent(id, method) {
return cacheWrap(remoteCache, `${USER_AGENT_KEY_PREFIX}:${id}`, method, { ttl: USER_AGENT_TTL });
}
module.exports = { cacheWrapStream, cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent };
function uncacheProxy(id) {
return remoteCache.del(`${PROXY_KEY_PREFIX}:${id}`);
}
module.exports = { cacheWrapStream, cacheWrapResolvedUrl, cacheWrapProxy, cacheUserAgent, uncacheProxy };

View File

@@ -17,4 +17,12 @@ function getRandomProxy() {
return undefined;
}
module.exports = { getRandomUserAgent, getRandomProxy };
function blacklistProxy(proxy) {
const proxyHost = proxy.replace(/.*@/, '');
console.warn(`Blacklisting ${proxyHost}`);
if (PROXY_HOSTS && PROXY_HOSTS.indexOf(proxyHost) > -1) {
PROXY_HOSTS.splice(PROXY_HOSTS.indexOf(proxyHost), 1);
}
}
module.exports = { getRandomUserAgent, getRandomProxy, blacklistProxy };

View File

@@ -3,20 +3,14 @@ const { encode } = require('magnet-uri');
const { isVideo, isArchive } = require('../lib/extension');
const delay = require('./delay');
const StaticResponse = require('./static');
const { getRandomProxy, getRandomUserAgent } = require('../lib/request_helper');
const { cacheWrapProxy, cacheUserAgent } = require('../lib/cache');
const { getRandomProxy, getRandomUserAgent, blacklistProxy } = require('../lib/request_helper');
const { cacheWrapProxy, cacheUserAgent, uncacheProxy } = require('../lib/cache');
const MIN_SIZE = 15728640; // 15 MB
async function getCachedStreams(streams, apiKey) {
const options = await getDefaultOptions(apiKey);
const RD = new RealDebridClient(apiKey, options);
const hashes = streams.map(stream => stream.infoHash);
const available = await RD.torrents.instantAvailability(hashes)
.catch(error => {
console.warn('Failed RealDebrid cached torrent availability request: ', error);
return undefined;
});
const available = await _getInstantAvailable(hashes, apiKey);
return available && streams
.reduce((mochStreams, stream) => {
const cachedEntry = available[stream.infoHash];
@@ -30,6 +24,20 @@ async function getCachedStreams(streams, apiKey) {
}, {})
}
async function _getInstantAvailable(hashes, apiKey, retries = 3) {
const options = await getDefaultOptions(apiKey);
const RD = new RealDebridClient(apiKey, options);
return RD.torrents.instantAvailability(hashes)
.catch(error => {
if (retries > 0 && ['ENOTFOUND', 'ETIMEDOUT'].some(v => error.message && error.message.includes(v))) {
blacklistProxy(options.proxy);
return uncacheProxy('moch').then(() => _getInstantAvailable(hashes, apiKey, retries - 1));
}
console.warn('Failed RealDebrid cached torrent availability request: ', error);
return undefined;
});
}
function _getCachedFileIds(fileIndex, hosterResults) {
if (!hosterResults || Array.isArray(hosterResults)) {
return [];