@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.
- package/dist/client/client.d.ts +20 -0
- package/dist/client/client.js +122 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +18 -0
- package/dist/client/types/client-events.d.ts +6 -0
- package/dist/client/types/client-events.js +2 -0
- package/dist/client/types/client-options.d.ts +12 -0
- package/dist/client/types/client-options.js +11 -0
- package/dist/client/types/index.d.ts +2 -0
- package/dist/client/types/index.js +18 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/server/connection.d.ts +16 -0
- package/dist/server/connection.js +30 -0
- package/dist/server/index.d.ts +3 -0
- package/dist/server/index.js +19 -0
- package/dist/server/server.d.ts +19 -0
- package/dist/server/server.js +119 -0
- package/dist/server/types/index.d.ts +2 -0
- package/dist/server/types/index.js +18 -0
- package/dist/server/types/server-events.d.ts +6 -0
- package/dist/server/types/server-events.js +2 -0
- package/dist/server/types/server-options.d.ts +10 -0
- package/dist/server/types/server-options.js +11 -0
- package/dist/shared/index.d.ts +3 -0
- package/dist/shared/index.js +19 -0
- package/dist/shared/network-session.d.ts +63 -0
- package/dist/shared/network-session.js +480 -0
- package/dist/shared/proto/enums/index.d.ts +4 -0
- package/dist/shared/proto/enums/index.js +20 -0
- package/dist/shared/proto/enums/packet.d.ts +21 -0
- package/dist/shared/proto/enums/packet.js +25 -0
- package/dist/shared/proto/enums/priority.d.ts +6 -0
- package/dist/shared/proto/enums/priority.js +8 -0
- package/dist/shared/proto/enums/reliability.d.ts +10 -0
- package/dist/shared/proto/enums/reliability.js +14 -0
- package/dist/shared/proto/enums/status.d.ts +6 -0
- package/dist/shared/proto/enums/status.js +10 -0
- package/dist/shared/proto/index.d.ts +3 -0
- package/dist/shared/proto/index.js +19 -0
- package/dist/shared/proto/packets/data-packet.d.ts +7 -0
- package/dist/shared/proto/packets/data-packet.js +14 -0
- package/dist/shared/proto/packets/index.d.ts +3 -0
- package/dist/shared/proto/packets/index.js +19 -0
- package/dist/shared/proto/packets/offline/index.d.ts +6 -0
- package/dist/shared/proto/packets/offline/index.js +22 -0
- package/dist/shared/proto/packets/offline/open-connection-reply-one.d.ts +13 -0
- package/dist/shared/proto/packets/offline/open-connection-reply-one.js +46 -0
- package/dist/shared/proto/packets/offline/open-connection-reply-two.d.ts +12 -0
- package/dist/shared/proto/packets/offline/open-connection-reply-two.js +32 -0
- package/dist/shared/proto/packets/offline/open-connection-request-one.d.ts +9 -0
- package/dist/shared/proto/packets/offline/open-connection-request-one.js +26 -0
- package/dist/shared/proto/packets/offline/open-connection-request-two.d.ts +13 -0
- package/dist/shared/proto/packets/offline/open-connection-request-two.js +43 -0
- package/dist/shared/proto/packets/offline/unconnected-ping.d.ts +9 -0
- package/dist/shared/proto/packets/offline/unconnected-ping.js +25 -0
- package/dist/shared/proto/packets/offline/unconnected-pong.d.ts +10 -0
- package/dist/shared/proto/packets/offline/unconnected-pong.js +28 -0
- package/dist/shared/proto/packets/online/ack.d.ts +5 -0
- package/dist/shared/proto/packets/online/ack.js +9 -0
- package/dist/shared/proto/packets/online/acknowledgement.d.ts +6 -0
- package/dist/shared/proto/packets/online/acknowledgement.js +72 -0
- package/dist/shared/proto/packets/online/connected-ping.d.ts +8 -0
- package/dist/shared/proto/packets/online/connected-ping.js +19 -0
- package/dist/shared/proto/packets/online/connected-pong.d.ts +9 -0
- package/dist/shared/proto/packets/online/connected-pong.js +22 -0
- package/dist/shared/proto/packets/online/connection-request-accepted.d.ts +13 -0
- package/dist/shared/proto/packets/online/connection-request-accepted.js +41 -0
- package/dist/shared/proto/packets/online/connection-request.d.ts +10 -0
- package/dist/shared/proto/packets/online/connection-request.js +25 -0
- package/dist/shared/proto/packets/online/disconnect.d.ts +7 -0
- package/dist/shared/proto/packets/online/disconnect.js +16 -0
- package/dist/shared/proto/packets/online/frame-set.d.ts +10 -0
- package/dist/shared/proto/packets/online/frame-set.js +24 -0
- package/dist/shared/proto/packets/online/index.d.ts +10 -0
- package/dist/shared/proto/packets/online/index.js +26 -0
- package/dist/shared/proto/packets/online/nack.d.ts +5 -0
- package/dist/shared/proto/packets/online/nack.js +9 -0
- package/dist/shared/proto/packets/online/new-incoming-connection.d.ts +12 -0
- package/dist/shared/proto/packets/online/new-incoming-connection.js +33 -0
- package/dist/shared/proto/types/address.d.ts +11 -0
- package/dist/shared/proto/types/address.js +61 -0
- package/dist/shared/proto/types/data-type.d.ts +5 -0
- package/dist/shared/proto/types/data-type.js +9 -0
- package/dist/shared/proto/types/frame.d.ts +22 -0
- package/dist/shared/proto/types/frame.js +113 -0
- package/dist/shared/proto/types/index.d.ts +5 -0
- package/dist/shared/proto/types/index.js +21 -0
- package/dist/shared/proto/types/magic.d.ts +7 -0
- package/dist/shared/proto/types/magic.js +17 -0
- package/dist/shared/proto/types/mtu.d.ts +5 -0
- package/dist/shared/proto/types/mtu.js +12 -0
- package/dist/shared/socks5.d.ts +16 -0
- package/dist/shared/socks5.js +166 -0
- package/package.json +26 -0
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./acknowledgement";
|
|
2
|
+
export * from "./ack";
|
|
3
|
+
export * from "./nack";
|
|
4
|
+
export * from "./connected-ping";
|
|
5
|
+
export * from "./connected-pong";
|
|
6
|
+
export * from "./connection-request";
|
|
7
|
+
export * from "./connection-request-accepted";
|
|
8
|
+
export * from "./new-incoming-connection";
|
|
9
|
+
export * from "./disconnect";
|
|
10
|
+
export * from "./frame-set";
|
|
@@ -0,0 +1,26 @@
|
|
|
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("./acknowledgement"), exports);
|
|
18
|
+
__exportStar(require("./ack"), exports);
|
|
19
|
+
__exportStar(require("./nack"), exports);
|
|
20
|
+
__exportStar(require("./connected-ping"), exports);
|
|
21
|
+
__exportStar(require("./connected-pong"), exports);
|
|
22
|
+
__exportStar(require("./connection-request"), exports);
|
|
23
|
+
__exportStar(require("./connection-request-accepted"), exports);
|
|
24
|
+
__exportStar(require("./new-incoming-connection"), exports);
|
|
25
|
+
__exportStar(require("./disconnect"), exports);
|
|
26
|
+
__exportStar(require("./frame-set"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Nack = void 0;
|
|
4
|
+
const packet_1 = require("../../enums/packet");
|
|
5
|
+
const acknowledgement_1 = require("./acknowledgement");
|
|
6
|
+
class Nack extends acknowledgement_1.Acknowledgement {
|
|
7
|
+
static ID = packet_1.Packet.Nack;
|
|
8
|
+
}
|
|
9
|
+
exports.Nack = Nack;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Packet } from "../../enums/packet";
|
|
2
|
+
import { DataPacket } from "../data-packet";
|
|
3
|
+
import { Address } from "../../types/address";
|
|
4
|
+
export declare class NewIncomingConnection extends DataPacket {
|
|
5
|
+
static ID: Packet;
|
|
6
|
+
address: Address;
|
|
7
|
+
internalAddress: Address;
|
|
8
|
+
incomingTimestamp: bigint;
|
|
9
|
+
serverTimestamp: bigint;
|
|
10
|
+
serialize(): Buffer;
|
|
11
|
+
deserialize(): this;
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NewIncomingConnection = void 0;
|
|
4
|
+
const packet_1 = require("../../enums/packet");
|
|
5
|
+
const data_packet_1 = require("../data-packet");
|
|
6
|
+
const address_1 = require("../../types/address");
|
|
7
|
+
class NewIncomingConnection extends data_packet_1.DataPacket {
|
|
8
|
+
static ID = packet_1.Packet.NewIncomingConnection;
|
|
9
|
+
address;
|
|
10
|
+
internalAddress;
|
|
11
|
+
incomingTimestamp;
|
|
12
|
+
serverTimestamp;
|
|
13
|
+
serialize() {
|
|
14
|
+
this.writeUint8(NewIncomingConnection.ID);
|
|
15
|
+
address_1.Address.write(this, this.address);
|
|
16
|
+
for (let i = 0; i < 10; i++) {
|
|
17
|
+
address_1.Address.write(this, this.internalAddress);
|
|
18
|
+
}
|
|
19
|
+
this.writeInt64(this.incomingTimestamp);
|
|
20
|
+
this.writeInt64(this.serverTimestamp);
|
|
21
|
+
return this.getBuffer();
|
|
22
|
+
}
|
|
23
|
+
deserialize() {
|
|
24
|
+
this.address = address_1.Address.read(this);
|
|
25
|
+
for (let i = 0; i < 10; i++) {
|
|
26
|
+
this.internalAddress = address_1.Address.read(this);
|
|
27
|
+
}
|
|
28
|
+
this.incomingTimestamp = this.readInt64();
|
|
29
|
+
this.serverTimestamp = this.readInt64();
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.NewIncomingConnection = NewIncomingConnection;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { RemoteInfo } from "node:dgram";
|
|
2
|
+
import type { BinaryStream } from "@serenityjs/binarystream";
|
|
3
|
+
export declare class Address {
|
|
4
|
+
address: string;
|
|
5
|
+
port: number;
|
|
6
|
+
version: number;
|
|
7
|
+
constructor(address: string, port: number, version: number);
|
|
8
|
+
static fromIdentifier(identifier: RemoteInfo): Address;
|
|
9
|
+
static write(stream: BinaryStream, address: Address): void;
|
|
10
|
+
static read(stream: BinaryStream): Address;
|
|
11
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Address = void 0;
|
|
4
|
+
class Address {
|
|
5
|
+
address;
|
|
6
|
+
port;
|
|
7
|
+
version;
|
|
8
|
+
constructor(address, port, version) {
|
|
9
|
+
this.address = address;
|
|
10
|
+
this.port = port;
|
|
11
|
+
this.version = version;
|
|
12
|
+
}
|
|
13
|
+
static fromIdentifier(identifier) {
|
|
14
|
+
return new Address(identifier.address, identifier.port, identifier.family === "IPv4" ? 4 : 6);
|
|
15
|
+
}
|
|
16
|
+
static write(stream, address) {
|
|
17
|
+
stream.writeUint8(address.version);
|
|
18
|
+
if (address.version === 4) {
|
|
19
|
+
const addressBits = address.address.split(".", 4);
|
|
20
|
+
for (const bit of addressBits) {
|
|
21
|
+
stream.writeUint8(Number.parseInt(bit, 10) ^ 0xff);
|
|
22
|
+
}
|
|
23
|
+
stream.writeUint16(address.port);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
stream.writeUint16(23);
|
|
27
|
+
stream.writeUint16(address.port);
|
|
28
|
+
stream.writeUint32(0);
|
|
29
|
+
const addressParts = address.address.split(":");
|
|
30
|
+
for (const part of addressParts) {
|
|
31
|
+
const num = Number.parseInt(part, 16);
|
|
32
|
+
stream.writeUint16(num ^ 0xffff);
|
|
33
|
+
}
|
|
34
|
+
stream.writeUint32(0);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
static read(stream) {
|
|
38
|
+
const version = stream.readUint8();
|
|
39
|
+
if (version === 4) {
|
|
40
|
+
const bytes = stream.read(4);
|
|
41
|
+
const address = bytes.map((byte) => byte ^ 0xff).join(".");
|
|
42
|
+
const port = stream.readUint16();
|
|
43
|
+
return new Address(address, port, version);
|
|
44
|
+
}
|
|
45
|
+
if (version === 6) {
|
|
46
|
+
stream.offset += 2;
|
|
47
|
+
const port = stream.readUint16();
|
|
48
|
+
stream.offset += 4;
|
|
49
|
+
const addressParts = [];
|
|
50
|
+
for (let i = 0; i < 8; i++) {
|
|
51
|
+
const part = stream.readUint16() ^ 0xffff;
|
|
52
|
+
addressParts.push(part.toString(16).padStart(4, "0"));
|
|
53
|
+
}
|
|
54
|
+
const address = addressParts.join(":");
|
|
55
|
+
stream.offset += 4;
|
|
56
|
+
return new Address(address, port, version);
|
|
57
|
+
}
|
|
58
|
+
return new Address("0.0.0.0", 0, 0);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.Address = Address;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { BinaryStream } from "@serenityjs/binarystream";
|
|
2
|
+
import { Reliability } from "../enums/reliability";
|
|
3
|
+
export declare const SplitFlag = 16;
|
|
4
|
+
export declare class Frame {
|
|
5
|
+
reliability: Reliability;
|
|
6
|
+
reliableFrameIndex: number;
|
|
7
|
+
sequenceFrameIndex: number;
|
|
8
|
+
orderedFrameIndex: number;
|
|
9
|
+
orderChannel: number;
|
|
10
|
+
splitFrameIndex: number;
|
|
11
|
+
splitSize: number;
|
|
12
|
+
splitId: number;
|
|
13
|
+
payload: Buffer;
|
|
14
|
+
static write(stream: BinaryStream, frames: Array<Frame>): void;
|
|
15
|
+
static read(stream: BinaryStream): Array<Frame>;
|
|
16
|
+
isSplit(): boolean;
|
|
17
|
+
isReliable(): boolean;
|
|
18
|
+
isSequenced(): boolean;
|
|
19
|
+
isOrdered(): boolean;
|
|
20
|
+
isOrderExclusive(): boolean;
|
|
21
|
+
getByteLength(): number;
|
|
22
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Frame = exports.SplitFlag = void 0;
|
|
4
|
+
const binarystream_1 = require("@serenityjs/binarystream");
|
|
5
|
+
const reliability_1 = require("../enums/reliability");
|
|
6
|
+
exports.SplitFlag = 0x10;
|
|
7
|
+
class Frame {
|
|
8
|
+
reliability;
|
|
9
|
+
reliableFrameIndex;
|
|
10
|
+
sequenceFrameIndex;
|
|
11
|
+
orderedFrameIndex;
|
|
12
|
+
orderChannel;
|
|
13
|
+
splitFrameIndex;
|
|
14
|
+
splitSize;
|
|
15
|
+
splitId;
|
|
16
|
+
payload;
|
|
17
|
+
static write(stream, frames) {
|
|
18
|
+
for (const frame of frames) {
|
|
19
|
+
const isReliable = frame.isReliable();
|
|
20
|
+
const isSequenced = frame.isSequenced();
|
|
21
|
+
const isOrdered = frame.isOrdered();
|
|
22
|
+
const isSplit = frame.isSplit();
|
|
23
|
+
const header = (frame.reliability << 5) | (isSplit ? exports.SplitFlag : 0);
|
|
24
|
+
stream.writeUint8(header);
|
|
25
|
+
stream.writeUint16(frame.payload.byteLength << 3);
|
|
26
|
+
if (isReliable)
|
|
27
|
+
stream.writeUint24(frame.reliableFrameIndex, binarystream_1.Endianness.Little);
|
|
28
|
+
if (isSequenced)
|
|
29
|
+
stream.writeUint24(frame.sequenceFrameIndex, binarystream_1.Endianness.Little);
|
|
30
|
+
if (isOrdered) {
|
|
31
|
+
stream.writeUint24(frame.orderedFrameIndex, binarystream_1.Endianness.Little);
|
|
32
|
+
stream.writeUint8(frame.orderChannel);
|
|
33
|
+
}
|
|
34
|
+
if (isSplit) {
|
|
35
|
+
stream.writeUint32(frame.splitSize);
|
|
36
|
+
stream.writeUint16(frame.splitId);
|
|
37
|
+
stream.writeUint32(frame.splitFrameIndex);
|
|
38
|
+
}
|
|
39
|
+
stream.write(frame.payload);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
static read(stream) {
|
|
43
|
+
const frames = [];
|
|
44
|
+
while (!stream.feof()) {
|
|
45
|
+
const frame = new Frame();
|
|
46
|
+
const header = stream.readUint8();
|
|
47
|
+
const reliability = (header & 0xe0) >> 5;
|
|
48
|
+
const split = (header & exports.SplitFlag) !== 0;
|
|
49
|
+
const length = Math.ceil(stream.readUint16() / 8);
|
|
50
|
+
frame.reliability = reliability;
|
|
51
|
+
const isReliable = frame.isReliable();
|
|
52
|
+
const isSequenced = frame.isSequenced();
|
|
53
|
+
const isOrdered = frame.isOrdered();
|
|
54
|
+
if (isReliable)
|
|
55
|
+
frame.reliableFrameIndex = stream.readUint24(binarystream_1.Endianness.Little);
|
|
56
|
+
if (isSequenced)
|
|
57
|
+
frame.sequenceFrameIndex = stream.readUint24(binarystream_1.Endianness.Little);
|
|
58
|
+
if (isOrdered) {
|
|
59
|
+
frame.orderedFrameIndex = stream.readUint24(binarystream_1.Endianness.Little);
|
|
60
|
+
frame.orderChannel = stream.readUint8();
|
|
61
|
+
}
|
|
62
|
+
if (split) {
|
|
63
|
+
frame.splitSize = stream.readUint32();
|
|
64
|
+
frame.splitId = stream.readUint16();
|
|
65
|
+
frame.splitFrameIndex = stream.readUint32();
|
|
66
|
+
}
|
|
67
|
+
frame.payload = stream.read(length);
|
|
68
|
+
frames.push(frame);
|
|
69
|
+
}
|
|
70
|
+
return frames;
|
|
71
|
+
}
|
|
72
|
+
isSplit() {
|
|
73
|
+
return this.splitSize > 0;
|
|
74
|
+
}
|
|
75
|
+
isReliable() {
|
|
76
|
+
return [
|
|
77
|
+
reliability_1.Reliability.Reliable,
|
|
78
|
+
reliability_1.Reliability.ReliableOrdered,
|
|
79
|
+
reliability_1.Reliability.ReliableSequenced,
|
|
80
|
+
reliability_1.Reliability.ReliableWithAckReceipt,
|
|
81
|
+
reliability_1.Reliability.ReliableOrderedWithAckReceipt,
|
|
82
|
+
].includes(this.reliability);
|
|
83
|
+
}
|
|
84
|
+
isSequenced() {
|
|
85
|
+
return [
|
|
86
|
+
reliability_1.Reliability.ReliableSequenced,
|
|
87
|
+
reliability_1.Reliability.UnreliableSequenced,
|
|
88
|
+
].includes(this.reliability);
|
|
89
|
+
}
|
|
90
|
+
isOrdered() {
|
|
91
|
+
return [
|
|
92
|
+
reliability_1.Reliability.UnreliableSequenced,
|
|
93
|
+
reliability_1.Reliability.ReliableOrdered,
|
|
94
|
+
reliability_1.Reliability.ReliableSequenced,
|
|
95
|
+
reliability_1.Reliability.ReliableOrderedWithAckReceipt,
|
|
96
|
+
].includes(this.reliability);
|
|
97
|
+
}
|
|
98
|
+
isOrderExclusive() {
|
|
99
|
+
return [
|
|
100
|
+
reliability_1.Reliability.ReliableOrdered,
|
|
101
|
+
reliability_1.Reliability.ReliableOrderedWithAckReceipt,
|
|
102
|
+
].includes(this.reliability);
|
|
103
|
+
}
|
|
104
|
+
getByteLength() {
|
|
105
|
+
return (3 +
|
|
106
|
+
this.payload.byteLength +
|
|
107
|
+
(this.isReliable() ? 3 : 0) +
|
|
108
|
+
(this.isSequenced() ? 3 : 0) +
|
|
109
|
+
(this.isOrdered() ? 4 : 0) +
|
|
110
|
+
(this.isSplit() ? 10 : 0));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.Frame = Frame;
|
|
@@ -0,0 +1,21 @@
|
|
|
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("./data-type"), exports);
|
|
18
|
+
__exportStar(require("./magic"), exports);
|
|
19
|
+
__exportStar(require("./mtu"), exports);
|
|
20
|
+
__exportStar(require("./address"), exports);
|
|
21
|
+
__exportStar(require("./frame"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Magic = void 0;
|
|
4
|
+
const data_type_1 = require("./data-type");
|
|
5
|
+
const MAGIC = Buffer.from("00ffff00fefefefefdfdfdfd12345678", "hex");
|
|
6
|
+
class Magic extends data_type_1.DataType {
|
|
7
|
+
buffer = MAGIC;
|
|
8
|
+
write(stream) {
|
|
9
|
+
stream.write(MAGIC);
|
|
10
|
+
}
|
|
11
|
+
static read(stream) {
|
|
12
|
+
const magic = new Magic();
|
|
13
|
+
magic.buffer = stream.read(16);
|
|
14
|
+
return magic;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.Magic = Magic;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MTU = void 0;
|
|
4
|
+
class MTU {
|
|
5
|
+
static write(stream, mtu) {
|
|
6
|
+
stream.write(Buffer.alloc(mtu - stream.getBuffer().length));
|
|
7
|
+
}
|
|
8
|
+
static read(stream) {
|
|
9
|
+
return stream.buffer.byteLength;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.MTU = MTU;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Socket as TcpSocket } from "node:net";
|
|
2
|
+
import { Socket as UdpSocket } from "node:dgram";
|
|
3
|
+
export type Socks5Options = {
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
username?: string;
|
|
7
|
+
password?: string;
|
|
8
|
+
};
|
|
9
|
+
export type Socks5Relay = {
|
|
10
|
+
socket: UdpSocket;
|
|
11
|
+
tcp: TcpSocket;
|
|
12
|
+
send: (data: Buffer, targetAddress: string, targetPort: number) => void;
|
|
13
|
+
close: () => void;
|
|
14
|
+
onMessage: (handler: (data: Buffer, senderAddress: string, senderPort: number) => void) => void;
|
|
15
|
+
};
|
|
16
|
+
export declare function createSocks5Relay(proxy: Socks5Options, family?: "udp4" | "udp6"): Promise<Socks5Relay>;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSocks5Relay = createSocks5Relay;
|
|
4
|
+
const node_net_1 = require("node:net");
|
|
5
|
+
const node_dgram_1 = require("node:dgram");
|
|
6
|
+
async function createSocks5Relay(proxy, family = "udp4") {
|
|
7
|
+
const tcp = await tcpConnect(proxy.host, proxy.port);
|
|
8
|
+
await authenticate(tcp, proxy.username, proxy.password);
|
|
9
|
+
const relay = await udpAssociate(tcp);
|
|
10
|
+
const udp = (0, node_dgram_1.createSocket)(family);
|
|
11
|
+
await new Promise((resolve) => {
|
|
12
|
+
udp.bind(0, () => resolve());
|
|
13
|
+
});
|
|
14
|
+
let messageHandler = null;
|
|
15
|
+
udp.on("message", (msg) => {
|
|
16
|
+
if (msg.length < 10)
|
|
17
|
+
return;
|
|
18
|
+
if (msg[0] !== 0 || msg[1] !== 0 || msg[2] !== 0)
|
|
19
|
+
return;
|
|
20
|
+
const atyp = msg[3];
|
|
21
|
+
let offset;
|
|
22
|
+
let addr;
|
|
23
|
+
if (atyp === 0x01) {
|
|
24
|
+
addr = `${msg[4]}.${msg[5]}.${msg[6]}.${msg[7]}`;
|
|
25
|
+
offset = 8;
|
|
26
|
+
}
|
|
27
|
+
else if (atyp === 0x03) {
|
|
28
|
+
const len = msg[4];
|
|
29
|
+
addr = msg.subarray(5, 5 + len).toString("ascii");
|
|
30
|
+
offset = 5 + len;
|
|
31
|
+
}
|
|
32
|
+
else if (atyp === 0x04) {
|
|
33
|
+
const parts = [];
|
|
34
|
+
for (let i = 0; i < 8; i++)
|
|
35
|
+
parts.push(msg.readUInt16BE(4 + i * 2).toString(16));
|
|
36
|
+
addr = parts.join(":");
|
|
37
|
+
offset = 20;
|
|
38
|
+
}
|
|
39
|
+
else
|
|
40
|
+
return;
|
|
41
|
+
const port = msg.readUInt16BE(offset);
|
|
42
|
+
const payload = msg.subarray(offset + 2);
|
|
43
|
+
if (messageHandler)
|
|
44
|
+
messageHandler(payload, addr, port);
|
|
45
|
+
});
|
|
46
|
+
tcp.on("close", () => udp.close());
|
|
47
|
+
return {
|
|
48
|
+
socket: udp,
|
|
49
|
+
tcp,
|
|
50
|
+
send(data, targetAddress, targetPort) {
|
|
51
|
+
const header = buildUdpHeader(targetAddress, targetPort);
|
|
52
|
+
const packet = Buffer.concat([header, data]);
|
|
53
|
+
udp.send(packet, relay.port, relay.address);
|
|
54
|
+
},
|
|
55
|
+
close() {
|
|
56
|
+
udp.close();
|
|
57
|
+
tcp.destroy();
|
|
58
|
+
},
|
|
59
|
+
onMessage(handler) {
|
|
60
|
+
messageHandler = handler;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function tcpConnect(host, port) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const socket = (0, node_net_1.connect)(port, host, () => resolve(socket));
|
|
67
|
+
socket.once("error", reject);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function authenticate(tcp, username, password) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
const methods = username && password ? Buffer.from([0x05, 0x02, 0x00, 0x02]) : Buffer.from([0x05, 0x01, 0x00]);
|
|
73
|
+
tcp.write(methods);
|
|
74
|
+
tcp.once("data", (raw) => {
|
|
75
|
+
const response = Buffer.from(raw);
|
|
76
|
+
if (response[0] !== 0x05)
|
|
77
|
+
return reject(new Error("Invalid SOCKS5 response"));
|
|
78
|
+
const method = response[1];
|
|
79
|
+
if (method === 0x00)
|
|
80
|
+
return resolve();
|
|
81
|
+
if (method === 0x02 && username && password) {
|
|
82
|
+
const uBuf = Buffer.from(username, "utf8");
|
|
83
|
+
const pBuf = Buffer.from(password, "utf8");
|
|
84
|
+
const auth = Buffer.alloc(3 + uBuf.length + pBuf.length);
|
|
85
|
+
auth[0] = 0x01;
|
|
86
|
+
auth[1] = uBuf.length;
|
|
87
|
+
uBuf.copy(auth, 2);
|
|
88
|
+
auth[2 + uBuf.length] = pBuf.length;
|
|
89
|
+
pBuf.copy(auth, 3 + uBuf.length);
|
|
90
|
+
tcp.write(auth);
|
|
91
|
+
tcp.once("data", (authRaw) => {
|
|
92
|
+
const authResp = Buffer.from(authRaw);
|
|
93
|
+
if (authResp[1] === 0x00)
|
|
94
|
+
resolve();
|
|
95
|
+
else
|
|
96
|
+
reject(new Error("SOCKS5 authentication failed"));
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
reject(new Error(`SOCKS5 unsupported auth method: ${method}`));
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function udpAssociate(tcp) {
|
|
106
|
+
return new Promise((resolve, reject) => {
|
|
107
|
+
const request = Buffer.from([
|
|
108
|
+
0x05, 0x03, 0x00,
|
|
109
|
+
0x01, 0x00, 0x00, 0x00, 0x00,
|
|
110
|
+
0x00, 0x00,
|
|
111
|
+
]);
|
|
112
|
+
tcp.write(request);
|
|
113
|
+
tcp.once("data", (raw) => {
|
|
114
|
+
const response = Buffer.from(raw);
|
|
115
|
+
if (response[0] !== 0x05 || response[1] !== 0x00) {
|
|
116
|
+
return reject(new Error(`SOCKS5 UDP ASSOCIATE failed: ${response[1]}`));
|
|
117
|
+
}
|
|
118
|
+
const atyp = response[3];
|
|
119
|
+
let address;
|
|
120
|
+
let portOffset;
|
|
121
|
+
if (atyp === 0x01) {
|
|
122
|
+
address = `${response[4]}.${response[5]}.${response[6]}.${response[7]}`;
|
|
123
|
+
portOffset = 8;
|
|
124
|
+
}
|
|
125
|
+
else if (atyp === 0x03) {
|
|
126
|
+
const len = response[4];
|
|
127
|
+
address = response.subarray(5, 5 + len).toString("ascii");
|
|
128
|
+
portOffset = 5 + len;
|
|
129
|
+
}
|
|
130
|
+
else if (atyp === 0x04) {
|
|
131
|
+
const parts = [];
|
|
132
|
+
for (let i = 0; i < 8; i++)
|
|
133
|
+
parts.push(response.readUInt16BE(4 + i * 2).toString(16));
|
|
134
|
+
address = parts.join(":");
|
|
135
|
+
portOffset = 20;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return reject(new Error(`SOCKS5 unsupported address type: ${atyp}`));
|
|
139
|
+
}
|
|
140
|
+
const port = response.readUInt16BE(portOffset);
|
|
141
|
+
if (address === "0.0.0.0" || address === "::") {
|
|
142
|
+
address = tcp.remoteAddress;
|
|
143
|
+
}
|
|
144
|
+
resolve({ address, port });
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function buildUdpHeader(address, port) {
|
|
149
|
+
const isIPv4 = address.includes(".");
|
|
150
|
+
if (isIPv4) {
|
|
151
|
+
const header = Buffer.alloc(10);
|
|
152
|
+
header[3] = 0x01;
|
|
153
|
+
const parts = address.split(".");
|
|
154
|
+
for (let i = 0; i < 4; i++)
|
|
155
|
+
header[4 + i] = parseInt(parts[i], 10);
|
|
156
|
+
header.writeUInt16BE(port, 8);
|
|
157
|
+
return header;
|
|
158
|
+
}
|
|
159
|
+
const header = Buffer.alloc(22);
|
|
160
|
+
header[3] = 0x04;
|
|
161
|
+
const parts = address.split(":");
|
|
162
|
+
for (let i = 0; i < 8; i++)
|
|
163
|
+
header.writeUInt16BE(parseInt(parts[i], 16), 4 + i * 2);
|
|
164
|
+
header.writeUInt16BE(port, 20);
|
|
165
|
+
return header;
|
|
166
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@baltica/raknet",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "RakNet implementation for Baltica",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"watch": "tsc --watch",
|
|
10
|
+
"clean": "rimraf dist"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@baltica/utils": "*",
|
|
20
|
+
"@serenityjs/binarystream": "^3.1.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/bun": "^1.3.9",
|
|
24
|
+
"@types/node": "^25.3.0"
|
|
25
|
+
}
|
|
26
|
+
}
|