diff --git a/config/default.yaml b/config/default.yaml index cbccd88..cac426c 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -22,6 +22,12 @@ instances-index: # Must answer the following format: https://framagit.org/framasoft/peertube/instances-peertube#peertube-auto-follow-global-search url: 'https://instances.joinpeertube.org/api/v1/instances/hosts' -whitelist: - enabled: false - hosts: null + # Useful to do tests + whitelist: + enabled: false + hosts: null + +api: + blacklist: + enabled: false + hosts: null diff --git a/config/test.yaml b/config/test.yaml index 1cd6a94..699435f 100644 --- a/config/test.yaml +++ b/config/test.yaml @@ -3,13 +3,20 @@ elastic_search: videos: 'peertube-index-videos-test1' channels: 'peertube-index-channels-test1' -whitelist: - enabled: true - hosts: - - 'peertube.cpy.re' - - 'peertube2.cpy.re' - - 'peertube3.cpy.re' - - 'framatube.org' - - 'aperi.tube' - - 'peertube.datagueule.tv' - - 'thinkerview.video' +instances-index: + whitelist: + enabled: true + hosts: + - 'peertube.cpy.re' + - 'peertube2.cpy.re' + - 'peertube3.cpy.re' + - 'framatube.org' + - 'aperi.tube' + - 'peertube.datagueule.tv' + - 'thinkerview.video' + +api: + blacklist: + enabled: true + hosts: + - 'peertube.cpy.re' diff --git a/server/controllers/api/search-channels.ts b/server/controllers/api/search-channels.ts index a08314e..7994fa0 100644 --- a/server/controllers/api/search-channels.ts +++ b/server/controllers/api/search-channels.ts @@ -7,6 +7,7 @@ import { setDefaultSearchSort } from '../../middlewares/sort' import { paginationValidator } from '../../middlewares/validators/pagination' import { videoChannelsSearchValidator, commonFiltersValidators } from '../../middlewares/validators/search' import { channelsSearchSortValidator } from '../../middlewares/validators/sort' +import { CONFIG } from '../../initializers/constants' const searchChannelsRouter = express.Router() @@ -28,6 +29,15 @@ export { searchChannelsRouter } async function searchChannels (req: express.Request, res: express.Response) { const query = Object.assign(req.query || {}, req.body || {}) as ChannelsSearchQuery + + if (!Array.isArray(query.blockedHosts)) { + query.blockedHosts = [] + } + + if (CONFIG.API.BLACKLIST.ENABLED && Array.isArray(CONFIG.API.BLACKLIST.HOSTS)) { + query.blockedHosts = query.blockedHosts.concat(CONFIG.API.BLACKLIST.HOSTS) + } + const resultList = await queryChannels(query) return res.json({ diff --git a/server/controllers/api/search-videos.ts b/server/controllers/api/search-videos.ts index f734bec..26336d4 100644 --- a/server/controllers/api/search-videos.ts +++ b/server/controllers/api/search-videos.ts @@ -7,6 +7,7 @@ import { videosSearchSortValidator } from '../../middlewares/validators/sort' import { commonVideosFiltersValidator, videosSearchValidator, commonFiltersValidators } from '../../middlewares/validators/search' import { setDefaultSearchSort } from '../../middlewares/sort' import { VideosSearchQuery } from 'server/types/video-search.model' +import { CONFIG } from '../../initializers/constants' const searchVideosRouter = express.Router() @@ -30,6 +31,14 @@ export { searchVideosRouter } async function searchVideos (req: express.Request, res: express.Response) { const query = Object.assign(req.query || {}, req.body || {}) as VideosSearchQuery + if (!Array.isArray(query.blockedHosts)) { + query.blockedHosts = [] + } + + if (CONFIG.API.BLACKLIST.ENABLED && Array.isArray(CONFIG.API.BLACKLIST.HOSTS)) { + query.blockedHosts = query.blockedHosts.concat(CONFIG.API.BLACKLIST.HOSTS) + } + const resultList = await queryVideos(query) return res.json({ diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 88b6b66..df9b751 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -24,11 +24,17 @@ const CONFIG = { LEVEL: config.get('log.level') }, INSTANCES_INDEX: { - URL: config.get('instances-index.url') + URL: config.get('instances-index.url'), + WHITELIST: { + ENABLED: config.get('instances-index.whitelist.enabled'), + HOSTS: config.get('instances-index.whitelist.hosts') + } }, - WHITELIST: { - ENABLED: config.get('whitelist.enabled'), - HOSTS: config.get('whitelist.hosts') + API: { + BLACKLIST: { + ENABLED: config.get('api.blacklist.enabled'), + HOSTS: config.get('api.blacklist.hosts') + } } } diff --git a/server/lib/elastic-search-instances.ts b/server/lib/elastic-search-instances.ts index fd5421f..d32f406 100644 --- a/server/lib/elastic-search-instances.ts +++ b/server/lib/elastic-search-instances.ts @@ -23,8 +23,8 @@ async function listIndexInstances () { async function buildInstanceHosts () { let indexHosts = await listIndexInstancesHost() - if (CONFIG.WHITELIST.ENABLED) { - const whitelistHosts = Array.isArray(CONFIG.WHITELIST.HOSTS) ? CONFIG.WHITELIST.HOSTS : [] + if (CONFIG.INSTANCES_INDEX.WHITELIST.ENABLED) { + const whitelistHosts = Array.isArray(CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS) ? CONFIG.INSTANCES_INDEX.WHITELIST.HOSTS : [] indexHosts = indexHosts.filter(h => whitelistHosts.includes(h)) }