bedrock-portal-nethernet 1.0.27 → 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 +1 -1
- package/dist/handshake/loginVerify.js +1 -1
- package/dist/nethernet/connection.d.ts +7 -7
- package/dist/nethernet/connection.js +5 -2
- package/dist/nethernet/server.js +26 -22
- package/dist/serverPlayer.js +1 -1
- package/dist/signaling/signal.d.ts +11 -6
- package/dist/signaling/signal.js +40 -24
- package/package.json +2 -6
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# BedrockPortalNethernet
|
|
2
|
-
[](http://npmjs.com/package/bedrock-portal-nethernet)
|
|
2
|
+
[](http://npmjs.com/package/bedrock-portal-nethernet)
|
|
3
3
|
[](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:
|
|
8
|
-
reliable:
|
|
9
|
-
unreliable:
|
|
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:
|
|
13
|
-
setChannels(reliable:
|
|
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.
|
|
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.
|
|
76
|
+
this.reliable.sendMessageBinary(message);
|
|
74
77
|
n += frag.length;
|
|
75
78
|
}
|
|
76
79
|
if (segments !== 0) {
|
package/dist/nethernet/server.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Server = void 0;
|
|
4
|
-
const
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
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.
|
|
42
|
-
|
|
43
|
-
|
|
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.
|
|
52
|
+
if (channel.getLabel() === 'UnreliableDataChannel')
|
|
50
53
|
connection.setChannels(null, channel);
|
|
51
|
-
};
|
|
52
|
-
rtcConnection.
|
|
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
|
-
|
|
60
|
-
const answer =
|
|
61
|
-
|
|
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/serverPlayer.js
CHANGED
|
@@ -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?.
|
|
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
|
-
|
|
12
|
-
|
|
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>;
|
package/dist/signaling/signal.js
CHANGED
|
@@ -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.
|
|
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 =
|
|
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": "
|
|
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",
|