diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-07 10:10:00 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-07 10:10:00 +0200 |
commit | aff36eb063573532498979c83be1c279c84f2d53 (patch) | |
tree | 3f79dc7373689f0493e80a6e501225fd0cd7aa26 /server/lib | |
parent | c3117f13d599f37693f937865b6e84eb1f72d5c4 (diff) | |
parent | a6375e69668ea42e19531c6bc68dcd37f3f7cbd7 (diff) | |
download | PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.gz PeerTube-aff36eb063573532498979c83be1c279c84f2d53.tar.zst PeerTube-aff36eb063573532498979c83be1c279c84f2d53.zip |
Merge branch 'webseed'
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/webtorrent-process.js | 92 | ||||
-rw-r--r-- | server/lib/webtorrent.js | 160 |
2 files changed, 0 insertions, 252 deletions
diff --git a/server/lib/webtorrent-process.js b/server/lib/webtorrent-process.js deleted file mode 100644 index be7ac5bb4..000000000 --- a/server/lib/webtorrent-process.js +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const WebTorrent = require('webtorrent') | ||
4 | const ipc = require('node-ipc') | ||
5 | |||
6 | function webtorrent (args) { | ||
7 | if (args.length !== 3) { | ||
8 | throw new Error('Wrong arguments number: ' + args.length + '/3') | ||
9 | } | ||
10 | |||
11 | const host = args[1] | ||
12 | const port = args[2] | ||
13 | const nodeKey = 'webtorrentnode' + port | ||
14 | const processKey = 'webtorrentprocess' + port | ||
15 | |||
16 | ipc.config.silent = true | ||
17 | ipc.config.id = processKey | ||
18 | |||
19 | if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] | ||
20 | else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' | ||
21 | const wt = new WebTorrent({ dht: false }) | ||
22 | |||
23 | function seed (data) { | ||
24 | const args = data.args | ||
25 | const path = args.path | ||
26 | const _id = data._id | ||
27 | |||
28 | wt.seed(path, { announceList: '' }, function (torrent) { | ||
29 | const toSend = { | ||
30 | magnetUri: torrent.magnetURI | ||
31 | } | ||
32 | |||
33 | ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend) | ||
34 | }) | ||
35 | } | ||
36 | |||
37 | function add (data) { | ||
38 | const args = data.args | ||
39 | const magnetUri = args.magnetUri | ||
40 | const _id = data._id | ||
41 | |||
42 | wt.add(magnetUri, function (torrent) { | ||
43 | const toSend = { | ||
44 | files: [] | ||
45 | } | ||
46 | |||
47 | torrent.files.forEach(function (file) { | ||
48 | toSend.files.push({ path: file.path }) | ||
49 | }) | ||
50 | |||
51 | ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend) | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | function remove (data) { | ||
56 | const args = data.args | ||
57 | const magnetUri = args.magnetUri | ||
58 | const _id = data._id | ||
59 | |||
60 | try { | ||
61 | wt.remove(magnetUri, callback) | ||
62 | } catch (err) { | ||
63 | console.log('Cannot remove the torrent from WebTorrent.') | ||
64 | return callback(null) | ||
65 | } | ||
66 | |||
67 | function callback () { | ||
68 | const toSend = {} | ||
69 | ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend) | ||
70 | } | ||
71 | } | ||
72 | |||
73 | console.log('Configuration: ' + host + ':' + port) | ||
74 | console.log('Connecting to IPC...') | ||
75 | |||
76 | ipc.connectTo(nodeKey, function () { | ||
77 | ipc.of[nodeKey].on(processKey + '.seed', seed) | ||
78 | ipc.of[nodeKey].on(processKey + '.add', add) | ||
79 | ipc.of[nodeKey].on(processKey + '.remove', remove) | ||
80 | |||
81 | ipc.of[nodeKey].emit(processKey + '.ready') | ||
82 | console.log('Ready.') | ||
83 | }) | ||
84 | |||
85 | process.on('uncaughtException', function (e) { | ||
86 | ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() }) | ||
87 | }) | ||
88 | } | ||
89 | |||
90 | // --------------------------------------------------------------------------- | ||
91 | |||
92 | module.exports = webtorrent | ||
diff --git a/server/lib/webtorrent.js b/server/lib/webtorrent.js deleted file mode 100644 index dde046828..000000000 --- a/server/lib/webtorrent.js +++ /dev/null | |||
@@ -1,160 +0,0 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const ipc = require('node-ipc') | ||
4 | const pathUtils = require('path') | ||
5 | const spawn = require('electron-spawn') | ||
6 | |||
7 | const constants = require('../initializers/constants') | ||
8 | const logger = require('../helpers/logger') | ||
9 | |||
10 | let host = constants.CONFIG.WEBSERVER.HOST | ||
11 | let port = constants.CONFIG.WEBSERVER.PORT | ||
12 | let nodeKey = 'webtorrentnode' + port | ||
13 | let processKey = 'webtorrentprocess' + port | ||
14 | ipc.config.silent = true | ||
15 | ipc.config.id = nodeKey | ||
16 | |||
17 | const webtorrent = { | ||
18 | add: add, | ||
19 | app: null, // Pid of the app | ||
20 | create: create, | ||
21 | remove: remove, | ||
22 | seed: seed, | ||
23 | silent: false // Useful for beautiful tests | ||
24 | } | ||
25 | |||
26 | function create (options, callback) { | ||
27 | if (typeof options === 'function') { | ||
28 | callback = options | ||
29 | options = {} | ||
30 | } | ||
31 | |||
32 | // Override options | ||
33 | if (options.host) host = options.host | ||
34 | if (options.port) { | ||
35 | port = options.port | ||
36 | nodeKey = 'webtorrentnode' + port | ||
37 | processKey = 'webtorrentprocess' + port | ||
38 | ipc.config.id = nodeKey | ||
39 | } | ||
40 | |||
41 | ipc.serve(function () { | ||
42 | if (!webtorrent.silent) logger.info('IPC server ready.') | ||
43 | |||
44 | // Run a timeout of 30s after which we exit the process | ||
45 | const timeoutWebtorrentProcess = setTimeout(function () { | ||
46 | throw new Error('Timeout : cannot run the webtorrent process. Please ensure you have electron npm package installed with xvfb-run.') | ||
47 | }, 30000) | ||
48 | |||
49 | ipc.server.on(processKey + '.ready', function () { | ||
50 | if (!webtorrent.silent) logger.info('Webtorrent process ready.') | ||
51 | clearTimeout(timeoutWebtorrentProcess) | ||
52 | callback() | ||
53 | }) | ||
54 | |||
55 | ipc.server.on(processKey + '.exception', function (data) { | ||
56 | throw new Error('Received exception error from webtorrent process : ' + data.exception) | ||
57 | }) | ||
58 | |||
59 | const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true }) | ||
60 | |||
61 | if (constants.CONFIG.ELECTRON.DEBUG === true) { | ||
62 | webtorrentProcess.stderr.on('data', function (data) { | ||
63 | logger.debug('Webtorrent process stderr: ', data.toString()) | ||
64 | }) | ||
65 | |||
66 | webtorrentProcess.stdout.on('data', function (data) { | ||
67 | logger.debug('Webtorrent process:', data.toString()) | ||
68 | }) | ||
69 | } | ||
70 | |||
71 | webtorrent.app = webtorrentProcess | ||
72 | }) | ||
73 | |||
74 | ipc.server.start() | ||
75 | } | ||
76 | |||
77 | function seed (path, callback) { | ||
78 | const extension = pathUtils.extname(path) | ||
79 | const basename = pathUtils.basename(path, extension) | ||
80 | const data = { | ||
81 | _id: basename, | ||
82 | args: { | ||
83 | path: path | ||
84 | } | ||
85 | } | ||
86 | |||
87 | if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) | ||
88 | |||
89 | // Finish signal | ||
90 | const eventKey = nodeKey + '.seedDone.' + data._id | ||
91 | ipc.server.on(eventKey, function listener (received) { | ||
92 | if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) | ||
93 | |||
94 | // This is a fake object, we just use the magnetUri in this project | ||
95 | const torrent = { | ||
96 | magnetURI: received.magnetUri | ||
97 | } | ||
98 | |||
99 | ipc.server.off(eventKey, '*') | ||
100 | callback(torrent) | ||
101 | }) | ||
102 | |||
103 | ipc.server.broadcast(processKey + '.seed', data) | ||
104 | } | ||
105 | |||
106 | function add (magnetUri, callback) { | ||
107 | const data = { | ||
108 | _id: magnetUri, | ||
109 | args: { | ||
110 | magnetUri: magnetUri | ||
111 | } | ||
112 | } | ||
113 | |||
114 | if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) | ||
115 | |||
116 | // Finish signal | ||
117 | const eventKey = nodeKey + '.addDone.' + data._id | ||
118 | ipc.server.on(eventKey, function (received) { | ||
119 | if (!webtorrent.silent) logger.debug('Process added torrent.') | ||
120 | |||
121 | // This is a fake object, we just use the magnetUri in this project | ||
122 | const torrent = { | ||
123 | files: received.files | ||
124 | } | ||
125 | |||
126 | ipc.server.off(eventKey, '*') | ||
127 | callback(torrent) | ||
128 | }) | ||
129 | |||
130 | ipc.server.broadcast(processKey + '.add', data) | ||
131 | } | ||
132 | |||
133 | function remove (magnetUri, callback) { | ||
134 | const data = { | ||
135 | _id: magnetUri, | ||
136 | args: { | ||
137 | magnetUri: magnetUri | ||
138 | } | ||
139 | } | ||
140 | |||
141 | if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) | ||
142 | |||
143 | // Finish signal | ||
144 | const eventKey = nodeKey + '.removeDone.' + data._id | ||
145 | ipc.server.on(eventKey, function (received) { | ||
146 | if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) | ||
147 | |||
148 | let err = null | ||
149 | if (received.err) err = received.err | ||
150 | |||
151 | ipc.server.off(eventKey, '*') | ||
152 | callback(err) | ||
153 | }) | ||
154 | |||
155 | ipc.server.broadcast(processKey + '.remove', data) | ||
156 | } | ||
157 | |||
158 | // --------------------------------------------------------------------------- | ||
159 | |||
160 | module.exports = webtorrent | ||