Compare commits
146 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d7613c3ac1 | ||
![]() |
953df40533 | ||
![]() |
adf80f9e31 | ||
![]() |
a19b83ad6d | ||
![]() |
18c33fa5f7 | ||
![]() |
eeeebdda22 | ||
![]() |
4f99851a9e | ||
![]() |
c89df72e4e | ||
![]() |
5bdc9df4f7 | ||
![]() |
f2eb816fff | ||
![]() |
8cfba16e12 | ||
![]() |
1013a093c7 | ||
![]() |
cbd3f0d8b5 | ||
![]() |
4a049f18cd | ||
![]() |
d409e4bf2a | ||
![]() |
96cbab00ce | ||
![]() |
50ab830a75 | ||
![]() |
60f328d8ba | ||
![]() |
ea9fd78040 | ||
![]() |
106acca7c8 | ||
![]() |
a9826a9612 | ||
![]() |
4539a41dad | ||
![]() |
6a0f5aecbe | ||
![]() |
2034e9ed2c | ||
![]() |
f84070f89a | ||
![]() |
aa3f1459de | ||
![]() |
3405e9a94e | ||
![]() |
449dd19356 | ||
![]() |
95abc05086 | ||
![]() |
7b1713ef96 | ||
![]() |
1505eae11a | ||
![]() |
b45145bde5 | ||
![]() |
67b7fac40f | ||
![]() |
92fb4d380c | ||
![]() |
03050eca54 | ||
![]() |
6b8a93a9af | ||
![]() |
d2f8239760 | ||
![]() |
a2fa6e1212 | ||
![]() |
58b00eab05 | ||
![]() |
e19f8abcf6 | ||
![]() |
b584a2b93c | ||
![]() |
04b2b55be4 | ||
![]() |
e85020983e | ||
![]() |
084403d245 | ||
![]() |
7929a169e5 | ||
![]() |
ef43f5370f | ||
![]() |
bed0e361cc | ||
![]() |
79ef8e4470 | ||
![]() |
8bc4d72237 | ||
![]() |
635577950a | ||
![]() |
2a39a8fe1c | ||
![]() |
5aeaca0dd8 | ||
![]() |
9f78fc76ae | ||
![]() |
76362ff9ca | ||
![]() |
ebfcb7df5f | ||
![]() |
fb44d87d8a | ||
![]() |
123f56ea7f | ||
![]() |
6467e7d9c7 | ||
![]() |
ca209b0345 | ||
![]() |
5281ecf3a9 | ||
![]() |
b1b0beb92a | ||
![]() |
7c21d4d0ea | ||
![]() |
f80d9a5dde | ||
![]() |
50997b24c4 | ||
![]() |
db07b7dd29 | ||
![]() |
9e0a3efabd | ||
![]() |
4893baa94e | ||
![]() |
26f603e832 | ||
![]() |
6b96881402 | ||
![]() |
085e244ae8 | ||
![]() |
c3b453a5d8 | ||
![]() |
a86daa2460 | ||
![]() |
9ad818555d | ||
![]() |
cd0a63689c | ||
![]() |
9a1ddf367d | ||
![]() |
6a18461ba9 | ||
![]() |
e6aafdb332 | ||
![]() |
9e9c187bf3 | ||
![]() |
37c73958eb | ||
![]() |
50f97ee821 | ||
![]() |
6977f98a40 | ||
![]() |
7f2a017982 | ||
![]() |
934517cfe6 | ||
![]() |
9e5c1fbe94 | ||
![]() |
f872039875 | ||
![]() |
0d54b8a399 | ||
![]() |
297cf6e9a6 | ||
![]() |
84bc722156 | ||
![]() |
d20d1e10e8 | ||
![]() |
e247069bcf | ||
![]() |
c91d132b6b | ||
![]() |
dfcbfdf1d2 | ||
![]() |
5fad79dd03 | ||
![]() |
b92c13afa8 | ||
![]() |
e0bcac622e | ||
![]() |
0e05220ed9 | ||
![]() |
cb95505f14 | ||
![]() |
b2824bb228 | ||
![]() |
0734f4b4e0 | ||
![]() |
9bf62ed135 | ||
![]() |
a04ea1dab5 | ||
![]() |
afd5e5955e | ||
![]() |
6fc24a9db2 | ||
![]() |
aabe79dde1 | ||
![]() |
62fbe8bd1b | ||
![]() |
25598089a1 | ||
![]() |
5e87a38157 | ||
![]() |
9e819ba3fe | ||
![]() |
32f5c04b00 | ||
![]() |
f77090a35c | ||
![]() |
3b97b75c15 | ||
![]() |
6b4d4a3ff1 | ||
![]() |
6b2b79c875 | ||
![]() |
4a464c4bbb | ||
![]() |
093837f212 | ||
![]() |
9df4368701 | ||
![]() |
c7890651e7 | ||
![]() |
772567c1d6 | ||
![]() |
b0ec983a45 | ||
![]() |
fa6ef7a5ce | ||
![]() |
ed8b7caff5 | ||
![]() |
1e76d4d466 | ||
![]() |
a49ecbd4d9 | ||
![]() |
363ddd0acf | ||
![]() |
633f2ad531 | ||
![]() |
463c5278c5 | ||
![]() |
cc6c738538 | ||
![]() |
720da46c44 | ||
![]() |
19c4543e44 | ||
![]() |
1e4851c9a3 | ||
![]() |
3d37d497bf | ||
![]() |
2d15afd848 | ||
![]() |
9c7f5da432 | ||
![]() |
478cb13670 | ||
![]() |
c2ac02076b | ||
![]() |
2c65e06bbe | ||
![]() |
30b5380ce3 | ||
![]() |
6c7adecf25 | ||
![]() |
b1e28a04a5 | ||
![]() |
fe95752c37 | ||
![]() |
3ea372cfcf | ||
![]() |
b55e0c306c | ||
![]() |
a33e454002 | ||
![]() |
7b4a888157 | ||
![]() |
e2b3a25670 | ||
![]() |
69e25b5d2c |
2
.npmignore
Normal file
2
.npmignore
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.travis.yml
|
||||||
|
test/
|
@ -1,4 +1,3 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "0.11"
|
- lts/*
|
||||||
- "0.10"
|
|
||||||
|
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2013 Feross Aboukhadijeh
|
Copyright (c) Feross Aboukhadijeh and WebTorrent, LLC
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
63
README.md
63
README.md
@ -1,10 +1,17 @@
|
|||||||
# ut_metadata [](https://travis-ci.org/feross/ut_metadata) [](https://npmjs.org/package/ut_metadata) [](https://www.gittip.com/feross/)
|
# ut_metadata [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url]
|
||||||
|
|
||||||
|
[travis-image]: https://img.shields.io/travis/webtorrent/ut_metadata/master.svg
|
||||||
|
[travis-url]: https://travis-ci.org/webtorrent/ut_metadata
|
||||||
|
[npm-image]: https://img.shields.io/npm/v/ut_metadata.svg
|
||||||
|
[npm-url]: https://npmjs.org/package/ut_metadata
|
||||||
|
[downloads-image]: https://img.shields.io/npm/dm/ut_metadata.svg
|
||||||
|
[downloads-url]: https://npmjs.org/package/ut_metadata
|
||||||
|
[standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg
|
||||||
|
[standard-url]: https://standardjs.com
|
||||||
|
|
||||||
### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
|
### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
|
||||||
|
|
||||||
[](https://ci.testling.com/feross/ut_metadata)
|
JavaScript implementation of the [Extension for Peers to Send Metadata Files (BEP 9)](http://www.bittorrent.org/beps/bep_0009.html). Use with [bittorrent-protocol](https://www.npmjs.com/package/bittorrent-protocol).
|
||||||
|
|
||||||
Node.js implementation of the [Extension for Peers to Send Metadata Files (BEP 9)](http://www.bittorrent.org/beps/bep_0009.html). Use with [bittorrent-protocol](https://github.com/feross/bittorrent-protocol).
|
|
||||||
|
|
||||||
The purpose of this extension is to allow clients to join a swarm and complete a download without the need of downloading a .torrent file first. This extension instead allows clients to download the metadata from peers. It makes it possible to support magnet links, a link on a web page only containing enough information to join the swarm (the info hash).
|
The purpose of this extension is to allow clients to join a swarm and complete a download without the need of downloading a .torrent file first. This extension instead allows clients to download the metadata from peers. It makes it possible to support magnet links, a link on a web page only containing enough information to join the swarm (the info hash).
|
||||||
|
|
||||||
@ -18,20 +25,20 @@ npm install ut_metadata
|
|||||||
|
|
||||||
### usage
|
### usage
|
||||||
|
|
||||||
This package should be used with [bittorrent-protocol](https://github.com/feross/bittorrent-protocol), which supports a plugin-like system for extending the protocol with additional functionality.
|
This package should be used with [bittorrent-protocol](https://www.npmjs.com/package/bittorrent-protocol), which supports a plugin-like system for extending the protocol with additional functionality.
|
||||||
|
|
||||||
Say you're already using `bittorrent-protocol`. Your code might look something like this:
|
Say you're already using `bittorrent-protocol`. Your code might look something like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var Protocol = require('bittorrent-protocol')
|
const Protocol = require('bittorrent-protocol')
|
||||||
var net = require('net')
|
const net = require('net')
|
||||||
|
|
||||||
net.createServer(function (socket) {
|
net.createServer(socket => {
|
||||||
var wire = new Protocol()
|
var wire = new Protocol()
|
||||||
socket.pipe(wire).pipe(socket)
|
socket.pipe(wire).pipe(socket)
|
||||||
|
|
||||||
// handle handshake
|
// handle handshake
|
||||||
wire.on('handshake', function (infoHash, peerId) {
|
wire.on('handshake', (infoHash, peerId) => {
|
||||||
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
|
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -41,12 +48,12 @@ net.createServer(function (socket) {
|
|||||||
To add support for BEP 9, simply modify your code like this:
|
To add support for BEP 9, simply modify your code like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var Protocol = require('bittorrent-protocol')
|
const Protocol = require('bittorrent-protocol')
|
||||||
var net = require('net')
|
const net = require('net')
|
||||||
var ut_metadata = require('ut_metadata')
|
const ut_metadata = require('ut_metadata')
|
||||||
|
|
||||||
net.createServer(function (socket) {
|
net.createServer(socket => {
|
||||||
var wire = new Protocol()
|
const wire = new Protocol()
|
||||||
socket.pipe(wire).pipe(socket)
|
socket.pipe(wire).pipe(socket)
|
||||||
|
|
||||||
// initialize the extension
|
// initialize the extension
|
||||||
@ -58,7 +65,7 @@ net.createServer(function (socket) {
|
|||||||
wire.ut_metadata.fetch()
|
wire.ut_metadata.fetch()
|
||||||
|
|
||||||
// 'metadata' event will fire when the metadata arrives and is verified to be correct!
|
// 'metadata' event will fire when the metadata arrives and is verified to be correct!
|
||||||
wire.ut_metadata.on('metadata', function (metadata) {
|
wire.ut_metadata.on('metadata', metadata => {
|
||||||
// got metadata!
|
// got metadata!
|
||||||
|
|
||||||
// Note: the event will not fire if the peer does not support ut_metadata, if they
|
// Note: the event will not fire if the peer does not support ut_metadata, if they
|
||||||
@ -68,12 +75,12 @@ net.createServer(function (socket) {
|
|||||||
|
|
||||||
// optionally, listen to the 'warning' event if you want to know that metadata is
|
// optionally, listen to the 'warning' event if you want to know that metadata is
|
||||||
// probably not going to arrive for one of the above reasons.
|
// probably not going to arrive for one of the above reasons.
|
||||||
wire.ut_metadata.on('warning', function (err) {
|
wire.ut_metadata.on('warning', err => {
|
||||||
console.log(err.message)
|
console.log(err.message)
|
||||||
})
|
})
|
||||||
|
|
||||||
// handle handshake
|
// handle handshake
|
||||||
wire.on('handshake', function (infoHash, peerId) {
|
wire.on('handshake', (infoHash, peerId) => {
|
||||||
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
|
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -88,7 +95,7 @@ Initialize the extension. If you have the torrent metadata (Buffer), pass it int
|
|||||||
`ut_metadata` constructor so it's made available to the peer.
|
`ut_metadata` constructor so it's made available to the peer.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var metadata = fs.readFileSync(__dirname + '/file.torrent')
|
const metadata = fs.readFileSync(__dirname + '/file.torrent')
|
||||||
wire.use(ut_metadata(metadata))
|
wire.use(ut_metadata(metadata))
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -96,35 +103,23 @@ wire.use(ut_metadata(metadata))
|
|||||||
|
|
||||||
Ask the peer to send metadata.
|
Ask the peer to send metadata.
|
||||||
|
|
||||||
```js
|
|
||||||
wire.ut_metadata.fetch()
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `ut_metadata.cancel()`
|
#### `ut_metadata.cancel()`
|
||||||
|
|
||||||
Stop asking the peer to send metadata.
|
Stop asking the peer to send metadata.
|
||||||
|
|
||||||
```js
|
|
||||||
wire.ut_metadata.cancel()
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `ut_metadata.setMetadata(metadata)`
|
#### `ut_metadata.setMetadata(metadata)`
|
||||||
|
|
||||||
Set the metadata. If you didn't have the metadata at the time `ut_metadata` was
|
Set the metadata. If you didn't have the metadata at the time `ut_metadata` was
|
||||||
initialized, but you end up getting it from another peer (or somewhere else), you should
|
initialized, but you end up getting it from another peer (or somewhere else), you should
|
||||||
call `setMetadata` so the metadata will be available to the peer.
|
call `setMetadata` so the metadata will be available to the peer.
|
||||||
|
|
||||||
```js
|
|
||||||
wire.ut_metadata.setMetadata(metadata)
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `ut_metadata.on('metadata', function (metadata) {})`
|
#### `ut_metadata.on('metadata', function (metadata) {})`
|
||||||
|
|
||||||
Fired when metadata is available and verified to be correct. Called with a single
|
Fired when metadata is available and verified to be correct. Called with a single
|
||||||
parameter of type Buffer.
|
parameter of type Buffer.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
wire.ut_metadata.on('metadata', function (metadata) {
|
wire.ut_metadata.on('metadata', metadata => {
|
||||||
console.log(Buffer.isBuffer(metadata)) // true
|
console.log(Buffer.isBuffer(metadata)) // true
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -138,14 +133,14 @@ simply don't respond.
|
|||||||
Fired if:
|
Fired if:
|
||||||
- the peer does not support ut_metadata
|
- the peer does not support ut_metadata
|
||||||
- the peer doesn't have metadata yet
|
- the peer doesn't have metadata yet
|
||||||
- the repeatedly sent invalid data
|
- the peer repeatedly sent invalid data
|
||||||
|
|
||||||
```js
|
```js
|
||||||
wire.ut_metadata.on('warning', function (err) {
|
wire.ut_metadata.on('warning', err => {
|
||||||
console.log(err.message)
|
console.log(err.message)
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
### license
|
### license
|
||||||
|
|
||||||
MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org).
|
MIT. Copyright (c) [Feross Aboukhadijeh](https://feross.org) and [WebTorrent, LLC](https://webtorrent.io).
|
||||||
|
431
index.js
431
index.js
@ -1,233 +1,244 @@
|
|||||||
var BitField = require('bitfield')
|
/*! ut_metadata. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
|
||||||
var bncode = require('bncode')
|
const { EventEmitter } = require('events')
|
||||||
var EventEmitter = require('events').EventEmitter
|
const bencode = require('bencode')
|
||||||
var inherits = require('inherits')
|
const BitField = require('bitfield').default
|
||||||
var Rusha = require('rusha-browserify') // Fast SHA1 (works in browser)
|
const debug = require('debug')('ut_metadata')
|
||||||
|
const sha1 = require('simple-sha1')
|
||||||
|
|
||||||
var BITFIELD_GROW = 1000
|
const MAX_METADATA_SIZE = 1E7 // 10 MB
|
||||||
var PIECE_LENGTH = 16 * 1024
|
const BITFIELD_GROW = 1E3
|
||||||
|
const PIECE_LENGTH = 1 << 14 // 16 KiB
|
||||||
|
|
||||||
function sha1 (buf) {
|
module.exports = metadata => {
|
||||||
return (new Rusha()).digestFromBuffer(buf)
|
class utMetadata extends EventEmitter {
|
||||||
}
|
constructor (wire) {
|
||||||
|
super()
|
||||||
|
|
||||||
module.exports = function (metadata) {
|
this._wire = wire
|
||||||
|
|
||||||
inherits(ut_metadata, EventEmitter)
|
this._fetching = false
|
||||||
|
this._metadataComplete = false
|
||||||
|
this._metadataSize = null
|
||||||
|
// how many reject messages to tolerate before quitting
|
||||||
|
this._remainingRejects = null
|
||||||
|
|
||||||
function ut_metadata (wire) {
|
// The largest torrent file that I know of is ~1-2MB, which is ~100
|
||||||
EventEmitter.call(this)
|
// pieces. Therefore, cap the bitfield to 10x that (1000 pieces) so a
|
||||||
|
// malicious peer can't make it grow to fill all memory.
|
||||||
|
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
||||||
|
|
||||||
this._wire = wire
|
if (Buffer.isBuffer(metadata)) {
|
||||||
|
this.setMetadata(metadata)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this._metadataComplete = false
|
onHandshake (infoHash, peerId, extensions) {
|
||||||
this._metadataSize = null
|
this._infoHash = infoHash
|
||||||
this._remainingRejects = null // how many reject messages to tolerate before quitting
|
}
|
||||||
this._fetching = false
|
|
||||||
|
|
||||||
// The largest .torrent file that I know of is ~1-2MB, which is ~100 pieces.
|
onExtendedHandshake (handshake) {
|
||||||
// Therefore, cap the bitfield to 1,000 bits so a malicious peer can't make it grow
|
if (!handshake.m || !handshake.m.ut_metadata) {
|
||||||
// to fill all memory.
|
return this.emit('warning', new Error('Peer does not support ut_metadata'))
|
||||||
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
}
|
||||||
|
if (!handshake.metadata_size) {
|
||||||
|
return this.emit('warning', new Error('Peer does not have metadata'))
|
||||||
|
}
|
||||||
|
if (typeof handshake.metadata_size !== 'number' ||
|
||||||
|
MAX_METADATA_SIZE < handshake.metadata_size ||
|
||||||
|
handshake.metadata_size <= 0) {
|
||||||
|
return this.emit('warning', new Error('Peer gave invalid metadata size'))
|
||||||
|
}
|
||||||
|
|
||||||
if (Buffer.isBuffer(metadata)) {
|
this._metadataSize = handshake.metadata_size
|
||||||
var info = null
|
this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
|
||||||
|
this._remainingRejects = this._numPieces * 2
|
||||||
|
|
||||||
|
this._requestPieces()
|
||||||
|
}
|
||||||
|
|
||||||
|
onMessage (buf) {
|
||||||
|
let dict
|
||||||
|
let trailer
|
||||||
try {
|
try {
|
||||||
// if buffer fails to decode or there is no info key, then metadata is corrupt
|
const str = buf.toString()
|
||||||
info = bncode.encode(bncode.decode(metadata).info)
|
const trailerIndex = str.indexOf('ee') + 2
|
||||||
|
dict = bencode.decode(str.substring(0, trailerIndex))
|
||||||
|
trailer = buf.slice(trailerIndex)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error('`ut_metadata` constructed with corrupt/invalid metadata')
|
// drop invalid messages
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info)
|
switch (dict.msg_type) {
|
||||||
this.setMetadata(info)
|
case 0:
|
||||||
}
|
// ut_metadata request (from peer)
|
||||||
}
|
// example: { 'msg_type': 0, 'piece': 0 }
|
||||||
|
this._onRequest(dict.piece)
|
||||||
ut_metadata.prototype.onHandshake = function (infoHash, peerId, extensions) {
|
break
|
||||||
this._infoHash = infoHash
|
case 1:
|
||||||
}
|
// ut_metadata data (in response to our request)
|
||||||
|
// example: { 'msg_type': 1, 'piece': 0, 'total_size': 3425 }
|
||||||
ut_metadata.prototype.onExtendedHandshake = function (handshake) {
|
this._onData(dict.piece, trailer, dict.total_size)
|
||||||
if (!handshake.m || !handshake.m.ut_metadata) {
|
break
|
||||||
return this.emit('warning', new Error('Peer does not support ut_metadata'))
|
case 2:
|
||||||
}
|
// ut_metadata reject (peer doesn't have piece we requested)
|
||||||
if (!handshake.metadata_size) {
|
// { 'msg_type': 2, 'piece': 0 }
|
||||||
return this.emit('warning', new Error('Peer does not have metadata'))
|
this._onReject(dict.piece)
|
||||||
}
|
break
|
||||||
|
|
||||||
this._metadataSize = handshake.metadata_size
|
|
||||||
this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
|
|
||||||
this._remainingRejects = this._numPieces * 2
|
|
||||||
|
|
||||||
if (this._fetching) {
|
|
||||||
this._requestPieces()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype.onMessage = function (buf) {
|
|
||||||
var dict, trailer
|
|
||||||
try {
|
|
||||||
var str = buf.toString()
|
|
||||||
var trailerIndex = str.indexOf('ee') + 2
|
|
||||||
dict = bncode.decode(str.substring(0, trailerIndex))
|
|
||||||
trailer = buf.slice(trailerIndex)
|
|
||||||
} catch (err) {
|
|
||||||
// drop invalid messages
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (dict.msg_type) {
|
|
||||||
case 0:
|
|
||||||
// ut_metadata request (from peer)
|
|
||||||
// example: { 'msg_type': 0, 'piece': 0 }
|
|
||||||
this._onRequest(dict.piece)
|
|
||||||
break
|
|
||||||
case 1:
|
|
||||||
// ut_metadata data (in response to our request)
|
|
||||||
// example: { 'msg_type': 1, 'piece': 0, 'total_size': 3425 }
|
|
||||||
this._onData(dict.piece, trailer, dict.total_size)
|
|
||||||
break
|
|
||||||
case 2:
|
|
||||||
// ut_metadata reject (peer doesn't have piece we requested)
|
|
||||||
// { 'msg_type': 2, 'piece': 0 }
|
|
||||||
this._onReject(dict.piece)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ask the peer to send metadata.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
ut_metadata.prototype.fetch = function () {
|
|
||||||
if (this._metadataComplete) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
this._fetching = true
|
|
||||||
if (this._metadataSize) {
|
|
||||||
this._requestPieces()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop asking the peer to send metadata.
|
|
||||||
* @public
|
|
||||||
*/
|
|
||||||
ut_metadata.prototype.cancel = function () {
|
|
||||||
this._fetching = false
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype.setMetadata = function (_metadata) {
|
|
||||||
if (this._metadataComplete) return
|
|
||||||
this._metadataComplete = true
|
|
||||||
this.metadata = _metadata
|
|
||||||
this._metadataSize = this.metadata.length
|
|
||||||
this.cancel()
|
|
||||||
this._wire.extendedHandshake.metadata_size = this._metadataSize
|
|
||||||
this.emit('metadata', bncode.encode({ info: bncode.decode(this.metadata) }))
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._send = function (dict, trailer) {
|
|
||||||
var buf = bncode.encode(dict)
|
|
||||||
if (Buffer.isBuffer(trailer)) {
|
|
||||||
buf = Buffer.concat([buf, trailer])
|
|
||||||
}
|
|
||||||
this._wire.extended('ut_metadata', buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._request = function (piece) {
|
|
||||||
this._send({ msg_type: 0, piece: piece })
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._data = function (piece, buf, totalSize) {
|
|
||||||
var msg = { msg_type: 1, piece: piece }
|
|
||||||
if (typeof totalSize === 'number') {
|
|
||||||
msg.total_size = totalSize
|
|
||||||
}
|
|
||||||
this._send(msg, buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._reject = function (piece) {
|
|
||||||
this._send({ msg_type: 2, piece: piece })
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._onRequest = function (piece) {
|
|
||||||
if (!this._metadataComplete) {
|
|
||||||
this._reject(piece)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var start = piece * PIECE_LENGTH
|
|
||||||
var end = start + PIECE_LENGTH
|
|
||||||
if (end > this._metadataSize) {
|
|
||||||
end = this._metadataSize
|
|
||||||
}
|
|
||||||
var buf = this.metadata.slice(start, end)
|
|
||||||
this._data(piece, buf, this._metadataSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._onData = function (piece, buf, totalSize) {
|
|
||||||
if (buf.length > PIECE_LENGTH) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
buf.copy(this.metadata, piece * PIECE_LENGTH)
|
|
||||||
this._bitfield.set(piece)
|
|
||||||
this._checkDone()
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._onReject = function (piece) {
|
|
||||||
if (this._remainingRejects > 0 && this._fetching) {
|
|
||||||
// If we haven't been rejected too much, then try to request the piece again
|
|
||||||
this._request(piece)
|
|
||||||
this._remainingRejects -= 1
|
|
||||||
} else {
|
|
||||||
this.emit('warning', new Error('Peer sent "reject" too much'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._requestPieces = function () {
|
|
||||||
this.metadata = new Buffer(this._metadataSize)
|
|
||||||
|
|
||||||
for (var piece = 0; piece < this._numPieces; piece++) {
|
|
||||||
this._request(piece)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ut_metadata.prototype._checkDone = function () {
|
|
||||||
var done = true
|
|
||||||
for (var piece = 0; piece < this._numPieces; piece++) {
|
|
||||||
if (!this._bitfield.get(piece)) {
|
|
||||||
done = false
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!done) return
|
|
||||||
|
|
||||||
try {
|
/**
|
||||||
// if buffer fails to decode, then data was corrupt
|
* Ask the peer to send metadata.
|
||||||
bncode.decode(this.metadata)
|
* @public
|
||||||
} catch (err) {
|
*/
|
||||||
return this._failedMetadata()
|
fetch () {
|
||||||
|
if (this._metadataComplete) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
this._fetching = true
|
||||||
|
if (this._metadataSize) {
|
||||||
|
this._requestPieces()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check hash
|
/**
|
||||||
if (sha1(this.metadata) === this._infoHash.toString('hex')) {
|
* Stop asking the peer to send metadata.
|
||||||
this.setMetadata(this.metadata)
|
* @public
|
||||||
} else {
|
*/
|
||||||
this._failedMetadata()
|
cancel () {
|
||||||
|
this._fetching = false
|
||||||
|
}
|
||||||
|
|
||||||
|
setMetadata (metadata) {
|
||||||
|
if (this._metadataComplete) return true
|
||||||
|
debug('set metadata')
|
||||||
|
|
||||||
|
// if full torrent dictionary was passed in, pull out just `info` key
|
||||||
|
try {
|
||||||
|
const info = bencode.decode(metadata).info
|
||||||
|
if (info) {
|
||||||
|
metadata = bencode.encode(info)
|
||||||
|
}
|
||||||
|
} catch (err) {}
|
||||||
|
|
||||||
|
// check hash
|
||||||
|
if (this._infoHash && this._infoHash !== sha1.sync(metadata)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cancel()
|
||||||
|
|
||||||
|
this.metadata = metadata
|
||||||
|
this._metadataComplete = true
|
||||||
|
this._metadataSize = this.metadata.length
|
||||||
|
this._wire.extendedHandshake.metadata_size = this._metadataSize
|
||||||
|
|
||||||
|
this.emit('metadata', bencode.encode({
|
||||||
|
info: bencode.decode(this.metadata)
|
||||||
|
}))
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
_send (dict, trailer) {
|
||||||
|
let buf = bencode.encode(dict)
|
||||||
|
if (Buffer.isBuffer(trailer)) {
|
||||||
|
buf = Buffer.concat([buf, trailer])
|
||||||
|
}
|
||||||
|
this._wire.extended('ut_metadata', buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
_request (piece) {
|
||||||
|
this._send({ msg_type: 0, piece })
|
||||||
|
}
|
||||||
|
|
||||||
|
_data (piece, buf, totalSize) {
|
||||||
|
const msg = { msg_type: 1, piece }
|
||||||
|
if (typeof totalSize === 'number') {
|
||||||
|
msg.total_size = totalSize
|
||||||
|
}
|
||||||
|
this._send(msg, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
_reject (piece) {
|
||||||
|
this._send({ msg_type: 2, piece })
|
||||||
|
}
|
||||||
|
|
||||||
|
_onRequest (piece) {
|
||||||
|
if (!this._metadataComplete) {
|
||||||
|
this._reject(piece)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const start = piece * PIECE_LENGTH
|
||||||
|
let end = start + PIECE_LENGTH
|
||||||
|
if (end > this._metadataSize) {
|
||||||
|
end = this._metadataSize
|
||||||
|
}
|
||||||
|
const buf = this.metadata.slice(start, end)
|
||||||
|
this._data(piece, buf, this._metadataSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
_onData (piece, buf, totalSize) {
|
||||||
|
if (buf.length > PIECE_LENGTH || !this._fetching) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
buf.copy(this.metadata, piece * PIECE_LENGTH)
|
||||||
|
this._bitfield.set(piece)
|
||||||
|
this._checkDone()
|
||||||
|
}
|
||||||
|
|
||||||
|
_onReject (piece) {
|
||||||
|
if (this._remainingRejects > 0 && this._fetching) {
|
||||||
|
// If we haven't been rejected too much,
|
||||||
|
// then try to request the piece again
|
||||||
|
this._request(piece)
|
||||||
|
this._remainingRejects -= 1
|
||||||
|
} else {
|
||||||
|
this.emit('warning', new Error('Peer sent "reject" too much'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_requestPieces () {
|
||||||
|
if (!this._fetching) return
|
||||||
|
this.metadata = Buffer.alloc(this._metadataSize)
|
||||||
|
for (let piece = 0; piece < this._numPieces; piece++) {
|
||||||
|
this._request(piece)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_checkDone () {
|
||||||
|
let done = true
|
||||||
|
for (let piece = 0; piece < this._numPieces; piece++) {
|
||||||
|
if (!this._bitfield.get(piece)) {
|
||||||
|
done = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!done) return
|
||||||
|
|
||||||
|
// attempt to set metadata -- may fail sha1 check
|
||||||
|
const success = this.setMetadata(this.metadata)
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
this._failedMetadata()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_failedMetadata () {
|
||||||
|
// reset bitfield & try again
|
||||||
|
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
||||||
|
this._remainingRejects -= this._numPieces
|
||||||
|
if (this._remainingRejects > 0) {
|
||||||
|
this._requestPieces()
|
||||||
|
} else {
|
||||||
|
this.emit('warning', new Error('Peer sent invalid metadata'))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ut_metadata.prototype._failedMetadata = function () {
|
// Name of the bittorrent-protocol extension
|
||||||
// reset bitfield & try again
|
utMetadata.prototype.name = 'ut_metadata'
|
||||||
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
|
||||||
this._remainingRejects -= this._numPieces
|
|
||||||
if (this._remainingRejects > 0) {
|
|
||||||
this._requestPieces()
|
|
||||||
} else {
|
|
||||||
this.emit('warning', new Error('Peer sent invalid metadata'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ut_metadata
|
return utMetadata
|
||||||
}
|
}
|
||||||
|
71
package.json
71
package.json
@ -1,57 +1,60 @@
|
|||||||
{
|
{
|
||||||
"name": "ut_metadata",
|
"name": "ut_metadata",
|
||||||
"description": "Extension for Peers to Send Metadata Files (BEP 9)",
|
"description": "Extension for Peers to Send Metadata Files (BEP 9)",
|
||||||
"version": "2.1.0",
|
"version": "3.5.2",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Feross Aboukhadijeh",
|
"name": "WebTorrent LLC",
|
||||||
"email": "feross@feross.org",
|
"email": "feross@webtorrent.io",
|
||||||
"url": "http://feross.org/"
|
"url": "https://webtorrent.io"
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/feross/ut_metadata/issues"
|
"url": "https://github.com/webtorrent/ut_metadata/issues"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bitfield": "^1.0.1",
|
"bencode": "^2.0.1",
|
||||||
"bncode": "^0.5.0",
|
"bitfield": "^4.0.0",
|
||||||
"inherits": "^2.0.1",
|
"debug": "^4.2.0",
|
||||||
"rusha-browserify": "^0.7.3"
|
"simple-sha1": "^3.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bittorrent-protocol": "^1.0.0",
|
"bittorrent-protocol": "^3.1.2",
|
||||||
"brfs": "^1.0.0",
|
"brfs": "^2.0.2",
|
||||||
"parse-torrent": "^1.0.0",
|
"standard": "*",
|
||||||
"tape": "^2.12.0"
|
"tape": "^5.0.1",
|
||||||
|
"webtorrent-fixtures": "^1.7.3"
|
||||||
},
|
},
|
||||||
"homepage": "http://webtorrent.io",
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"torrent",
|
|
||||||
"bittorrent",
|
|
||||||
"ut_metadata",
|
|
||||||
"bep_0009",
|
|
||||||
"bep 9",
|
|
||||||
"bep",
|
|
||||||
"9",
|
|
||||||
"Extension for Peers to Send Metadata Files",
|
"Extension for Peers to Send Metadata Files",
|
||||||
|
"bep",
|
||||||
|
"bep 9",
|
||||||
|
"bep_0009",
|
||||||
|
"bittorrent",
|
||||||
"metadata",
|
"metadata",
|
||||||
"p2p"
|
"p2p",
|
||||||
|
"torrent",
|
||||||
|
"ut_metadata"
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/feross/ut_metadata.git"
|
"url": "git://github.com/webtorrent/ut_metadata.git"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "tape test/*.js"
|
"test": "standard && tape test/*.js"
|
||||||
},
|
},
|
||||||
"testling": {
|
"funding": [
|
||||||
"files": "test/*.js",
|
{
|
||||||
"browsers": [
|
"type": "github",
|
||||||
"ie/9..latest",
|
"url": "https://github.com/sponsors/feross"
|
||||||
"chrome/25..latest",
|
},
|
||||||
"firefox/20..latest",
|
{
|
||||||
"safari/6..latest",
|
"type": "patreon",
|
||||||
"opera/15.0..latest"
|
"url": "https://www.patreon.com/feross"
|
||||||
]
|
},
|
||||||
}
|
{
|
||||||
|
"type": "consulting",
|
||||||
|
"url": "https://feross.org/support"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,14 @@
|
|||||||
var fs = require('fs')
|
const { leavesMetadata } = require('webtorrent-fixtures')
|
||||||
var Protocol = require('bittorrent-protocol')
|
const bencode = require('bencode')
|
||||||
var ut_metadata = require('../')
|
const Protocol = require('bittorrent-protocol')
|
||||||
var bncode = require('bncode')
|
const test = require('tape')
|
||||||
var test = require('tape')
|
const utMetadata = require('../')
|
||||||
|
|
||||||
// Used in multiple tests
|
test('wire.use(utMetadata())', t => {
|
||||||
var metadata = fs.readFileSync(__dirname + '/torrents/leaves-magnet.torrent')
|
const wire = new Protocol()
|
||||||
|
|
||||||
test('wire.use(ut_metadata())', function (t) {
|
|
||||||
var wire = new Protocol()
|
|
||||||
wire.pipe(wire)
|
wire.pipe(wire)
|
||||||
|
|
||||||
wire.use(ut_metadata())
|
wire.use(utMetadata())
|
||||||
|
|
||||||
t.ok(wire.ut_metadata)
|
t.ok(wire.ut_metadata)
|
||||||
t.ok(wire.ut_metadata.fetch)
|
t.ok(wire.ut_metadata.fetch)
|
||||||
@ -20,15 +17,18 @@ test('wire.use(ut_metadata())', function (t) {
|
|||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('wire.use(ut_metadata(metadata))', function (t) {
|
test('wire.use(utMetadata(metadata))', t => {
|
||||||
var wire = new Protocol()
|
const wire = new Protocol()
|
||||||
wire.pipe(wire)
|
wire.pipe(wire)
|
||||||
|
|
||||||
wire.use(ut_metadata(metadata))
|
wire.use(utMetadata(leavesMetadata.torrent))
|
||||||
|
|
||||||
t.ok(wire.ut_metadata)
|
t.ok(wire.ut_metadata)
|
||||||
t.ok(wire.ut_metadata.fetch)
|
t.ok(wire.ut_metadata.fetch)
|
||||||
t.ok(wire.ut_metadata.cancel)
|
t.ok(wire.ut_metadata.cancel)
|
||||||
t.equal(wire.ut_metadata.metadata.toString('hex'), bncode.encode(bncode.decode(metadata).info).toString('hex'))
|
t.equal(
|
||||||
|
wire.ut_metadata.metadata.toString('hex'),
|
||||||
|
bencode.encode(bencode.decode(leavesMetadata.torrent).info).toString('hex')
|
||||||
|
)
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
198
test/fetch.js
198
test/fetch.js
@ -1,42 +1,39 @@
|
|||||||
var fs = require('fs')
|
const { leavesMetadata, sintel } = require('webtorrent-fixtures')
|
||||||
var parseTorrent = require('parse-torrent')
|
const bencode = require('bencode')
|
||||||
var Protocol = require('bittorrent-protocol')
|
const Protocol = require('bittorrent-protocol')
|
||||||
var ut_metadata = require('../')
|
const test = require('tape')
|
||||||
var bncode = require('bncode')
|
const utMetadata = require('../')
|
||||||
var test = require('tape')
|
|
||||||
|
|
||||||
// Used in multiple tests
|
const id1 = Buffer.from('01234567890123456789')
|
||||||
var metadata = fs.readFileSync(__dirname + '/torrents/leaves-magnet.torrent')
|
const id2 = Buffer.from('12345678901234567890')
|
||||||
var parsedTorrent = parseTorrent(metadata)
|
|
||||||
|
|
||||||
var largeMetadata = fs.readFileSync(__dirname + '/torrents/ubuntu-12.04.4-alternate-amd64.iso.torrent')
|
test('fetch()', t => {
|
||||||
var largeParsedTorrent = parseTorrent(largeMetadata)
|
|
||||||
|
|
||||||
var id1 = new Buffer('01234567890123456789')
|
|
||||||
var id2 = new Buffer('12345678901234567890')
|
|
||||||
|
|
||||||
test('fetch()', function (t) {
|
|
||||||
t.plan(3)
|
t.plan(3)
|
||||||
|
|
||||||
var wire1 = new Protocol()
|
const wire1 = new Protocol()
|
||||||
var wire2 = new Protocol()
|
const wire2 = new Protocol()
|
||||||
wire1.pipe(wire2).pipe(wire1)
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
wire1.use(ut_metadata(metadata)) // wire1 already has metadata
|
wire1.use(utMetadata(leavesMetadata.torrent)) // wire1 already has metadata
|
||||||
wire2.use(ut_metadata()) // wire2 does not
|
wire2.use(utMetadata()) // wire2 does not
|
||||||
|
|
||||||
wire2.ut_metadata.fetch()
|
wire2.ut_metadata.fetch()
|
||||||
|
|
||||||
wire2.ut_metadata.on('metadata', function (_metadata) {
|
wire2.ut_metadata.on('metadata', _metadata => {
|
||||||
// got metadata!
|
// got metadata!
|
||||||
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(metadata).info }).toString('hex'))
|
t.equal(
|
||||||
|
_metadata.toString('hex'),
|
||||||
|
bencode.encode({
|
||||||
|
info: bencode.decode(leavesMetadata.torrent).info
|
||||||
|
}).toString('hex')
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('handshake', function (infoHash, peerId, extensions) {
|
wire2.on('handshake', (infoHash, peerId, extensions) => {
|
||||||
wire2.handshake(parsedTorrent.infoHash, id2)
|
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('extended', function (ext) {
|
wire2.on('extended', ext => {
|
||||||
if (ext === 'handshake') {
|
if (ext === 'handshake') {
|
||||||
t.pass('got extended handshake')
|
t.pass('got extended handshake')
|
||||||
} else if (ext === 'ut_metadata') {
|
} else if (ext === 'ut_metadata') {
|
||||||
@ -49,40 +46,40 @@ test('fetch()', function (t) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.handshake(parsedTorrent.infoHash, id1)
|
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fetch() from peer without metadata', function (t) {
|
test('fetch() from peer without metadata', t => {
|
||||||
t.plan(2)
|
t.plan(2)
|
||||||
|
|
||||||
var wire1 = new Protocol()
|
const wire1 = new Protocol()
|
||||||
var wire2 = new Protocol()
|
const wire2 = new Protocol()
|
||||||
wire1.pipe(wire2).pipe(wire1)
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
wire1.use(ut_metadata()) // neither wire has metadata
|
wire1.use(utMetadata()) // neither wire has metadata
|
||||||
wire2.use(ut_metadata())
|
wire2.use(utMetadata())
|
||||||
|
|
||||||
wire2.ut_metadata.fetch()
|
wire2.ut_metadata.fetch()
|
||||||
|
|
||||||
wire2.ut_metadata.on('metadata', function () {
|
wire2.ut_metadata.on('metadata', () => {
|
||||||
t.fail('No "metadata" event should fire')
|
t.fail('No "metadata" event should fire')
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.ut_metadata.onMessage = function () {
|
wire1.ut_metadata.onMessage = () => {
|
||||||
t.fail('No messages should be sent to wire1')
|
t.fail('No messages should be sent to wire1')
|
||||||
// No messages should be sent because wire1 never sent metadata_size in the
|
// No messages should be sent because wire1 never sent metadata_size
|
||||||
// extended handshake, so he doesn't have metadata
|
// in the extended handshake, so he doesn't have metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
wire2.ut_metadata.on('warning', function (err) {
|
wire2.ut_metadata.on('warning', () => {
|
||||||
t.pass('got warning about peer missing metadata')
|
t.pass('got warning about peer missing metadata')
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('handshake', function (infoHash, peerId, extensions) {
|
wire2.on('handshake', (infoHash, peerId, extensions) => {
|
||||||
wire2.handshake(parsedTorrent.infoHash, id2)
|
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('extended', function (ext) {
|
wire2.on('extended', ext => {
|
||||||
if (ext === 'handshake') {
|
if (ext === 'handshake') {
|
||||||
t.pass('got extended handshake')
|
t.pass('got extended handshake')
|
||||||
} else if (ext === 'ut_metadata') {
|
} else if (ext === 'ut_metadata') {
|
||||||
@ -92,36 +89,44 @@ test('fetch() from peer without metadata', function (t) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.handshake(parsedTorrent.infoHash, id1)
|
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fetch when peer gets metadata later (setMetadata)', function (t) {
|
test('fetch when peer gets metadata later (setMetadata)', t => {
|
||||||
t.plan(3)
|
t.plan(3)
|
||||||
|
|
||||||
var wire1 = new Protocol()
|
const wire1 = new Protocol()
|
||||||
var wire2 = new Protocol()
|
const wire2 = new Protocol()
|
||||||
|
|
||||||
wire1.pipe(wire2).pipe(wire1)
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
wire1.use(ut_metadata()) // wire1 starts without metadata
|
wire1.use(utMetadata()) // wire1 starts without metadata
|
||||||
|
|
||||||
process.nextTick(function () {
|
process.nextTick(() => {
|
||||||
wire1.ut_metadata.setMetadata(metadata) // wire1 gets metadata later
|
// wire1 gets metadata later
|
||||||
|
wire1.ut_metadata.setMetadata(leavesMetadata.torrent)
|
||||||
|
|
||||||
process.nextTick(function () {
|
process.nextTick(() => {
|
||||||
// wire2 does not start with metadata, but connects to wire1 after it gets metadata
|
// wire2 does not start with metadata,
|
||||||
wire2.use(ut_metadata())
|
// but connects to wire1 after it gets metadata
|
||||||
|
wire2.use(utMetadata())
|
||||||
wire2.ut_metadata.fetch()
|
wire2.ut_metadata.fetch()
|
||||||
|
|
||||||
wire2.ut_metadata.on('metadata', function (_metadata) {
|
wire2.ut_metadata.on('metadata', _metadata => {
|
||||||
// got metadata!
|
// got metadata!
|
||||||
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(metadata).info }).toString('hex'))
|
t.equal(
|
||||||
|
_metadata.toString('hex'),
|
||||||
|
bencode.encode({
|
||||||
|
info: bencode.decode(leavesMetadata.torrent).info
|
||||||
|
}).toString('hex')
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('handshake', function (infoHash, peerId, extensions) {
|
wire2.on('handshake', (infoHash, peerId, extensions) => {
|
||||||
wire2.handshake(parsedTorrent.infoHash, id2)
|
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('extended', function (ext) {
|
wire2.on('extended', ext => {
|
||||||
if (ext === 'handshake') {
|
if (ext === 'handshake') {
|
||||||
t.pass('got extended handshake')
|
t.pass('got extended handshake')
|
||||||
} else if (ext === 'ut_metadata') {
|
} else if (ext === 'ut_metadata') {
|
||||||
@ -134,37 +139,43 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.handshake(parsedTorrent.infoHash, id1)
|
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fetch() large torrent', function (t) {
|
test('fetch() large torrent', t => {
|
||||||
t.plan(4)
|
t.plan(4)
|
||||||
|
|
||||||
var wire1 = new Protocol()
|
const wire1 = new Protocol()
|
||||||
var wire2 = new Protocol()
|
const wire2 = new Protocol()
|
||||||
wire1.pipe(wire2).pipe(wire1)
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
wire1.use(ut_metadata(largeMetadata)) // wire1 already has metadata
|
wire1.use(utMetadata(sintel.torrent)) // wire1 already has metadata
|
||||||
wire2.use(ut_metadata()) // wire2 does not
|
wire2.use(utMetadata()) // wire2 does not
|
||||||
|
|
||||||
wire2.ut_metadata.fetch()
|
wire2.ut_metadata.fetch()
|
||||||
|
|
||||||
wire2.ut_metadata.on('metadata', function (_metadata) {
|
wire2.ut_metadata.on('metadata', _metadata => {
|
||||||
// got metadata!
|
// got metadata!
|
||||||
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(largeMetadata).info }).toString('hex'))
|
t.equal(
|
||||||
|
_metadata.toString('hex'),
|
||||||
|
bencode.encode({
|
||||||
|
info: bencode.decode(sintel.torrent).info
|
||||||
|
}).toString('hex')
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('handshake', function (infoHash, peerId, extensions) {
|
wire2.on('handshake', (infoHash, peerId, extensions) => {
|
||||||
wire2.handshake(largeParsedTorrent.infoHash, id2)
|
wire2.handshake(sintel.parsedTorrent.infoHash, id2)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('extended', function (ext) {
|
wire2.on('extended', ext => {
|
||||||
if (ext === 'handshake') {
|
if (ext === 'handshake') {
|
||||||
t.pass('got extended handshake')
|
t.pass('got extended handshake')
|
||||||
} else if (ext === 'ut_metadata') {
|
} else if (ext === 'ut_metadata') {
|
||||||
// note: this should get called twice, once for each block of the ubuntu metadata
|
// note: this should get called twice,
|
||||||
|
// once for each block of the sintel metadata
|
||||||
t.pass('got extended ut_metadata message')
|
t.pass('got extended ut_metadata message')
|
||||||
|
|
||||||
// this is emitted for consistency's sake, but it's ignored
|
// this is emitted for consistency's sake, but it's ignored
|
||||||
@ -175,35 +186,66 @@ test('fetch() large torrent', function (t) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.handshake(largeParsedTorrent.infoHash, id1)
|
wire1.handshake(sintel.parsedTorrent.infoHash, id1)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('discard invalid metadata', function (t) {
|
test('discard invalid metadata', t => {
|
||||||
t.plan(1)
|
t.plan(1)
|
||||||
|
|
||||||
var wire1 = new Protocol()
|
const wire1 = new Protocol()
|
||||||
var wire2 = new Protocol()
|
const wire2 = new Protocol()
|
||||||
wire1.pipe(wire2).pipe(wire1)
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
var invalidMetadata = metadata.slice(0)
|
const invalidMetadata = leavesMetadata.torrent.slice(0)
|
||||||
invalidMetadata[55] = 65 // mess up a byte in the info block
|
invalidMetadata[55] = 65 // mess up a byte in the info block
|
||||||
|
|
||||||
wire1.use(ut_metadata(invalidMetadata))
|
wire1.use(utMetadata(invalidMetadata))
|
||||||
wire2.use(ut_metadata())
|
wire2.use(utMetadata())
|
||||||
|
|
||||||
wire2.ut_metadata.fetch()
|
wire2.ut_metadata.fetch()
|
||||||
|
|
||||||
wire2.ut_metadata.on('metadata', function () {
|
wire2.ut_metadata.on('metadata', () => {
|
||||||
t.fail('No "metadata" event should fire')
|
t.fail('No "metadata" event should fire')
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.ut_metadata.on('warning', function (err) {
|
wire2.ut_metadata.on('warning', () => {
|
||||||
t.pass('got warning because peer sent reject too much')
|
t.pass('got warning because peer sent reject too much')
|
||||||
})
|
})
|
||||||
|
|
||||||
wire2.on('handshake', function (infoHash, peerId, extensions) {
|
wire2.on('handshake', (infoHash, peerId, extensions) => {
|
||||||
wire2.handshake(parsedTorrent.infoHash, id2)
|
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
|
||||||
})
|
})
|
||||||
|
|
||||||
wire1.handshake(parsedTorrent.infoHash, id1)
|
wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('stop receiving data after cancel', t => {
|
||||||
|
t.plan(1)
|
||||||
|
|
||||||
|
const wire1 = new Protocol()
|
||||||
|
const wire2 = new Protocol()
|
||||||
|
|
||||||
|
wire1.pipe(wire2).pipe(wire1)
|
||||||
|
|
||||||
|
wire1.use(utMetadata(sintel.torrent))
|
||||||
|
wire2.use(utMetadata())
|
||||||
|
|
||||||
|
wire2.ut_metadata.once('metadata', () => {
|
||||||
|
t.fail('No "metadata" event should fire')
|
||||||
|
})
|
||||||
|
|
||||||
|
wire2.once('handshake', (infoHash, peerId, extensions) => {
|
||||||
|
wire2.handshake(sintel.parsedTorrent.infoHash, id2)
|
||||||
|
wire2.ut_metadata.fetch()
|
||||||
|
})
|
||||||
|
|
||||||
|
wire2.on('extended', ext => {
|
||||||
|
if (ext === 'ut_metadata') {
|
||||||
|
wire2.ut_metadata.cancel()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
wire1.handshake(sintel.parsedTorrent.infoHash, id1)
|
||||||
|
|
||||||
|
process.nextTick(() => t.pass('no metadata received'))
|
||||||
})
|
})
|
||||||
|
@ -1,13 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "test",
|
"name": "test",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"author": "Feross Aboukhadijeh <feross@feross.org> (http://feross.org/)",
|
|
||||||
"browserify": {
|
"browserify": {
|
||||||
"transform": ["brfs"]
|
"transform": ["brfs"]
|
||||||
},
|
|
||||||
"license": "MIT",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "tape test/*.js"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user