This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (172 loc) · 4.57 KB
/
index.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
module.exports = PeerDB
var concat = require('simple-concat')
var crypto = require('./lib/crypto')
var get = require('simple-get')
var magnet = require('magnet-uri')
var once = require('once')
var parallel = require('run-parallel')
var pump = require('pump')
var stream = require('readable-stream')
var toStream = require('./lib/to-stream')
var WebTorrent = require('webtorrent')
var ANNOUNCE = 'wss://tracker.btorrent.xyz'
var TIMEOUT = 20000
var UPLOAD_URL = 'http://peerdb.io/uploads/'
function PeerDB () {
this.destroyed = false
this._client = new WebTorrent({
dht: false
})
}
PeerDB._getDefaultInstance = function () {
if (!PeerDB._defaultInstance) {
PeerDB._defaultInstance = new PeerDB()
}
return PeerDB._defaultInstance
}
PeerDB.put = function (value, cb) {
var db = PeerDB._getDefaultInstance()
db.put(value, cb)
}
PeerDB.get = function (key, cb) {
var db = PeerDB._getDefaultInstance()
db.get(key, cb)
}
PeerDB.del = function (key, cb) {
var db = PeerDB._getDefaultInstance()
db.del(key, cb)
}
PeerDB.close = function (cb) {
var db = PeerDB._getDefaultInstance()
PeerDB._defaultInstance = null
db.close(cb)
}
PeerDB.prototype.put = function (value, cb) {
this._checkOpen()
if (!cb) cb = noop
cb = once(cb)
var opts = {
announce: ANNOUNCE,
name: 'PeerDB User Data'
}
var valueStream = toStream(value)
var cipher = crypto.createCipher()
var cipherStream = pump(valueStream, cipher, onError)
var torrent = this._client.seed(cipherStream, opts)
torrent.once('error', onError)
torrent.once('seed', onSeed)
function onSeed () {
torrent.files[0].getBuffer(function (err, buf) {
if (err) return cb(err)
parallel([
// Upload torrent data
function (cb) {
get.concat({
method: 'POST',
body: buf,
url: UPLOAD_URL + '?key=' + torrent.infoHash
}, function (err, res) {
if (err) return cb(err)
if (res.statusCode !== 200) {
return cb(new Error('PeerDB: Upload failed with http response: ' + res.statusCode))
}
cb(null)
})
},
// Upload torrent metadata (.torrent file)
function (cb) {
get.concat({
method: 'POST',
body: torrent.torrentFile,
url: UPLOAD_URL + '?key=' + torrent.infoHash + '&torrent=true'
}, function (err, res) {
if (err) return cb(err)
if (res.statusCode !== 200) {
return cb(new Error('PeerDB: Upload failed with http response: ' + res.statusCode))
}
cb(null)
})
}
], function (err) {
cb(err, torrent.infoHash + ':' + cipher.key.toString('hex'))
})
})
}
function onError (err) {
if (err) cb(err)
}
}
PeerDB.prototype.get = function (key, cb) {
this._checkOpen()
if (!cb) cb = noop
cb = once(cb)
var parts = key.split(':')
key = parts[0]
var cipherKey = parts[1]
if (cipherKey) {
cipherKey = Buffer.from(cipherKey, 'hex')
}
var torrent = this._client.get(key)
if (!torrent) {
var opts = {
infoHash: key,
announce: ANNOUNCE,
urlList: UPLOAD_URL + key,
xs: UPLOAD_URL + key + '.torrent'
}
torrent = this._client.add(magnet.encode(opts), opts)
torrent.once('error', function (err) {
cb(err)
})
}
if (torrent.ready) {
onReady(torrent)
} else {
torrent.once('ready', function () {
onReady(torrent)
})
}
var timeout = setTimeout(function () {
cb(new Error('PeerDB: key "' + key + '" not found'))
}, TIMEOUT)
function onReady (torrent) {
torrent.files[0].getBuffer(function (err, cipherText) {
clearTimeout(timeout)
if (err) return cb(err)
var readable = stream.PassThrough()
readable.end(cipherText)
var decipher = crypto.createDecipher(cipherKey)
var decipherStream = pump(readable, decipher, onError)
concat(decipherStream, cb)
})
}
function onError (err) {
if (err) cb(err)
}
}
PeerDB.prototype.del = function (key, cb) {
this._checkOpen()
if (!cb) cb = noop
cb = once(cb)
this._client.remove(key)
// TODO: delete from central server
process.nextTick(function () {
cb(null)
})
}
PeerDB.prototype.close = function (cb) {
this._checkOpen()
if (!cb) cb = noop
cb = once(cb)
this._client.destroy(function (err) {
cb(err)
})
this.destroyed = true
this._client = null
}
PeerDB.prototype._checkOpen = function () {
if (this.destroyed) {
throw new Error('PeerDB: database is closed')
}
}
function noop () {}