|
| 1 | +/** |
| 2 | + * Standalone TCP listener for Phase 5 live interop testing. |
| 3 | + * |
| 4 | + * Listens on TCP port 8000, performs a NoiseHFS (XXhfs) handshake as the |
| 5 | + * RESPONDER for each incoming connection, then: |
| 6 | + * 1. Sends "hello from JS" to the peer |
| 7 | + * 2. Reads back whatever the peer sends and prints it |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * cd js-libp2p-noise |
| 11 | + * node scripts/node-listener.mjs |
| 12 | + * |
| 13 | + * Then in another terminal: |
| 14 | + * cd py-libp2p && python scripts/interop_dial.py |
| 15 | + */ |
| 16 | + |
| 17 | +import net from 'net' |
| 18 | +import { generateKeyPair } from '@libp2p/crypto/keys' |
| 19 | +import { defaultLogger } from '@libp2p/logger' |
| 20 | +import { peerIdFromPrivateKey } from '@libp2p/peer-id' |
| 21 | +import { AbstractMultiaddrConnection, ipPortToMultiaddr } from '@libp2p/utils' |
| 22 | +import { multiaddr } from '@multiformats/multiaddr' |
| 23 | +import { NoiseHFS } from '../dist/src/noise-hfs.js' |
| 24 | + |
| 25 | +const PORT = 8000 |
| 26 | + |
| 27 | +// ─── Inline TCP socket → MultiaddrConnection adapter ──────────────────────── |
| 28 | +// @libp2p/tcp does not export socket-to-conn directly; we inline it here. |
| 29 | +// Based on @libp2p/tcp dist/src/socket-to-conn.js |
| 30 | +class TCPSocketConnection extends AbstractMultiaddrConnection { |
| 31 | + #socket |
| 32 | + |
| 33 | + constructor (init) { |
| 34 | + super(init) |
| 35 | + this.#socket = init.socket |
| 36 | + |
| 37 | + this.#socket.on('data', buf => this.onData(buf)) |
| 38 | + this.#socket.on('error', err => this.abort(err)) |
| 39 | + this.#socket.setTimeout(120_000) |
| 40 | + this.#socket.once('timeout', () => this.abort(new Error('TCP timeout'))) |
| 41 | + this.#socket.once('end', () => this.onTransportClosed()) |
| 42 | + this.#socket.once('close', hadError => { |
| 43 | + if (hadError) { |
| 44 | + this.abort(new Error('TCP transmission error')) |
| 45 | + } else { |
| 46 | + this.onTransportClosed() |
| 47 | + } |
| 48 | + }) |
| 49 | + this.#socket.on('drain', () => this.safeDispatchEvent('drain')) |
| 50 | + } |
| 51 | + |
| 52 | + sendData (data) { |
| 53 | + let sentBytes = 0 |
| 54 | + let canSendMore = true |
| 55 | + for (const buf of data) { |
| 56 | + sentBytes += buf.byteLength |
| 57 | + canSendMore = this.#socket.write(buf) |
| 58 | + } |
| 59 | + return { sentBytes, canSendMore } |
| 60 | + } |
| 61 | + |
| 62 | + async sendClose (options) { |
| 63 | + if (this.#socket.destroyed) return |
| 64 | + await new Promise((resolve) => { |
| 65 | + this.#socket.once('close', resolve) |
| 66 | + this.#socket.destroySoon() |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + sendReset () { |
| 71 | + this.#socket.resetAndDestroy() |
| 72 | + } |
| 73 | + |
| 74 | + sendPause () { this.#socket.pause() } |
| 75 | + sendResume () { this.#socket.resume() } |
| 76 | +} |
| 77 | + |
| 78 | +function socketToMultiaddrConn (socket, log, localAddr) { |
| 79 | + const remoteAddr = ipPortToMultiaddr(socket.remoteAddress, socket.remotePort) |
| 80 | + return new TCPSocketConnection({ |
| 81 | + socket, |
| 82 | + remoteAddr, |
| 83 | + localAddr, |
| 84 | + direction: 'inbound', |
| 85 | + log: log.newScope('tcp-conn') |
| 86 | + }) |
| 87 | +} |
| 88 | + |
| 89 | +// ─── Main ──────────────────────────────────────────────────────────────────── |
| 90 | + |
| 91 | +async function main () { |
| 92 | + const privateKey = await generateKeyPair('Ed25519') |
| 93 | + const peerId = peerIdFromPrivateKey(privateKey) |
| 94 | + const log = defaultLogger().forComponent('noise-hfs:listener') |
| 95 | + |
| 96 | + console.log(`Listener peer ID: ${peerId.toString()}`) |
| 97 | + |
| 98 | + const components = { |
| 99 | + privateKey, |
| 100 | + peerId, |
| 101 | + logger: defaultLogger(), |
| 102 | + upgrader: { getStreamMuxers: () => new Map() } |
| 103 | + } |
| 104 | + |
| 105 | + const noiseHfs = new NoiseHFS(components) |
| 106 | + console.log(`Protocol: ${noiseHfs.protocol}`) |
| 107 | + |
| 108 | + const localAddr = multiaddr(`/ip4/127.0.0.1/tcp/${PORT}`) |
| 109 | + |
| 110 | + const server = net.createServer(async (socket) => { |
| 111 | + console.log(`\nIncoming TCP connection from ${socket.remoteAddress}:${socket.remotePort}`) |
| 112 | + |
| 113 | + const maConn = socketToMultiaddrConn(socket, log, localAddr) |
| 114 | + |
| 115 | + try { |
| 116 | + console.log('Starting NoiseHFS responder handshake...') |
| 117 | + const { connection, remotePeer } = await noiseHfs.secureInbound(maConn) |
| 118 | + console.log(`Handshake complete! Remote peer: ${remotePeer.toString()}`) |
| 119 | + |
| 120 | + // Send greeting — connection.send() sends uint16(ct_len) || AEAD(plaintext) |
| 121 | + // which matches Python's NoisePacketReadWriter framing exactly. |
| 122 | + const greeting = new TextEncoder().encode('hello from JS\n') |
| 123 | + connection.send(greeting) |
| 124 | + console.log('Sent: "hello from JS"') |
| 125 | + |
| 126 | + // Read Python reply — iterate the async stream for one decrypted message |
| 127 | + for await (const chunk of connection) { |
| 128 | + const replyStr = new TextDecoder().decode(chunk instanceof Uint8Array ? chunk : chunk.slice()) |
| 129 | + console.log(`Received: "${replyStr.trim()}"`) |
| 130 | + |
| 131 | + if (replyStr.trim() === 'hello from Python') { |
| 132 | + console.log('\n✅ INTEROP SUCCESS: Both sides exchanged messages through NoiseHFS!') |
| 133 | + } else { |
| 134 | + console.log('\n⚠️ Unexpected reply:', JSON.stringify(replyStr)) |
| 135 | + } |
| 136 | + break // one message is enough |
| 137 | + } |
| 138 | + |
| 139 | + connection.close() |
| 140 | + } catch (err) { |
| 141 | + console.error('Handshake or messaging error:', err.message) |
| 142 | + socket.destroy() |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + server.listen(PORT, '127.0.0.1', () => { |
| 147 | + console.log(`\nListening on tcp://127.0.0.1:${PORT}`) |
| 148 | + console.log('Waiting for Python dialer...\n') |
| 149 | + }) |
| 150 | + |
| 151 | + server.on('error', err => { |
| 152 | + console.error('Server error:', err) |
| 153 | + process.exit(1) |
| 154 | + }) |
| 155 | +} |
| 156 | + |
| 157 | +main().catch(err => { |
| 158 | + console.error(err) |
| 159 | + process.exit(1) |
| 160 | +}) |
0 commit comments