From 69bbd673ca2792002596c24b8742cbabb6185ad8 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Wed, 16 Nov 2022 20:21:19 +0700 Subject: [PATCH] add self care and chat to the proxy plugins!!!! --- plugins/chat.js | 194 +++++++++++++++++++++++-------------------- plugins/proxy.js | 11 +++ plugins/self_care.js | 49 ++++++++++- util/loadPlugins.js | 37 ++++++--- 4 files changed, 189 insertions(+), 102 deletions(-) diff --git a/plugins/chat.js b/plugins/chat.js index 69d38b1..d244709 100644 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -3,6 +3,98 @@ // eslint-disable-next-line no-undef // const parse = require('../util/text_parser'); const {containsIllegalCharacters} = require('../util/containsIllegalCharacters'); +async function chatPacketListener(packet, ChatMessage, previousMessage, bot) { + // try catch prevents hi % exploit (it uses prismarine-chat) + // and try catch also prevents json parse error + try { + const parsedMessage = JSON.parse(packet.message); + if (parsedMessage.translate === 'translation.test.invalid' || + parsedMessage.translate === 'translation.test.invalid2') return; + + // down here it prevents command set message + + // for ayunboom cuz its 1.17.1 + // VVVVVVVVVVVVVVVVVVVVVVVVVVVV + if (parsedMessage.extra) { + if (parsedMessage.extra[0].text === 'Command set: ') return; + } + // for 1.18 or newer(?) + // VVVVVVVVVVVVVVVVVVVVV + if (parsedMessage.translate === 'advMode.setCommand.success') return; + + const message = ChatMessage.fromNotch(packet.message); + + // before emitting, prevent spam first!!! + if (previousMessage === message.toString()) return; + previousMessage = message.toString(); + + bot.emit('parsed_chat', message, packet); + } catch (e) { + return; + } +} + +async function parsedChatListener(message, packet, bot) { + try { + // prevent braille cuz it CRASHES THE ENTIRE LAPTOP + // but sometimes this may not work + if (message.toString().includes('⣿')) return; + + // then here is all the player message parsing thing + const raw = message.toMotd().substring(0, 32767); + if (raw.match(/.* .*: .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, ''); + const message = raw.split(': ')[1]; + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/.* .*\u203a .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]; + const message = raw.split('\u203a ')[1].substring(2); + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/.* .*\u00BB .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]; + const message = raw.split('\u00BB ')[1].substring(2); + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/<.*§r> .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + + const username = raw.substring(3).split('§r>')[0]; + const message = raw.split('§r> ')[1]; + + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/<.*> .*/g)) { + // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; + + const username = raw.substring(3).split('>')[0]; + const message = raw.split('> ')[1]; + + bot.emit('message', username, message, packet.sender); + } else if (raw.match(/§.*§b: §b\/.*/g)) { + const username = raw.split('§b: §b')[0]; + const command = raw.split('§b: §b')[1]; + + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§e: §e\/.*/g)) { + const username = raw.split('§e: §e')[0]; + const command = raw.split('§e: §e')[1]; + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§b: \/.*/g)) { + const username = raw.split('§b: ')[0]; + const command = raw.split('§b: ')[1]; + + bot.emit('cspy', username, command); + } else if (raw.match(/§.*§e: \/.*/g)) { + const username = raw.split('§e: ')[0]; + const command = raw.split('§e: ')[1]; + bot.emit('cspy', username, command); + } + } catch (e) { + return; + } +}; + function inject(bot) { bot.chatQueue = []; @@ -36,97 +128,17 @@ function inject(bot) { let previousMessage; const ChatMessage = require('prismarine-chat')(bot.version); - bot._client.on('chat', async (packet) => { - // try catch prevents hi % exploit (it uses prismarine-chat) - // and try catch also prevents json parse error - try { - const parsedMessage = JSON.parse(packet.message); - if (parsedMessage.translate === 'translation.test.invalid' || - parsedMessage.translate === 'translation.test.invalid2') return; + bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, previousMessage, bot)); - // down here it prevents command set message - - // for ayunboom cuz its 1.17.1 - // VVVVVVVVVVVVVVVVVVVVVVVVVVVV - if (parsedMessage.extra) { - if (parsedMessage.extra[0].text === 'Command set: ') return; - } - // for 1.18 or newer(?) - // VVVVVVVVVVVVVVVVVVVVV - if (parsedMessage.translate === 'advMode.setCommand.success') return; - - const message = ChatMessage.fromNotch(packet.message); - - // before emitting, prevent spam first!!! - if (previousMessage === message.toString()) return; - previousMessage = message.toString(); - - bot.emit('parsed_chat', message, packet); - } catch (e) { - return; - } - }); - - bot.on('parsed_chat', (message, packet) => { - try { - // prevent braille cuz it CRASHES THE ENTIRE LAPTOP - // but sometimes this may not work - if (message.toString().includes('⣿')) return; - - // then here is all the player message parsing thing - const raw = message.toMotd().substring(0, 32767); - if (raw.match(/.* .*: .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /, '').replace(/:.*/g, '').replace(/§#....../gm, ''); - const message = raw.split(': ')[1]; - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/.* .*\u203a .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u203a.*/g, '').replace(/§#....../gm, '').split(' ')[0]; - const message = raw.split('\u203a ')[1].substring(2); - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/.* .*\u00BB .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - const username = raw.replace(/.*?\[.*?\] /g, '').replace(/\u00BB.*/g, '').replace(/§#....../gm, '').split(' ')[0]; - const message = raw.split('\u00BB ')[1].substring(2); - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/<.*§r> .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - - const username = raw.substring(3).split('§r>')[0]; - const message = raw.split('§r> ')[1]; - - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/<.*> .*/g)) { - // if (packet.sender === '00000000-0000-0000-0000-000000000000') return; - - const username = raw.substring(3).split('>')[0]; - const message = raw.split('> ')[1]; - - bot.emit('message', username, message, packet.sender); - } else if (raw.match(/§.*§b: §b\/.*/g)) { - const username = raw.split('§b: §b')[0]; - const command = raw.split('§b: §b')[1]; - - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§e: §e\/.*/g)) { - const username = raw.split('§e: §e')[0]; - const command = raw.split('§e: §e')[1]; - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§b: \/.*/g)) { - const username = raw.split('§b: ')[0]; - const command = raw.split('§b: ')[1]; - - bot.emit('cspy', username, command); - } else if (raw.match(/§.*§e: \/.*/g)) { - const username = raw.split('§e: ')[0]; - const command = raw.split('§e: ')[1]; - bot.emit('cspy', username, command); - } - } catch (e) { - return; - } - }); + bot.on('parsed_chat', (message, packet) => parsedChatListener(message, packet, bot)); } -module.exports = {inject}; +function proxy(bot, client, targetClient) { + const ChatMessage = require('prismarine-chat')(bot.version); + + targetClient.on('chat', (packet) => chatPacketListener(packet, ChatMessage, '', targetClient)); + + targetClient.on('parsed_chat', (message, packet) => parsedChatListener(message, packet, targetClient)); +}; + +module.exports = {inject, proxy}; diff --git a/plugins/proxy.js b/plugins/proxy.js index 58652c2..cd393b3 100644 --- a/plugins/proxy.js +++ b/plugins/proxy.js @@ -1,5 +1,6 @@ /* eslint-disable require-jsdoc */ const mc = require('minecraft-protocol'); +const {loadPlugins} = require('../util/loadPlugins'); function inject(bot, dcclient, config) { config.servers.forEach((server, index) => { @@ -26,6 +27,15 @@ function inject(bot, dcclient, config) { version, }); + targetClient.chat = function(message) { + targetClient.write('chat', {message}); + }; + + targetClient.on('login', (packet) => { + targetClient.entityId = packet.entityId; + loadPlugins(bot, null, config, null, targetClient, client, true); + }); + targetClient.on('packet', (data, meta) => { if (!endedClient && meta.state === mc.states.PLAY && @@ -88,6 +98,7 @@ function inject(bot, dcclient, config) { } targetClient.write(meta.name, data); }); + bot.on('end', () => srv.close()); }); }); diff --git a/plugins/self_care.js b/plugins/self_care.js index 9e0a5d7..f5368b6 100644 --- a/plugins/self_care.js +++ b/plugins/self_care.js @@ -1,3 +1,4 @@ +/* eslint-disable require-jsdoc */ /* eslint-disable max-len */ function inject(bot, dcclient, config) { let vanish = false; @@ -69,4 +70,50 @@ function inject(bot, dcclient, config) { }); } -module.exports = {inject}; +function proxy(bot, client, targetClient, config) { + let cspy = false; + let op = true; + let gameMode = 1; + + targetClient.on('parsed_chat', (data) => { + if (data.toString() === 'Successfully enabled CommandSpy' || data.toString() === ' Enabled your command spy.' || data.toString() === ' Your command spy is already enabled.') cspy = true; + if (data.toString() === 'Successfully disabled CommandSpy' || data.toString() === ' Disabled your command spy.') cspy = false; + }); + + targetClient.on('entity_status', (data) => { + if (data.entityId !== targetClient.entityId) return; + + switch (data.entityStatus) { + case 24: + op = false; + break; + case 28: + op = true; + break; + } + }); + + targetClient.on('game_state_change', (data) => { + if (data.reason !== 3) return; + + gameMode = data.gameMode; + }); + + targetClient.once('login', (data) => { + gameMode = data.gameMode; + }); + + const interval = setInterval(() => { + if (bot.options.host !== '0.tcp.ap.ngrok.io') { + if (!op && config.self_care.op) targetClient.chat('/minecraft:op @s[type=player]'); + if (!cspy && config.self_care.cspy) targetClient.chat('/commandspy:commandspy on'); + } + if (gameMode !== 1 && config.self_care.gamemode) targetClient.chat('/minecraft:gamemode creative @s[type=player]'); + }, config.self_care_check_interval); + + bot.once('end', () => { + clearInterval(interval); + }); +}; + +module.exports = {inject, proxy}; diff --git a/util/loadPlugins.js b/util/loadPlugins.js index 5cc3706..d063126 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -1,3 +1,4 @@ +/* eslint-disable max-len */ const fs = require('fs/promises'); const util = require('util'); const path = require('path'); @@ -8,19 +9,35 @@ const path = require('path'); * @param {object} dcclient discord client * @param {object} config the config * @param {object} rl readline + * @param {object} targetClient proxy target + * @param {object} client proxy client + * @param {boolean} proxy is proxy */ -async function loadPlugins(bot, dcclient, config, rl) { +async function loadPlugins(bot, dcclient, config, rl, targetClient, client, proxy) { const plugins = await fs.readdir(path.join(__dirname, '..', 'plugins')); - plugins.forEach((plugin) => { - try { - const plug = require(path.join(__dirname, '..', 'plugins', plugin)); - plug.inject(bot, dcclient, config, rl); - } catch (e) { - console.log(`Plugin ${plugin} is having exception loading the plugin:`); - console.log(util.inspect(e)); - } - }); + if (!proxy) { + plugins.forEach((plugin) => { + try { + const plug = require(path.join(__dirname, '..', 'plugins', plugin)); + plug.inject(bot, dcclient, config, rl); + } catch (e) { + console.log(`Plugin ${plugin} is having exception loading the plugin:`); + console.log(util.inspect(e)); + } + }); + } else { + plugins.forEach((plugin) => { + try { + const plug = require(path.join(__dirname, '..', 'plugins', plugin)); + if (!plug.proxy) return; + plug.proxy(bot, client, targetClient, config); + } catch (e) { + console.log(`Proxy Plugin ${plugin} is having exception loading the plugin:`); + console.log(util.inspect(e)); + } + }); + } }; module.exports = {loadPlugins};