2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const WebTorrent = require('webtorrent')
|
|
|
|
const ipc = require('node-ipc')
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 07:12:32 -05:00
|
|
|
function webtorrent (args) {
|
2016-02-07 05:23:23 -05:00
|
|
|
if (args.length !== 3) {
|
2016-02-07 06:01:40 -05:00
|
|
|
throw new Error('Wrong arguments number: ' + args.length + '/3')
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const host = args[1]
|
|
|
|
const port = args[2]
|
|
|
|
const nodeKey = 'webtorrentnode' + port
|
|
|
|
const processKey = 'webtorrentprocess' + port
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.config.silent = true
|
|
|
|
ipc.config.id = processKey
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
|
|
|
|
else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
|
2016-03-16 17:29:27 -04:00
|
|
|
const wt = new WebTorrent({ dht: false })
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function seed (data) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const args = data.args
|
|
|
|
const path = args.path
|
|
|
|
const _id = data._id
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
wt.seed(path, { announceList: '' }, function (torrent) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const to_send = {
|
2016-02-07 05:23:23 -05:00
|
|
|
magnetUri: torrent.magnetURI
|
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
|
|
|
|
})
|
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function add (data) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const args = data.args
|
|
|
|
const magnetUri = args.magnetUri
|
|
|
|
const _id = data._id
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
wt.add(magnetUri, function (torrent) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const to_send = {
|
2016-02-07 05:23:23 -05:00
|
|
|
files: []
|
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
torrent.files.forEach(function (file) {
|
|
|
|
to_send.files.push({ path: file.path })
|
2016-02-06 10:22:39 -05:00
|
|
|
})
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
|
|
|
|
})
|
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function remove (data) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const args = data.args
|
|
|
|
const magnetUri = args.magnetUri
|
|
|
|
const _id = data._id
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
try {
|
|
|
|
wt.remove(magnetUri, callback)
|
|
|
|
} catch (err) {
|
|
|
|
console.log('Cannot remove the torrent from WebTorrent.')
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
|
|
|
|
function callback () {
|
2016-03-16 17:29:27 -04:00
|
|
|
const to_send = {}
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
|
2016-02-06 10:22:39 -05:00
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
console.log('Configuration: ' + host + ':' + port)
|
|
|
|
console.log('Connecting to IPC...')
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.connectTo(nodeKey, function () {
|
|
|
|
ipc.of[nodeKey].on(processKey + '.seed', seed)
|
|
|
|
ipc.of[nodeKey].on(processKey + '.add', add)
|
|
|
|
ipc.of[nodeKey].on(processKey + '.remove', remove)
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
ipc.of[nodeKey].emit(processKey + '.ready')
|
|
|
|
console.log('Ready.')
|
|
|
|
})
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
process.on('uncaughtException', function (e) {
|
2016-03-19 04:08:10 -04:00
|
|
|
ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() })
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-06 10:22:39 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = webtorrent
|