aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-14 11:00:03 +0200
committerChocobozzz <me@florianbigard.com>2018-08-14 11:00:03 +0200
commitcc43831a90ab8b1b460cd7021c100190049ec64b (patch)
tree187f3602fd8749b66d5782174ba48f8a33453d7d /server
parent08c11bec43987364e83246e4e76958f5ccf89fe8 (diff)
downloadPeerTube-cc43831a90ab8b1b460cd7021c100190049ec64b.tar.gz
PeerTube-cc43831a90ab8b1b460cd7021c100190049ec64b.tar.zst
PeerTube-cc43831a90ab8b1b460cd7021c100190049ec64b.zip
Filter tracker based on infohash
Diffstat (limited to 'server')
-rw-r--r--server/controllers/tracker.ts8
-rw-r--r--server/models/video/video-file.ts15
-rw-r--r--server/tests/api/index-fast.ts1
-rw-r--r--server/tests/api/server/tracker.ts71
4 files changed, 94 insertions, 1 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
index 42f5aea81..9bc7586d1 100644
--- a/server/controllers/tracker.ts
+++ b/server/controllers/tracker.ts
@@ -5,6 +5,7 @@ import * as bitTorrentTracker from 'bittorrent-tracker'
5import * as proxyAddr from 'proxy-addr' 5import * 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'
8 9
9const TrackerServer = bitTorrentTracker.Server 10const TrackerServer = bitTorrentTracker.Server
10 11
@@ -37,7 +38,12 @@ const trackerServer = new TrackerServer({
37 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`)) 38 return cb(new Error(`Too many requests (${peersIpInfoHash[ key ]} of ip ${ip} for torrent ${infoHash}`))
38 } 39 }
39 40
40 return cb() 41 VideoFileModel.isInfohashExists(infoHash)
42 .then(exists => {
43 if (exists === false) return cb(new Error(`Unknown infoHash ${infoHash}`))
44
45 return cb()
46 })
41 } 47 }
42}) 48})
43 49
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts
index f5a2b6c1f..3bc4855f3 100644
--- a/server/models/video/video-file.ts
+++ b/server/models/video/video-file.ts
@@ -9,6 +9,7 @@ import {
9import { CONSTRAINTS_FIELDS } from '../../initializers' 9import { CONSTRAINTS_FIELDS } from '../../initializers'
10import { throwIfNotValid } from '../utils' 10import { throwIfNotValid } from '../utils'
11import { VideoModel } from './video' 11import { VideoModel } from './video'
12import * as Sequelize from 'sequelize'
12 13
13@Table({ 14@Table({
14 tableName: 'videoFile', 15 tableName: 'videoFile',
@@ -68,4 +69,18 @@ export class VideoFileModel extends Model<VideoFileModel> {
68 onDelete: 'CASCADE' 69 onDelete: 'CASCADE'
69 }) 70 })
70 Video: VideoModel 71 Video: VideoModel
72
73 static isInfohashExists (infoHash: string) {
74 const query = 'SELECT 1 FROM "videoFile" WHERE "infoHash" = $infoHash LIMIT 1'
75 const options = {
76 type: Sequelize.QueryTypes.SELECT,
77 bind: { infoHash },
78 raw: true
79 }
80
81 return VideoModel.sequelize.query(query, options)
82 .then(results => {
83 return results.length === 1
84 })
85 }
71} 86}
diff --git a/server/tests/api/index-fast.ts b/server/tests/api/index-fast.ts
index 531a09b82..02ffdd4f1 100644
--- a/server/tests/api/index-fast.ts
+++ b/server/tests/api/index-fast.ts
@@ -15,3 +15,4 @@ import './server/email'
15import './server/config' 15import './server/config'
16import './server/reverse-proxy' 16import './server/reverse-proxy'
17import './search/search-videos' 17import './search/search-videos'
18import './server/tracker'
diff --git a/server/tests/api/server/tracker.ts b/server/tests/api/server/tracker.ts
new file mode 100644
index 000000000..5d61c1558
--- /dev/null
+++ b/server/tests/api/server/tracker.ts
@@ -0,0 +1,71 @@
1/* tslint:disable:no-unused-expression */
2
3import * as magnetUtil from 'magnet-uri'
4import * as chai from 'chai'
5import 'mocha'
6import { getVideo, killallServers, runServer, ServerInfo, uploadVideo } from '../../utils'
7import { flushTests, setAccessTokensToServers } from '../../utils/index'
8import { VideoDetails } from '../../../../shared/models/videos'
9import * as WebTorrent from 'webtorrent'
10
11describe('Test tracker', function () {
12 let server: ServerInfo
13 let badMagnet: string
14 let goodMagnet: string
15
16 before(async function () {
17 this.timeout(60000)
18
19 await flushTests()
20 server = await runServer(1)
21 await setAccessTokensToServers([ server ])
22
23 {
24 const res = await uploadVideo(server.url, server.accessToken, {})
25 const videoUUID = res.body.video.uuid
26
27 const resGet = await getVideo(server.url, videoUUID)
28 const video: VideoDetails = resGet.body
29 goodMagnet = video.files[0].magnetUri
30
31 const parsed = magnetUtil.decode(goodMagnet)
32 parsed.infoHash = '010597bb88b1968a5693a4fa8267c592ca65f2e9'
33
34 badMagnet = magnetUtil.encode(parsed)
35 }
36 })
37
38 it('Should return an error when adding an incorrect infohash', done => {
39 this.timeout(10000)
40 const webtorrent = new WebTorrent()
41
42 const torrent = webtorrent.add(badMagnet)
43
44 torrent.on('error', done)
45 torrent.on('warning', warn => {
46 const message = typeof warn === 'string' ? warn : warn.message
47 if (message.indexOf('Unknown infoHash ') !== -1) return done()
48 })
49
50 torrent.on('done', () => done(new Error('No error on infohash')))
51 })
52
53 it('Should succeed with the correct infohash', done => {
54 this.timeout(10000)
55 const webtorrent = new WebTorrent()
56
57 const torrent = webtorrent.add(goodMagnet)
58
59 torrent.on('error', done)
60 torrent.on('warning', warn => {
61 const message = typeof warn === 'string' ? warn : warn.message
62 if (message.indexOf('Unknown infoHash ') !== -1) return done(new Error('Error on infohash'))
63 })
64
65 torrent.on('done', done)
66 })
67
68 after(async function () {
69 killallServers([ server ])
70 })
71})