Advertisement
EXTREMEXPLOIT

vale crec que si

Nov 20th, 2022
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. This file is used to configure and run a public node.
  3. The public node is used to listen/accept incoming connections.
  4. This public node can be reached by other nodes behind a NAT.
  5.  */
  6.  
  7. import { createLibp2p } from 'libp2p'
  8. import { webSockets } from '@libp2p/websockets'
  9. import { noise } from '@chainsafe/libp2p-noise'
  10. import { mplex } from '@libp2p/mplex'
  11. import {publicIpv4} from 'public-ip';
  12.  
  13. var myNode;
  14.  
  15. async function startNode ()
  16. {
  17.     let publicIP = await publicIpv4();
  18.     let localIP = "127.0.0.1";
  19.     myNode = await createLibp2p({
  20.         addresses: { listen: ['/ip4/' + publicIP + '/tcp/0/ws', '/ip4/' + localIP + '/tcp/0/ws'] },
  21.         transports: [ webSockets() ],
  22.         connectionEncryption: [ noise() ],
  23.         streamMuxers: [ mplex() ]
  24.     })
  25.  
  26.     await myNode.start()
  27.             .then(() =>
  28.             {
  29.                 console.log("Node ID: " + myNode.peerId.toString())
  30.             })
  31.             .then(() =>
  32.             {
  33.                 console.log("Listening:") // Display the listening addresses.
  34.                 myNode.getMultiaddrs().forEach((ma) => console.log("\t" + ma.toString()))
  35.             })
  36.             .catch((err) => console.error(err))
  37. }
  38.  
  39. function connectToNode(addr)
  40. {
  41.     // Try to connect to the node and return if the connection was successful.
  42.     try
  43.     {
  44.         myNode.dial(addr)
  45.         console.log("Connected to " + addr)
  46.         return true
  47.     }
  48.     catch (e)
  49.     {
  50.         console.log("Failed to connect to " + addr)
  51.         return false
  52.     }
  53. }
  54.  
  55. async function sendMsg(msg, toAddr) {
  56.     // Send a message to the specified node.
  57.     try {
  58.         const {stream} = await myNode.dialProtocol(toAddr, '/echo/1.0.0')
  59.         await pipe(
  60.             [msg],
  61.             stream,
  62.             async function (source) {
  63.                 for await (const msg of source) {
  64.                     console.log('Received:', msg.toString())
  65.                 }
  66.             }
  67.         )
  68.         return true
  69.     } catch (e) {
  70.         console.log("Failed to send message to " + toAddr)
  71.         return false
  72.     }
  73. }
  74.  
  75. startNode()
  76.     .then(() => connectToNode("/ip4/90.167.86.174/tcp/40189/ws/p2p/12D3KooWNxtRr8TRfD3tzE8M6De74bYk4UKivCUzb8LbbYMZ1KG2"))
  77.     .catch((err) => console.error(err))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement