Add Realm support to Relay (#226)

* add(): realm relay

* fix(): Non-async solution to realm proxy

* fix(): follow the guidlines of the strict linter

* fix(): Use Client constructor instead of createClient function

* types(): Update index.d.ts to include realms option in destination
This commit is contained in:
ATXLtheAxolotl 2022-06-17 05:55:50 -04:00 committed by GitHub
commit 6e73a75138
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

29
examples/realmRelay.js Normal file
View file

@ -0,0 +1,29 @@
const { Relay } = require('bedrock-protocol')
function createRelay () {
console.log('Creating relay')
/* Example to create a non-transparent proxy (or 'Relay') connection to destination server */
const relay = new Relay({
/* host and port for clients to listen to */
host: '0.0.0.0',
port: 19130,
offline: false,
/* Where to send upstream packets to */
destination: {
realms: {
pickRealm: (realms) => realms.find(e => e.name === 'Realm Name')
},
offline: false
}
})
relay.conLog = console.debug
relay.listen()
relay.on('connect', player => {
// Server is sending a message to the client.
player.on('clientbound', ({ name, params }) => {
if (name === 'text') console.log(params)
})
})
}
createRelay()

1
index.d.ts vendored
View file

@ -153,6 +153,7 @@ declare module "bedrock-protocol" {
authTitle: title | string
// Where to proxy requests to.
destination: {
realms?: RealmsOptions
host: string,
port: number,
// Skip authentication connecting to the remote server?

View file

@ -1,6 +1,7 @@
const { Client } = require('./client')
const { Server } = require('./server')
const { Player } = require('./serverPlayer')
const { realmAuthenticate } = require('./client/auth')
const debug = globalThis.isElectron ? console.debug : require('debug')('minecraft-protocol')
const debugging = false // Do re-encoding tests
@ -170,18 +171,25 @@ class Relay extends Server {
// a packet, no matter what state it's in. For example, if the client wants to send a
// packet to the server but it's not connected, it will add to the queue and send as soon
// as a connection with the server is established.
openUpstreamConnection (ds, clientAddr) {
const client = new Client({
async openUpstreamConnection (ds, clientAddr) {
const options = {
authTitle: this.options.authTitle,
offline: this.options.destination.offline ?? this.options.offline,
username: this.options.offline ? ds.profile.name : null,
version: this.options.version,
realms: this.options.destination.realms,
host: this.options.destination.host,
port: this.options.destination.port,
onMsaCode: this.options.onMsaCode,
profilesFolder: this.options.profilesFolder,
autoInitPlayer: false
})
}
if (this.options.destination.realms) {
await realmAuthenticate(options)
}
const client = new Client(options)
// Set the login payload unless `noLoginForward` option
if (!client.noLoginForward) client.options.skinData = ds.skinData
client.ping().then(pongData => {
@ -189,7 +197,7 @@ class Relay extends Server {
}).catch(err => {
this.emit('error', err)
})
this.conLog('Connecting to', this.options.destination.host, this.options.destination.port)
this.conLog('Connecting to', options.host, options.port)
client.outLog = ds.upOutLog
client.inLog = ds.upInLog
client.once('join', () => {