mirror of https://github.com/Chocobozzz/PeerTube
Don't need to use redis to block tracker ips
There could be many of them, so reduce load by storing IPs directly inside node memorypull/5486/head
parent
7624270a63
commit
1fed9cb8d3
|
@ -1,17 +1,22 @@
|
|||
import { Server as TrackerServer } from 'bittorrent-tracker'
|
||||
import express from 'express'
|
||||
import { createServer } from 'http'
|
||||
import LRUCache from 'lru-cache'
|
||||
import proxyAddr from 'proxy-addr'
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { Redis } from '@server/lib/redis'
|
||||
import { logger } from '../helpers/logger'
|
||||
import { CONFIG } from '../initializers/config'
|
||||
import { TRACKER_RATE_LIMITS } from '../initializers/constants'
|
||||
import { LRU_CACHE, TRACKER_RATE_LIMITS } from '../initializers/constants'
|
||||
import { VideoFileModel } from '../models/video/video-file'
|
||||
import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
|
||||
|
||||
const trackerRouter = express.Router()
|
||||
|
||||
const blockedIPs = new LRUCache<string, boolean>({
|
||||
max: LRU_CACHE.TRACKER_IPS.MAX_SIZE,
|
||||
ttl: TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME
|
||||
})
|
||||
|
||||
let peersIps = {}
|
||||
let peersIpInfoHash = {}
|
||||
runPeersChecker()
|
||||
|
@ -55,8 +60,7 @@ const trackerServer = new TrackerServer({
|
|||
|
||||
// Close socket connection and block IP for a few time
|
||||
if (params.type === 'ws') {
|
||||
Redis.Instance.setTrackerBlockIP(ip)
|
||||
.catch(err => logger.error('Cannot set tracker block ip.', { err }))
|
||||
blockedIPs.set(ip, true)
|
||||
|
||||
// setTimeout to wait filter response
|
||||
setTimeout(() => params.socket.close(), 0)
|
||||
|
@ -102,9 +106,7 @@ function createWebsocketTrackerServer (app: express.Application) {
|
|||
if (request.url === '/tracker/socket') {
|
||||
const ip = proxyAddr(request, CONFIG.TRUST_PROXY)
|
||||
|
||||
Redis.Instance.doesTrackerBlockIPExist(ip)
|
||||
.then(result => {
|
||||
if (result === true) {
|
||||
if (blockedIPs.has(ip)) {
|
||||
logger.debug('Blocking IP %s from tracker.', ip)
|
||||
|
||||
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
|
||||
|
@ -114,8 +116,6 @@ function createWebsocketTrackerServer (app: express.Application) {
|
|||
|
||||
// FIXME: typings
|
||||
return wss.handleUpgrade(request, socket as any, head, ws => wss.emit('connection', ws, request))
|
||||
})
|
||||
.catch(err => logger.error('Cannot check if tracker block ip exists.', { err }))
|
||||
}
|
||||
|
||||
// Don't destroy socket, we have Socket.IO too
|
||||
|
|
|
@ -781,6 +781,9 @@ const LRU_CACHE = {
|
|||
VIDEO_TOKENS: {
|
||||
MAX_SIZE: 100_000,
|
||||
TTL: parseDurationToMs('8 hours')
|
||||
},
|
||||
TRACKER_IPS: {
|
||||
MAX_SIZE: 100_000
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -884,7 +887,7 @@ const TRACKER_RATE_LIMITS = {
|
|||
INTERVAL: 60000 * 5, // 5 minutes
|
||||
ANNOUNCES_PER_IP_PER_INFOHASH: 15, // maximum announces per torrent in the interval
|
||||
ANNOUNCES_PER_IP: 30, // maximum announces for all our torrents in the interval
|
||||
BLOCK_IP_LIFETIME: 60000 * 3 // 3 minutes
|
||||
BLOCK_IP_LIFETIME: parseDurationToMs('3 minutes')
|
||||
}
|
||||
|
||||
const P2P_MEDIA_LOADER_PEER_VERSION = 2
|
||||
|
|
|
@ -8,7 +8,6 @@ import {
|
|||
AP_CLEANER,
|
||||
CONTACT_FORM_LIFETIME,
|
||||
RESUMABLE_UPLOAD_SESSION_LIFETIME,
|
||||
TRACKER_RATE_LIMITS,
|
||||
TWO_FACTOR_AUTH_REQUEST_TOKEN_LIFETIME,
|
||||
USER_EMAIL_VERIFY_LIFETIME,
|
||||
USER_PASSWORD_CREATE_LIFETIME,
|
||||
|
@ -157,16 +156,6 @@ class Redis {
|
|||
return this.exists(this.generateIPViewKey(ip, videoUUID))
|
||||
}
|
||||
|
||||
/* ************ Tracker IP block ************ */
|
||||
|
||||
setTrackerBlockIP (ip: string) {
|
||||
return this.setValue(this.generateTrackerBlockIPKey(ip), '1', TRACKER_RATE_LIMITS.BLOCK_IP_LIFETIME)
|
||||
}
|
||||
|
||||
async doesTrackerBlockIPExist (ip: string) {
|
||||
return this.exists(this.generateTrackerBlockIPKey(ip))
|
||||
}
|
||||
|
||||
/* ************ Video views stats ************ */
|
||||
|
||||
addVideoViewStats (videoId: number) {
|
||||
|
@ -365,10 +354,6 @@ class Redis {
|
|||
return `views-${videoUUID}-${ip}`
|
||||
}
|
||||
|
||||
private generateTrackerBlockIPKey (ip: string) {
|
||||
return `tracker-block-ip-${ip}`
|
||||
}
|
||||
|
||||
private generateContactFormKey (ip: string) {
|
||||
return 'contact-form-' + ip
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue