@baltica/raknet 0.0.1

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.
Files changed (95) hide show
  1. package/dist/client/client.d.ts +20 -0
  2. package/dist/client/client.js +122 -0
  3. package/dist/client/index.d.ts +2 -0
  4. package/dist/client/index.js +18 -0
  5. package/dist/client/types/client-events.d.ts +6 -0
  6. package/dist/client/types/client-events.js +2 -0
  7. package/dist/client/types/client-options.d.ts +12 -0
  8. package/dist/client/types/client-options.js +11 -0
  9. package/dist/client/types/index.d.ts +2 -0
  10. package/dist/client/types/index.js +18 -0
  11. package/dist/index.d.ts +3 -0
  12. package/dist/index.js +19 -0
  13. package/dist/server/connection.d.ts +16 -0
  14. package/dist/server/connection.js +30 -0
  15. package/dist/server/index.d.ts +3 -0
  16. package/dist/server/index.js +19 -0
  17. package/dist/server/server.d.ts +19 -0
  18. package/dist/server/server.js +119 -0
  19. package/dist/server/types/index.d.ts +2 -0
  20. package/dist/server/types/index.js +18 -0
  21. package/dist/server/types/server-events.d.ts +6 -0
  22. package/dist/server/types/server-events.js +2 -0
  23. package/dist/server/types/server-options.d.ts +10 -0
  24. package/dist/server/types/server-options.js +11 -0
  25. package/dist/shared/index.d.ts +3 -0
  26. package/dist/shared/index.js +19 -0
  27. package/dist/shared/network-session.d.ts +63 -0
  28. package/dist/shared/network-session.js +480 -0
  29. package/dist/shared/proto/enums/index.d.ts +4 -0
  30. package/dist/shared/proto/enums/index.js +20 -0
  31. package/dist/shared/proto/enums/packet.d.ts +21 -0
  32. package/dist/shared/proto/enums/packet.js +25 -0
  33. package/dist/shared/proto/enums/priority.d.ts +6 -0
  34. package/dist/shared/proto/enums/priority.js +8 -0
  35. package/dist/shared/proto/enums/reliability.d.ts +10 -0
  36. package/dist/shared/proto/enums/reliability.js +14 -0
  37. package/dist/shared/proto/enums/status.d.ts +6 -0
  38. package/dist/shared/proto/enums/status.js +10 -0
  39. package/dist/shared/proto/index.d.ts +3 -0
  40. package/dist/shared/proto/index.js +19 -0
  41. package/dist/shared/proto/packets/data-packet.d.ts +7 -0
  42. package/dist/shared/proto/packets/data-packet.js +14 -0
  43. package/dist/shared/proto/packets/index.d.ts +3 -0
  44. package/dist/shared/proto/packets/index.js +19 -0
  45. package/dist/shared/proto/packets/offline/index.d.ts +6 -0
  46. package/dist/shared/proto/packets/offline/index.js +22 -0
  47. package/dist/shared/proto/packets/offline/open-connection-reply-one.d.ts +13 -0
  48. package/dist/shared/proto/packets/offline/open-connection-reply-one.js +46 -0
  49. package/dist/shared/proto/packets/offline/open-connection-reply-two.d.ts +12 -0
  50. package/dist/shared/proto/packets/offline/open-connection-reply-two.js +32 -0
  51. package/dist/shared/proto/packets/offline/open-connection-request-one.d.ts +9 -0
  52. package/dist/shared/proto/packets/offline/open-connection-request-one.js +26 -0
  53. package/dist/shared/proto/packets/offline/open-connection-request-two.d.ts +13 -0
  54. package/dist/shared/proto/packets/offline/open-connection-request-two.js +43 -0
  55. package/dist/shared/proto/packets/offline/unconnected-ping.d.ts +9 -0
  56. package/dist/shared/proto/packets/offline/unconnected-ping.js +25 -0
  57. package/dist/shared/proto/packets/offline/unconnected-pong.d.ts +10 -0
  58. package/dist/shared/proto/packets/offline/unconnected-pong.js +28 -0
  59. package/dist/shared/proto/packets/online/ack.d.ts +5 -0
  60. package/dist/shared/proto/packets/online/ack.js +9 -0
  61. package/dist/shared/proto/packets/online/acknowledgement.d.ts +6 -0
  62. package/dist/shared/proto/packets/online/acknowledgement.js +72 -0
  63. package/dist/shared/proto/packets/online/connected-ping.d.ts +8 -0
  64. package/dist/shared/proto/packets/online/connected-ping.js +19 -0
  65. package/dist/shared/proto/packets/online/connected-pong.d.ts +9 -0
  66. package/dist/shared/proto/packets/online/connected-pong.js +22 -0
  67. package/dist/shared/proto/packets/online/connection-request-accepted.d.ts +13 -0
  68. package/dist/shared/proto/packets/online/connection-request-accepted.js +41 -0
  69. package/dist/shared/proto/packets/online/connection-request.d.ts +10 -0
  70. package/dist/shared/proto/packets/online/connection-request.js +25 -0
  71. package/dist/shared/proto/packets/online/disconnect.d.ts +7 -0
  72. package/dist/shared/proto/packets/online/disconnect.js +16 -0
  73. package/dist/shared/proto/packets/online/frame-set.d.ts +10 -0
  74. package/dist/shared/proto/packets/online/frame-set.js +24 -0
  75. package/dist/shared/proto/packets/online/index.d.ts +10 -0
  76. package/dist/shared/proto/packets/online/index.js +26 -0
  77. package/dist/shared/proto/packets/online/nack.d.ts +5 -0
  78. package/dist/shared/proto/packets/online/nack.js +9 -0
  79. package/dist/shared/proto/packets/online/new-incoming-connection.d.ts +12 -0
  80. package/dist/shared/proto/packets/online/new-incoming-connection.js +33 -0
  81. package/dist/shared/proto/types/address.d.ts +11 -0
  82. package/dist/shared/proto/types/address.js +61 -0
  83. package/dist/shared/proto/types/data-type.d.ts +5 -0
  84. package/dist/shared/proto/types/data-type.js +9 -0
  85. package/dist/shared/proto/types/frame.d.ts +22 -0
  86. package/dist/shared/proto/types/frame.js +113 -0
  87. package/dist/shared/proto/types/index.d.ts +5 -0
  88. package/dist/shared/proto/types/index.js +21 -0
  89. package/dist/shared/proto/types/magic.d.ts +7 -0
  90. package/dist/shared/proto/types/magic.js +17 -0
  91. package/dist/shared/proto/types/mtu.d.ts +5 -0
  92. package/dist/shared/proto/types/mtu.js +12 -0
  93. package/dist/shared/socks5.d.ts +16 -0
  94. package/dist/shared/socks5.js +166 -0
  95. package/package.json +26 -0
@@ -0,0 +1,20 @@
1
+ import { Emitter } from "@baltica/utils";
2
+ import { Priority } from "../shared";
3
+ import { ClientEvents, ClientOptions } from "./types";
4
+ export declare class Client extends Emitter<ClientEvents> {
5
+ private socket;
6
+ private network;
7
+ private tickInterval?;
8
+ private relay?;
9
+ options: ClientOptions;
10
+ constructor(options?: Partial<ClientOptions>);
11
+ private bindPromise;
12
+ private ensureBound;
13
+ private init;
14
+ private initProxy;
15
+ private initDirect;
16
+ ping(address?: string, port?: number): Promise<string>;
17
+ connect(): Promise<void>;
18
+ sendReliable(data: Buffer, priority?: Priority): void;
19
+ close(): void;
20
+ }
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = void 0;
4
+ const node_dgram_1 = require("node:dgram");
5
+ const node_crypto_1 = require("node:crypto");
6
+ const promises_1 = require("node:dns/promises");
7
+ const utils_1 = require("@baltica/utils");
8
+ const shared_1 = require("../shared");
9
+ const types_1 = require("./types");
10
+ class Client extends utils_1.Emitter {
11
+ socket;
12
+ network;
13
+ tickInterval;
14
+ relay;
15
+ options;
16
+ constructor(options = {}) {
17
+ super();
18
+ this.options = { ...types_1.defaultClientOptions, guid: (0, node_crypto_1.randomBytes)(8).readBigUInt64BE(), ...options };
19
+ this.network = new shared_1.NetworkSession(this.options.mtu, true);
20
+ this.network.guid = this.options.guid;
21
+ this.network.maxRetransmit = this.options.maxRetransmit;
22
+ this.network.on("connect", () => this.emit("connect"));
23
+ this.network.on("disconnect", () => this.emit("disconnect"));
24
+ this.network.on("encapsulated", (buf) => this.emit("encapsulated", buf));
25
+ this.network.on("error", (err) => this.emit("error", err));
26
+ }
27
+ bindPromise = null;
28
+ ensureBound() {
29
+ if (this.bindPromise)
30
+ return this.bindPromise;
31
+ this.bindPromise = this.init();
32
+ return this.bindPromise;
33
+ }
34
+ async init() {
35
+ const family = this.options.family === "udp4" ? 4 : 6;
36
+ const resolved = await (0, promises_1.lookup)(this.options.address, family);
37
+ this.network.address = new shared_1.Address(resolved.address, this.options.port, family);
38
+ if (this.options.proxy)
39
+ return this.initProxy();
40
+ return this.initDirect();
41
+ }
42
+ async initProxy() {
43
+ this.relay = await (0, shared_1.createSocks5Relay)(this.options.proxy, this.options.family);
44
+ this.socket = this.relay.socket;
45
+ const targetAddress = this.network.address.address;
46
+ const targetPort = this.network.address.port;
47
+ this.network.send = (data) => this.relay.send(data, targetAddress, targetPort);
48
+ this.relay.onMessage((data) => this.network.receive(data));
49
+ this.tickInterval = setInterval(() => this.network.tick(), 50);
50
+ }
51
+ initDirect() {
52
+ this.socket = (0, node_dgram_1.createSocket)(this.options.family);
53
+ this.network.send = (data) => this.socket.send(data, this.options.port, this.options.address);
54
+ return new Promise((resolve) => {
55
+ try {
56
+ this.socket.address();
57
+ this.socket.on("message", (buf) => this.network.receive(buf));
58
+ this.tickInterval = setInterval(() => this.network.tick(), 50);
59
+ resolve();
60
+ }
61
+ catch {
62
+ this.socket.bind();
63
+ this.socket.on("listening", () => {
64
+ this.socket.on("message", (buf) => this.network.receive(buf));
65
+ this.tickInterval = setInterval(() => this.network.tick(), 50);
66
+ resolve();
67
+ });
68
+ }
69
+ });
70
+ }
71
+ async ping(address, port) {
72
+ await this.ensureBound();
73
+ const targetAddress = address ?? this.network.address.address;
74
+ const targetPort = port ?? this.network.address.port;
75
+ return new Promise((resolve) => {
76
+ this.network.once("pong", resolve);
77
+ const ping = new shared_1.UnconnectedPing();
78
+ ping.guid = this.options.guid;
79
+ ping.timestamp = BigInt(Date.now());
80
+ const data = ping.serialize();
81
+ if (this.relay)
82
+ this.relay.send(data, targetAddress, targetPort);
83
+ else
84
+ this.socket.send(data, targetPort, targetAddress);
85
+ });
86
+ }
87
+ async connect() {
88
+ await this.ensureBound();
89
+ return new Promise((resolve, reject) => {
90
+ const cleanup = () => {
91
+ this.network.off("connect", onConnect);
92
+ this.network.off("error", onError);
93
+ clearTimeout(timer);
94
+ };
95
+ const onConnect = () => { cleanup(); resolve(); };
96
+ const onError = (err) => { cleanup(); reject(err); };
97
+ const timer = setTimeout(() => {
98
+ cleanup();
99
+ this.network.status = shared_1.Status.Disconnected;
100
+ reject(new Error("Connection timed out"));
101
+ }, this.options.timeout);
102
+ this.network.once("connect", onConnect);
103
+ this.network.once("error", onError);
104
+ const req = new shared_1.OpenConnectionRequestOne();
105
+ req.protocol = 11;
106
+ req.mtu = this.options.mtu;
107
+ this.network.sendOfflineWithRetry(req.serialize());
108
+ });
109
+ }
110
+ sendReliable(data, priority = shared_1.Priority.Medium) {
111
+ this.network.frameAndSend(data, priority);
112
+ }
113
+ close() {
114
+ if (this.tickInterval)
115
+ clearInterval(this.tickInterval);
116
+ if (this.relay)
117
+ this.relay.close();
118
+ else
119
+ this.socket.close();
120
+ }
121
+ }
122
+ exports.Client = Client;
@@ -0,0 +1,2 @@
1
+ export * from "./types";
2
+ export * from "./client";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./client"), exports);
@@ -0,0 +1,6 @@
1
+ export type ClientEvents = {
2
+ connect: [];
3
+ disconnect: [];
4
+ encapsulated: [buffer: Buffer];
5
+ error: [error: Error];
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import type { Socks5Options } from "../../shared";
2
+ export type ClientOptions = {
3
+ mtu: number;
4
+ timeout: number;
5
+ maxRetransmit: number;
6
+ address: string;
7
+ port: number;
8
+ family: "udp4" | "udp6";
9
+ guid: bigint;
10
+ proxy?: Socks5Options;
11
+ };
12
+ export declare const defaultClientOptions: Omit<ClientOptions, "guid">;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultClientOptions = void 0;
4
+ exports.defaultClientOptions = {
5
+ address: "127.0.0.1",
6
+ family: "udp4", // IPV4/udp4 is used by default in most of the things
7
+ maxRetransmit: 3,
8
+ mtu: 1028, // The most stable from what i can see
9
+ port: 19132,
10
+ timeout: 5000,
11
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./client-events";
2
+ export * from "./client-options";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./client-events"), exports);
18
+ __exportStar(require("./client-options"), exports);
@@ -0,0 +1,3 @@
1
+ export * from "./shared";
2
+ export * from "./client";
3
+ export * from "./server";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./shared"), exports);
18
+ __exportStar(require("./client"), exports);
19
+ __exportStar(require("./server"), exports);
@@ -0,0 +1,16 @@
1
+ import { Emitter } from "@baltica/utils";
2
+ import { NetworkSession, Address, Priority } from "../shared";
3
+ export type ConnectionEvents = {
4
+ connect: [];
5
+ disconnect: [];
6
+ encapsulated: [payload: Buffer];
7
+ };
8
+ export declare class Connection extends Emitter<ConnectionEvents> {
9
+ readonly network: NetworkSession;
10
+ readonly address: Address;
11
+ readonly guid: bigint;
12
+ constructor(network: NetworkSession);
13
+ send(data: Buffer, priority?: Priority): void;
14
+ disconnect(): void;
15
+ get identifier(): string;
16
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Connection = void 0;
4
+ const utils_1 = require("@baltica/utils");
5
+ const shared_1 = require("../shared");
6
+ class Connection extends utils_1.Emitter {
7
+ network;
8
+ address;
9
+ guid;
10
+ constructor(network) {
11
+ super();
12
+ this.network = network;
13
+ this.address = network.address;
14
+ this.guid = network.remoteGuid;
15
+ network.on("connect", () => this.emit("connect"));
16
+ network.on("disconnect", () => this.emit("disconnect"));
17
+ network.on("encapsulated", (payload) => this.emit("encapsulated", payload));
18
+ }
19
+ send(data, priority = shared_1.Priority.Medium) {
20
+ this.network.frameAndSend(data, priority);
21
+ }
22
+ disconnect() {
23
+ this.network.status = 3;
24
+ this.network.emit("disconnect");
25
+ }
26
+ get identifier() {
27
+ return `${this.address.address}:${this.address.port}`;
28
+ }
29
+ }
30
+ exports.Connection = Connection;
@@ -0,0 +1,3 @@
1
+ export * from "./types";
2
+ export * from "./connection";
3
+ export * from "./server";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./types"), exports);
18
+ __exportStar(require("./connection"), exports);
19
+ __exportStar(require("./server"), exports);
@@ -0,0 +1,19 @@
1
+ import { RemoteInfo } from "node:dgram";
2
+ import { Emitter } from "@baltica/utils";
3
+ import { ServerEvents, ServerOptions } from "./types";
4
+ import { Connection } from "./connection";
5
+ export declare class Server extends Emitter<ServerEvents> {
6
+ private socket;
7
+ private tickInterval?;
8
+ private connections;
9
+ options: ServerOptions;
10
+ constructor(options?: Partial<ServerOptions>);
11
+ private static key;
12
+ start(): Promise<void>;
13
+ stop(): void;
14
+ getConnection(rinfo: RemoteInfo): Connection | undefined;
15
+ getConnections(): Map<string, Connection>;
16
+ private tick;
17
+ private sendTo;
18
+ private handleMessage;
19
+ }
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Server = void 0;
4
+ const node_dgram_1 = require("node:dgram");
5
+ const node_crypto_1 = require("node:crypto");
6
+ const utils_1 = require("@baltica/utils");
7
+ const shared_1 = require("../shared");
8
+ const types_1 = require("./types");
9
+ const connection_1 = require("./connection");
10
+ const RAKNET_PROTOCOL = 11;
11
+ class Server extends utils_1.Emitter {
12
+ socket;
13
+ tickInterval;
14
+ connections = new Map();
15
+ options;
16
+ constructor(options = {}) {
17
+ super();
18
+ this.options = { ...types_1.defaultServerOptions, guid: (0, node_crypto_1.randomBytes)(8).readBigUInt64BE(), ...options };
19
+ this.socket = (0, node_dgram_1.createSocket)(this.options.family);
20
+ }
21
+ static key(rinfo) {
22
+ return `${rinfo.address}:${rinfo.port}`;
23
+ }
24
+ async start() {
25
+ return new Promise((resolve) => {
26
+ this.socket.bind(this.options.port, this.options.address);
27
+ this.socket.on("listening", () => {
28
+ this.socket.on("message", (buffer, rinfo) => this.handleMessage(buffer, rinfo));
29
+ this.tickInterval = setInterval(() => this.tick(), 50);
30
+ resolve();
31
+ });
32
+ });
33
+ }
34
+ stop() {
35
+ if (this.tickInterval)
36
+ clearInterval(this.tickInterval);
37
+ for (const conn of this.connections.values())
38
+ conn.disconnect();
39
+ this.connections.clear();
40
+ this.socket.close();
41
+ }
42
+ getConnection(rinfo) {
43
+ return this.connections.get(Server.key(rinfo));
44
+ }
45
+ getConnections() {
46
+ return this.connections;
47
+ }
48
+ tick() {
49
+ for (const conn of this.connections.values())
50
+ conn.network.tick();
51
+ }
52
+ sendTo(data, rinfo) {
53
+ this.socket.send(data, rinfo.port, rinfo.address);
54
+ }
55
+ handleMessage(buffer, rinfo) {
56
+ const key = Server.key(rinfo);
57
+ const conn = this.connections.get(key);
58
+ if (conn) {
59
+ conn.network.receive(buffer);
60
+ return;
61
+ }
62
+ const id = buffer[0];
63
+ const payload = buffer.subarray(1);
64
+ switch (id) {
65
+ case shared_1.Packet.UnconnectedPing:
66
+ case shared_1.Packet.UnconnectedPingOpenConnections: {
67
+ const ping = new shared_1.UnconnectedPing(payload).deserialize();
68
+ const pong = new shared_1.UnconnectedPong();
69
+ pong.timestamp = ping.timestamp;
70
+ pong.guid = this.options.guid;
71
+ pong.message = this.options.message;
72
+ this.sendTo(pong.serialize(), rinfo);
73
+ break;
74
+ }
75
+ case shared_1.Packet.OpenConnectionRequest1: {
76
+ const req = new shared_1.OpenConnectionRequestOne(payload).deserialize();
77
+ if (req.protocol !== RAKNET_PROTOCOL)
78
+ break;
79
+ const reply = new shared_1.OpenConnectionReplyOne();
80
+ reply.guid = this.options.guid;
81
+ reply.security = false;
82
+ reply.cookie = null;
83
+ reply.hasCookie = false;
84
+ reply.serverPublicKey = null;
85
+ reply.mtu = Math.min(this.options.mtu, req.mtu);
86
+ this.sendTo(reply.serialize(), rinfo);
87
+ break;
88
+ }
89
+ case shared_1.Packet.OpenConnectionRequest2: {
90
+ if (this.connections.size >= this.options.maxConnections)
91
+ break;
92
+ const req = new shared_1.OpenConnectionRequestTwo(payload).deserialize();
93
+ const mtu = Math.min(this.options.mtu, req.mtu);
94
+ const session = new shared_1.NetworkSession(mtu, false);
95
+ session.guid = this.options.guid;
96
+ session.remoteGuid = req.guid;
97
+ session.address = shared_1.Address.fromIdentifier(rinfo);
98
+ session.serverMessage = this.options.message;
99
+ session.send = (data) => this.sendTo(data, rinfo);
100
+ const reply = new shared_1.OpenConnectionReplyTwo();
101
+ reply.guid = this.options.guid;
102
+ reply.address = shared_1.Address.fromIdentifier(rinfo);
103
+ reply.mtu = mtu;
104
+ reply.encryptionEnabled = false;
105
+ session.send(reply.serialize());
106
+ const connection = new connection_1.Connection(session);
107
+ connection.on("connect", () => this.emit("connect", connection));
108
+ connection.on("disconnect", () => {
109
+ this.connections.delete(key);
110
+ this.emit("disconnect", connection);
111
+ });
112
+ connection.on("encapsulated", (payload) => this.emit("encapsulated", payload, connection));
113
+ this.connections.set(key, connection);
114
+ break;
115
+ }
116
+ }
117
+ }
118
+ }
119
+ exports.Server = Server;
@@ -0,0 +1,2 @@
1
+ export * from "./server-events";
2
+ export * from "./server-options";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./server-events"), exports);
18
+ __exportStar(require("./server-options"), exports);
@@ -0,0 +1,6 @@
1
+ import type { Connection } from "../connection";
2
+ export type ServerEvents = {
3
+ connect: [connection: Connection];
4
+ disconnect: [connection: Connection];
5
+ encapsulated: [payload: Buffer, connection: Connection];
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ export type ServerOptions = {
2
+ address: string;
3
+ port: number;
4
+ family: "udp4" | "udp6";
5
+ mtu: number;
6
+ maxConnections: number;
7
+ message: string;
8
+ guid: bigint;
9
+ };
10
+ export declare const defaultServerOptions: Omit<ServerOptions, "guid">;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultServerOptions = void 0;
4
+ exports.defaultServerOptions = {
5
+ address: "0.0.0.0",
6
+ port: 19132,
7
+ family: "udp4",
8
+ mtu: 1028,
9
+ maxConnections: 20,
10
+ message: "MCPE;Baltica;0;0.0.0;0;0;0;Baltica;Survival;1;19132;19133;0;",
11
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./network-session";
2
+ export * from "./socks5";
3
+ export * from "./proto";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./network-session"), exports);
18
+ __exportStar(require("./socks5"), exports);
19
+ __exportStar(require("./proto"), exports);
@@ -0,0 +1,63 @@
1
+ import { Frame, Priority, Status, Address } from "./proto";
2
+ import { Emitter } from "@baltica/utils";
3
+ export type NetworkEvents = {
4
+ encapsulated: [payload: Buffer];
5
+ pong: [message: string];
6
+ connect: [];
7
+ disconnect: [];
8
+ error: [error: Error];
9
+ };
10
+ export declare class NetworkSession extends Emitter<NetworkEvents> {
11
+ mtu: number;
12
+ client: boolean;
13
+ guid: bigint;
14
+ remoteGuid: bigint;
15
+ status: Status;
16
+ address: Address;
17
+ send: (data: Buffer) => void;
18
+ serverMessage?: string;
19
+ maxRetransmit: number;
20
+ retransmitInterval: number;
21
+ outputReliableIndex: number;
22
+ outputSplitIndex: number;
23
+ private outputSequence;
24
+ outputSequenceIndex: number[];
25
+ outputOrderIndex: number[];
26
+ outputFrames: Set<Frame>;
27
+ outputBackup: Map<number, Frame[]>;
28
+ receivedFrameSequences: Set<number>;
29
+ lostFrameSequences: Set<number>;
30
+ pendingAcks: Set<number>;
31
+ lastInputSequence: number;
32
+ fragmentsQueue: Map<number, {
33
+ frames: Map<number, Frame>;
34
+ timestamp: number;
35
+ }>;
36
+ inputHighestSequenceIndex: number[];
37
+ inputOrderIndex: number[];
38
+ private inputOrderingQueue;
39
+ private receivedReliableFrameIndices;
40
+ private highestReliableIndex;
41
+ private offlineRetry;
42
+ private outputBackupTimestamps;
43
+ constructor(mtu: number, client: boolean);
44
+ sendOfflineWithRetry(data: Buffer): void;
45
+ private clearOfflineRetry;
46
+ receive(buffer: Buffer): void;
47
+ private handleClientOffline;
48
+ private handleServerOffline;
49
+ private handleOnlinePacket;
50
+ tick(): void;
51
+ private onAck;
52
+ private onNack;
53
+ frameAndSend(data: Buffer, priority?: Priority): void;
54
+ sendFrame(frame: Frame, priority?: Priority): void;
55
+ private sendSplit;
56
+ private queueFrame;
57
+ private flush;
58
+ private onFrameSet;
59
+ private handleFrame;
60
+ private handleSplit;
61
+ private handleSequenced;
62
+ private handleOrdered;
63
+ }