2022-12-05 22:06:54 +00:00
|
|
|
import Client from '../index.js'
|
|
|
|
import common from './common.js'
|
|
|
|
import test from 'tape'
|
2024-05-22 21:51:48 +00:00
|
|
|
import wrtc from 'webrtc-polyfill'
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
|
|
|
|
const peerId = Buffer.from('01234567890123456789')
|
|
|
|
const peerId2 = Buffer.from('12345678901234567890')
|
|
|
|
const peerId3 = Buffer.from('23456789012345678901')
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-03-24 08:35:19 +00:00
|
|
|
function serverTest (t, serverType, serverFamily) {
|
2017-02-08 21:11:48 +00:00
|
|
|
t.plan(40)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const hostname = serverFamily === 'inet6'
|
2016-03-01 02:09:04 +00:00
|
|
|
? '[::1]'
|
|
|
|
: '127.0.0.1'
|
2020-10-29 04:57:47 +00:00
|
|
|
const clientIp = serverFamily === 'inet6'
|
2016-03-01 02:09:04 +00:00
|
|
|
? '::1'
|
|
|
|
: '127.0.0.1'
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const opts = {
|
2020-04-25 09:34:40 +00:00
|
|
|
serverType
|
2016-06-08 23:28:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
common.createServer(t, opts, server => {
|
2017-01-30 00:25:16 +00:00
|
|
|
// Not using announceUrl param from `common.createServer()` since we
|
|
|
|
// want to control IPv4 vs IPv6.
|
2020-10-29 04:57:47 +00:00
|
|
|
const port = server[serverType].address().port
|
2021-06-15 01:54:41 +00:00
|
|
|
const announceUrl = `${serverType}://${hostname}:${port}/announce`
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const client1 = new Client({
|
2020-04-25 09:34:40 +00:00
|
|
|
infoHash,
|
2019-07-05 21:36:14 +00:00
|
|
|
announce: [announceUrl],
|
2020-04-25 09:34:40 +00:00
|
|
|
peerId,
|
2017-01-30 00:25:16 +00:00
|
|
|
port: 6881,
|
2020-04-25 09:34:40 +00:00
|
|
|
wrtc
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
})
|
2017-01-30 00:25:16 +00:00
|
|
|
if (serverType === 'ws') common.mockWebsocketTracker(client1)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2015-05-17 06:24:20 +00:00
|
|
|
client1.start()
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.once('start', () => {
|
2014-05-19 01:35:12 +00:00
|
|
|
t.pass('got start message from client1')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.once('update', data => {
|
2014-03-27 07:17:49 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 0)
|
|
|
|
t.equal(data.incomplete, 1)
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.getSwarm(infoHash, (err, swarm) => {
|
2016-01-03 18:50:23 +00:00
|
|
|
t.error(err)
|
|
|
|
|
|
|
|
t.equal(Object.keys(server.torrents).length, 1)
|
|
|
|
t.equal(swarm.complete, 0)
|
|
|
|
t.equal(swarm.incomplete, 1)
|
2016-06-10 08:00:19 +00:00
|
|
|
t.equal(swarm.peers.length, 1)
|
2016-03-13 22:57:39 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const id = serverType === 'ws'
|
2016-03-17 00:58:47 +00:00
|
|
|
? peerId.toString('hex')
|
2021-06-15 01:54:41 +00:00
|
|
|
: `${hostname}:6881`
|
2016-03-17 00:58:47 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const peer = swarm.peers.peek(id)
|
2016-06-08 21:57:01 +00:00
|
|
|
t.equal(peer.type, serverType)
|
|
|
|
t.equal(peer.ip, clientIp)
|
|
|
|
t.equal(peer.peerId, peerId.toString('hex'))
|
|
|
|
t.equal(peer.complete, false)
|
2016-03-17 00:58:47 +00:00
|
|
|
if (serverType === 'ws') {
|
2016-06-08 21:57:01 +00:00
|
|
|
t.equal(typeof peer.port, 'number')
|
|
|
|
t.ok(peer.socket)
|
2016-03-13 22:57:39 +00:00
|
|
|
} else {
|
2016-06-08 21:57:01 +00:00
|
|
|
t.equal(peer.port, 6881)
|
|
|
|
t.notOk(peer.socket)
|
2016-03-13 22:57:39 +00:00
|
|
|
}
|
2014-04-19 06:00:40 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client1.complete()
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.once('update', data => {
|
2016-01-03 18:50:23 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 0)
|
2014-03-27 07:17:49 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client1.scrape()
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.once('scrape', data => {
|
2016-01-03 18:50:23 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2017-01-30 00:25:16 +00:00
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 0)
|
2016-01-03 18:50:23 +00:00
|
|
|
t.equal(typeof data.downloaded, 'number')
|
2014-05-18 16:15:20 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const client2 = new Client({
|
2020-04-25 09:34:40 +00:00
|
|
|
infoHash,
|
2019-07-05 21:36:14 +00:00
|
|
|
announce: [announceUrl],
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
peerId: peerId2,
|
2017-01-30 00:25:16 +00:00
|
|
|
port: 6882,
|
2020-04-25 09:34:40 +00:00
|
|
|
wrtc
|
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments:
`new Client(peerId, port, parsedTorrent, opts)`
Now, passing in the torrent is no longer required, just the `announce`
and `infoHash` properties. This decouples this package from
`parse-torrent`.
All options get passed in together now:
new Client({
infoHash: '', // hex string or Buffer
peerId: '', // hex string or Buffer
announce: [], // list of tracker server urls
port: 6881 // torrent client port, (in browser, optional)
})
All the normal optional arguments (rtcConfig, wrtc, etc.) can still be
passed in with the rest of these options.
Fixes #118. Fixes #115.
Added ws tests for scrape.
2016-04-01 04:37:51 +00:00
|
|
|
})
|
2017-01-30 00:25:16 +00:00
|
|
|
if (serverType === 'ws') common.mockWebsocketTracker(client2)
|
2014-05-19 01:35:12 +00:00
|
|
|
|
2016-01-03 18:50:23 +00:00
|
|
|
client2.start()
|
2014-05-19 01:35:12 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.once('start', () => {
|
2016-01-03 18:50:23 +00:00
|
|
|
t.pass('got start message from client2')
|
|
|
|
})
|
2014-05-12 00:32:57 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client2.once('update', data => {
|
2017-01-30 00:25:16 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 1)
|
2014-05-09 06:38:41 +00:00
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const client3 = new Client({
|
2020-04-25 09:34:40 +00:00
|
|
|
infoHash,
|
2019-07-05 21:36:14 +00:00
|
|
|
announce: [announceUrl],
|
2016-06-08 23:28:49 +00:00
|
|
|
peerId: peerId3,
|
2017-01-30 00:25:16 +00:00
|
|
|
port: 6880,
|
2020-04-25 09:34:40 +00:00
|
|
|
wrtc
|
2016-06-08 23:28:49 +00:00
|
|
|
})
|
2017-01-30 00:25:16 +00:00
|
|
|
if (serverType === 'ws') common.mockWebsocketTracker(client3)
|
|
|
|
|
2016-06-08 23:28:49 +00:00
|
|
|
client3.start()
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.once('start', () => {
|
2016-06-08 23:28:49 +00:00
|
|
|
t.pass('got start message from client3')
|
|
|
|
})
|
2016-01-03 18:50:23 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client3.once('update', data => {
|
2017-01-30 00:25:16 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 2)
|
|
|
|
|
2016-06-08 23:28:49 +00:00
|
|
|
client2.stop()
|
2021-06-15 01:54:41 +00:00
|
|
|
client2.once('update', data => {
|
2016-01-03 18:50:23 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
2016-06-08 23:28:49 +00:00
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 1)
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client2.destroy(() => {
|
2017-02-08 21:11:48 +00:00
|
|
|
t.pass('client2 destroyed')
|
2017-01-30 00:25:16 +00:00
|
|
|
client3.stop()
|
2021-06-15 01:54:41 +00:00
|
|
|
client3.once('update', data => {
|
2017-01-30 00:25:16 +00:00
|
|
|
t.equal(data.announce, announceUrl)
|
|
|
|
t.equal(data.complete, 1)
|
|
|
|
t.equal(data.incomplete, 0)
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client1.destroy(() => {
|
2017-02-08 21:11:48 +00:00
|
|
|
t.pass('client1 destroyed')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
client3.destroy(() => {
|
2017-02-08 21:11:48 +00:00
|
|
|
t.pass('client3 destroyed')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
server.close(() => {
|
2017-02-08 21:11:48 +00:00
|
|
|
t.pass('server destroyed')
|
2016-06-08 23:28:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-03-27 03:29:05 +00:00
|
|
|
})
|
2016-01-03 18:50:23 +00:00
|
|
|
})
|
2014-05-19 01:35:12 +00:00
|
|
|
})
|
2014-05-18 16:15:20 +00:00
|
|
|
})
|
2014-05-09 06:38:41 +00:00
|
|
|
})
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-07-11 10:49:45 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('http ipv4 server', t => {
|
2014-12-12 23:09:40 +00:00
|
|
|
serverTest(t, 'http', 'inet')
|
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('http ipv6 server', t => {
|
2014-12-12 23:09:40 +00:00
|
|
|
serverTest(t, 'http', 'inet6')
|
2014-07-11 10:49:45 +00:00
|
|
|
})
|
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('udp server', t => {
|
2014-12-12 23:09:40 +00:00
|
|
|
serverTest(t, 'udp', 'inet')
|
2014-03-27 07:17:49 +00:00
|
|
|
})
|
2017-01-30 00:25:16 +00:00
|
|
|
|
2021-06-15 01:54:41 +00:00
|
|
|
test('ws server', t => {
|
2019-08-08 01:55:59 +00:00
|
|
|
serverTest(t, 'ws', 'inet')
|
2017-01-30 00:25:16 +00:00
|
|
|
})
|