Compare commits
No commits in common. "tcp" and "master" have entirely different histories.
4 changed files with 29 additions and 32 deletions
|
@ -1,6 +1,5 @@
|
|||
{
|
||||
"remoteHost": "localhost",
|
||||
"remotePort": 18472,
|
||||
"remoteAddress": "ws://localhost: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, Socket } from "net";
|
||||
import { createServer } 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 Socket();
|
||||
client.on("connect", () => {
|
||||
const client = new WebSocket(config.remoteAddress);
|
||||
client.on("open", () => {
|
||||
// Send off auth as soon as we connect
|
||||
client.write(createWriter(Endian.LE, 2 + config.authKey.length).writeUByte(Packet.Auth).writeShortString(config.authKey).toBuffer());
|
||||
client.send(createWriter(Endian.LE, 2 + config.authKey.length).writeUByte(Packet.Auth).writeShortString(config.authKey).toBuffer());
|
||||
});
|
||||
|
||||
client.on("data", (data) => {
|
||||
client.on("message", (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.write(Constants.KEEPALIVE_PACKET);
|
||||
client.send(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.write(message);
|
||||
client.send(message);
|
||||
}
|
||||
queuedMessages.length = 0;
|
||||
}
|
||||
|
@ -79,24 +79,19 @@ 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.write(bufferData);
|
||||
client.send(bufferData);
|
||||
} else {
|
||||
queuedMessages.push(bufferData);
|
||||
}
|
||||
});
|
||||
|
||||
function serverCloseOrError() {
|
||||
client.end();
|
||||
client.close();
|
||||
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, createServer } from "net";
|
||||
import { Socket } 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,7 +14,9 @@ export default class Server {
|
|||
process.exit(1);
|
||||
}
|
||||
const config:IServerConfig = JSON.parse(readFileSync("./config/server-config.json").toString());
|
||||
const server = createServer((socket) => {
|
||||
const server = new WebSocketServer({ port: config.port }, () => console.log(`Server started at ${config.port}`));
|
||||
|
||||
server.on("connection", (socket) => {
|
||||
console.log("Connection");
|
||||
let queuedMessages = new Array<Buffer>();
|
||||
let connectedToServer = false;
|
||||
|
@ -22,10 +24,14 @@ export default class Server {
|
|||
let authed = false;
|
||||
let client:Socket | null = null;
|
||||
const clientKeepAlive = setInterval(() => {
|
||||
socket.end(Constants.KEEPALIVE_PACKET);
|
||||
socket.send(Constants.KEEPALIVE_PACKET);
|
||||
}, 5000);
|
||||
|
||||
socket.on("data", (data) => {
|
||||
socket.on("message", (data, isBinary) => {
|
||||
if (!isBinary) {
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE: The types declarations for ws are really messed up >:(
|
||||
// @ts-ignore
|
||||
const packetData = createReader(Endian.LE, data);
|
||||
|
@ -37,11 +43,11 @@ export default class Server {
|
|||
case Packet.Auth:
|
||||
try {
|
||||
if (packetData.readShortString() !== config.authKey) {
|
||||
socket.write(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Bad).toBuffer());
|
||||
socket.end();
|
||||
socket.send(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Bad).toBuffer());
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
socket.write(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Good).toBuffer());
|
||||
socket.send(createWriter(Endian.LE, 2).writeUByte(Packet.Auth).writeUByte(AuthState.Good).toBuffer());
|
||||
authed = true;
|
||||
|
||||
client = new Socket();
|
||||
|
@ -58,7 +64,7 @@ export default class Server {
|
|||
});
|
||||
|
||||
client.on("data", (chunk) => {
|
||||
socket.write(createWriter(Endian.LE, 5)
|
||||
socket.send(createWriter(Endian.LE, 5)
|
||||
.writeUByte(Packet.Data)
|
||||
.writeUInt(chunk.length)
|
||||
.writeBuffer(chunk)
|
||||
|
@ -66,7 +72,7 @@ export default class Server {
|
|||
});
|
||||
|
||||
function clientCloseOrError() {
|
||||
socket.end();
|
||||
socket.close();
|
||||
}
|
||||
client.on("close", clientCloseOrError);
|
||||
client.on("error", clientCloseOrError);
|
||||
|
@ -79,7 +85,7 @@ export default class Server {
|
|||
} catch (e) {
|
||||
client?.end();
|
||||
client = null;
|
||||
socket.end();
|
||||
socket.close();
|
||||
}
|
||||
break;
|
||||
case Packet.Data:
|
||||
|
@ -104,8 +110,6 @@ export default class Server {
|
|||
}
|
||||
socket.on("close", closeOrError);
|
||||
socket.on("error", closeOrError);
|
||||
});
|
||||
|
||||
server.listen(config.port, () => console.log(`Server started at ${config.port}`));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
export default interface IClientConfig {
|
||||
remoteHost: string,
|
||||
remotePort: number,
|
||||
remoteAddress: string,
|
||||
localPort: number,
|
||||
authKey: string
|
||||
}
|
Loading…
Reference in a new issue