This repository was archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 969
/
Copy pathwebtorrent.js
115 lines (103 loc) · 3.41 KB
/
webtorrent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const electron = require('electron')
const ipc = electron.ipcMain
const appUrlUtil = require('../../js/lib/appUrlUtil')
const messages = require('../../js/constants/messages')
const Filtering = require('../filtering')
// Set to see communication between WebTorrent and torrent viewer tabs
const DEBUG_IPC = false
if (DEBUG_IPC) console.log('WebTorrent IPC debugging enabled')
var VIEWER_URL = appUrlUtil.getTorrentExtUrl('webtorrent.html')
function getViewerURL (torrentUrl) {
return VIEWER_URL + '#' + encodeURIComponent(torrentUrl)
}
// Connects to the BitTorrent network
// Communicates with the WebTorrentRemoteClients via message passing
let server = null
let channels = {}
// Receive messages via the window process, ultimately from the UI in a <webview> process
function init (state, action) {
if (DEBUG_IPC) console.log('WebTorrent IPC init')
ipc.on(messages.TORRENT_MESSAGE, function (e, msg) {
if (server === null) {
const WebTorrentRemoteServer = require('webtorrent-remote/server')
server = new WebTorrentRemoteServer(send, {trace: DEBUG_IPC})
}
if (DEBUG_IPC) console.log('Received IPC: ' + JSON.stringify(msg))
channels[msg.clientKey] = e.sender
server.receive(msg)
})
setupFiltering()
return state
}
// Send messages from the browser process (here), thru the window process, to the <webview>
function send (msg) {
if (DEBUG_IPC) console.log('Sending IPC: ' + JSON.stringify(msg))
const channel = channels[msg.clientKey]
if (!channel) {
if (DEBUG_IPC) console.error('Ignoring unrecognized clientKey ' + msg.clientKey)
return
}
if (channel.isDestroyed()) {
if (DEBUG_IPC) console.log('Removing destroyed channel, clientKey ' + msg.clientKey)
delete channels[msg.clientKey]
return
}
channel.send(messages.TORRENT_MESSAGE, msg)
}
function setupFiltering () {
Filtering.registerHeadersReceivedFilteringCB(function (details, isPrivate) {
if (details.method !== 'GET') {
return {}
}
if (!isTorrentFile(details)) {
return {}
}
var viewerUrl = getViewerURL(details.url)
return {
responseHeaders: {
'Location': [ viewerUrl ]
},
statusLine: 'HTTP/1.1 301 Moved Permanently',
resourceName: 'webtorrent'
}
})
}
/**
* Check if the request is a torrent file.
* @param {Object} details First argument of the webRequest.onHeadersReceived
* event. The properties "responseHeaders" and "url"
* are read.
* @return {boolean} True if the resource is a torrent file.
*/
function isTorrentFile (details) {
var header = getHeader(details.responseHeaders, 'content-type')
if (header) {
var headerValue = header.toLowerCase().split(';', 1)[0].trim()
if (headerValue === 'application/x-bittorrent') {
return true
}
if (headerValue === 'application/octet-stream') {
if (details.url.toLowerCase().indexOf('.torrent') > 0) {
return true
}
var cdHeader =
getHeader(details.responseHeaders, 'content-disposition')
if (cdHeader && /\.torrent(["']|$)/i.test(cdHeader)) {
return true
}
}
}
return false
}
function getHeader (headers, headerName) {
var headerNames = Object.keys(headers)
for (var i = 0; i < headerNames.length; ++i) {
if (headerNames[i].toLowerCase() === headerName) {
return headers[headerNames[i]][0]
}
}
}
module.exports = {
init,
resourceName: 'webtorrent'
}