ut_pex

Implementation of ut_pex bittorrent protocol (PEX) for webtorrent

Github stars Tracking Chart

ut_pex travis npm downloads javascript style guide Greenkeeper badge

BitTorrent Extension for Peer Discovery (PEX)

Node.js implementation of the ut_pex protocol, which is the most popular PEX (peer exchange) protocol used by bittorrent clients.

The purpose of this extension is to allow peers to exchange known peers directly with each other, thereby facilitating more efficient peer discovery and healthier swarms. The best description of the (nonstandardized) ut_pex protocol I could find is in section 2.1.4.3 of this paper.

Works in the browser with browserify! This module is used by WebTorrent.

install

npm install ut_pex

usage

This package should be used with 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:

var Protocol = require('bittorrent-protocol')
var net = require('net')

net.createServer(function (socket) {
  var wire = new Protocol()
  socket.pipe(wire).pipe(socket)

  // handle handshake
  wire.on('handshake', function (infoHash, peerId) {
    wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
  })

}).listen(6881)

To add support for PEX, simply modify your code like this:

var Protocol = require('bittorrent-protocol')
var net = require('net')
var ut_pex = require('ut_pex')

net.createServer(function (socket) {
  var wire = new Protocol()
  socket.pipe(wire).pipe(socket)

  // initialize the extension
  wire.use(ut_pex())

  // all `ut_pex` functionality can now be accessed at wire.ut_pex

  // (optional) start sending peer information to remote peer
  wire.ut_pex.start()

  // 'peer' event will fire for every new peer sent by the remote peer
  wire.ut_pex.on('peer', function (peer) {
    // got a peer
    // probably add it to peer connections queue
  })

  // handle handshake
  wire.on('handshake', function (infoHash, peerId) {
    wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
  })

}).listen(6881)

methods

start

Start sending regular PEX updates to the remote peer. Use addPeer and dropPeer to control the
content of PEX messages. PEX messages will be sent once every ~65 seconds.

wire.ut_pex.start()

Note that ut_pex may be used for one-way peer discovery without sending PEX updates to the remote peer,
but this use case is discouraged because PEX, like bittorrent is more efficient through altruism.

stop

Stop sending PEX updates to the remote peer.

wire.ut_pex.stop()

reset

Stops sending updates to the remote peer and resets internal state of peers seen.

wire.ut_pex.reset()

addPeer

Adds a peer to the locally discovered peer list to send with the next PEX message.

wire.ut_pex.addPeer('127.0.0.1:6889')

dropPeer

Adds a peer to the locally dropped peer list to send with the next PEX message.

wire.ut_pex.dropPeer('127.0.0.1:6889')

events

event: 'peer'

Fired for every new peer received from PEX.

wire.ut_pex.on('peer', function (peer) {
  var parts = peer.split(':')
  var ip = parts[0]
  var port = parts[1]
  // ...
})

Note: the event will not fire if the peer does not support ut_pex or if they don't respond.

event: 'dropped'

Fired for every peer dropped from the swarm notified via PEX.

wire.ut_pex.on('dropped', function (peer) {
  var parts = peer.split(':')
  var ip = parts[0]
  var port = parts[1]
  // ...
})

Note: the event will not fire if the peer does not support ut_pex or if they don't respond.

todo

(prioritized highest to lowest)

  • basic discovery
  • basic advertisement
  • basic unit tests
  • better unit tests
  • peer flag support
  • destroy wire if peer sends PEX messages too frequently
  • ipv6 support

license

MIT. Copyright (c) Travis Fischer and WebTorrent, LLC

Main metrics

Overview
Name With Ownerwebtorrent/ut_pex
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2014-06-02 05:10:15
Pushed At2025-08-05 16:59:37
Last Commit At2025-08-05 13:30:29
Release Count19
Last Release Namev4.0.4 (Posted on )
First Release Namev1.1.0 (Posted on 2016-03-29 02:43:26)
用户参与
Stargazers Count61
Watchers Count5
Fork Count19
Commits Count174
Has Issues Enabled
Issues Count13
Issue Open Count1
Pull Requests Count90
Pull Requests Open Count5
Pull Requests Close Count3
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private