diff --git a/src/@types/@hyperswarm__dht/index.d.ts b/src/@types/@hyperswarm__dht/index.d.ts index b6824fe..efb9ad4 100644 --- a/src/@types/@hyperswarm__dht/index.d.ts +++ b/src/@types/@hyperswarm__dht/index.d.ts @@ -1 +1 @@ -declare module '@hyperswarm/dht'; \ No newline at end of file +declare module '@hyperswarm/dht' diff --git a/src/index.ts b/src/index.ts index bb5e750..11ed204 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,86 +1,86 @@ -import DHT from "@hyperswarm/dht"; -import { between } from "./utils/between"; +/* eslint-disable no-console */ +import DHT from '@hyperswarm/dht' +import { between } from './utils/between' -const TOPIC = DHT.hash(Buffer.from("GRADIDO")); +const TOPIC = DHT.hash(Buffer.from('GRADIDO')) -const POLLTIME = 20000; -const SUCCESSTIME = 120000; -const ERRORTIME = 240000; -const nodeRand = between(1, 99); -const nodeURL = `https://test${nodeRand}.org`; +const POLLTIME = 20000 +const SUCCESSTIME = 120000 +const ERRORTIME = 240000 +const nodeRand = between(1, 99) +const nodeURL = `https://test${nodeRand}.org` const nodeAPI = { API_1_00: `${nodeURL}/api/1_00/`, API_1_01: `${nodeURL}/api/1_01/`, API_2_00: `${nodeURL}/graphql/2_00/`, -}; +} const main = async () => { // make a ed25519 keypair to listen on - const keyPair = DHT.keyPair(); + const keyPair = DHT.keyPair() - console.log("I am:", keyPair.publicKey.toString("hex"), nodeAPI); + console.log('I am:', keyPair.publicKey.toString('hex'), nodeAPI) - const node = new DHT({ keyPair }); + const node = new DHT({ keyPair }) // create a server to listen for secure connections - const server = node.createServer(); + const server = node.createServer() - server.on("connection", function (socket) { + server.on('connection', function (socket: any) { // noiseSocket is E2E between you and the other peer // pipe it somewhere like any duplex stream - console.log( - "Remote public key", - socket.remotePublicKey.toString("hex") - ); + console.log('Remote public key', socket.remotePublicKey.toString('hex')) // console.log("Local public key", noiseSocket.publicKey.toString("hex")); // same as keyPair.publicKey - socket.on('data', data => console.log('data:', data.toString('ascii'))) + socket.on('data', (data: Buffer) => console.log('data:', data.toString('ascii'))) // process.stdin.pipe(noiseSocket).pipe(process.stdout); - }); + }) - await server.listen(); + await server.listen() - await node.announce(TOPIC, keyPair).finished(); + await node.announce(TOPIC, keyPair).finished() - let successfulRequests = []; - let errorfulRequests = []; + let successfulRequests: string[] = [] + let errorfulRequests: string[] = [] setInterval(async () => { - successfulRequests = []; - }, SUCCESSTIME); + console.log('Refreshing successful nodes') + successfulRequests = [] + }, SUCCESSTIME) setInterval(async () => { - errorfulRequests = []; - }, ERRORTIME); + console.log('Refreshing errorful nodes') + errorfulRequests = [] + }, ERRORTIME) setInterval(async () => { - const result = await node.lookup(TOPIC); + const result = await node.lookup(TOPIC) - const collectedPubKeys = []; + const collectedPubKeys: string[] = [] for await (const data of result) { /* console.log( `${data.from.host}:${data.from.port}: peers: ${data.peers.length}` ); */ - data.peers.forEach((peer) => { - const pubKey = peer.publicKey.toString("hex"); + data.peers.forEach((peer: any) => { + const pubKey = peer.publicKey.toString('hex') if ( - pubKey !== keyPair.publicKey.toString("hex") && + pubKey !== keyPair.publicKey.toString('hex') && !successfulRequests.includes(pubKey) && !errorfulRequests.includes(pubKey) && !collectedPubKeys.includes(pubKey) ) { - collectedPubKeys.push(peer.publicKey.toString("hex")); + collectedPubKeys.push(peer.publicKey.toString('hex')) } - }); + }) } - console.log('Found new peers: ', collectedPubKeys); + console.log('Found new peers: ', collectedPubKeys) collectedPubKeys.forEach((remotePubKey) => { // publicKey here is keyPair.publicKey from above - const socket = node.connect(Buffer.from(remotePubKey, "hex")); + const socket = node.connect(Buffer.from(remotePubKey, 'hex')) /* socket.once("connect", function () { console.log("client side emitted connect"); @@ -90,25 +90,25 @@ const main = async () => { console.log("client side ended"); }); */ - socket.once("error", (err) => { - errorfulRequests.push(remotePubKey); - console.log(`error on peer ${remotePubKey}: ${err.message}`); - }); + socket.once('error', (err: any) => { + errorfulRequests.push(remotePubKey) + console.log(`error on peer ${remotePubKey}: ${err.message}`) + }) - socket.on("open", function () { + socket.on('open', function () { // noiseSocket fully open with the other peer // console.log("writing to socket"); - socket.write(Buffer.from(`${nodeRand}`)); - socket.write(Buffer.from(JSON.stringify(nodeAPI))); - successfulRequests.push(remotePubKey); - }); + socket.write(Buffer.from(`${nodeRand}`)) + socket.write(Buffer.from(JSON.stringify(nodeAPI))) + successfulRequests.push(remotePubKey) + }) // pipe it somewhere like any duplex stream // process.stdin.pipe(noiseSocket).pipe(process.stdout) - }); - }, POLLTIME); + }) + }, POLLTIME) // node.destroy() // await node.unannounce(TOPIC, keyPair); -}; +} -main(); +main() diff --git a/src/utils/between.ts b/src/utils/between.ts index 144e846..f70afb4 100644 --- a/src/utils/between.ts +++ b/src/utils/between.ts @@ -1,6 +1,3 @@ -export function between(min: number, max: number) { - return Math.floor( - Math.random() * (max - min + 1) + min - ) - } - \ No newline at end of file +export function between(min: number, max: number) { + return Math.floor(Math.random() * (max - min + 1) + min) +}