rename hostname to host (#74)

This commit is contained in:
u9g 2021-04-23 03:23:43 -04:00 committed by GitHub
commit 39659cf48b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 62 additions and 64 deletions

View file

@ -63,7 +63,7 @@ client.on('text', (packet) => { // Listen for chat messages and echo them back.
```js
const bedrock = require('bedrock-protocol')
const server = new bedrock.createServer({
host: '0.0.0.0', // optional. Hostname to bind as.
host: '0.0.0.0', // optional. host to bind as.
port: 19132, // optional
version: '1.16.220' // optional. The server version, latest if not specified.
})

View file

@ -8,7 +8,7 @@ Returns a `Client` instance and connects to the server.
| Parameter | Optionality | Description |
| ----------- | ----------- |-|
| host | **Required** | Hostname to connect to, for example `127.0.0.1`. |
| host | **Required** | host to connect to, for example `127.0.0.1`. |
| port | *optional* | port to connect to, default to **19132** |
| version | *optional* | Version to connect as. <br/>(Future feature, see [#69][1]) If not specified, should automatically match server version. <br/>(Current feature) Defaults to latest version. |
| offline | *optional* | default to **false**. Set this to true to disable Microsoft/Xbox auth. |
@ -28,7 +28,7 @@ authenticated unless offline is set to true.
| Parameter | Optionality | Description |
| ----------- | ----------- |-|
| host | **Required** | The hostname to bind to. use `0.0.0.0` to bind all IPv4 addresses. |
| host | **Required** | The host to bind to. use `0.0.0.0` to bind all IPv4 addresses. |
| port | *optional* | the port to bind to, default **19132** |
| version | *optional* | Version to run server as. Clients below this version will be kicked, clients above will still be permitted. |
| offline | *optional* | default to **false**. Set this to true to disable Microsoft/Xbox auth enforcement. |
@ -55,7 +55,7 @@ You can create a server as such:
```js
const bedrock = require('bedrock-protocol')
const server = bedrock.createServer({
host: '0.0.0.0', // the hostname to bind to, use '0.0.0.0' to bind all hostnames
host: '0.0.0.0', // the host to bind to, use '0.0.0.0' to bind all hosts
port: 19132, // optional, port to bind to, default 19132
offline: false, // default false. verify connections with XBL
motd: {
@ -90,7 +90,7 @@ You can create a server as such:
```js
const bedrock = require('bedrock-protocol')
const client = bedrock.createClient({
host: '127.0.0.1', // the hostname to bind to, use '0.0.0.0' to bind all hostnames
host: '127.0.0.1', // the host to bind to, use '0.0.0.0' to bind all hosts
port: 19132, // optional, port to bind to, default 19132
username: 'Notch' // Any profile name, only used internally for account caching when in online mode. In offline mode, the username to connect with.
})
@ -129,12 +129,12 @@ You can create a proxy ("Relay") to create a machine-in-the-middle (MITM) connec
const { Relay } = require('bedrock-protocol')
const relay = new Relay({
version: '1.16.220', // The version
/* Hostname and port to listen for clients on */
hostname: '0.0.0.0',
/* host and port to listen for clients on */
host: '0.0.0.0',
port: 19132,
/* Where to send upstream packets to */
destination: {
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19131
}
})

View file

@ -4,7 +4,7 @@ const { ChunkColumn, Version } = require('bedrock-provider')
async function test () {
const client = new Client({
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19132
// You can specify version by adding :
// version: '1.16.210'

View file

@ -1,6 +1,6 @@
const { createClient } = require('bedrock-protocol')
const client = createClient({ hostname: '127.0.0.1' })
const client = createClient({ host: '127.0.0.1' })
let ix = 0
client.on('packet', (args) => {

View file

@ -4,12 +4,12 @@ function createRelay () {
console.log('Creating relay')
/* Example to create a non-transparent proxy (or 'Relay') connection to destination server */
const relay = new Relay({
/* Hostname and port for clients to listen to */
hostname: '0.0.0.0',
/* host and port for clients to listen to */
host: '0.0.0.0',
port: 19130,
/* Where to send upstream packets to */
destination: {
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19132
}
})

View file

@ -5,12 +5,12 @@ const { Relay } = require('bedrock-protocol')
// Start the proxy server
const relay = new Relay({
version: '1.16.220', // The version
/* Hostname and port to listen for clients on */
hostname: '0.0.0.0',
/* host and port to listen for clients on */
host: '0.0.0.0',
port: 19132,
/* Where to send upstream packets to */
destination: {
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19131
}
})

View file

@ -23,7 +23,7 @@ async function startServer (version = '1.16.220', ok) {
const Item = require('../types/Item')(version)
const port = 19132
const server = new Server({ hostname: '0.0.0.0', port, version })
const server = new Server({ host: '0.0.0.0', port, version })
let loop
const getPath = (packetPath) => DataProvider(server.options.protocolVersion).getPath(packetPath)

View file

@ -14,7 +14,7 @@ class ClientProvider extends BotProvider {
downKeys = new Set()
connect () {
const client = new Client({ hostname: '127.0.0.1', version: '1.16.210', username: 'notch', offline: true, port: 19132, connectTimeout: 100000 })
const client = new Client({ host: '127.0.0.1', version: '1.16.210', username: 'notch', offline: true, port: 19132, connectTimeout: 100000 })
client.once('resource_packs_info', (packet) => {
client.write('resource_pack_client_response', {

View file

@ -7,11 +7,11 @@ class ProxyProvider extends BotProvider {
connect () {
const proxy = new Relay({
hostname: '0.0.0.0',
host: '0.0.0.0',
port: 19130,
// logging: true,
destination: {
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19132
}
})

8
index.d.ts vendored
View file

@ -6,8 +6,8 @@ declare module "bedrock-protocol" {
export interface Options {
// The string version to start the client or server as
version: number,
// For the client, the hostname of the server to connect to (default: 127.0.0.1)
// For the server, the hostname to bind to (default: 0.0.0.0)
// For the client, the host of the server to connect to (default: 127.0.0.1)
// For the server, the host to bind to (default: 0.0.0.0)
host: string,
// The port to connect or bind to, default: 19132
port: number,
@ -103,13 +103,13 @@ declare module "bedrock-protocol" {
}
type RelayOptions = Options & {
hostname: string,
host: string,
port: number,
// Toggle packet logging.
logging: boolean,
// Where to proxy requests to.
destination: {
hostname: string,
host: string,
port: number
}
}

View file

@ -66,7 +66,7 @@ module.exports = (client, server, options) => {
PlayFabId: '5eb65f73-af11-448e-82aa-1b7b165316ad.persona-e199672a8c1a87e0-0', // 1.16.210
PremiumSkin: false,
SelfSignedId: nextUUID(),
ServerAddress: `${options.hostname}:${options.port}`,
ServerAddress: `${options.host}:${options.port}`,
SkinAnimationData: '',
SkinColor: '#ffffcd96',
SkinData: 'AAAAAA==',

View file

@ -17,7 +17,7 @@ class Client extends Connection {
// The RakNet connection
connection
/** @param {{ version: number, hostname: string, port: number }} options */
/** @param {{ version: number, host: string, port: number }} options */
constructor (options) {
super()
this.options = { ...Options.defaultOptions, ...options }
@ -29,9 +29,9 @@ class Client extends Connection {
Login(this, null, this.options)
LoginVerify(this, null, this.options)
const hostname = this.options.hostname
const host = this.options.host
const port = this.options.port
this.connection = new RakClient({ useWorkers: true, hostname, port })
this.connection = new RakClient({ useWorkers: true, host, port })
this.startGameData = {}
this.clientRuntimeId = null
@ -54,7 +54,7 @@ class Client extends Connection {
}
validateOptions () {
if (!this.options.hostname || this.options.port == null) throw Error('Invalid hostname/port')
if (!this.options.host || this.options.port == null) throw Error('Invalid host/port')
if (!Options.Versions[this.options.version]) {
console.warn('Supported versions: ', Options.Versions)
@ -79,13 +79,13 @@ class Client extends Connection {
try {
return await this.connection.ping(this.options.connectTimeout)
} catch (e) {
console.warn(`Unable to connect to [${this.options.hostname}]/${this.options.port}. Is the server running?`)
console.warn(`Unable to connect to [${this.options.host}]/${this.options.port}. Is the server running?`)
throw e
}
}
_connect = async (sessionData) => {
debug('[client] connecting to', this.options.hostname, this.options.port, sessionData, this.connection)
debug('[client] connecting to', this.options.host, this.options.port, sessionData, this.connection)
this.connection.onConnected = () => this.sendLogin()
this.connection.onCloseConnection = () => this.close()
this.connection.onEncapsulated = this.onEncapsulated

View file

@ -3,10 +3,9 @@ const { RakClient } = require('./rak')
const assert = require('assert')
const advertisement = require('./server/advertisement')
/** @param {{ version?: number, hostname: string, port?: number, connectTimeout?: number }} options */
/** @param {{ version?: number, host: string, port?: number, connectTimeout?: number }} options */
function createClient (options) {
assert(options)
if (options.host) options.hostname = options.host
const client = new Client({ port: 19132, ...options })
if (options.skipPing) {
@ -67,7 +66,7 @@ function connect (client) {
}
async function ping ({ host, port }) {
const con = new RakClient({ hostname: host, port })
const con = new RakClient({ host, port })
const ret = await con.ping()
con.close()
return advertisement.fromServerName(ret)

View file

@ -1,7 +1,6 @@
const { Server } = require('./server')
function createServer (options) {
if (options.host) options.hostname = options.host
if (!options.port) options.port = 19132
const server = new Server(options)
server.listen()

View file

@ -19,7 +19,7 @@ class RakNativeClient extends EventEmitter {
this.onCloseConnection = () => { }
this.onEncapsulated = () => { }
this.raknet = new Client(options.hostname, options.port, { protocolVersion: 10 })
this.raknet = new Client(options.host, options.port, { protocolVersion: 10 })
this.raknet.on('encapsulated', ({ buffer, address }) => {
this.onEncapsulated(buffer, address)
})
@ -70,7 +70,7 @@ class RakNativeServer extends EventEmitter {
this.onOpenConnection = () => { }
this.onCloseConnection = () => { }
this.onEncapsulated = () => { }
this.raknet = new Server(options.hostname, options.port, {
this.raknet = new Server(options.host, options.port, {
maxConnections: options.maxPlayers || 3,
protocolVersion: 10,
message: server.getAdvertisement().toBuffer()
@ -120,8 +120,8 @@ class RakJsClient extends EventEmitter {
}
}
workerConnect (hostname = this.options.hostname, port = this.options.port) {
this.worker = ConnWorker.connect(hostname, port)
workerConnect (host = this.options.host, port = this.options.port) {
this.worker = ConnWorker.connect(host, port)
this.worker.on('message', (evt) => {
switch (evt.type) {
@ -138,12 +138,12 @@ class RakJsClient extends EventEmitter {
})
}
async plainConnect (hostname = this.options.hostname, port = this.options.port) {
this.raknet = new RakClient(hostname, port)
async plainConnect (host = this.options.host, port = this.options.port) {
this.raknet = new RakClient(host, port)
await this.raknet.connect()
this.raknet.on('connecting', () => {
console.log(`[client] connecting to ${hostname}/${port}`)
console.log(`[client] connecting to ${host}/${port}`)
})
this.raknet.on('connected', this.onConnected)
@ -180,7 +180,7 @@ class RakJsServer extends EventEmitter {
async plainListen () {
this.raknet = new Listener()
await this.raknet.listen(this.options.hostname, this.options.port)
await this.raknet.listen(this.options.host, this.options.port)
this.raknet.on('openConnection', (conn) => {
conn.sendReliable = function (buffer, immediate) {
const sendPacket = new EncapsulatedPacket()

View file

@ -3,10 +3,10 @@ const { Worker, isMainThread, parentPort } = require('worker_threads')
const EncapsulatedPacket = require('jsp-raknet/protocol/encapsulated_packet')
const Reliability = require('jsp-raknet/protocol/reliability')
function connect (hostname, port) {
function connect (host, port) {
if (isMainThread) {
const worker = new Worker(__filename)
worker.postMessage({ type: 'connect', hostname, port })
worker.postMessage({ type: 'connect', host, port })
return worker
}
}
@ -16,15 +16,15 @@ let raknet
function main () {
parentPort.on('message', (evt) => {
if (evt.type === 'connect') {
const { hostname, port } = evt
raknet = new RakClient(hostname, port)
const { host, port } = evt
raknet = new RakClient(host, port)
raknet.connect().then(() => {
console.log('Raknet Connected!')
})
raknet.on('connecting', () => {
console.log(`[client] connecting to ${hostname}/${port}`)
console.log(`[client] connecting to ${host}/${port}`)
parentPort.postMessage('message', { type: 'connecting' })
console.log('Raknet', raknet)
})

View file

@ -138,14 +138,14 @@ class Relay extends Server {
offline: this.options.offline,
username: this.options.offline ? ds.profile.name : null,
version: this.options.version,
hostname: this.options.destination.hostname,
host: this.options.destination.host,
port: this.options.destination.port,
autoInitPlayer: false
})
// Set the login payload unless `noLoginForward` option
if (!client.noLoginForward) client.skinData = ds.skinData
client.connect()
this.conLog('Connecting to', this.options.destination.hostname, this.options.destination.port)
this.conLog('Connecting to', this.options.destination.host, this.options.destination.port)
client.outLog = ds.upOutLog
client.inLog = ds.upInLog
client.once('join', () => { // Intercept once handshaking done

View file

@ -68,15 +68,15 @@ class Server extends EventEmitter {
return this.advertisement
}
async listen (hostname = this.options.hostname, port = this.options.port) {
this.raknet = new RakServer({ hostname, port }, this)
async listen (host = this.options.host, port = this.options.port) {
this.raknet = new RakServer({ host, port }, this)
try {
await this.raknet.listen()
} catch (e) {
console.warn(`Failed to bind server on [${this.options.hostname}]/${this.options.port}, is the port free?`)
console.warn(`Failed to bind server on [${this.options.host}]/${this.options.port}, is the port free?`)
throw e
}
this.conLog('Listening on', hostname, port, this.options.version)
this.conLog('Listening on', host, port, this.options.version)
this.raknet.onOpenConnection = this.onOpenConnection
this.raknet.onCloseConnection = this.onCloseConnection
this.raknet.onEncapsulated = this.onEncapsulated
@ -85,7 +85,7 @@ class Server extends EventEmitter {
this.raknet.updateAdvertisement()
}, 1000)
return { hostname, port }
return { host, port }
}
async close (disconnectReason) {

View file

@ -14,7 +14,7 @@ async function startTest (version = '1.16.220', ok) {
await prepare(version)
const Item = require('../types/Item')(version)
const port = 19130
const server = new Server({ hostname: '0.0.0.0', port, version, offline: true })
const server = new Server({ host: '0.0.0.0', port, version, offline: true })
function getPath (packetPath) {
return DataProvider(server.options.protocolVersion).getPath(packetPath)
@ -108,7 +108,7 @@ async function startTest (version = '1.16.220', ok) {
// client logic
const client = new Client({
hostname: '127.0.0.1',
host: '127.0.0.1',
port,
username: 'Notch',
version,

View file

@ -23,12 +23,12 @@ function proxyTest (version, timeout = 1000 * 20) {
const relay = new Relay({
version,
offline: true,
/* Hostname and port for clients to listen to */
hostname: '0.0.0.0',
/* host and port for clients to listen to */
host: '0.0.0.0',
port: 19132,
/* Where to send upstream packets to */
destination: {
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19131
}
})
@ -37,7 +37,7 @@ function proxyTest (version, timeout = 1000 * 20) {
console.debug('Proxy started', server.options.version)
const client = createClient({ hostname: '127.0.0.1', version, username: 'Boat', offline: true })
const client = createClient({ host: '127.0.0.1', version, username: 'Boat', offline: true })
console.debug('Client started')

View file

@ -11,7 +11,7 @@ async function test (version) {
console.log('Started server')
const client = new Client({
hostname: '127.0.0.1',
host: '127.0.0.1',
port: 19130,
username: 'Notch',
version,

View file

@ -20,7 +20,7 @@ async function dump (version) {
console.log('Started server')
const client = new Client({
hostname: '127.0.0.1',
host: '127.0.0.1',
port,
username: 'dumpBot',
offline: true

View file

@ -25,7 +25,7 @@ async function dump (version, force) {
console.log('Started server')
const client = new Client({
hostname: '127.0.0.1',
host: '127.0.0.1',
port,
version,
username: 'Boat' + random,