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, "localPort": 25566,
"authKey": "default" "authKey": "default"
} }

View file

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

View file

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