diff --git a/addon/lib/cache.js b/addon/lib/cache.js index da067b6..9158eca 100644 --- a/addon/lib/cache.js +++ b/addon/lib/cache.js @@ -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 }; diff --git a/addon/lib/request_helper.js b/addon/lib/request_helper.js index bbd799c..4ba4de3 100644 --- a/addon/lib/request_helper.js +++ b/addon/lib/request_helper.js @@ -17,4 +17,12 @@ function getRandomProxy() { return undefined; } -module.exports = { getRandomUserAgent, getRandomProxy }; \ No newline at end of file +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 }; \ No newline at end of file diff --git a/addon/moch/realdebrid.js b/addon/moch/realdebrid.js index 51c60bf..cdd8e45 100644 --- a/addon/moch/realdebrid.js +++ b/addon/moch/realdebrid.js @@ -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 [];