From 9a53fceaee6a23dc1aa19920bed030ecaa0b4834 Mon Sep 17 00:00:00 2001 From: raven Date: Tue, 10 Jan 2023 12:27:25 -0500 Subject: [PATCH] changes, adding error handles for connections and command blocking --- discordBridge/.env | 3 +++ discordBridge/bridge.js | 45 +++++++++++++++++++++++++++++++++++++++++ sshChat.mjs | 22 ++++++++++++++++---- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 discordBridge/.env create mode 100644 discordBridge/bridge.js diff --git a/discordBridge/.env b/discordBridge/.env new file mode 100644 index 0000000..197bf82 --- /dev/null +++ b/discordBridge/.env @@ -0,0 +1,3 @@ +TOKEN=MTA2MjM4NjI4MjM1MDc3NjQzMA.GxffCt.RWpwcdgSS7zl4aivcXSL3vRL06lcfnmDIx_1Q0 +TOPIC_HASH=64ae5b405f523f45c7ee68287ff89c281c5d297fe2fce622d51650fad7975178 +CHANNEL=928115276438978632 diff --git a/discordBridge/bridge.js b/discordBridge/bridge.js new file mode 100644 index 0000000..4bb54cc --- /dev/null +++ b/discordBridge/bridge.js @@ -0,0 +1,45 @@ +const Discord = require('discord.js'); +const Hyperswarm = require('hyperswarm'); +const b4a = require('b4a'); +const crypto = require('hypercore-crypto'); + +client = new Discord.Client({intents: 32767}) +const swarm = new Hyperswarm(); +let channel + +let topic; + +client.on('ready', () => { + console.log(`Logged in as ${client.user.tag}!`); +}) + +topic = b4a.from("64ae5b405f523f45c7ee68287ff89c281c5d297fe2fce622d51650fad7975178", 'hex') // Generate a random topic + + // Join the swarm network with the topic + swarm.join(topic, { + lookup: true, // Enable peer discovery + announce: true // Enable peer announcement + }); + + swarm.on('connection', (conn, info) => { + console.log(`Connected to a peer for topic ${topic.toString('hex')}`); + + conn.on('data', data => { + // Send the message from the topic to the Discord channel + client.channels.cache.get("818514972610134069").send(b4a.toString(data)); + }); + }); + +client.on('message', msg => { + if (msg.channel.id === '818514972610134069') { + // Send the message from the Discord channel to the topic + swarm.peers(topic, (err, peers) => { + peers.forEach(peer => { + const conn = swarm.connection(peer); + conn.write(b4a.fromString(msg.content)); + }); + }); + } +}); + +client.login("MTA2MjM4NjI4MjM1MDc3NjQzMA.GxffCt.RWpwcdgSS7zl4aivcXSL3vRL06lcfnmDIx_1Q0"); diff --git a/sshChat.mjs b/sshChat.mjs index df33cb6..e0bd74d 100644 --- a/sshChat.mjs +++ b/sshChat.mjs @@ -18,7 +18,7 @@ goodbye(() => swarm.destroy()) const conns = [] let USERPWD = "/" let DAPI_KEY = {} -let USERNAME = "annon" + rand +let USERNAME = "anon" + rand let LOGGEDIN = false async function clearCursor() { @@ -105,6 +105,10 @@ swarm.on('connection', conn => { }) }) +swarm.on('error', (err) => { + console.log('Error connecting to peer:', err); +}); + // Use readline to accept input from the user const rl = readline.createInterface({ input: process.stdin, @@ -145,6 +149,8 @@ rl.on('line', input => { } const execute = input.startsWith(">") + if (input.startsWith("/") || input.startsWith("!")) return + if (execute) { let inputdata = input.split(2) const cmdToRun = inputdata.join(" ").replace("> ", "").replace(">", "") @@ -169,7 +175,6 @@ rl.on('line', input => { }) } - const start = input.startsWith("/start") if (start) { startContainer(DAPI_KEY.key).then((data) => { @@ -224,11 +229,20 @@ rl.on('line', input => { // Join a common topic const topic = process.argv[2] ? b4a.from(process.argv[2], 'hex') : crypto.randomBytes(32) -const discovery = swarm.join(topic, { client: true, server: true }) -// The flushed promise will resolve when the topic has been fully announced to the DHT +setTimeout(() => { + const discovery = swarm.join(topic, { + lookup: true, + announce: true + + }); + + // The flushed promise will resolve when the topic has been fully announced to the DHT discovery.flushed().then(() => { console.log(`joined topic: ${b4a.toString(topic, 'hex')}\n(Share this key to others so they may join.)`) console.log('You are now in a chatroom for your topic, feel free to chat.\n') console.log('Want to login to the SSH.SURF API? Type "/login [APIKEY]" to login.\n\n') }) +}, 3000); + +