From e0ff3c6504d042b1f72f2776dec09d89e6fd096d Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Wed, 30 Nov 2022 18:01:46 +0700 Subject: [PATCH] readd proxy :)) --- commands/refillcore.js | 4 +- config.js | 13 ++--- index.js | 2 +- plugins/proxy.js | 100 +++++++++++++++++++++++++++++++++++ plugins/proxy/chat.js | 15 ++++++ plugins/proxy/custom_chat.js | 32 +++++++++++ plugins/proxy/self_care.js | 48 +++++++++++++++++ util/loadPlugins.js | 12 +++-- 8 files changed, 213 insertions(+), 13 deletions(-) create mode 100644 plugins/proxy.js create mode 100644 plugins/proxy/chat.js create mode 100644 plugins/proxy/custom_chat.js create mode 100644 plugins/proxy/self_care.js diff --git a/commands/refillcore.js b/commands/refillcore.js index 50a5bb8..91833d5 100644 --- a/commands/refillcore.js +++ b/commands/refillcore.js @@ -5,9 +5,9 @@ module.exports = { usage: '', trusted: 0, execute (bot, username, usernameraw, sender, prefix, args, config, hash, ownerhash, selector) { - bot.core.loopPlace() + bot.core.fillCore() }, discordExecute (bot) { - bot.core.loopPlace() + bot.core.fillCore() } } diff --git a/config.js b/config.js index 1ca3990..9db97cf 100644 --- a/config.js +++ b/config.js @@ -11,6 +11,7 @@ module.exports = { normalKey: '�iB_D���k��j8H�{?[/ڭ�f�}Ѣ�^-=�Ț��v]��g>��=c', ownerHashKey: 'b)R��nF�CW���#�\\[�S*8"t^eia�Z��k����K1�8zȢ�' }, + proxy: true, console: true, useChat: false, core: { @@ -74,11 +75,11 @@ module.exports = { host: 'real.chipmunk.land', port: 25565, kaboom: true - }, - { - host: 'mc.chomens41793.ga', - port: 25565, - kaboom: true - } + }// , + // { + // host: 'mc.chomens41793.ga', + // port: 25565, + // kaboom: true + // } ] } diff --git a/index.js b/index.js index 194edf3..da56281 100644 --- a/index.js +++ b/index.js @@ -32,5 +32,5 @@ dcclient.on('ready', () => { dcclient.login(config.discord.token) process.on('uncaughtException', (e) => { - console.log('uncaught ' + e) + console.log('uncaught ' + e.stack) }) diff --git a/plugins/proxy.js b/plugins/proxy.js new file mode 100644 index 0000000..292f364 --- /dev/null +++ b/plugins/proxy.js @@ -0,0 +1,100 @@ +/* eslint-disable require-jsdoc */ +const util = require('util') +const mc = require('minecraft-protocol') +const { loadPlugins } = require('../util/loadPlugins') + +function inject (bot, dcclient, config) { + if (!config.proxy) return + + let index + config.servers.forEach((server, _index) => { + if (bot.options.host !== server.host) return + index = _index + }) + + const version = bot.version + const srv = mc.createServer({ + 'online-mode': false, + port: 25566 + index, + keepAlive: false, + version + }) + + srv.on('login', function (client) { + bot.console.info(`[Proxy] ${client.username} connected to proxy`) + let clientEnded = false + let targetEnded = false + + const target = mc.createClient({ + username: client.username, + host: bot.options.host, + version + }) + + target.chat = function (message) { + target.write('chat', { message }) + } + + target.on('login', (packet) => { + bot.console.info(`[Proxy] ${client.username} target logged in`) + target.entityId = packet.entityId + loadPlugins(bot, null, config, null, target, client, true) + }) + + target.on('packet', (data, meta) => { + if (!clientEnded && + meta.state === mc.states.PLAY && + client.state === mc.states.PLAY + ) client.write(meta.name, data) + }) + + target.on('error', () => {}) + + target.on('end', () => { + target.end() + targetEnded = true + }) + + client.on('end', function () { + clientEnded = true + target.end() + target.removeAllListeners() + client.removeAllListeners() + bot.console.info(`[Proxy] ${client.username} ended`) + }) + + client.on('error', function () { + clientEnded = true + target.removeAllListeners() + client.removeAllListeners() + bot.console.info(`[Proxy] ${client.username} got error`) + }) + + client.on('packet', (data, meta) => { + if (meta.name === 'chat' && !data.message?.startsWith('/')) { + if (data.message.startsWith('.')) { + return bot.command_handler.run( + client.username, + client.username, + '*' + data.message.substring(1), + client.uuid, + null, + 'h', + 'o', + client.username + ) + } + return + } + target.write(meta.name, data) + }) + function endListener (reason) { + client.end(`Bot disconnected with reason: ${util.inspect(reason)}`) + bot.off('end', endListener) + srv.removeAllListeners() + } + bot.on('end', endListener) + }) +}; + +module.exports = { inject } diff --git a/plugins/proxy/chat.js b/plugins/proxy/chat.js new file mode 100644 index 0000000..3de990a --- /dev/null +++ b/plugins/proxy/chat.js @@ -0,0 +1,15 @@ +/* eslint-disable require-jsdoc */ +const { chatPacketListener, parsePlayerMessages } = require('../../util/chat') +function inject (bot, client, target) { + const ChatMessage = require('prismarine-chat')(bot.version) + + target.on('chat', (packet) => { + chatPacketListener(packet, ChatMessage, target) + }) + + target.on('parsed_chat', (message, packet) => { + parsePlayerMessages(message, packet, target) + }) +}; + +module.exports = { inject } diff --git a/plugins/proxy/custom_chat.js b/plugins/proxy/custom_chat.js new file mode 100644 index 0000000..7920295 --- /dev/null +++ b/plugins/proxy/custom_chat.js @@ -0,0 +1,32 @@ +/* eslint-disable require-jsdoc */ +function inject (bot, client, target) { + const { MessageBuilder } = require('prismarine-chat')(bot.version) + client.on('packet', (data, meta) => { + if (meta.name === 'chat' && + !data.message?.startsWith('/') && + !data.message?.startsWith('.') + ) { + bot.tellraw('@a', { + color: 'dark_gray', + translate: '[%s] [%s] %s \u203a %s', + with: [ + { + text: 'Chat', + color: 'gray' + }, + { + text: 'Proxy', + color: 'gray' + }, + { + text: client.username, + color: 'green' + }, + MessageBuilder.fromString('&7' + data.message) + ] + }) + } + }) +}; + +module.exports = { inject } diff --git a/plugins/proxy/self_care.js b/plugins/proxy/self_care.js new file mode 100644 index 0000000..16f6a68 --- /dev/null +++ b/plugins/proxy/self_care.js @@ -0,0 +1,48 @@ +/* eslint-disable require-jsdoc */ +function inject (bot, client, target, config) { + let cspy = false + let op = true + let gameMode = 1 + + target.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 + }) + + target.on('entity_status', (data) => { + if (data.entityId !== target.entityId) return + + switch (data.entityStatus) { + case 24: + op = false + break + case 28: + op = true + break + } + }) + + target.on('game_state_change', (data) => { + if (data.reason !== 3) return + + gameMode = data.gameMode + }) + + target.on('login', (data) => { + gameMode = data.gameMode + }) + + const interval = setInterval(() => { + if (bot.options.kaboom) { + if (!op && config.self_care.op) target.chat('/minecraft:op @s[type=player]') + if (!cspy && config.self_care.cspy) target.chat('/commandspy:commandspy on') + } + if (gameMode !== 1 && config.self_care.gamemode) target.chat('/minecraft:gamemode creative @s[type=player]') + }, config.self_care_check_interval) + + bot.on('end', () => { + clearInterval(interval) + }) +}; + +module.exports = { inject } diff --git a/util/loadPlugins.js b/util/loadPlugins.js index 6e29fa2..78ede11 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -8,16 +8,20 @@ const path = require('path') * @param {object} bot the bot object * @param {object} dcclient discord client * @param {object} config the config - * @param {object} rl readline. + * @param {object} rl readline + * @param {object} target proxy target + * @param {object} client proxy client + * @param {boolean} proxy is proxy */ -async function loadPlugins (bot, dcclient, config, rl) { - const dir = path.join(__dirname, '..', 'plugins') +async function loadPlugins (bot, dcclient, config, rl, target, client, proxy) { + const dir = path.join(__dirname, '..', 'plugins', proxy ? 'proxy' : '') const plugins = await fs.readdir(dir) plugins.forEach((plugin) => { if (!plugin.endsWith('.js')) return try { const plug = require(path.join(dir, plugin)) - plug.inject(bot, dcclient, config, rl) + if (!proxy) plug.inject(bot, dcclient, config, rl) + else plug.inject(bot, client, target, config) } catch (e) { console.log(`Plugin ${plugin} is having exception loading the plugin:`) console.log(util.inspect(e))