Skip to content

Commit

Permalink
Restarting client forever
Browse files Browse the repository at this point in the history
  • Loading branch information
freeall committed Mar 20, 2024
1 parent 6b62af3 commit ebf2617
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ async function runTests () {
await import('./pool.js')
await import('./relaying.js')
await import('./storing.js')
await import('./keet-relay-bug-hunter.js')
await import('./keep-alive.js')
await import('./keet-relay-bug-hunter.js')

test.resume()
}
16 changes: 6 additions & 10 deletions test/fixtures/server-through-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@ const DHT = require('../../')
const publicKey = Buffer.from(process.argv[2], 'hex')
const secretKey = Buffer.from(process.argv[3], 'hex')
const relayServerPublicKey = Buffer.from(process.argv[4], 'hex')
const bootstrap = JSON.parse(process.argv[5])
const keyPair = { publicKey, secretKey }
main()

console.log('publicKey', publicKey.toString('hex'))
console.log('secretKey', secretKey.toString('hex'))
console.log('relayServerPublicKey', relayServerPublicKey.toString('hex'))
main()

async function main () {
const node = new DHT()
const node = new DHT({ bootstrap })
const server = node.createServer({
holepunch: false, // To ensure it relies only on relaying
shareLocalAddress: false, // To help ensure it relies only on relaying (otherwise it can connect directly over LAN, without even trying to holepunch)
relayThrough: relayServerPublicKey
}, socket => {
socket.setKeepAlive(5000)
socket
.on('open', () => console.log('socket_opened'))
.on('data', data => {
console.log(`socket_ondata ${data.toString('hex')}`)
// client.destroy()
})
.on('open', () => console.log('socket_onopen'))
.on('close', () => console.log('socket_onclose'))
.on('error', err => console.log(`socket_onerror ${err.code}`))
setTimeout(() => socket.destroy(), 1000 + 10 * Math.random())
})
server.on('open', () => console.log('server_onopen'))
server.on('error', err => console.log(`server_onerror ${err.code}`))
Expand Down
60 changes: 28 additions & 32 deletions test/keet-relay-bug-hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ const path = require('path')
// client.set keep-alive
// client - should reconnect when connection dies

test.solo('Client connects to Server and keeps reconnectings - with relay', async t => {
test.solo('Client connects to Server and keeps reconnectings - with relay', { timeout: 0 }, async t => {
t.plan(2000)

const { bootstrap } = await swarm(t)

const a = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const b = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })
const c = new DHT({ bootstrap, quickFirewall: false, ephemeral: true })

t.plan(5)
t.teardown(async () => {
await a.destroy()
await c.destroy()
Expand All @@ -27,19 +28,16 @@ test.solo('Client connects to Server and keeps reconnectings - with relay', asyn

const relay = new RelayServer({
createStream (opts) {
console.log('[relay] createStream()')
return a.createRawStream({ ...opts, framed: true })
}
})

const relayServer = a.createServer(function (socket) {
console.log('[relayServer] Got connection')

const relayServer = a.createServer(socket => {
const relaySession = relay.accept(socket, { id: socket.remotePublicKey })

relaySession.on('pair', (isInitiator) => {
console.log(`[relayServer] on(pair) isInitiator=${isInitiator}`)
})
// relaySession.on('pair', (isInitiator) => {
// console.log(`[relayServer] on(pair) isInitiator=${isInitiator}`)
// })
relaySession.on('error', (err) => t.comment(err.message))
})

Expand All @@ -53,44 +51,42 @@ test.solo('Client connects to Server and keeps reconnectings - with relay', asyn
startServer()

function startServer () {
const serverProcess = spawn('node', [path.join(__dirname, 'fixtures/server-through-relay.js'), serverPublicKey, serverSecretKey, relayServerPublicKey])
const serverProcess = spawn('node', [
path.join(__dirname, 'fixtures/server-through-relay.js'),
serverPublicKey,
serverSecretKey,
relayServerPublicKey,
JSON.stringify(bootstrap)
])
serverProcess.stderr.on('data', () => t.fail())
serverProcess.stdout.on('data', data => {
data = data.toString().trim()
console.log(`[serverProcess] ${data}`)
// console.log(`[serverProcess] ${data}`)
const isStarted = data === 'started'
const isSocketConnected = data === 'socket_connected'
const isSocketOpened = data === 'socket_onopen'
const isSocketClosed = data === 'socket_onclose'
const isSocketError = data.startsWith('socket_onerror')
if (isStarted) {
t.pass('Server started. Now starting client')
setTimeout(() => startClient(), 1000)
}
if (isSocketConnected) {
t.pass('Client connected. Killing server process in 1 second')
// setTimeout(() => serverProcess.kill('SIGKILL'), 1000) // Wait a bit to make sure the handshake has happened
// setTimeout(() => {
// timedout = true

// t.pass('After 20 seconds the connection was still open. Closing it now')
// node.destroy()
// }, 24000)
t.pass('[server] Started. Now starting client')
startClient()
}
if (isSocketOpened) t.pass('[server] Socket connected')
if (isSocketClosed) t.pass('[server] Socket closed')
if (isSocketError) console.error(data)
})
serverProcess.on('close', () => console.log('[serverProcess] on(close)'))
serverProcess.on('exit', () => console.log('[serverProcess] on(exit)'))
}

function startClient () {
console.log('[startClient()] serverKeyPair.publicKey', serverKeyPair.publicKey.toString('hex'))
console.log('[startClient()] relayServer.publicKey', relayServer.publicKey.toString('hex'))
const client = c.connect(serverKeyPair.publicKey, { relayThrough: relayServer.publicKey })
client.setKeepAlive(5000)
client
.on('open', () => {
t.pass('client socket opened')
client.write('hello world')
t.pass('[client] Socket opened')
})
.on('close', () => {
t.pass('client socket closed')
t.pass('[client] Socket closed. reconnecting')
startClient()
})
.on('error', err => console.error('client error', err))
.on('error', err => console.error('[client] error', err))
}
})

0 comments on commit ebf2617

Please sign in to comment.