-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·306 lines (275 loc) · 9.35 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env node
const express = require('express')
const app = express()
app.get('/', (req, res) => {
const peer0 = require('peer0')
console.log('peer0 test', peer0.hello())
console.log('triple 3', peer0.triple(3))
res.send('Are you looking for ZeroNet?')
})
app.get('/:path', (req, res) => {
const path = req.params.path
console.log('path', path)
res.send('Are you looking for ' + path)
})
app.listen(43110, () => console.log('Listening on ZeroNet default [43110]'))
function _announce() {
/* Localize this. */
const self = this
console.log('Announcing...')
const Client = require('zeronet-tracker')
const crypto = require('crypto')
let shasum = crypto.createHash('sha1')
shasum.update('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
const infoHash = shasum.digest('hex')
console.log('Info hash', infoHash)
var requiredOpts = {
infoHash,
// infoHash: Buffer.from('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D'),
// infoHash: Buffer.from('37826C0C2ECC81F06B39C124DDA88E00E9D559D1'),
peerId: this.state.peerId,
announce: [
// 'udp://tracker.coppersurfer.tk:6969',
'udp://tracker.leechers-paradise.org:6969'
],
port: 6881
}
// var optionalOpts = {
// getAnnounceOpts: function () {
// // Provide a callback that will be called whenever announce() is called
// // internally (on timer), or by the user
// return {
// uploaded: 0,
// downloaded: 0,
// left: 0,
// customParam: 'blah' // custom parameters supported
// }
// },
//
// // RTCPeerConnection config object (only used in browser)
// rtcConfig: {},
//
// // User-Agent header for http requests
// userAgent: '',
//
// // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
// wrtc: {},
// }
var client = new Client(requiredOpts)
client.on('error', function (err) {
// fatal client error!
console.log(err.message)
})
client.on('warning', function (err) {
// a tracker was unavailable or sent bad data to the client. you can probably ignore it
console.log(err.message)
})
// start getting peers from the tracker
client.start()
client.on('update', function (data) {
console.log('got an announce response from tracker: ', data.announce)
console.log('number of seeders vs. leechers in the swarm: ', data.complete, data.incomplete)
})
client.once('peer', function (_address) {
console.log('found a peer: ' + _address) // 85.10.239.191:48623
const ipAddress = _address.split(':')[0]
const port = _address.split(':')[1]
const peer = { ipAddress, port }
self.hostIp = ipAddress
self.hostPort = port
self._addPeerToDb(infoHash, peer)
})
// announce that download has completed (and you are now a seeder)
// client.complete()
// force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
// client.update()
// provide parameters to the tracker
// client.update({
// uploaded: 0,
// downloaded: 0,
// left: 0,
// customParam: 'blah' // custom parameters supported
// })
// stop getting peers from the tracker, gracefully leave the swarm
// client.stop()
// ungracefully leave the swarm (without sending final 'stop' message)
// client.destroy()
// scrape
client.scrape()
client.on('scrape', function (data) {
console.log('got a scrape response from tracker: ', data.announce)
console.log('number of seeders vs. leechers in the swarm: ', data.complete, data.incomplete)
// console.log('number of total downloads of this torrent: ', data.downloaded)
})
}
// _initPeer() {
// /* Localize this. */
// const self = this
//
// /* Initialize netcat client module. */
// const NetcatClient = require('netcat/client')
// this.client = new NetcatClient()
//
// /* Initialize peer id. */
// const peerId = '-UT3530-FFFFFFFFFFFF'
// // const peerid = require('peerid')
// // this.peerId = peerid()
//
// this.client.on('connect', function () {
// console.info('Connection opened.')
//
// /* Create encoded package. */
// const pkg = self._encode(self._handshakePkg)
//
// /* Send the handshake. */
// self.client.write(pkg, function () {
// console.log('Sent handshake.')
// // console.log('sent handshake', pkg)
// })
// })
//
// this.client.on('close', function () {
// console.info('Connection closed.')
// })
//
// this.client.on('error', function (err) {
// console.error('Error detected', err)
// })
//
// // const parser = require('./ZeroNet/handleIncomingData').default
// this.client.on('data', function (data) {
// self.setState({ debug: data })
// // parser(self, data)
// })
// }
// _announce() {
// /* Localize this. */
// const self = this
//
// // console.log('Announcing...')
// this.setState({ debug: 'Announcing...' })
//
// const Client = require('zeronet-tracker')
//
// const crypto = require('crypto')
// // const SHA = require('crypto-js/sha1')
// // console.log(SHA256("Message"))
//
// let shasum = crypto.createHash('sha1')
// shasum.update('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
// // const infoHash = SHA('1HeLLo4uzjaLetFx6NH3PMwFP3qbRbTf3D')
// const infoHash = shasum.digest('hex')
// console.log('Info hash', infoHash)
// this.setState({ debug: infoHash })
//
// // return
//
// /* Initialize peer id. */
// const peerId = '-UT3530-FFFFFFFFFFFF'
//
// /* Initialize announce. */
// const announce = [
// // 'udp://tracker.coppersurfer.tk:6969',
// 'udp://tracker.leechers-paradise.org:6969'
// ]
//
// /* Initialize client port. */
// const port = 6881
//
// var requiredOpts = {
// infoHash,
// peerId,
// announce,
// port
// }
//
// // var optionalOpts = {
// // getAnnounceOpts: function () {
// // // Provide a callback that will be called whenever announce() is called
// // // internally (on timer), or by the user
// // return {
// // uploaded: 0,
// // downloaded: 0,
// // left: 0,
// // customParam: 'blah' // custom parameters supported
// // }
// // },
// //
// // // RTCPeerConnection config object (only used in browser)
// // rtcConfig: {},
// //
// // // User-Agent header for http requests
// // userAgent: '',
// //
// // // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
// // wrtc: {},
// // }
//
// const client = new Client(requiredOpts)
//
// client.on('error', function (err) {
// // fatal client error!
// console.log(err.message)
// return self.setState({ debug: err.message })
// })
//
// client.on('warning', function (err) {
// // a tracker was unavailable or sent bad data to the client. you can probably ignore it
// console.log(err.message)
// return self.setState({ debug: err.message })
// })
//
// // start getting peers from the tracker
// client.start()
//
// client.on('update', function (data) {
// console.log('got an announce response from tracker: ', data.announce)
// console.log('number of seeders vs. leechers in the swarm: ', data.complete, data.incomplete)
// return self.setState({ debug: 'number of seeders vs. leechers in the swarm: ' + data.complete + ' / ' + data.incomplete })
// })
//
// client.once('peer', function (_address) {
// return self.setState({ debug: _address })
//
// console.log('found a peer: ' + _address) // 85.10.239.191:48623
//
// const ipAddress = _address.split(':')[0]
// const port = _address.split(':')[1]
//
// const peer = { ipAddress, port }
//
// self.hostIp = ipAddress
// self.hostPort = port
//
// self._addPeerToDb(infoHash, peer)
// })
//
// // announce that download has completed (and you are now a seeder)
// // client.complete()
//
// // force a tracker announce. will trigger more 'update' events and maybe more 'peer' events
// // client.update()
//
// // provide parameters to the tracker
// // client.update({
// // uploaded: 0,
// // downloaded: 0,
// // left: 0,
// // customParam: 'blah' // custom parameters supported
// // })
//
// // stop getting peers from the tracker, gracefully leave the swarm
// // client.stop()
//
// // ungracefully leave the swarm (without sending final 'stop' message)
// // client.destroy()
//
// // scrape
// // client.scrape()
//
// // client.on('scrape', function (data) {
// // console.log('got a scrape response from tracker: ', data.announce)
// // console.log('number of seeders vs. leechers in the swarm: ', data.complete, data.incomplete)
// // // console.log('number of total downloads of this torrent: ', data.downloaded)
// // })
// }