bedrock-portal-nethernet 1.0.26 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # BedrockPortalNethernet
2
- [![NPM version](https://img.shields.io/npm/v/bedrock-portal.svg)](http://npmjs.com/package/bedrock-portal-nethernet)
2
+ [![NPM version](https://img.shields.io/npm/v/bedrock-portal-nethernet.svg)](http://npmjs.com/package/bedrock-portal-nethernet)
3
3
  [![Discord](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg)](https://discord.gg/KTyd9HWuBD)
4
4
 
5
5
  A simple server implementation for the Bedrock Edition Nethernet protocol used by [BedrockPortal](https://github.com/LucienHH/bedrock-portal). Join our [Discord](https://discord.com/invite/KTyd9HWuBD) for support.
@@ -3,9 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const crypto_1 = __importDefault(require("crypto"));
6
7
  const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
7
8
  const constants_1 = require("./constants");
8
- const crypto_1 = __importDefault(require("crypto"));
9
9
  const debug = require('debug')('bedrock-portal-nethernet');
10
10
  exports.default = (client) => {
11
11
  // Refer to the docs:
@@ -1,17 +1,17 @@
1
+ import { PeerConnection, DataChannel } from 'node-datachannel';
1
2
  import { Server } from './server';
2
- import { RTCDataChannel, RTCPeerConnection } from 'werift';
3
3
  export declare const maxMessageSize = 10000;
4
4
  export declare class Connection {
5
5
  nethernet: Server;
6
6
  connectionId: bigint;
7
- rtcConnection: RTCPeerConnection;
8
- reliable: RTCDataChannel | null;
9
- unreliable: RTCDataChannel | null;
7
+ rtcConnection: PeerConnection;
8
+ reliable: DataChannel | null;
9
+ unreliable: DataChannel | null;
10
10
  promisedSegments: number;
11
11
  buf: Buffer | null;
12
- constructor(nethernet: Server, connectionId: bigint, rtcConnection: RTCPeerConnection);
13
- setChannels(reliable: RTCDataChannel | null, unreliable?: RTCDataChannel): void;
14
- handleMessage(data: string | Buffer): void;
12
+ constructor(nethernet: Server, connectionId: bigint, rtcConnection: PeerConnection);
13
+ setChannels(reliable: DataChannel | null, unreliable?: DataChannel): void;
14
+ handleMessage(data: string | Buffer | ArrayBuffer): void;
15
15
  onPacket(packet: Buffer): void;
16
16
  write(data: string | Buffer): number;
17
17
  close(): void;
@@ -23,7 +23,7 @@ class Connection {
23
23
  setChannels(reliable, unreliable) {
24
24
  if (reliable) {
25
25
  this.reliable = reliable;
26
- this.reliable.onmessage = (msg) => this.handleMessage(msg.data);
26
+ this.reliable.onMessage((msg) => this.handleMessage(msg));
27
27
  }
28
28
  if (unreliable) {
29
29
  this.unreliable = unreliable;
@@ -33,6 +33,9 @@ class Connection {
33
33
  if (typeof data === 'string') {
34
34
  data = Buffer.from(data);
35
35
  }
36
+ if (data instanceof ArrayBuffer) {
37
+ data = Buffer.from(data);
38
+ }
36
39
  if (data.length < 2) {
37
40
  throw new Error('Unexpected EOF');
38
41
  }
@@ -70,7 +73,7 @@ class Connection {
70
73
  const frag = data.subarray(i, end);
71
74
  const message = Buffer.concat([Buffer.from([segments]), frag]);
72
75
  debugFn('Sending fragment', segments, 'header', message[0]);
73
- this.reliable.send(message);
76
+ this.reliable.sendMessageBinary(message);
74
77
  n += frag.length;
75
78
  }
76
79
  if (segments !== 0) {
@@ -1,11 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Server = void 0;
4
- const werift_1 = require("werift");
4
+ const node_datachannel_1 = require("node-datachannel");
5
5
  const connection_1 = require("./connection");
6
- const signal_1 = require("../signaling/signal");
7
6
  const struct_1 = require("../signaling/struct");
8
7
  const debugFn = require('debug')('bedrock-portal-nethernet');
8
+ const getRandomUint64 = () => {
9
+ const high = Math.floor(Math.random() * 0xFFFFFFFF);
10
+ const low = Math.floor(Math.random() * 0xFFFFFFFF);
11
+ return (BigInt(high) << 32n) | BigInt(low);
12
+ };
9
13
  class Server {
10
14
  networkId;
11
15
  connectionId;
@@ -14,7 +18,7 @@ class Server {
14
18
  onOpenConnection;
15
19
  onCloseConnection;
16
20
  onEncapsulated;
17
- constructor(signaling, networkId = (0, signal_1.getRandomUint64)(), connectionId = (0, signal_1.getRandomUint64)()) {
21
+ constructor(signaling, networkId = getRandomUint64(), connectionId = getRandomUint64()) {
18
22
  this.signaling = signaling;
19
23
  this.networkId = networkId;
20
24
  this.connectionId = connectionId;
@@ -26,39 +30,39 @@ class Server {
26
30
  async handleCandidate(signal) {
27
31
  const conn = this.connections.get(signal.connectionId);
28
32
  if (conn) {
29
- await conn.rtcConnection.addIceCandidate(new werift_1.RTCIceCandidate({ candidate: signal.data }));
33
+ conn.rtcConnection.addRemoteCandidate(signal.data, '0');
30
34
  }
31
35
  else {
32
36
  debugFn('Received candidate for unknown connection', signal);
33
37
  }
34
38
  }
35
39
  async handleOffer(signal) {
36
- const rtcConnection = new werift_1.RTCPeerConnection({
37
- iceServers: this.signaling.credentials,
38
- });
40
+ if (!this.signaling.credentials) {
41
+ throw new Error('No credentials set');
42
+ }
43
+ const rtcConnection = new node_datachannel_1.PeerConnection('pc', { iceServers: this.signaling.credentials });
39
44
  const connection = new connection_1.Connection(this, signal.connectionId, rtcConnection);
40
45
  this.connections.set(signal.connectionId, connection);
41
- rtcConnection.onicecandidate = (e) => {
42
- if (e.candidate) {
43
- this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.CandidateAdd, signal.connectionId, e.candidate.candidate, signal.networkId));
44
- }
45
- };
46
- rtcConnection.ondatachannel = ({ channel }) => {
47
- if (channel.label === 'ReliableDataChannel')
46
+ rtcConnection.onLocalCandidate(candidate => {
47
+ this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.CandidateAdd, signal.connectionId, candidate, signal.networkId));
48
+ });
49
+ rtcConnection.onDataChannel(channel => {
50
+ if (channel.getLabel() === 'ReliableDataChannel')
48
51
  connection.setChannels(channel);
49
- if (channel.label === 'UnreliableDataChannel')
52
+ if (channel.getLabel() === 'UnreliableDataChannel')
50
53
  connection.setChannels(null, channel);
51
- };
52
- rtcConnection.onconnectionstatechange = () => {
53
- const state = rtcConnection.connectionState;
54
+ });
55
+ rtcConnection.onIceStateChange(state => {
54
56
  if (state === 'connected')
55
57
  this.onOpenConnection(connection);
56
58
  if (state === 'disconnected')
57
59
  this.onCloseConnection(signal.connectionId, 'disconnected');
58
- };
59
- await rtcConnection.setRemoteDescription({ type: 'offer', sdp: signal.data });
60
- const answer = await rtcConnection.createAnswer();
61
- await rtcConnection.setLocalDescription(answer);
60
+ });
61
+ rtcConnection.setRemoteDescription(signal.data, 'offer');
62
+ const answer = rtcConnection.localDescription();
63
+ if (!answer) {
64
+ throw new Error('No answer');
65
+ }
62
66
  this.signaling.write(new struct_1.SignalStructure(struct_1.SignalType.ConnectResponse, signal.connectionId, answer.sdp, signal.networkId));
63
67
  }
64
68
  async listen() {
package/dist/options.d.ts CHANGED
@@ -9,7 +9,7 @@ export type Options = {
9
9
  compressionThreshold: number;
10
10
  protocolVersion: number;
11
11
  };
12
- export declare const CURRENT_VERSION = "1.21.50";
12
+ export declare const CURRENT_VERSION = "1.21.60";
13
13
  export declare const defaultOptions: {
14
14
  version: string;
15
15
  autoInitPlayer: boolean;
package/dist/options.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultOptions = exports.CURRENT_VERSION = void 0;
4
4
  const framer_1 = require("./transforms/framer");
5
5
  // Currently supported verson. Note, clients with newer versions can still connect as long as data is in minecraft-data
6
- exports.CURRENT_VERSION = '1.21.50';
6
+ exports.CURRENT_VERSION = '1.21.60';
7
7
  exports.defaultOptions = {
8
8
  // https://minecraft.wiki/w/Protocol_version#Bedrock_Edition_2
9
9
  version: exports.CURRENT_VERSION,
@@ -248,7 +248,7 @@ class Player extends tiny_typed_emitter_1.TypedEmitter {
248
248
  this.sendPacket(batch.encode());
249
249
  }
250
250
  sendPacket(buffer) {
251
- if (this.connection.reliable?.readyState !== 'open' || this.status === exports.ClientStatus.Disconnected)
251
+ if (!this.connection.reliable?.isOpen() || this.status === exports.ClientStatus.Disconnected)
252
252
  return;
253
253
  try {
254
254
  this.connection.write(buffer);
@@ -1,15 +1,20 @@
1
1
  import type { Authflow } from 'prismarine-auth';
2
+ import { EventEmitter } from 'events';
2
3
  import { Data, ErrorEvent, WebSocket } from 'ws';
3
4
  import { SignalStructure } from './struct';
4
- import { EventEmitter } from 'events';
5
- export declare function getRandomUint64(): bigint;
6
5
  export declare class Signal extends EventEmitter {
7
- authflow: Authflow;
8
6
  ws: WebSocket | null;
9
- pingInterval: NodeJS.Timeout | null;
10
7
  networkId: bigint;
11
- version: string;
12
- credentials: any;
8
+ credentials: {
9
+ hostname: string;
10
+ port: number;
11
+ username: string;
12
+ password: string;
13
+ }[] | null;
14
+ private authflow;
15
+ private version;
16
+ private pingInterval;
17
+ private retryCount;
13
18
  constructor(authflow: Authflow, networkId: bigint, version: string);
14
19
  connect(): Promise<void>;
15
20
  destroy(resume?: boolean): Promise<void>;
@@ -4,41 +4,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Signal = void 0;
7
- exports.getRandomUint64 = getRandomUint64;
8
7
  const debug_1 = __importDefault(require("debug"));
8
+ const json_bigint_1 = require("json-bigint");
9
+ const events_1 = require("events");
9
10
  const ws_1 = require("ws");
10
11
  const struct_1 = require("./struct");
11
- const events_1 = require("events");
12
- const json_bigint_1 = require("json-bigint");
13
12
  const MessageType = {
14
13
  RequestPing: 0,
15
14
  Signal: 1,
16
15
  Credentials: 2,
17
16
  };
18
17
  const debug = (0, debug_1.default)('bedrock-portal-nethernet');
19
- function getRandomUint64() {
20
- // Generate two 32-bit random integers
21
- const high = Math.floor(Math.random() * 0xFFFFFFFF);
22
- const low = Math.floor(Math.random() * 0xFFFFFFFF);
23
- // Combine them to create a 64-bit unsigned integer
24
- const result = (BigInt(high) << 32n) | BigInt(low);
25
- return result;
26
- }
27
18
  class Signal extends events_1.EventEmitter {
28
- authflow;
29
19
  ws;
30
- pingInterval;
31
20
  networkId;
32
- version;
33
21
  credentials;
22
+ authflow;
23
+ version;
24
+ pingInterval;
25
+ retryCount;
34
26
  constructor(authflow, networkId, version) {
35
27
  super();
36
28
  this.authflow = authflow;
37
29
  this.networkId = networkId;
38
30
  this.version = version;
39
31
  this.ws = null;
40
- this.pingInterval = null;
41
32
  this.credentials = null;
33
+ this.pingInterval = null;
34
+ this.retryCount = 0;
42
35
  }
43
36
  async connect() {
44
37
  if (this.ws?.readyState === ws_1.WebSocket.OPEN)
@@ -83,7 +76,7 @@ class Signal extends events_1.EventEmitter {
83
76
  if (this.ws && this.ws.readyState === ws_1.WebSocket.OPEN) {
84
77
  this.ws.send(JSON.stringify({ Type: MessageType.RequestPing }));
85
78
  }
86
- });
79
+ }, 5000);
87
80
  ws.onopen = () => {
88
81
  this.onOpen();
89
82
  };
@@ -108,7 +101,14 @@ class Signal extends events_1.EventEmitter {
108
101
  debug(`Signal Disconnected with code ${code} and reason ${reason}`);
109
102
  if (code === 1006) {
110
103
  debug('Signal Connection Closed Unexpectedly');
111
- this.destroy(true);
104
+ if (this.retryCount < 5) {
105
+ this.retryCount++;
106
+ this.destroy(true);
107
+ }
108
+ else {
109
+ this.destroy();
110
+ throw new Error('Signal Connection Closed Unexpectedly');
111
+ }
112
112
  }
113
113
  }
114
114
  onMessage(res) {
@@ -122,13 +122,7 @@ class Signal extends events_1.EventEmitter {
122
122
  debug('received credentials from non-Server', 'message', message);
123
123
  return;
124
124
  }
125
- this.credentials = JSON.parse(message.Message).TurnAuthServers.map((credential) => {
126
- return {
127
- urls: credential.Urls.join(','),
128
- credential: credential.Password,
129
- username: credential.Username,
130
- };
131
- });
125
+ this.credentials = parseTurnServers(message.Message);
132
126
  this.emit('credentials', this.credentials);
133
127
  break;
134
128
  }
@@ -151,3 +145,25 @@ class Signal extends events_1.EventEmitter {
151
145
  }
152
146
  }
153
147
  exports.Signal = Signal;
148
+ function parseTurnServers(dataString) {
149
+ const servers = [];
150
+ const data = JSON.parse(dataString);
151
+ if (!data.TurnAuthServers)
152
+ return servers;
153
+ for (const server of data.TurnAuthServers) {
154
+ if (!server.Urls)
155
+ continue;
156
+ for (const url of server.Urls) {
157
+ const match = url.match(/(stun|turn):([^:]+):(\d+)/);
158
+ if (match) {
159
+ servers.push({
160
+ hostname: match[2],
161
+ port: parseInt(match[3], 10),
162
+ username: server.Username || undefined,
163
+ password: server.Password || undefined,
164
+ });
165
+ }
166
+ }
167
+ }
168
+ return servers;
169
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bedrock-portal-nethernet",
3
- "version": "1.0.26",
3
+ "version": "2.0.0",
4
4
  "description": "A simple server implementation for the Bedrock Edition Nethernet protocol",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,23 +15,19 @@
15
15
  "debug": "^4.3.4",
16
16
  "json-bigint": "^1.0.0",
17
17
  "jsonwebtoken": "^9.0.0",
18
+ "node-datachannel": "^0.25.0",
18
19
  "prismarine-auth": "github:LucienHH/prismarine-auth#playfab",
19
20
  "prismarine-nbt": "^2.0.0",
20
21
  "protodef": "^1.14.0",
21
- "sdp-transform": "^2.14.2",
22
22
  "tiny-typed-emitter": "^2.1.0",
23
23
  "uuid-1345": "^1.0.2",
24
- "werift": "^0.19.9",
25
24
  "ws": "^8.18.0"
26
25
  },
27
26
  "devDependencies": {
28
27
  "@types/debug": "^4.1.12",
29
28
  "@types/json-bigint": "^1.0.4",
30
29
  "@types/jsonwebtoken": "^9.0.6",
31
- "@types/multicast-dns": "^7.2.4",
32
30
  "@types/node": "^20.10.6",
33
- "@types/sdp-transform": "^2.4.9",
34
- "@types/uuid": "^9.0.8",
35
31
  "@types/uuid-1345": "^0.99.25",
36
32
  "@types/ws": "^8.5.12",
37
33
  "@typescript-eslint/eslint-plugin": "^6.17.0",