2022-12-05 03:16:48 -05:00
|
|
|
|
|
2022-11-30 06:01:46 -05:00
|
|
|
|
const util = require('util')
|
|
|
|
|
const mc = require('minecraft-protocol')
|
|
|
|
|
const { loadPlugins } = require('../util/loadPlugins')
|
2022-12-10 01:31:01 -05:00
|
|
|
|
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
|
2022-11-30 06:01:46 -05:00
|
|
|
|
|
|
|
|
|
function inject (bot, dcclient, config) {
|
2022-12-04 07:58:22 -05:00
|
|
|
|
if (!config.proxy.enabled) return
|
2022-11-30 06:01:46 -05:00
|
|
|
|
|
|
|
|
|
let index
|
|
|
|
|
config.servers.forEach((server, _index) => {
|
2023-01-19 05:13:25 -05:00
|
|
|
|
if (bot.server.host !== server.host) return
|
2022-11-30 06:01:46 -05:00
|
|
|
|
index = _index
|
|
|
|
|
})
|
|
|
|
|
|
2022-12-10 23:52:00 -05:00
|
|
|
|
bot.proxy = {}
|
|
|
|
|
|
2022-12-04 07:58:22 -05:00
|
|
|
|
const version = config.proxy.version
|
2022-11-30 06:01:46 -05:00
|
|
|
|
const srv = mc.createServer({
|
|
|
|
|
'online-mode': false,
|
|
|
|
|
port: 25566 + index,
|
|
|
|
|
keepAlive: false,
|
|
|
|
|
version
|
|
|
|
|
})
|
|
|
|
|
|
2023-01-10 05:09:31 -05:00
|
|
|
|
srv.on('login', (client) => {
|
2022-11-30 06:01:46 -05:00
|
|
|
|
bot.console.info(`[Proxy] ${client.username} connected to proxy`)
|
|
|
|
|
let clientEnded = false
|
2022-12-18 07:53:53 -05:00
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-11-30 06:01:46 -05:00
|
|
|
|
let targetEnded = false
|
|
|
|
|
|
|
|
|
|
const target = mc.createClient({
|
|
|
|
|
username: client.username,
|
2023-01-19 05:13:25 -05:00
|
|
|
|
host: bot.server.host,
|
|
|
|
|
port: bot.server.port,
|
2022-11-30 06:01:46 -05:00
|
|
|
|
version
|
|
|
|
|
})
|
|
|
|
|
|
2022-12-10 01:31:01 -05:00
|
|
|
|
const clientPacketBlacklist = []
|
2022-12-07 06:39:37 -05:00
|
|
|
|
const targetPacketBlacklist = []
|
|
|
|
|
|
2023-02-26 04:50:15 -05:00
|
|
|
|
// should this be here or in the chat plugin?
|
2023-01-16 07:20:39 -05:00
|
|
|
|
target.sendMessage = function (message) {
|
2023-02-26 04:50:15 -05:00
|
|
|
|
if (message.startsWith('/') && minecraftVersionToNumber(target.version) >= 1.19) {
|
|
|
|
|
// totallynotskidded™️ from mineflayer
|
|
|
|
|
const command = message.slice(1)
|
|
|
|
|
const timestamp = BigInt(Date.now())
|
|
|
|
|
target.write('chat_command', {
|
|
|
|
|
command,
|
|
|
|
|
timestamp,
|
|
|
|
|
salt: 0n,
|
|
|
|
|
argumentSignatures: [],
|
|
|
|
|
signedPreview: false,
|
|
|
|
|
messageCount: 0,
|
|
|
|
|
acknowledged: Buffer.alloc(3),
|
|
|
|
|
// 1.19.2 Chat Command packet also includes an array of last seen messages
|
|
|
|
|
previousMessages: []
|
|
|
|
|
})
|
2022-12-10 01:31:01 -05:00
|
|
|
|
} else {
|
2023-02-26 04:50:15 -05:00
|
|
|
|
target.chat(message)
|
2022-12-10 01:31:01 -05:00
|
|
|
|
}
|
2022-11-30 06:01:46 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
target.on('login', (packet) => {
|
|
|
|
|
bot.console.info(`[Proxy] ${client.username} target logged in`)
|
|
|
|
|
target.entityId = packet.entityId
|
2022-12-10 01:31:01 -05:00
|
|
|
|
loadPlugins(bot, null, config, null, target, client, true, clientPacketBlacklist, targetPacketBlacklist)
|
2022-11-30 06:01:46 -05:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
target.on('packet', (data, meta) => {
|
|
|
|
|
if (!clientEnded &&
|
|
|
|
|
meta.state === mc.states.PLAY &&
|
2022-12-07 06:39:37 -05:00
|
|
|
|
client.state === mc.states.PLAY &&
|
|
|
|
|
!targetPacketBlacklist.includes(meta.name)
|
2022-11-30 06:01:46 -05:00
|
|
|
|
) client.write(meta.name, data)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
target.on('error', () => {})
|
|
|
|
|
|
2023-01-22 00:28:32 -05:00
|
|
|
|
target.on('end', targetEndListener)
|
2023-01-22 01:43:53 -05:00
|
|
|
|
target.on('kick_disconnect', ({ reason }) => targetEndListener(JSON.parse(reason)))
|
|
|
|
|
target.on('disconnect', ({ reason }) => targetEndListener(JSON.parse(reason)))
|
2023-01-22 00:28:32 -05:00
|
|
|
|
|
|
|
|
|
function targetEndListener (reason) {
|
2022-11-30 06:01:46 -05:00
|
|
|
|
target.end()
|
2022-12-10 01:31:01 -05:00
|
|
|
|
client.end(`Target disconnected with reason: ${util.inspect(reason)}`)
|
2022-11-30 06:01:46 -05:00
|
|
|
|
targetEnded = true
|
2023-01-22 00:28:32 -05:00
|
|
|
|
}
|
2022-11-30 06:01:46 -05:00
|
|
|
|
|
2023-01-10 05:09:31 -05:00
|
|
|
|
client.on('end', () => {
|
2022-11-30 06:01:46 -05:00
|
|
|
|
clientEnded = true
|
|
|
|
|
target.end()
|
|
|
|
|
target.removeAllListeners()
|
|
|
|
|
client.removeAllListeners()
|
|
|
|
|
bot.console.info(`[Proxy] ${client.username} ended`)
|
|
|
|
|
})
|
|
|
|
|
|
2023-01-10 05:09:31 -05:00
|
|
|
|
client.on('error', () => {
|
2022-11-30 06:01:46 -05:00
|
|
|
|
clientEnded = true
|
|
|
|
|
target.removeAllListeners()
|
|
|
|
|
client.removeAllListeners()
|
|
|
|
|
bot.console.info(`[Proxy] ${client.username} got error`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
client.on('packet', (data, meta) => {
|
2022-12-10 01:31:01 -05:00
|
|
|
|
if (clientPacketBlacklist.includes(meta.name)) return
|
2022-11-30 06:01:46 -05:00
|
|
|
|
target.write(meta.name, data)
|
|
|
|
|
})
|
2022-12-07 06:39:37 -05:00
|
|
|
|
|
2022-12-10 23:52:00 -05:00
|
|
|
|
bot.proxy[client.username] = {
|
|
|
|
|
target,
|
|
|
|
|
client
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-22 00:28:32 -05:00
|
|
|
|
function botEndListener (reason) {
|
2022-12-10 23:52:00 -05:00
|
|
|
|
delete bot.proxy[client.username]
|
2022-12-08 08:06:00 -05:00
|
|
|
|
client.end(`Bot disconnected with reason: ${util.inspect(reason)}`)
|
2023-01-22 00:28:32 -05:00
|
|
|
|
bot.off('end', botEndListener)
|
2022-11-30 06:01:46 -05:00
|
|
|
|
}
|
2023-01-22 00:28:32 -05:00
|
|
|
|
bot.on('end', botEndListener)
|
2022-11-30 06:01:46 -05:00
|
|
|
|
})
|
2022-12-08 08:06:00 -05:00
|
|
|
|
|
|
|
|
|
bot.on('end', () => {
|
|
|
|
|
srv.close()
|
|
|
|
|
srv.removeAllListeners()
|
|
|
|
|
})
|
2022-11-30 06:01:46 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { inject }
|