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.ts21
1 files changed, 16 insertions, 5 deletions
diff --git a/server/controllers/tracker.ts b/server/controllers/tracker.ts
index 9bc7586d1..1deb8c402 100644
--- a/server/controllers/tracker.ts
+++ b/server/controllers/tracker.ts
@@ -6,6 +6,7 @@ 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'
9 10
10const TrackerServer = bitTorrentTracker.Server 11const TrackerServer = bitTorrentTracker.Server
11 12
@@ -59,16 +60,26 @@ const onHttpRequest = trackerServer.onHttpRequest.bind(trackerServer)
59trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' })) 60trackerRouter.get('/tracker/announce', (req, res) => onHttpRequest(req, res, { action: 'announce' }))
60trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' })) 61trackerRouter.get('/tracker/scrape', (req, res) => onHttpRequest(req, res, { action: 'scrape' }))
61 62
62function createWebsocketServer (app: express.Application) { 63function createWebsocketTrackerServer (app: express.Application) {
63 const server = http.createServer(app) 64 const server = http.createServer(app)
64 const wss = new WebSocketServer({ server: server, path: '/tracker/socket' }) 65 const wss = new WebSocketServer({ noServer: true })
66
65 wss.on('connection', function (ws, req) { 67 wss.on('connection', function (ws, req) {
66 const ip = proxyAddr(req, CONFIG.TRUST_PROXY) 68 ws['ip'] = proxyAddr(req, CONFIG.TRUST_PROXY)
67 ws['ip'] = ip
68 69
69 trackerServer.onWebSocketConnection(ws) 70 trackerServer.onWebSocketConnection(ws)
70 }) 71 })
71 72
73 server.on('upgrade', (request, socket, head) => {
74 const pathname = parse(request.url).pathname
75
76 if (pathname === '/tracker/socket') {
77 wss.handleUpgrade(request, socket, head, ws => wss.emit('connection', ws, request))
78 }
79
80 // Don't destroy socket, we have Socket.IO too
81 })
82
72 return server 83 return server
73} 84}
74 85
@@ -76,7 +87,7 @@ function createWebsocketServer (app: express.Application) {
76 87
77export { 88export {
78 trackerRouter, 89 trackerRouter,
79 createWebsocketServer 90 createWebsocketTrackerServer
80} 91}
81 92
82// --------------------------------------------------------------------------- 93// ---------------------------------------------------------------------------