Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Local Service Discovery (BEP14) #48

Merged
merged 2 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

### Discover BitTorrent and WebTorrent peers

This module bundles [bittorrent-dht](https://www.npmjs.com/package/bittorrent-dht) and
[bittorrent-tracker](https://www.npmjs.com/package/bittorrent-tracker) clients and exposes a
single API for discovering BitTorrent peers via both discovery methods.
This module bundles [bittorrent-dht](https://www.npmjs.com/package/bittorrent-dht),
[bittorrent-tracker](https://www.npmjs.com/package/bittorrent-tracker) and [bittorrent-lsd](https://www.npmjs.com/package/bittorrent-lsd) clients and exposes a single API for discovering BitTorrent peers via both discovery methods.

## features

- simple API
- find peers from trackers and the DHT
- find peers from trackers, DHT and LSD
- automatically announces, so other peers can discover us
- can start finding peers with just an info hash, before full metadata is available

Expand Down Expand Up @@ -54,12 +53,12 @@ Optional options are:
dhtPort: 0, // custom listen port for the DHT instance (not used if DHT instance is given via `opts.dht`)
userAgent: '', // User-Agent header for http requests
tracker: true, // use trackers? optionally, this can be an `opts` object
lsd: true // use lsd?
}
```

See the documentation for [bittorrent-dht](https://www.npmjs.com/package/bittorrent-dht) and
[bittorrent-tracker](https://www.npmjs.com/package/bittorrent-tracker) for information on what
options are available via the `opts` object.
See the documentation for [bittorrent-dht](https://www.npmjs.com/package/bittorrent-dht),
[bittorrent-tracker](https://www.npmjs.com/package/bittorrent-tracker) and [bittorrant-lsd](https://www.npmjs.com/package/bittorrent-lsd) for information on what options are available via the `opts` object.

**This module automatically handles announcing on intervals, for maximum peer discovery.**

Expand All @@ -85,13 +84,13 @@ Optional `opts` object with the following options:

### `discovery.destroy()`

Destroy and cleanup the DHT and tracker instances.
Destroy and cleanup the DHT, tracker and LSD instances.

### events

### `discovery.on('peer', (peer, source) => {})`

Emitted whenever a new peer is discovered. Source is either 'tracker' or 'dht' based on peer source.
Emitted whenever a new peer is discovered. Source is either 'tracker', 'dht' or 'lsd' based on peer source.

**In node**, `peer` is a string in the form `ip:port`, e.g. `12.34.56.78:4000`.

Expand All @@ -105,12 +104,12 @@ Emitted whenever an `announce` message has been sent to the DHT.

### `discovery.on('warning', err => {})`

Emitted when there is a non-fatal DHT or tracker error, like an inaccessible tracker
Emitted when there is a non-fatal DHT, tracker or LSD error, like an inaccessible tracker
server. Useful for logging. This is non-fatal.

### `discovery.on('error', err => {})`

Emitted when there is a fatal, unrecoverable DHT or tracker error.
Emitted when there is a fatal, unrecoverable DHT, tracker or LSD error.

## license

Expand Down
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const DHT = require('bittorrent-dht/client') // empty object in browser
const EventEmitter = require('events').EventEmitter
const parallel = require('run-parallel')
const Tracker = require('bittorrent-tracker/client')
const LSD = require('bittorrent-lsd')

class Discovery extends EventEmitter {
constructor (opts) {
Expand Down Expand Up @@ -47,6 +48,9 @@ class Discovery extends EventEmitter {
this._onTrackerAnnounce = () => {
this.emit('trackerAnnounce')
}
this._onLSDPeer = (peer, infoHash) => {
this.emit('peer', peer, 'lsd')
}

const createDHT = (port, opts) => {
const dht = new DHT(opts)
Expand Down Expand Up @@ -80,6 +84,12 @@ class Discovery extends EventEmitter {
this.dht.on('peer', this._onDHTPeer)
this._dhtAnnounce()
}

if (opts.lsd === false) {
this.lsd = null
} else {
this.lsd = this._createLSD()
}
}

updatePort (port) {
Expand Down Expand Up @@ -133,11 +143,21 @@ class Discovery extends EventEmitter {
})
}

if (this.lsd) {
this.lsd.removeListener('warning', this._onWarning)
this.lsd.removeListener('error', this._onError)
this.lsd.removeListener('peer', this._onLSDPeer)
tasks.push(cb => {
this.lsd.destroy(cb)
})
}

parallel(tasks, cb)

// cleanup
this.dht = null
this.tracker = null
this.lsd = null
this._announce = null
}

Expand Down Expand Up @@ -182,6 +202,21 @@ class Discovery extends EventEmitter {
}
})
}

_createLSD () {
const opts = Object.assign({}, {
infoHash: this.infoHash,
peerId: this.peerId,
port: this._port
})

const lsd = new LSD(opts)
lsd.on('warning', this._onWarning)
lsd.on('error', this._onError)
lsd.on('peer', this._onLSDPeer)
lsd.start()
return lsd
}
}

module.exports = Discovery
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"url": "https://webtorrent.io"
},
"browser": {
"bittorrent-dht/client": false
"bittorrent-dht/client": false,
"bittorrent-lsd": false
},
"chromeapp": {},
"bugs": {
Expand All @@ -17,6 +18,7 @@
"dependencies": {
"bittorrent-dht": "^10.0.0",
"bittorrent-tracker": "^9.0.0",
"bittorrent-lsd": "^1.0.0",
"debug": "^4.0.0",
"run-parallel": "^1.1.2"
},
Expand All @@ -33,6 +35,7 @@
"discovery",
"tracker",
"dht",
"lsd",
"trackers",
"find peers",
"peer-to-peer",
Expand Down
20 changes: 18 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ test('initialize with dht', t => {
})
})

test('initialize with default dht', t => {
t.plan(1)
test('initialize with default dht and lsd', t => {
t.plan(3)
const discovery = new Discovery({
infoHash: randombytes(20),
peerId: randombytes(20),
port: 6000
})
t.ok(discovery.dht)
t.ok(discovery.lsd)
discovery.destroy(() => {
t.pass()
})
Expand All @@ -44,3 +46,17 @@ test('initialize without dht', t => {
t.pass()
})
})

test('initialize without lsd', t => {
t.plan(2)
const discovery = new Discovery({
infoHash: randombytes(20),
peerId: randombytes(20),
port: 6000,
lsd: false
})
t.equal(discovery.lsd, null)
discovery.destroy(() => {
t.pass()
})
})