Add option for port redirection, fix Realm handling (#282)

* Port Redirect Fix - Only Redirect on Unspecified Port

* Update to use `followPort` option

Co-authored-by: Stephen O'Connor <oconnor39@gmail.com>
This commit is contained in:
extremeheat 2022-09-22 15:14:49 -04:00 committed by GitHub
commit 99153c2de2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 4 deletions

View file

@ -16,8 +16,9 @@ Returns a `Client` instance and connects to the server.
| connectTimeout | *optional* | default to **9000ms**. How long to wait in milliseconds while trying to connect to server. |
| onMsaCode | *optional* | Callback called when signing in with a microsoft account with device code auth, `data` is an object documented [here](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-device-code#device-authorization-response) |
| profilesFolder | *optional* | Where to store cached authentication tokens. Defaults to .minecraft, or the node_modules folder if not found. |
| autoInitPlayer | *optional* | default to true, If we should send SetPlayerInitialized to the server after getting play_status spawn. |
| skipPing | *optional* | Whether pinging the server to check its version should be skipped. |
| followPort | *optional* | Update the options' port parameter to match the port broadcast on the server's ping data (default to true if `realms` not specified) |
| autoInitPlayer | *optional* | default to true, If we should send SetPlayerInitialized to the server after getting play_status spawn. |
| conLog | *optional* | Where to log connection information (server join, kick messages to). Defaults to console.log, set to `null` to not log anywhere. |
| raknetBackend | *optional* | Specifies the raknet implementation to use. Possible options are 'raknet-native' (default, original C++ implementation), 'jsp-raknet' (JS port), and 'raknet-node' (Rust port). Please note when using the non-JS implementation you may the need approporate build tools on your system (for example a C++ or Rust compiler). |
| compressionLevel | *optional* | What zlib compression level to use, default to **7** |

2
index.d.ts vendored
View file

@ -39,6 +39,8 @@ declare module "bedrock-protocol" {
connectTimeout?: number
// whether to skip initial ping and immediately connect
skipPing?: boolean
// Update the options' port parameter to match the port broadcast on the server's ping data (default to true if `realms` not specified)
followPort?: boolean
// where to log connection information to (default to console.log)
conLog?
// used to join a Realm instead of supplying a host/port

View file

@ -9,7 +9,7 @@ const auth = require('./client/auth')
/** @param {{ version?: number, host: string, port?: number, connectTimeout?: number, skipPing?: boolean }} options */
function createClient (options) {
assert(options)
const client = new Client({ port: 19132, ...options, delayedInit: true })
const client = new Client({ port: 19132, followPort: !options.realms, ...options, delayedInit: true })
function onServerInfo () {
client.on('connect_allowed', () => connect(client))
@ -20,11 +20,11 @@ function createClient (options) {
const adVersion = ad.version?.split('.').slice(0, 3).join('.') // Only 3 version units
client.options.version = options.version ?? (Options.Versions[adVersion] ? adVersion : Options.CURRENT_VERSION)
if (ad.port) {
if (ad.port && options.followPort) {
client.options.port = ad.port
}
client.conLog?.(`Connecting to ${client.options.host}:${client.options.port} ${ad.motd} (${ad.levelName}), version ${ad.version}`, client.options.version !== ad.version ? ` (as ${client.options.version})` : '')
client.conLog?.(`Connecting to ${client.options.host}:${client.options.port} ${ad.motd} (${ad.levelName}), version ${ad.version} ${client.options.version !== ad.version ? ` (as ${client.options.version})` : ''}`)
client.init()
}).catch(e => client.emit('error', e))
}