Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/peer.js')
-rw-r--r--lib/peer.js250
1 files changed, 125 insertions, 125 deletions
diff --git a/lib/peer.js b/lib/peer.js
index abb0bcc..98847b2 100644
--- a/lib/peer.js
+++ b/lib/peer.js
@@ -1,8 +1,8 @@
-const EventEmitter = require('events')
-const { Transform } = require('stream')
-const arrayRemove = require('unordered-array-remove')
-const debugFactory = require('debug')
-const Wire = require('bittorrent-protocol')
+import EventEmitter from 'events'
+import { Transform } from 'stream'
+import arrayRemove from 'unordered-array-remove'
+import debugFactory from 'debug'
+import Wire from 'bittorrent-protocol'
const CONNECT_TIMEOUT_TCP = 5_000
const CONNECT_TIMEOUT_UTP = 5_000
@@ -24,139 +24,21 @@ const SOURCE_DHT = 'dht'
const SOURCE_LSD = 'lsd'
const SOURCE_UT_PEX = 'ut_pex'
-exports.TYPE_TCP_INCOMING = TYPE_TCP_INCOMING
-exports.TYPE_TCP_OUTGOING = TYPE_TCP_OUTGOING
-exports.TYPE_UTP_INCOMING = TYPE_UTP_INCOMING
-exports.TYPE_UTP_OUTGOING = TYPE_UTP_OUTGOING
-exports.TYPE_WEBRTC = TYPE_WEBRTC
-exports.TYPE_WEBSEED = TYPE_WEBSEED
-
-exports.SOURCE_MANUAL = SOURCE_MANUAL
-exports.SOURCE_TRACKER = SOURCE_TRACKER
-exports.SOURCE_DHT = SOURCE_DHT
-exports.SOURCE_LSD = SOURCE_LSD
-exports.SOURCE_UT_PEX = SOURCE_UT_PEX
-
const debug = debugFactory('webtorrent:peer')
let secure = false
-exports.enableSecure = () => {
+export const enableSecure = () => {
secure = true
}
/**
- * WebRTC peer connections start out connected, because WebRTC peers require an
- * "introduction" (i.e. WebRTC signaling), and there's no equivalent to an IP address
- * that lets you refer to a WebRTC endpoint.
- */
-exports.createWebRTCPeer = (conn, swarm, throttleGroups) => {
- const peer = new Peer(conn.id, 'webrtc')
- peer.conn = conn
- peer.swarm = swarm
- peer.throttleGroups = throttleGroups
-
- if (peer.conn.connected) {
- peer.onConnect()
- } else {
- const cleanup = () => {
- peer.conn.removeListener('connect', onConnect)
- peer.conn.removeListener('error', onError)
- }
- const onConnect = () => {
- cleanup()
- peer.onConnect()
- }
- const onError = err => {
- cleanup()
- peer.destroy(err)
- }
- peer.conn.once('connect', onConnect)
- peer.conn.once('error', onError)
- peer.startConnectTimeout()
- }
-
- return peer
-}
-
-/**
- * Incoming TCP peers start out connected, because the remote peer connected to the
- * listening port of the TCP server. Until the remote peer sends a handshake, we don't
- * know what swarm the connection is intended for.
- */
-exports.createTCPIncomingPeer = (conn, throttleGroups) => {
- return _createIncomingPeer(conn, TYPE_TCP_INCOMING, throttleGroups)
-}
-
-/**
- * Incoming uTP peers start out connected, because the remote peer connected to the
- * listening port of the uTP server. Until the remote peer sends a handshake, we don't
- * know what swarm the connection is intended for.
- */
-exports.createUTPIncomingPeer = (conn, throttleGroups) => {
- return _createIncomingPeer(conn, TYPE_UTP_INCOMING, throttleGroups)
-}
-
-/**
- * Outgoing TCP peers start out with just an IP address. At some point (when there is an
- * available connection), the client can attempt to connect to the address.
- */
-exports.createTCPOutgoingPeer = (addr, swarm, throttleGroups) => {
- return _createOutgoingPeer(addr, swarm, TYPE_TCP_OUTGOING, throttleGroups)
-}
-
-/**
- * Outgoing uTP peers start out with just an IP address. At some point (when there is an
- * available connection), the client can attempt to connect to the address.
- */
-exports.createUTPOutgoingPeer = (addr, swarm, throttleGroups) => {
- return _createOutgoingPeer(addr, swarm, TYPE_UTP_OUTGOING, throttleGroups)
-}
-
-const _createIncomingPeer = (conn, type, throttleGroups) => {
- const addr = `${conn.remoteAddress}:${conn.remotePort}`
- const peer = new Peer(addr, type)
- peer.conn = conn
- peer.addr = addr
- peer.throttleGroups = throttleGroups
-
- peer.onConnect()
-
- return peer
-}
-
-const _createOutgoingPeer = (addr, swarm, type, throttleGroups) => {
- const peer = new Peer(addr, type)
- peer.addr = addr
- peer.swarm = swarm
- peer.throttleGroups = throttleGroups
-
- return peer
-}
-
-/**
- * Peer that represents a Web Seed (BEP17 / BEP19).
- */
-
-exports.createWebSeedPeer = (conn, id, swarm, throttleGroups) => {
- const peer = new Peer(id, TYPE_WEBSEED)
-
- peer.swarm = swarm
- peer.conn = conn
- peer.throttleGroups = throttleGroups
-
- peer.onConnect()
-
- return peer
-}
-
-/**
* Peer. Represents a peer in the torrent swarm.
*
* @param {string} id "ip:port" string, peer id (for WebRTC peers), or url (for Web Seeds)
* @param {string} type the type of the peer
*/
-class Peer extends EventEmitter {
+export default class Peer extends EventEmitter {
constructor (id, type) {
super()
@@ -418,3 +300,121 @@ class Peer extends EventEmitter {
if (swarm) swarm.removePeer(this.id)
}
}
+
+Peer.TYPE_TCP_INCOMING = TYPE_TCP_INCOMING
+Peer.TYPE_TCP_OUTGOING = TYPE_TCP_OUTGOING
+Peer.TYPE_UTP_INCOMING = TYPE_UTP_INCOMING
+Peer.TYPE_UTP_OUTGOING = TYPE_UTP_OUTGOING
+Peer.TYPE_WEBRTC = TYPE_WEBRTC
+Peer.TYPE_WEBSEED = TYPE_WEBSEED
+
+Peer.SOURCE_MANUAL = SOURCE_MANUAL
+Peer.SOURCE_TRACKER = SOURCE_TRACKER
+Peer.SOURCE_DHT = SOURCE_DHT
+Peer.SOURCE_LSD = SOURCE_LSD
+Peer.SOURCE_UT_PEX = SOURCE_UT_PEX
+
+/**
+ * WebRTC peer connections start out connected, because WebRTC peers require an
+ * "introduction" (i.e. WebRTC signaling), and there's no equivalent to an IP address
+ * that lets you refer to a WebRTC endpoint.
+ */
+Peer.createWebRTCPeer = (conn, swarm, throttleGroups) => {
+ const peer = new Peer(conn.id, 'webrtc')
+ peer.conn = conn
+ peer.swarm = swarm
+ peer.throttleGroups = throttleGroups
+
+ if (peer.conn.connected) {
+ peer.onConnect()
+ } else {
+ const cleanup = () => {
+ peer.conn.removeListener('connect', onConnect)
+ peer.conn.removeListener('error', onError)
+ }
+ const onConnect = () => {
+ cleanup()
+ peer.onConnect()
+ }
+ const onError = err => {
+ cleanup()
+ peer.destroy(err)
+ }
+ peer.conn.once('connect', onConnect)
+ peer.conn.once('error', onError)
+ peer.startConnectTimeout()
+ }
+
+ return peer
+}
+
+/**
+ * Incoming TCP peers start out connected, because the remote peer connected to the
+ * listening port of the TCP server. Until the remote peer sends a handshake, we don't
+ * know what swarm the connection is intended for.
+ */
+Peer.createTCPIncomingPeer = (conn, throttleGroups) => {
+ return Peer._createIncomingPeer(conn, TYPE_TCP_INCOMING, throttleGroups)
+}
+
+/**
+ * Incoming uTP peers start out connected, because the remote peer connected to the
+ * listening port of the uTP server. Until the remote peer sends a handshake, we don't
+ * know what swarm the connection is intended for.
+ */
+Peer.createUTPIncomingPeer = (conn, throttleGroups) => {
+ return Peer._createIncomingPeer(conn, TYPE_UTP_INCOMING, throttleGroups)
+}
+
+/**
+ * Outgoing TCP peers start out with just an IP address. At some point (when there is an
+ * available connection), the client can attempt to connect to the address.
+ */
+Peer.createTCPOutgoingPeer = (addr, swarm, throttleGroups) => {
+ return Peer._createOutgoingPeer(addr, swarm, TYPE_TCP_OUTGOING, throttleGroups)
+}
+
+/**
+ * Outgoing uTP peers start out with just an IP address. At some point (when there is an
+ * available connection), the client can attempt to connect to the address.
+ */
+Peer.createUTPOutgoingPeer = (addr, swarm, throttleGroups) => {
+ return Peer._createOutgoingPeer(addr, swarm, TYPE_UTP_OUTGOING, throttleGroups)
+}
+
+Peer._createIncomingPeer = (conn, type, throttleGroups) => {
+ const addr = `${conn.remoteAddress}:${conn.remotePort}`
+ const peer = new Peer(addr, type)
+ peer.conn = conn
+ peer.addr = addr
+ peer.throttleGroups = throttleGroups
+
+ peer.onConnect()
+
+ return peer
+}
+
+Peer._createOutgoingPeer = (addr, swarm, type, throttleGroups) => {
+ const peer = new Peer(addr, type)
+ peer.addr = addr
+ peer.swarm = swarm
+ peer.throttleGroups = throttleGroups
+
+ return peer
+}
+
+/**
+ * Peer that represents a Web Seed (BEP17 / BEP19).
+ */
+
+Peer.createWebSeedPeer = (conn, id, swarm, throttleGroups) => {
+ const peer = new Peer(id, TYPE_WEBSEED)
+
+ peer.swarm = swarm
+ peer.conn = conn
+ peer.throttleGroups = throttleGroups
+
+ peer.onConnect()
+
+ return peer
+}