Compare commits
2 commits
Author | SHA1 | Date | |
---|---|---|---|
7e646efd28 | |||
720ffdb8a1 |
4 changed files with 32 additions and 29 deletions
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"remoteAddress": "ws://localhost:18472",
|
||||
"remoteHost": "localhost",
|
||||
"remotePort": 18472,
|
||||
"localPort": 25566,
|
||||
"authKey": "default"
|
||||
}
|
|
@ -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,13 +27,13 @@ export default class Client {
|
|||
txBytes = rxBytes = 0;
|
||||
}, 1000);
|
||||
|
||||
const client = new WebSocket(config.remoteAddress);
|
||||
client.on("open", () => {
|
||||
const client = new Socket();
|
||||
client.on("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("data", (data) => {
|
||||
// @ts-ignore
|
||||
const packetData = createReader(Endian.LE, data);
|
||||
const packetId = packetData.readUByte();
|
||||
|
@ -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}`));
|
||||
|
|
|
@ -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,14 +22,10 @@ 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) => {
|
||||
if (!isBinary) {
|
||||
return;
|
||||
}
|
||||
|
||||
socket.on("data", (data) => {
|
||||
// NOTE: The types declarations for ws are really messed up >:(
|
||||
// @ts-ignore
|
||||
const packetData = createReader(Endian.LE, data);
|
||||
|
@ -43,11 +37,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 +58,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 +66,7 @@ export default class Server {
|
|||
});
|
||||
|
||||
function clientCloseOrError() {
|
||||
socket.close();
|
||||
socket.end();
|
||||
}
|
||||
client.on("close", clientCloseOrError);
|
||||
client.on("error", clientCloseOrError);
|
||||
|
@ -85,7 +79,7 @@ export default class Server {
|
|||
} catch (e) {
|
||||
client?.end();
|
||||
client = null;
|
||||
socket.close();
|
||||
socket.end();
|
||||
}
|
||||
break;
|
||||
case Packet.Data:
|
||||
|
@ -110,6 +104,8 @@ export default class Server {
|
|||
}
|
||||
socket.on("close", closeOrError);
|
||||
socket.on("error", closeOrError);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(config.port, () => console.log(`Server started at ${config.port}`));
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
export default interface IClientConfig {
|
||||
remoteAddress: string,
|
||||
remoteHost: string,
|
||||
remotePort: number,
|
||||
localPort: number,
|
||||
authKey: string
|
||||
}
|
Loading…
Reference in a new issue