2015-12-01 03:01:26 +00:00
|
|
|
trackers = [
|
2016-02-02 17:20:39 +00:00
|
|
|
'wss://tracker.btorrent.xyz'
|
|
|
|
'wss://tracker.webtorrent.io'
|
|
|
|
'wss://tracker.openwebtorrent.com'
|
2015-12-01 03:01:26 +00:00
|
|
|
]
|
|
|
|
|
2015-12-08 15:13:17 +00:00
|
|
|
opts = {
|
|
|
|
announce: trackers
|
|
|
|
}
|
2015-12-01 03:01:26 +00:00
|
|
|
|
2015-12-08 16:29:19 +00:00
|
|
|
rtcConfig = {
|
|
|
|
"iceServers":[
|
2016-01-02 05:58:17 +00:00
|
|
|
{"url":"stun:23.21.150.121","urls":"stun:23.21.150.121"}
|
|
|
|
{"url":"stun:stun.l.google.com:19302","urls":"stun:stun.l.google.com:19302"}
|
2015-12-08 16:29:19 +00:00
|
|
|
{
|
2016-01-02 05:58:17 +00:00
|
|
|
"url":"turn:global.turn.twilio.com:3478?transport=udp"
|
|
|
|
"urls":"turn:global.turn.twilio.com:3478?transport=udp"
|
|
|
|
"username":"857315a4616be37252127d4ff924c3a3536dd3fa729b56206dfa0e6808a80478"
|
2015-12-08 16:29:19 +00:00
|
|
|
"credential":"EEEr7bxx8umMHC4sOoWDC/4MxU/4JCfL+W7KeSJEsBQ="
|
2016-01-02 05:58:17 +00:00
|
|
|
}
|
2015-12-08 16:29:19 +00:00
|
|
|
{
|
2016-01-02 05:58:17 +00:00
|
|
|
"url": "turn:numb.viagenie.ca"
|
|
|
|
"urls": "turn:numb.viagenie.ca"
|
|
|
|
"credential": "webrtcdemo"
|
2015-12-08 16:29:19 +00:00
|
|
|
"username": "louis%40mozilla.com"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2016-01-02 05:58:17 +00:00
|
|
|
debug = window.localStorage.getItem('debug')?
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2015-12-30 05:00:35 +00:00
|
|
|
dbg = (string, item, color) ->
|
2015-12-08 15:13:17 +00:00
|
|
|
color = if color? then color else '#333333'
|
2016-01-02 05:58:17 +00:00
|
|
|
if debug
|
2015-12-30 05:00:35 +00:00
|
|
|
if item? && item.name
|
|
|
|
console.debug '%cβTorrent:' + (if item.infoHash? then 'torrent ' else 'torrent ' + item._torrent.name + ':file ') + item.name + (if item.infoHash? then ' (' + item.infoHash + ')' else '') + ' %c' + string, 'color: #33C3F0', 'color: ' + color
|
2015-12-03 01:05:27 +00:00
|
|
|
else
|
2015-12-08 15:13:17 +00:00
|
|
|
console.debug '%cβTorrent:client %c' + string, 'color: #33C3F0', 'color: ' + color
|
2015-12-03 01:05:27 +00:00
|
|
|
|
2016-01-02 08:27:43 +00:00
|
|
|
er = (err, item) ->
|
|
|
|
dbg err, item, '#FF0000'
|
2015-12-08 15:13:17 +00:00
|
|
|
|
2016-01-02 05:58:17 +00:00
|
|
|
client = new WebTorrent {rtcConfig: rtcConfig}
|
2016-01-04 06:20:11 +00:00
|
|
|
scope = null
|
2016-01-02 05:58:17 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
app = angular.module 'BTorrent', ['ngRoute', 'ui.grid', 'ui.grid.resizeColumns', 'ui.grid.selection', 'ngFileUpload', 'ngNotify'], ['$compileProvider','$locationProvider', '$routeProvider', ($compileProvider, $locationProvider, $routeProvider) ->
|
2015-11-30 18:34:09 +00:00
|
|
|
$compileProvider.aHrefSanitizationWhitelist /^\s*(https?|magnet|blob|javascript):/
|
|
|
|
$locationProvider.html5Mode(
|
|
|
|
enabled: true
|
2016-01-02 08:27:43 +00:00
|
|
|
requireBase: false
|
|
|
|
).hashPrefix '#'
|
2016-01-04 06:20:11 +00:00
|
|
|
|
2016-02-28 05:47:19 +00:00
|
|
|
$routeProvider.when '/view',
|
|
|
|
templateUrl: 'views/view.html'
|
|
|
|
controller: 'ViewCtrl'
|
2016-01-12 05:38:42 +00:00
|
|
|
.when '/download',
|
|
|
|
templateUrl: 'views/download.html'
|
|
|
|
controller: 'DownloadCtrl'
|
2016-01-04 06:20:11 +00:00
|
|
|
.otherwise
|
2016-02-28 05:47:19 +00:00
|
|
|
templateUrl: 'views/full.html'
|
|
|
|
controller: 'FullCtrl'
|
2015-11-30 18:34:09 +00:00
|
|
|
]
|
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
app.controller 'BTorrentCtrl', ['$scope','$rootScope','$http','$log','$location', 'ngNotify', ($scope, $rootScope, $http, $log, $location, ngNotify) ->
|
|
|
|
ngNotify.config
|
2016-02-28 17:23:45 +00:00
|
|
|
duration: 5000
|
2016-01-04 06:20:11 +00:00
|
|
|
html: true
|
|
|
|
|
2016-01-02 21:49:48 +00:00
|
|
|
if !WebTorrent.WEBRTC_SUPPORT?
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.disabled = true
|
2016-01-02 08:41:37 +00:00
|
|
|
ngNotify.set 'Please use latest Chrome, Firefox or Opera', {type: 'error', sticky: true, button: false}
|
2016-01-02 08:27:43 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.client = client
|
|
|
|
scope = $rootScope
|
2015-11-30 18:34:09 +00:00
|
|
|
|
|
|
|
updateAll = ->
|
2016-01-04 06:20:11 +00:00
|
|
|
if $rootScope.client.processing
|
2015-12-03 01:05:27 +00:00
|
|
|
return
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.$apply()
|
2016-01-02 08:27:43 +00:00
|
|
|
|
2016-01-02 06:06:46 +00:00
|
|
|
setInterval updateAll, 500
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.seedFiles = (files) ->
|
2015-12-30 05:00:35 +00:00
|
|
|
if files?
|
|
|
|
if files.length == 1
|
|
|
|
dbg 'Seeding file ' + files[0].name
|
|
|
|
else
|
|
|
|
dbg 'Seeding ' + files.length + ' files'
|
|
|
|
name = prompt('Please name your torrent', 'My Awesome Torrent') || 'My Awesome Torrent'
|
|
|
|
opts.name = name
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.client.processing = true
|
|
|
|
$rootScope.client.seed files, opts, $rootScope.onSeed
|
2015-12-30 05:00:35 +00:00
|
|
|
delete opts.name
|
2016-01-02 08:27:43 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.openTorrentFile = (file) ->
|
2015-12-07 03:22:09 +00:00
|
|
|
if file?
|
2016-01-04 06:20:11 +00:00
|
|
|
dbg 'Adding torrent file ' + file.name
|
|
|
|
$rootScope.client.processing = true
|
|
|
|
$rootScope.client.add file, opts, $rootScope.onTorrent
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.client.on 'error', (err, torrent) ->
|
|
|
|
$rootScope.client.processing = false
|
2016-01-02 08:27:43 +00:00
|
|
|
ngNotify.set err, 'error'
|
2015-12-08 15:13:17 +00:00
|
|
|
er err, torrent
|
|
|
|
|
2016-02-28 05:47:19 +00:00
|
|
|
$rootScope.addMagnet = (magnet, onTorrent) ->
|
2016-01-04 06:20:11 +00:00
|
|
|
if magnet? && magnet.length > 0
|
|
|
|
dbg 'Adding magnet/hash ' + magnet
|
|
|
|
$rootScope.client.processing = true
|
2016-02-28 05:47:19 +00:00
|
|
|
$rootScope.client.add magnet, opts, onTorrent || $rootScope.onTorrent
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.destroyedTorrent = (err) ->
|
2015-11-30 18:34:09 +00:00
|
|
|
if err
|
|
|
|
throw err
|
2016-01-04 06:20:11 +00:00
|
|
|
dbg 'Destroyed torrent', $rootScope.selectedTorrent
|
|
|
|
$rootScope.selectedTorrent = null
|
|
|
|
$rootScope.client.processing = false
|
2016-01-02 08:27:43 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.changePriority = (file) ->
|
2015-12-30 05:00:35 +00:00
|
|
|
if file.priority == '-1'
|
|
|
|
dbg 'Deselected', file
|
|
|
|
file.deselect()
|
|
|
|
else
|
2016-01-04 01:13:53 +00:00
|
|
|
dbg 'Selected with priority ' + file.priority, file
|
|
|
|
file.select(file.priority)
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.onTorrent = (torrent, isSeed) ->
|
2016-01-26 06:19:22 +00:00
|
|
|
dbg torrent.magnetURI
|
2016-01-12 05:38:42 +00:00
|
|
|
torrent.safeTorrentFileURL = torrent.torrentFileBlobURL
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.fileName = torrent.name + '.torrent'
|
2016-01-04 06:20:11 +00:00
|
|
|
if !isSeed
|
2016-02-28 05:47:19 +00:00
|
|
|
dbg 'Received metadata', torrent
|
|
|
|
ngNotify.set 'Received ' + torrent.name + ' metadata'
|
2016-01-04 06:20:11 +00:00
|
|
|
if !($rootScope.selectedTorrent?)
|
|
|
|
$rootScope.selectedTorrent = torrent
|
|
|
|
$rootScope.client.processing = false
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.files.forEach (file) ->
|
|
|
|
file.getBlobURL (err, url) ->
|
|
|
|
if err
|
|
|
|
throw err
|
2015-12-01 07:52:10 +00:00
|
|
|
if isSeed
|
2016-01-04 06:20:11 +00:00
|
|
|
dbg 'Started seeding', torrent
|
|
|
|
if !($rootScope.selectedTorrent?)
|
|
|
|
$rootScope.selectedTorrent = torrent
|
|
|
|
$rootScope.client.processing = false
|
2015-11-30 18:34:09 +00:00
|
|
|
file.url = url
|
|
|
|
if !isSeed
|
2016-01-02 05:58:17 +00:00
|
|
|
dbg 'Done ', file
|
2016-01-04 06:20:11 +00:00
|
|
|
ngNotify.set '<b>' + file.name + '</b> ready for download', 'success'
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.on 'download', (chunkSize) ->
|
2016-01-04 01:13:53 +00:00
|
|
|
#if !isSeed
|
|
|
|
# dbg 'Downloaded chunk', torrent
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.on 'upload', (chunkSize) ->
|
2016-01-04 01:13:53 +00:00
|
|
|
#dbg 'Uploaded chunk', torrent
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.on 'done', ->
|
|
|
|
if !isSeed
|
|
|
|
dbg 'Done', torrent
|
2016-01-04 06:20:11 +00:00
|
|
|
ngNotify.set '<b>' + torrent.name + '</b> has finished downloading', 'success'
|
2015-11-30 18:34:09 +00:00
|
|
|
torrent.on 'wire', (wire, addr) ->
|
|
|
|
dbg 'Wire ' + addr, torrent
|
2016-01-02 08:27:43 +00:00
|
|
|
torrent.on 'error', (err) ->
|
|
|
|
er err
|
2015-12-07 03:22:09 +00:00
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$rootScope.onSeed = (torrent) ->
|
|
|
|
$rootScope.onTorrent torrent, true
|
2015-11-30 18:34:09 +00:00
|
|
|
|
2016-01-02 05:58:17 +00:00
|
|
|
dbg 'Ready'
|
2015-11-30 18:34:09 +00:00
|
|
|
]
|
|
|
|
|
2016-02-28 05:47:19 +00:00
|
|
|
app.controller 'FullCtrl', ['$scope','$rootScope','$http','$log','$location', 'ngNotify', ($scope, $rootScope, $http, $log, $location, ngNotify) ->
|
2016-01-04 06:20:11 +00:00
|
|
|
ngNotify.config
|
2016-02-28 17:23:45 +00:00
|
|
|
duration: 5000
|
2016-01-04 06:20:11 +00:00
|
|
|
html: true
|
|
|
|
|
|
|
|
$scope.addMagnet = ->
|
|
|
|
$rootScope.addMagnet($scope.torrentInput)
|
|
|
|
$scope.torrentInput = ''
|
|
|
|
|
|
|
|
$scope.columns = [
|
|
|
|
{field: 'name', cellTooltip: true, minWidth: '200'}
|
|
|
|
{field: 'length', name: 'Size', cellFilter: 'pbytes', width: '80'}
|
|
|
|
{field: 'received', displayName: 'Downloaded', cellFilter: 'pbytes', width: '135'}
|
2016-01-12 05:38:42 +00:00
|
|
|
{field: 'downloadSpeed', displayName: '↓ Speed', cellFilter: 'pbytes:1', width: '100'}
|
2016-01-04 06:20:11 +00:00
|
|
|
{field: 'progress', displayName: 'Progress', cellFilter: 'progress', width: '100'}
|
|
|
|
{field: 'timeRemaining', displayName: 'ETA', cellFilter: 'humanTime', width: '140'}
|
|
|
|
{field: 'uploaded', displayName: 'Uploaded', cellFilter: 'pbytes', width: '125'}
|
2016-01-12 05:38:42 +00:00
|
|
|
{field: 'uploadSpeed', displayName: '↑ Speed', cellFilter: 'pbytes:1', width: '100'}
|
2016-01-04 06:20:11 +00:00
|
|
|
{field: 'numPeers', displayName: 'Peers', width: '80'}
|
|
|
|
{field: 'ratio', cellFilter: 'number:2', width: '80'}
|
|
|
|
]
|
|
|
|
|
|
|
|
$scope.gridOptions =
|
|
|
|
columnDefs: $scope.columns
|
|
|
|
data: $rootScope.client.torrents
|
|
|
|
enableColumnResizing: true
|
|
|
|
enableColumnMenus: false
|
|
|
|
enableRowSelection: true
|
|
|
|
enableRowHeaderSelection: false
|
|
|
|
multiSelect: false
|
|
|
|
|
|
|
|
$scope.gridOptions.onRegisterApi = (gridApi) ->
|
|
|
|
$scope.gridApi = gridApi
|
|
|
|
gridApi.selection.on.rowSelectionChanged $scope, (row) ->
|
|
|
|
if !row.isSelected && $rootScope.selectedTorrent? && $rootScope.selectedTorrent.infoHash = row.entity.infoHash
|
|
|
|
$rootScope.selectedTorrent = null
|
|
|
|
else
|
|
|
|
$rootScope.selectedTorrent = row.entity
|
2016-02-28 05:47:19 +00:00
|
|
|
|
|
|
|
if $location.hash() != ''
|
|
|
|
$rootScope.client.processing = true
|
|
|
|
setTimeout ->
|
|
|
|
dbg 'Adding ' + $location.hash()
|
|
|
|
$rootScope.client.add $location.hash(), $rootScope.onTorrent
|
|
|
|
, 0
|
2016-01-04 06:20:11 +00:00
|
|
|
]
|
|
|
|
|
2016-01-12 05:38:42 +00:00
|
|
|
app.controller 'DownloadCtrl', ['$scope','$rootScope','$http','$log','$location', 'ngNotify', ($scope, $rootScope, $http, $log, $location, ngNotify) ->
|
|
|
|
ngNotify.config
|
2016-02-28 17:23:45 +00:00
|
|
|
duration: 5000
|
2016-01-12 05:38:42 +00:00
|
|
|
html: true
|
|
|
|
|
|
|
|
$scope.addMagnet = ->
|
|
|
|
$rootScope.addMagnet($scope.torrentInput)
|
|
|
|
$scope.torrentInput = ''
|
2016-02-28 05:47:19 +00:00
|
|
|
|
|
|
|
if $location.hash() != ''
|
|
|
|
$rootScope.client.processing = true
|
|
|
|
setTimeout ->
|
|
|
|
dbg 'Adding ' + $location.hash()
|
|
|
|
$rootScope.client.add $location.hash(), $rootScope.onTorrent
|
|
|
|
, 0
|
2016-01-12 05:38:42 +00:00
|
|
|
]
|
|
|
|
|
2016-02-28 05:47:19 +00:00
|
|
|
app.controller 'ViewCtrl', ['$scope','$rootScope','$http','$log','$location', 'ngNotify', ($scope, $rootScope, $http, $log, $location, ngNotify) ->
|
2016-01-04 06:20:11 +00:00
|
|
|
ngNotify.config
|
2016-02-28 17:23:45 +00:00
|
|
|
duration: 2000
|
2016-01-04 06:20:11 +00:00
|
|
|
html: true
|
|
|
|
|
2016-02-28 05:47:19 +00:00
|
|
|
onTorrent = (torrent, isSeed) ->
|
2016-02-28 05:59:02 +00:00
|
|
|
$rootScope.viewerStyle = {'margin-top': '-20px'}
|
2016-02-28 05:47:19 +00:00
|
|
|
dbg torrent.magnetURI
|
|
|
|
torrent.safeTorrentFileURL = torrent.torrentFileBlobURL
|
|
|
|
torrent.fileName = torrent.name + '.torrent'
|
|
|
|
$rootScope.selectedTorrent = torrent
|
|
|
|
$rootScope.client.processing = false
|
|
|
|
dbg 'Received metadata', torrent
|
|
|
|
ngNotify.set 'Received ' + torrent.name + ' metadata'
|
|
|
|
torrent.files.forEach (file) ->
|
|
|
|
file.appendTo '#viewer'
|
|
|
|
file.getBlobURL (err, url) ->
|
|
|
|
if err
|
|
|
|
throw err
|
|
|
|
file.url = url
|
|
|
|
dbg 'Done ', file
|
|
|
|
torrent.on 'download', (chunkSize) ->
|
|
|
|
# dbg 'Downloaded chunk', torrent
|
|
|
|
torrent.on 'upload', (chunkSize) ->
|
|
|
|
#dbg 'Uploaded chunk', torrent
|
|
|
|
torrent.on 'done', ->
|
|
|
|
dbg 'Done', torrent
|
|
|
|
torrent.on 'wire', (wire, addr) ->
|
|
|
|
dbg 'Wire ' + addr, torrent
|
|
|
|
torrent.on 'error', (err) ->
|
|
|
|
er err
|
|
|
|
|
2016-01-04 06:20:11 +00:00
|
|
|
$scope.addMagnet = ->
|
2016-02-28 05:47:19 +00:00
|
|
|
$rootScope.addMagnet $scope.torrentInput, onTorrent
|
2016-01-04 06:20:11 +00:00
|
|
|
$scope.torrentInput = ''
|
2016-02-28 05:47:19 +00:00
|
|
|
|
|
|
|
if $location.hash() != ''
|
|
|
|
$rootScope.client.processing = true
|
|
|
|
setTimeout ->
|
|
|
|
dbg 'Adding ' + $location.hash()
|
|
|
|
$rootScope.client.add $location.hash(), onTorrent
|
|
|
|
, 0
|
2016-01-04 06:20:11 +00:00
|
|
|
]
|
|
|
|
|
2015-11-30 18:34:09 +00:00
|
|
|
app.filter 'html', ['$sce', ($sce) ->
|
|
|
|
(input) ->
|
|
|
|
$sce.trustAsHtml input
|
2015-12-01 01:12:35 +00:00
|
|
|
return
|
2015-11-30 18:34:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
app.filter 'pbytes', ->
|
2015-12-03 01:05:27 +00:00
|
|
|
(num, speed) ->
|
2015-12-01 07:52:10 +00:00
|
|
|
if isNaN(num)
|
|
|
|
return ''
|
2015-11-30 18:34:09 +00:00
|
|
|
units = [
|
|
|
|
'B'
|
|
|
|
'kB'
|
|
|
|
'MB'
|
|
|
|
'GB'
|
|
|
|
'TB'
|
|
|
|
]
|
|
|
|
if num < 1
|
2015-12-03 01:05:27 +00:00
|
|
|
return (if speed then '' else '0 B')
|
2016-01-02 05:58:17 +00:00
|
|
|
exponent = Math.min(Math.floor(Math.log(num) / 6.907755278982137), 8)
|
2015-11-30 18:34:09 +00:00
|
|
|
num = (num / 1000 ** exponent).toFixed(1) * 1
|
|
|
|
unit = units[exponent]
|
2015-12-03 01:05:27 +00:00
|
|
|
num + ' ' + unit + (if speed then '/s' else '')
|
|
|
|
|
|
|
|
app.filter 'humanTime', ->
|
|
|
|
(millis) ->
|
|
|
|
if millis < 1000
|
|
|
|
return ''
|
2016-01-02 05:58:17 +00:00
|
|
|
remaining = moment.duration(millis).humanize()
|
2015-12-03 01:05:27 +00:00
|
|
|
remaining[0].toUpperCase() + remaining.substr(1)
|
|
|
|
|
|
|
|
app.filter 'progress', ->
|
|
|
|
(num) ->
|
2016-01-24 11:50:24 +00:00
|
|
|
(100 * num).toFixed(1) + '%'
|