From 4b857f50c855314588be15cddcc7e122b0fc85f0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Feb 2024 10:09:03 +0100 Subject: [PATCH] Anonymize IP stored in Redis too --- scripts/simulate-many-viewers-worker.js | 9 +++++ scripts/simulate-many-viewers-worker.ts | 17 ++++++++++ scripts/simulate-many-viewers.ts | 44 +++++++++++++++++++------ server/core/lib/redis.ts | 8 +++-- 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 scripts/simulate-many-viewers-worker.js create mode 100644 scripts/simulate-many-viewers-worker.ts diff --git a/scripts/simulate-many-viewers-worker.js b/scripts/simulate-many-viewers-worker.js new file mode 100644 index 000000000..b37ec57fb --- /dev/null +++ b/scripts/simulate-many-viewers-worker.js @@ -0,0 +1,9 @@ +import Bluebird from "bluebird"; +import { PeerTubeServer } from "@peertube/peertube-server-commands"; +module.exports = async function sendViews(options) { + const { url, videoId, viewers } = options; + const server = new PeerTubeServer({ url }); + await Bluebird.map(viewers, (viewer) => { + return server.views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor }).catch((err) => console.error("Cannot simulate viewer", err)); + }, { concurrency: 500 }); +}; diff --git a/scripts/simulate-many-viewers-worker.ts b/scripts/simulate-many-viewers-worker.ts new file mode 100644 index 000000000..eab39d616 --- /dev/null +++ b/scripts/simulate-many-viewers-worker.ts @@ -0,0 +1,17 @@ +import Bluebird from 'bluebird' +import { PeerTubeServer } from '@peertube/peertube-server-commands' + +module.exports = async function sendViews (options: { + url: string + videoId: number + viewers: { xForwardedFor: string }[] +}) { + const { url, videoId, viewers } = options + + const server = new PeerTubeServer({ url }) + + await Bluebird.map(viewers, viewer => { + return server.views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor }) + .catch(err => console.error('Cannot simulate viewer', err)) + }, { concurrency: 500 }) +} diff --git a/scripts/simulate-many-viewers.ts b/scripts/simulate-many-viewers.ts index 5d12bba28..97ab968b7 100644 --- a/scripts/simulate-many-viewers.ts +++ b/scripts/simulate-many-viewers.ts @@ -1,4 +1,3 @@ -import Bluebird from 'bluebird' import { wait } from '@peertube/peertube-core-utils' import { createSingleServer, @@ -8,15 +7,27 @@ import { setAccessTokensToServers, waitJobs } from '@peertube/peertube-server-commands' +import { isMainThread } from 'worker_threads' +import Piscina from 'piscina' +import { fileURLToPath } from 'url' +import { dirname, join } from 'path' + +const THOUSAND_VIEWERS = 2 +const TOTAL_THREADS = 20 let servers: PeerTubeServer[] const viewers: { xForwardedFor: string }[] = [] let videoId: string +let pool: Piscina -run() - .then(() => process.exit(0)) - .catch(err => console.error(err)) - .finally(() => killallServers(servers)) +if (isMainThread) { + run() + .then(() => process.exit(0)) + .catch(err => console.error(err)) + .finally(() => killallServers(servers)) +} + +// --------------------------------------------------------------------------- async function run () { await prepare() @@ -27,6 +38,12 @@ async function run () { } async function prepare () { + pool = new Piscina({ + filename: join(dirname(fileURLToPath(import.meta.url)), 'simulate-many-viewers-worker.js'), + minThreads: 20, + maxThreads: 20 + }) + console.log('Preparing servers...') const config = { @@ -64,8 +81,6 @@ async function prepare () { await waitJobs(servers) - const THOUSAND_VIEWERS = 2 - for (let i = 2; i < 252; i++) { for (let j = 2; j < 6; j++) { for (let k = 2; k < THOUSAND_VIEWERS + 2; k++) { @@ -82,9 +97,18 @@ async function runViewers () { const before = new Date().getTime() - await Bluebird.map(viewers, viewer => { - return servers[0].views.simulateView({ id: videoId, xForwardedFor: viewer.xForwardedFor }) - }, { concurrency: 500 }) + const promises: Promise[] = [] + + for (let i = 0; i < TOTAL_THREADS; i++) { + const start = i * THOUSAND_VIEWERS * 1000 / TOTAL_THREADS + const end = (i + 1) * THOUSAND_VIEWERS * 1000 / TOTAL_THREADS + + console.log(`Sending viewers ${start} to ${end}`) + + promises.push(pool.run({ url: servers[0].url, viewers: viewers.slice(start, end), videoId })) + } + + await Promise.all(promises) console.log('Finished to run views in %d seconds.', (new Date().getTime() - before) / 1000) diff --git a/server/core/lib/redis.ts b/server/core/lib/redis.ts index 57231e63b..5dcd2b20b 100644 --- a/server/core/lib/redis.ts +++ b/server/core/lib/redis.ts @@ -341,7 +341,9 @@ class Redis { generateLocalVideoViewerKeys (ip: string, videoId: number): { setKey: string, viewerKey: string } generateLocalVideoViewerKeys (): { setKey: string } generateLocalVideoViewerKeys (ip?: string, videoId?: number) { - return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${ip}-${videoId}` } + const anonymousIP = sha256(CONFIG.SECRETS + '-' + ip) + + return { setKey: `local-video-viewer-stats-keys`, viewerKey: `local-video-viewer-stats-${anonymousIP}-${videoId}` } } private generateVideoViewStatsKeys (options: { videoId?: number, hour?: number }) { @@ -369,11 +371,11 @@ class Redis { } generateIPViewKey (ip: string, videoUUID: string) { - return `views-${videoUUID}-${ip}` + return `views-${videoUUID}-${sha256(CONFIG.SECRETS.PEERTUBE + '-' + ip)}` } private generateContactFormKey (ip: string) { - return 'contact-form-' + ip + return 'contact-form-' + sha256(CONFIG.SECRETS.PEERTUBE + '-' + ip) } private generateAPUnavailabilityKey (url: string) {