node-minecraft-protocol/examples/client_http_proxy/client_http_proxy.js
Idan Horowitz 54380a3cb7
Pass through http agent option to yggdrasil for proxy support (#676)
* initialize yggdrasil with agent option if it exists

* standardjs

* move require statements to the top

* add agent option to docs and ts type information
also removed useless typescript import and moved class fields from wrong class to the correct one

* update proxy examples and add http agents for proxying yggdrasil

* update to agent supporting yggdrasil version
2020-02-11 12:31:24 +01:00

50 lines
1.4 KiB
JavaScript

const mc = require('minecraft-protocol')
const Http = require('http')
const ProxyAgent = require('proxy-agent')
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)
}
const proxyHost = process.argv[4]
const proxyPort = process.argv[5]
const client = mc.createClient({
connect: (client) => {
const req = Http.request({
host: proxyHost,
port: proxyPort,
method: 'CONNECT',
path: process.argv[2] + ':' + parseInt(process.argv[3])
})
req.end()
req.on('connect', (res, stream) => {
client.setSocket(stream)
client.emit('connect')
})
},
agent: new ProxyAgent({ protocol: 'http', host: proxyHost, port: proxyPort }),
username: process.argv[6] ? process.argv[6] : 'echo',
password: process.argv[7]
})
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
client.write('chat', { message: msg })
}
})