2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('minecraft-protocol')
|
|
|
|
const Http = require('http')
|
2017-07-13 08:42:42 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
if (process.argv.length < 6 || process.argv.length > 8) {
|
|
|
|
console.log('Usage : node client_http_proxy.js <host> <port> <proxyHost> <proxyPort> [<name>] [<password>]')
|
|
|
|
process.exit(1)
|
2017-07-13 08:42:42 -04:00
|
|
|
}
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const proxyHost = process.argv[4]
|
|
|
|
const proxyPort = process.argv[5]
|
2017-07-13 08:42:42 -04:00
|
|
|
|
2017-07-19 12:39:25 -04:00
|
|
|
const client = mc.createClient({
|
2018-05-13 16:50:16 -04:00
|
|
|
connect: (client) => {
|
|
|
|
const req = Http.request({
|
|
|
|
host: proxyHost,
|
|
|
|
port: proxyPort,
|
|
|
|
method: 'CONNECT',
|
|
|
|
path: process.argv[2] + ':' + parseInt(process.argv[3])
|
|
|
|
})
|
|
|
|
req.end()
|
2017-07-13 08:42:42 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
req.on('connect', function (res, stream) {
|
|
|
|
client.setSocket(stream)
|
|
|
|
client.emit('connect')
|
|
|
|
})
|
|
|
|
},
|
|
|
|
username: process.argv[6] ? process.argv[6] : 'echo',
|
2017-07-19 12:39:25 -04:00
|
|
|
password: process.argv[7]
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2017-07-13 08:42:42 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
client.on('connect', function () {
|
|
|
|
console.info('connected')
|
|
|
|
})
|
|
|
|
client.on('disconnect', function (packet) {
|
|
|
|
console.log('disconnected: ' + packet.reason)
|
|
|
|
})
|
|
|
|
client.on('end', function () {
|
|
|
|
console.log('Connection lost')
|
|
|
|
})
|
|
|
|
client.on('chat', function (packet) {
|
|
|
|
const jsonMsg = JSON.parse(packet.message)
|
|
|
|
if (jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
|
|
|
const username = jsonMsg.with[0].text
|
|
|
|
const msg = jsonMsg.with[1]
|
|
|
|
if (username === client.username) return
|
2018-09-24 16:07:34 -04:00
|
|
|
client.write('chat', { message: msg })
|
2017-07-19 12:39:25 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|