From 6d9ce7f407688ba6a66e8665386bee6caacc677b Mon Sep 17 00:00:00 2001 From: Billie Hilton <587740+billiegoose@users.noreply.github.com> Date: Mon, 8 Jul 2024 12:39:40 -0400 Subject: [PATCH] cleanup --- lib/connect.js | 19 ++--------------- lib/raw-stream-set.js | 4 ---- lib/server.js | 26 +++++++++-------------- test/relaying.js | 48 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/lib/connect.js b/lib/connect.js index b7688e61..6a7b4257 100644 --- a/lib/connect.js +++ b/lib/connect.js @@ -1,5 +1,3 @@ -/** @import HyperDHT from '../index' */ -/** @import UDXStream from 'udx-native/lib/stream' */ const NoiseSecretStream = require('@hyperswarm/secret-stream') const b4a = require('b4a') const relay = require('blind-relay') @@ -32,12 +30,6 @@ const { SUSPENDED } = require('./errors') -/** - * @param {HyperDHT} dht - * @param {*} publicKey - * @param {*} opts - * @returns - */ module.exports = function connect (dht, publicKey, opts = {}) { dht.tracer.trace('connect', { publicKey, options: opts }) @@ -425,7 +417,6 @@ async function connectThroughNode (c, address, socket) { if (c.rawStream === null) return // Already hole punched if (c.rawStream.connected) { - // TODO: ask @mafintosh if this "un-relaying" logic is correct c.rawStream.once('remote-changed', () => { c.dht.tracer.trace('remote-changed', { relay: c.encryptedSocket.relay }) c.encryptedSocket.relay = { @@ -434,9 +425,7 @@ async function connectThroughNode (c, address, socket) { relayThrough: null, remotePublicKey: null } - // Note - this is really an "un-relay" event but subscribers should know - // to look at the .relay.relaying property - this.encryptedSocket.emit('relay', c.encryptedSocket.relay) + this.encryptedSocket.emit('unrelay', c.encryptedSocket.relay) }) const remoteChanging = c.rawStream.changeRemote(socket, c.connect.payload.udx.id, port, host) @@ -717,9 +706,6 @@ function relayConnection (c, relayThrough, payload, hs) { relayThrough: publicKey, remotePublicKey: c.remotePublicKey } - c.encryptedSocket.emit('relay', c.encryptedSocket.relay) - c.dht.tracer.trace('relay-connect', { relayThrough: publicKey, remotePublicKey: c.remotePublicKey }) - c.relayToken = token c.relaySocket = c.dht.connect(publicKey) c.relaySocket.setKeepAlive(c.relayKeepAlive) @@ -732,7 +718,6 @@ function relayConnection (c, relayThrough, payload, hs) { .on('data', ondata) function ondata (remoteId) { - c.dht.tracer.trace('relay-connected', { relayThrough: publicKey, remotePublicKey: c.remotePublicKey }) if (c.relayTimeout) clearRelayTimeout(c) if (c.rawStream === null) { onabort(null) @@ -772,6 +757,7 @@ function relayConnection (c, relayThrough, payload, hs) { relayThrough: null, remotePublicKey: null } + c.encryptedSocket.emit('relay-aborted', c.encryptedSocket.relay) if (socket) socket.destroy() maybeDestroyEncryptedSocket(c, err || RELAY_ABORTED()) } @@ -788,7 +774,6 @@ function clearRelayTimeout (c) { } function destroyPuncher (c) { - c.dht.tracer.trace('destroy-puncher') if (c.puncher) c.puncher.destroy() c.session.destroy() } diff --git a/lib/raw-stream-set.js b/lib/raw-stream-set.js index 8e8f92c4..9ad04923 100644 --- a/lib/raw-stream-set.js +++ b/lib/raw-stream-set.js @@ -1,9 +1,5 @@ -/** @import HyperDHT from '../index' */ - module.exports = class RawStreamSet { - /** @param {HyperDHT} dht */ constructor (dht) { - /** @type {HyperDHT} */ this._dht = dht this._prefix = 16 - 1 // 16 is the default stream-set side in udx diff --git a/lib/server.js b/lib/server.js index a9cbb728..8843c7a4 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,4 +1,3 @@ -/** @import HyperDHT from '../index' */ const { EventEmitter } = require('events') const safetyCatch = require('safety-catch') const NoiseSecretStream = require('@hyperswarm/secret-stream') @@ -22,7 +21,6 @@ const HANDSHAKE_INITIAL_TIMEOUT = 10000 module.exports = class Server extends EventEmitter { constructor (dht, opts = {}) { super() - /** @type {HyperDHT} */ this.dht = dht this.target = null @@ -307,7 +305,6 @@ module.exports = class Server extends EventEmitter { hs.rawStream.removeListener('error', autoDestroy) if (hs.rawStream.connected) { - // TODO: ask @mafintosh if this "un-relaying" logic is correct hs.rawStream.once('remote-changed', () => { this.tracer.trace('remote-changed', { relay: hs.encryptedSocket.relay }) hs.encryptedSocket.relay = { @@ -316,9 +313,7 @@ module.exports = class Server extends EventEmitter { relayThrough: null, remotePublicKey: null } - // Note - this is really an "un-relay" event but subscribers should know - // to look at the .relay.relaying property - hs.encryptedSocket.emit('relay', hs.encryptedSocket.relay) + hs.encryptedSocket.emit('unrelay', hs.encryptedSocket.relay) }) const remoteChanging = hs.rawStream.changeRemote(socket, remotePayload.udx.id, port, host) @@ -335,6 +330,13 @@ module.exports = class Server extends EventEmitter { keepAlive: this.dht.connectionKeepAlive }) + hs.encryptedSocket.relay = { + relaying: false, + relayPaired: false, + relayThrough: null, + remotePublicKey: null + } + this.onconnection(hs.encryptedSocket) } @@ -628,14 +630,6 @@ module.exports = class Server extends EventEmitter { token = remotePayload.relayThrough.token } - this.emit('relay', { - relaying: true, - relayPaired: false, - relayThrough: publicKey, - remotePublicKey: h.remotePublicKey - }) - hs.tracer.trace('relay-connect', { relayThrough: publicKey, remotePublicKey: h.remotePublicKey }) - hs.relayToken = token hs.relaySocket = this.dht.connect(publicKey) hs.relaySocket.setKeepAlive(this.relayKeepAlive) @@ -646,7 +640,6 @@ module.exports = class Server extends EventEmitter { .pair(isInitiator, token, hs.rawStream) .on('error', onabort) .on('data', (remoteId) => { - hs.tracer.trace('relay-connected', { relayThrough: publicKey, remotePublicKey: hs.remotePublicKey }) if (hs.relayTimeout) clearRelayTimeout(hs) if (hs.rawStream === null) { onabort(null) @@ -679,7 +672,7 @@ module.exports = class Server extends EventEmitter { relayThrough: publicKey, remotePublicKey: h.remotePublicKey } - this.emit('relay', hs.encryptedSocket.relay) + hs.encryptedSocket.emit('relay', hs.encryptedSocket.relay) this.onconnection(hs.encryptedSocket) }) @@ -696,6 +689,7 @@ module.exports = class Server extends EventEmitter { relayThrough: null, remotePublicKey: null } + hs.encryptedSocket.emit('relay-aborted', hs.encryptedSocket.relay) if (socket) socket.destroy() } } diff --git a/test/relaying.js b/test/relaying.js index 3ac8edfe..e0c13a1e 100644 --- a/test/relaying.js +++ b/test/relaying.js @@ -11,7 +11,7 @@ test('relay connections through node, client side', async function (t) { const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(8) const aServer = a.createServer(function (socket) { lc.pass('server socket opened') @@ -46,8 +46,13 @@ test('relay connections through node, client side', async function (t) { const aSocket = c.connect(aServer.publicKey, { relayThrough: bServer.publicKey }) aSocket + .on('relay-aborted', (data) => { + lc.pass('relay aborted') + lc.is(aSocket.relay.relaying, false) + }) .on('open', () => { lc.pass('client socket opened') + lc.is(aSocket.relay.relaying, true) }) .on('close', () => { lc.pass('client socket closed') @@ -69,7 +74,7 @@ test('relay connections through node, client side, client aborts hole punch', as const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const aServer = a.createServer(function (socket) { lc.pass('server socket opened') @@ -110,6 +115,9 @@ test('relay connections through node, client side, client aborts hole punch', as .on('close', () => { lc.pass('client socket closed') }) + .on('relay-aborted', (data) => { + lc.pass('client relay aborted') + }) .end('hello world') await lc @@ -127,7 +135,7 @@ test('relay connections through node, client side, server aborts hole punch', as const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const aServer = a.createServer({ holepunch: () => false }, function (socket) { lc.pass('server socket opened') @@ -168,6 +176,9 @@ test('relay connections through node, client side, server aborts hole punch', as .on('close', () => { lc.pass('client socket closed') }) + .on('relay-aborted', (data) => { + lc.pass('client relay aborted') + }) .end('hello world') await lc @@ -185,7 +196,7 @@ test('relay connections through node, server side', async function (t) { const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const relay = new RelayServer({ createStream (opts) { @@ -226,6 +237,9 @@ test('relay connections through node, server side', async function (t) { .on('close', () => { lc.pass('client socket closed') }) + .on('relay-aborted', (data) => { + lc.pass('client relay aborted') + }) .end('hello world') await lc @@ -243,7 +257,7 @@ test('relay connections through node, server side, client aborts hole punch', as const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const relay = new RelayServer({ createStream (opts) { @@ -284,6 +298,9 @@ test('relay connections through node, server side, client aborts hole punch', as .on('close', () => { lc.pass('client socket closed') }) + .on('relay-aborted', (data) => { + lc.pass('client relay aborted') + }) .end('hello world') await lc @@ -301,7 +318,7 @@ test('relay connections through node, server side, server aborts hole punch', as const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const relay = new RelayServer({ createStream (opts) { @@ -342,6 +359,9 @@ test('relay connections through node, server side, server aborts hole punch', as .on('close', () => { lc.pass('client socket closed') }) + .on('relay-aborted', (data) => { + lc.pass('client relay aborted') + }) .end('hello world') await lc @@ -359,7 +379,7 @@ test('relay connections through node, client and server side', async function (t const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(5) + lc.plan(6) const testRelay = t.test('relay server') testRelay.plan(2) // One each for the initiator and the follower const testRelayInitiator = t.test('relay initiator') @@ -412,6 +432,12 @@ test('relay connections through node, client and server side', async function (t const bSocket = c.connect(bServer.publicKey, { relayThrough: aServer.publicKey }) bSocket + .on('relay', () => { + lc.pass('client socket is being relayed') + }) + .on('relay-aborted', (data) => { + lc.fail('client relay aborted') + }) .on('open', () => { lc.pass('client socket opened') }) @@ -435,7 +461,7 @@ test.skip('relay several connections through node with pool', async function (t) const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true }) const lc = t.test('socket lifecycle') - lc.plan(10) + lc.plan(12) const aServer = a.createServer(function (socket) { lc.pass('server socket opened') @@ -475,6 +501,9 @@ test.skip('relay several connections through node with pool', async function (t) .on('open', () => { lc.pass('1st client socket opened') }) + .on('relay-aborted', () => { + lc.pass('client relay aborted') + }) .on('close', () => { lc.pass('1st client socket closed') @@ -487,6 +516,9 @@ test.skip('relay several connections through node with pool', async function (t) .on('close', () => { lc.pass('2nd client socket closed') }) + .on('relay-aborted', () => { + lc.pass('client relay aborted') + }) .end('hello world') }) .end('hello world')