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')
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
if (bot.options.host !== server.host) return
|
|
|
|
index = _index
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
2022-12-07 06:39:37 -05:00
|
|
|
const targetPacketBlacklist = []
|
|
|
|
|
2022-11-30 06:01:46 -05:00
|
|
|
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
|
2022-12-07 06:39:37 -05:00
|
|
|
loadPlugins(bot, null, config, null, target, client, true, 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', () => {})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2022-12-07 06:39:37 -05:00
|
|
|
|
2022-11-30 06:01:46 -05:00
|
|
|
function endListener (reason) {
|
|
|
|
client.end(`Bot disconnected with reason: ${util.inspect(reason)}`)
|
|
|
|
bot.off('end', endListener)
|
2022-11-30 07:19:05 -05:00
|
|
|
srv.close()
|
2022-11-30 06:01:46 -05:00
|
|
|
srv.removeAllListeners()
|
|
|
|
}
|
|
|
|
bot.on('end', endListener)
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { inject }
|