diff --git a/examples/realmRelay.js b/examples/realmRelay.js new file mode 100644 index 0000000..2f40d8a --- /dev/null +++ b/examples/realmRelay.js @@ -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() diff --git a/index.d.ts b/index.d.ts index c2bc9f9..746506f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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? diff --git a/src/relay.js b/src/relay.js index 086b0be..bcb1295 100644 --- a/src/relay.js +++ b/src/relay.js @@ -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', () => {