OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-proto-peer / scripts / dht-server.mjs

16 lines · javascript ref:
import '../lib/bare-globals.js'
import DHT from 'hyperdht'
import crypto from 'hypercore-crypto'
import b4a from 'b4a'
const keyPair = crypto.keyPair(crypto.hash(b4a.from('hpp-dht-direct-test-seed-v1')))
console.log(JSON.stringify({ role: 'server', pubkey: b4a.toString(keyPair.publicKey, 'hex').slice(0, 16) }))
const dht = new DHT()
const server = dht.createServer((socket) => {
  console.log(JSON.stringify({ event: 'SERVER_GOT_CONNECTION' }))
  socket.on('error', () => {})
  socket.on('data', () => { try { socket.write(b4a.from('pong')) } catch {} })
})
await server.listen(keyPair)
console.log(JSON.stringify({ event: 'SERVER_LISTENING', firewalled: dht.firewalled }))
setTimeout(() => process.exit(0), 38000)