mirror of
https://github.com/DinheroDevelopmentGroup/modular-minecraft-proxy.git
synced 2024-11-27 09:35:49 -05:00
More appropiate terminology
client-side, server-side (highly ambiguous and context-dependant) to upstream and downstream (industry-standard)
This commit is contained in:
parent
cc9c274325
commit
c0d1876548
3 changed files with 48 additions and 73 deletions
|
@ -5,8 +5,8 @@ import { type RawPacket } from '../../util/packet.js';
|
||||||
import { type Message } from './shared.js';
|
import { type Message } from './shared.js';
|
||||||
|
|
||||||
export default async function (instance: Instance): Promise<void> {
|
export default async function (instance: Instance): Promise<void> {
|
||||||
const clientQueue: RawPacket[] = [];
|
const downstreamQueue: RawPacket[] = [];
|
||||||
const serverQueue: RawPacket[] = [];
|
const upstreamQueue: RawPacket[] = [];
|
||||||
|
|
||||||
const channel = instance.createChannel<Message>('proxy');
|
const channel = instance.createChannel<Message>('proxy');
|
||||||
|
|
||||||
|
@ -39,10 +39,8 @@ export default async function (instance: Instance): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('packet', (data, meta) => {
|
client.on('packet', (data, meta) => {
|
||||||
// if (meta.state !== states.PLAY) return;
|
|
||||||
|
|
||||||
channel.write({
|
channel.write({
|
||||||
direction: 'downstream',
|
direction: 'upstream',
|
||||||
packet: {
|
packet: {
|
||||||
...meta,
|
...meta,
|
||||||
data,
|
data,
|
||||||
|
@ -51,10 +49,8 @@ export default async function (instance: Instance): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on('packet', (data, meta) => {
|
server.on('packet', (data, meta) => {
|
||||||
// if (meta.state !== states.PLAY) return;
|
|
||||||
|
|
||||||
channel.write({
|
channel.write({
|
||||||
direction: 'upstream',
|
direction: 'downstream',
|
||||||
packet: {
|
packet: {
|
||||||
...meta,
|
...meta,
|
||||||
data,
|
data,
|
||||||
|
@ -67,40 +63,36 @@ export default async function (instance: Instance): Promise<void> {
|
||||||
client.on('state', (state) => {
|
client.on('state', (state) => {
|
||||||
if (state !== states.PLAY) return;
|
if (state !== states.PLAY) return;
|
||||||
|
|
||||||
const queue = clientQueue;
|
for (const packet of downstreamQueue) writeDownstreamPacket(packet);
|
||||||
|
|
||||||
for (const packet of queue) client.write(packet.name, packet.data);
|
downstreamQueue.length = 0;
|
||||||
|
|
||||||
queue.length = 0;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.on('state', (state) => {
|
server.on('state', (state) => {
|
||||||
if (state !== states.PLAY) return;
|
if (state !== states.PLAY) return;
|
||||||
|
|
||||||
const queue = serverQueue;
|
for (const packet of upstreamQueue) writeUpstreamPacket(packet);
|
||||||
|
|
||||||
for (const packet of queue) server.write(packet.name, packet.data);
|
upstreamQueue.length = 0;
|
||||||
|
|
||||||
queue.length = 0;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
channel.subscribe(({ direction: side, packet }) => {
|
channel.subscribe(({ direction: direction, packet }) => {
|
||||||
switch (side) {
|
switch (direction) {
|
||||||
case 'downstream':
|
case 'downstream':
|
||||||
writeClientPacket(packet);
|
writeDownstreamPacket(packet);
|
||||||
break;
|
break;
|
||||||
case 'upstream':
|
case 'upstream':
|
||||||
writeServerPacket(packet);
|
writeUpstreamPacket(packet);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(`Invalid side: ${side as any}`);
|
throw new Error(`Unknown direction: ${direction as any}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function writeClientPacket(packet: RawPacket): void {
|
function writeDownstreamPacket(packet: RawPacket): void {
|
||||||
// wait until play state
|
// queue until play state
|
||||||
if (client.state !== states.PLAY) {
|
if (client.state !== states.PLAY) {
|
||||||
clientQueue.push(packet);
|
downstreamQueue.push(packet);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -110,10 +102,10 @@ export default async function (instance: Instance): Promise<void> {
|
||||||
client.write(packet.name, packet.data);
|
client.write(packet.name, packet.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeServerPacket(packet: RawPacket): void {
|
function writeUpstreamPacket(packet: RawPacket): void {
|
||||||
// wait until play state
|
// queue until play state
|
||||||
if (server.state !== states.PLAY) {
|
if (server.state !== states.PLAY) {
|
||||||
serverQueue.push(packet);
|
upstreamQueue.push(packet);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
import type { States } from 'minecraft-protocol';
|
import type { EventMap } from '../../util/events.js';
|
||||||
import { states } from 'minecraft-protocol';
|
|
||||||
|
|
||||||
import { PublicEventHandler } from '../../util/events.js';
|
import { PublicEventHandler } from '../../util/events.js';
|
||||||
import { Packet, type RawPacket } from '../../util/packet.js';
|
import { Packet, type RawPacket } from '../../util/packet.js';
|
||||||
import { type AsyncVoid } from '../../util/types.js';
|
import { type AsyncVoid } from '../../util/types.js';
|
||||||
|
@ -19,51 +17,39 @@ function write(direction: Direction, packet: RawPacket): void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const proxy = {
|
class ConnectionSide<
|
||||||
client: new PublicEventHandler<PacketEventMap>(),
|
TEventMap extends EventMap<TEventMap>,
|
||||||
server: new PublicEventHandler<PacketEventMap>(),
|
> extends PublicEventHandler<TEventMap> {
|
||||||
|
constructor(protected readonly direction: Direction) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
writeDownstream(
|
public write(packet: Packet): void {
|
||||||
name: string,
|
write(this.direction, packet);
|
||||||
data: unknown,
|
}
|
||||||
state: States = states.PLAY,
|
}
|
||||||
): void {
|
|
||||||
write('downstream', { name, data, state });
|
// ngl I feel like I need rx soo bad
|
||||||
},
|
// then I could just do
|
||||||
writeUpstream(
|
// channel.filter(({ direction }) => direction === 'downstream')
|
||||||
name: string,
|
// and it would just work
|
||||||
data: unknown,
|
|
||||||
state: States = states.PLAY,
|
export const proxy = {
|
||||||
): void {
|
upstream: new ConnectionSide<PacketEventMap>('upstream'),
|
||||||
write('upstream', { name, data, state });
|
downstream: new ConnectionSide<PacketEventMap>('downstream'),
|
||||||
},
|
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
channel.subscribe(({ direction, packet: raw }: Message) => {
|
channel.subscribe(({ direction, packet: raw }: Message) => {
|
||||||
void (async () => {
|
void (async () => {
|
||||||
const sourceHandler = {
|
const connection = proxy[direction];
|
||||||
downstream: proxy.server,
|
|
||||||
upstream: proxy.client,
|
|
||||||
}[direction];
|
|
||||||
|
|
||||||
const packet = new Packet(raw.name, raw.data);
|
const packet = new Packet(raw.name, raw.data, raw.state);
|
||||||
|
|
||||||
await sourceHandler.emit('packet', packet);
|
await connection.emit('packet', packet);
|
||||||
await sourceHandler.emit(packet.name, packet);
|
await connection.emit(packet.name, packet);
|
||||||
|
|
||||||
if (packet.canceled) return;
|
if (packet.canceled) return;
|
||||||
|
|
||||||
switch (direction) {
|
|
||||||
case 'downstream':
|
|
||||||
direction = 'upstream';
|
|
||||||
break;
|
|
||||||
case 'upstream':
|
|
||||||
direction = 'downstream';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new Error(`Invalid direction: ${direction as any}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Forward packet
|
// Forward packet
|
||||||
channel.write({
|
channel.write({
|
||||||
direction,
|
direction,
|
||||||
|
|
|
@ -6,14 +6,11 @@ export interface RawPacket extends PacketMeta {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Packet<Data = unknown> {
|
export class Packet<Data = unknown> {
|
||||||
public name;
|
|
||||||
public state: States = states.PLAY;
|
|
||||||
public data;
|
|
||||||
|
|
||||||
public canceled: boolean = false;
|
public canceled: boolean = false;
|
||||||
|
|
||||||
constructor(name: string, data: Data) {
|
constructor(
|
||||||
this.name = name;
|
public name: string,
|
||||||
this.data = data;
|
public data: Data,
|
||||||
}
|
public state: States = states.PLAY,
|
||||||
|
) {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue