diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/controllers/tracker.ts | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/controllers/tracker.ts')
-rw-r--r-- | server/controllers/tracker.ts | 46 |
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' | |||
6 | import { Server as WebSocketServer } from 'ws' | 6 | import { Server as WebSocketServer } from 'ws' |
7 | import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants' | 7 | import { CONFIG, TRACKER_RATE_LIMITS } from '../initializers/constants' |
8 | import { VideoFileModel } from '../models/video/video-file' | 8 | import { VideoFileModel } from '../models/video/video-file' |
9 | import { parse } from 'url' | ||
10 | import { VideoStreamingPlaylistModel } from '../models/video/video-streaming-playlist' | ||
9 | 11 | ||
10 | const TrackerServer = bitTorrentTracker.Server | 12 | const 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) | |||
59 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) | 67 | trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) |
60 | trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) | 68 | trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) |
61 | 69 | ||
62 | function createWebsocketServer (app: express.Application) { | 70 | function 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 | ||
77 | export { | 95 | export { |
78 | trackerRouter, | 96 | trackerRouter, |
79 | createWebsocketServer | 97 | createWebsocketTrackerServer |
80 | } | 98 | } |
81 | 99 | ||
82 | // --------------------------------------------------------------------------- | 100 | // --------------------------------------------------------------------------- |