Advertisement
EXTREMEXPLOIT

joder que asco

Nov 26th, 2022
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { createLibp2p } from 'libp2p'
  2. import { webSockets } from '@libp2p/websockets'
  3. import { noise } from '@chainsafe/libp2p-noise'
  4. import { mplex } from '@libp2p/mplex'
  5. import {publicIpv4} from 'public-ip';
  6.  
  7. let myNode;
  8.  
  9. async function startNode ()
  10. {
  11.     let publicIP = await publicIpv4();
  12.     myNode = await createLibp2p({
  13.         addresses: { listen: ['/ip4/' + publicIP + '/tcp/0/ws'] },
  14.         transports: [ webSockets() ],
  15.         connectionEncryption: [ noise() ],
  16.         streamMuxers: [ mplex() ]
  17.     })
  18.  
  19.     await myNode.start()
  20.         .then(() =>
  21.         {
  22.             console.log("Listening:") // Display the listening addresses.
  23.             myNode.getMultiaddrs().forEach((ma) => console.log("\t" + ma.toString()))
  24.         })
  25.         .catch((err) => console.error(err))
  26. }
  27.  
  28. function connectToNode(addr)
  29. {
  30.     // Try to connect to the node and return if the connection was successful.
  31.     myNode.dial(addr)
  32.         .then(() => console.log("Connected to " + addr))
  33.         .catch((err) => console.error(err))
  34. }
  35.  
  36. async function sendMsg(msg, toAddr) {
  37.     // Send a message to the specified node.
  38.     try {
  39.         const {stream} = await myNode.dialProtocol(toAddr, '/echo/1.0.0')
  40.         await pipe(
  41.             [msg],
  42.             stream,
  43.             async function (source) {
  44.                 for await (const msg of source) {
  45.                     console.log('Received:', msg.toString())
  46.                 }
  47.             }
  48.         )
  49.         return true
  50.     } catch (e) {
  51.         console.log("Failed to send message to " + toAddr)
  52.         return false
  53.     }
  54. }
  55.  
  56. startNode()
  57.     .catch((err) => console.error(err))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement