aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/tracker.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/tracker.ts')
-rw-r--r--server/controllers/tracker.ts46
1 files changed, 32 insertions, 14 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
index 9bc7586d1..8b77d9de7 100644
--- a/server/controllers/tracker.ts
+++ b/server/controllers/tracker.ts
@@ -6,6 +6,8 @@ import * as proxyAddr from 'proxy-addr'
6import { Server as WebSocketServer } from 'ws' 6import { Server as WebSocketServer } from 'ws'
7import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants' 7import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants'
8import { VideoFileModel } from '../models/video/video-file' 8import { VideoFileModel } from '../models/video/video-file'
9import { parse } from 'url'
10import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist'
9 11
10const TrackerServer = bitTorrentTracker.Server 12const TrackerServer = bitTorrentTracker.Server
11 13
@@ -20,7 +22,7 @@ const trackerServer = new TrackerServer({
20 udp: false, 22 udp: false,
21 ws: false, 23 ws: false,
22 dht: false, 24 dht: false,
23 filter: function (infoHash, params, cb) { 25 filter: async function (infoHash, params, cb) {
24 let ip: string 26 let ip: string
25 27
26 if (params.type === 'ws') { 28 if (params.type === 'ws') {
@@ -31,19 +33,25 @@ const trackerServer = new TrackerServer({
31 33
32 const key = ip + '-' + infoHash 34 const key = ip + '-' + infoHash
33 35
34 peersIps[ip] = peersIps[ip] ? peersIps[ip] + 1 : 1 36 peersIps[ ip ] = peersIps[ ip ] ? peersIps[ ip ] + 1 : 1
35 peersIpInfoHash[key] = peersIpInfoHash[key] ? peersIpInfoHash[key] + 1 : 1 37 peersIpInfoHash[ key ] = peersIpInfoHash[ key ] ? peersIpInfoHash[ key ] + 1 : 1
36 38
37 if (peersIpInfoHash[key] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) { 39 if (peersIpInfoHash[ key ] > TRACKER_RATE_LIMITS.ANNOUNCES_PER_IP_PER_INFOHASH) {
38 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) 40 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
39 } 41 }
40 42
41 VideoFileModel.isInfohashExists(infoHash) 43 try {
42 .then(exists => { 44 const videoFileExists = await VideoFileModel.doesInfohashExist(infoHash)
43 if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`)) 45 if (videoFileExists === true) return cb()
44 46
45 return cb() 47 const playlistExists = await VideoStreamingPlaylistModel.doesInfohashExist(infoHash)
46 }) 48 if (playlistExists === true) return cb()
49
50 return cb(new Error(`Unknown infoHash ${infoHash}`))
51 } catch (err) {
52 logger.error('Error in tracker filter.', { err })
53 return cb(err)
54 }
47 } 55 }
48}) 56})
49 57
@@ -59,16 +67,26 @@ const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
59trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) 67trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
60trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) 68trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
61 69
62function createWebsocketServer (app: express.Application) { 70function createWebsocketTrackerServer (app: express.Application) {
63 const server = http.createServer(app) 71 const server = http.createServer(app)
64 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) 72 const wss = new WebSocketServer({ noServer: true })
73
65 wss.on('connection', function (ws, req) { 74 wss.on('connection', function (ws, req) {
66 const ip = proxyAddr(req, CONFIG.TRUST_PROXY) 75 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
67 ws['ip'] = ip
68 76
69 trackerServer.onWebSocketConnection(ws) 77 trackerServer.onWebSocketConnection(ws)
70 }) 78 })
71 79
80 server.on('upgrade', (request, socket, head) => {
81 const pathname = parse(request.url).pathname
82
83 if (pathname === '/tracker/socket') {
84 wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
85 }
86
87 // Don't destroy socket, we have Socket.IO too
88 })
89
72 return server 90 return server
73} 91}
74 92
@@ -76,7 +94,7 @@ function createWebsocketServer (app: express.Application) {
76 94
77export { 95export {
78 trackerRouter, 96 trackerRouter,
79 createWebsocketServer 97 createWebsocketTrackerServer
80} 98}
81 99
82// --------------------------------------------------------------------------- 100// ---------------------------------------------------------------------------