let's to tcp as well

This commit is contained in:
Holly Stubbs 2024-04-28 12:38:49 +01:00
parent 32a622e2a6
commit 720ffdb8a1
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
4 changed files with 29 additions and 22 deletions

View file

@ -1,5 +1,6 @@
{
"remoteAddress": "ws://localhost:18472",
"remoteHost": "localhost",
"remotePort": 18472,
"localPort": 25566,
"authKey": "default"
}

View file

@ -1,7 +1,7 @@
import { AuthState } from "./enum/AuthState";
import { Endian, createReader, createWriter } from "bufferstuff";
import { existsSync, readFileSync } from "fs";
import { createServer } from "net";
import { createServer, Socket } from "net";
import { Packet } from "./enum/Packet";
import { WebSocket } from "ws";
import IClientConfig from "./interface/IClientConfig";
@ -27,10 +27,10 @@ export default class Client {
txBytes = rxBytes = 0;
}, 1000);
const client = new WebSocket(config.remoteAddress);
const client = new Socket();
client.on("open", () => {
// Send off auth as soon as we connect
client.send(createWriter(Endian.LE, 2 + config.authKey.length).writeUByte(Packet.Auth).writeShortString(config.authKey).toBuffer());
client.write(createWriter(Endian.LE, 2 + config.authKey.length).writeUByte(Packet.Auth).writeShortString(config.authKey).toBuffer());
});
client.on("message", (data) => {
@ -42,7 +42,7 @@ export default class Client {
switch (packetId) {
case Packet.KeepAlive:
client.send(Constants.KEEPALIVE_PACKET);
client.write(Constants.KEEPALIVE_PACKET);
break;
case Packet.Auth:
@ -53,7 +53,7 @@ export default class Client {
authed = packetData.readUByte() === AuthState.Good;
if (authed && queuedMessages.length > 0) {
for (const message of queuedMessages) {
client.send(message);
client.write(message);
}
queuedMessages.length = 0;
}
@ -79,19 +79,24 @@ export default class Client {
const bufferData = createWriter(Endian.LE, 5).writeUByte(Packet.Data).writeUInt(chunk.length).writeBuffer(chunk).toBuffer();
txBytes += chunk.length;
if (authed) {
client.send(bufferData);
client.write(bufferData);
} else {
queuedMessages.push(bufferData);
}
});
function serverCloseOrError() {
client.close();
client.end();
clearInterval(txrxInterval);
console.log(`[LOCAL CLIENT ${thisClientId}] Disconnected`);
}
socket.on("close", serverCloseOrError);
socket.on("error", serverCloseOrError);
client.connect({
host: config.remoteHost,
port: config.remotePort
});
});
server.listen(config.localPort, () => console.log(`Local server listening at ${config.localPort}`));

View file

@ -1,9 +1,9 @@
import { AuthState } from "./enum/AuthState";
import { Endian, createReader, createWriter } from "bufferstuff";
import { existsSync, readFileSync } from "fs";
import { Socket } from "net";
import { Socket, createServer } from "net";
import { Packet } from "./enum/Packet";
import { WebSocketServer } from "ws";
//import { WebSocketServer } from "ws";
import IServerConfig from "./interface/IServerConfig";
import Constants from "./Constants";
@ -14,9 +14,7 @@ export default class Server {
process.exit(1);
}
const config:IServerConfig = JSON.parse(readFileSync("./config/server-config.json").toString());
const server = new WebSocketServer({ port: config.port }, () => console.log(`Server started at ${config.port}`));
server.on("connection", (socket) => {
const server = createServer((socket) => {
console.log("Connection");
let queuedMessages = new Array<Buffer>();
let connectedToServer = false;
@ -24,7 +22,7 @@ export default class Server {
let authed = false;
let client:Socket | null = null;
const clientKeepAlive = setInterval(() => {
socket.send(Constants.KEEPALIVE_PACKET);
socket.end(Constants.KEEPALIVE_PACKET);
}, 5000);
socket.on("message", (data, isBinary) => {
@ -43,11 +41,11 @@ export default class Server {
case Packet.Auth:
try {
if (packetData.readShortString() !== config.authKey) {
socket.send(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Bad).toBuffer());
socket.close();
socket.write(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Bad).toBuffer());
socket.end();
return;
}
socket.send(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Good).toBuffer());
socket.write(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Good).toBuffer());
authed = true;
client = new Socket();
@ -64,7 +62,7 @@ export default class Server {
});
client.on("data", (chunk) => {
socket.send(createWriter(Endian.LE, 5)
socket.write(createWriter(Endian.LE, 5)
.writeUByte(Packet.Data)
.writeUInt(chunk.length)
.writeBuffer(chunk)
@ -72,7 +70,7 @@ export default class Server {
});
function clientCloseOrError() {
socket.close();
socket.end();
}
client.on("close", clientCloseOrError);
client.on("error", clientCloseOrError);
@ -85,7 +83,7 @@ export default class Server {
} catch (e) {
client?.end();
client = null;
socket.close();
socket.end();
}
break;
case Packet.Data:
@ -111,5 +109,7 @@ export default class Server {
socket.on("close", closeOrError);
socket.on("error", closeOrError);
});
server.listen(config.port, () => console.log(`Server started at ${config.port}`));
}
}

View file

@ -1,5 +1,6 @@
export default interface IClientConfig {
remoteAddress: string,
remoteHost: string,
remotePort: number,
localPort: number,
authKey: string
}