Skip to content

Commit

Permalink
Updating packages to latest versions for the new eslint-config package`
Browse files Browse the repository at this point in the history
Also fixing breaking changes from said updates.
  • Loading branch information
Tynarus committed Aug 27, 2021
1 parent f0abb28 commit 6b03d2c
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 1,382 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ node_modules
/data/dump
/data/houses
/data/saves/*.json
/data/config/server-config.yaml
/config/server-config.json
server-config.yaml

# local env files
.env.local
Expand Down
23 changes: 23 additions & 0 deletions config/server-config.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configDir": "./config",
"cacheDir": "./cache",

"host": "0.0.0.0",
"port": 43594,

"updateServerHost": "0.0.0.0",
"updateServerPort": 43592,

"loginServerHost": "0.0.0.0",
"loginServerPort": 43591,
"rsaMod": "119568088839203297999728368933573315070738693395974011872885408638642676871679245723887367232256427712869170521351089799352546294030059890127723509653145359924771433131004387212857375068629466435244653901851504845054452735390701003613803443469723435116497545687393297329052988014281948392136928774011011998343",
"rsaExp": "12747337179295870166838611986189126026507945904720545965726999254744592875817063488911622974072289858092633084100280214658532446654378876853112046049506789703022033047774294965255097838909779899992870910011426403494610880634275141204442441976355383839981584149269550057129306515912021704593400378690444280161",
"playerSavePath": "./data/saves",

"showWelcome": true,
"expRate": 1,
"giveAchievements": true,
"checkCredentials": true,
"tutorialEnabled": false,
"adminDropsEnabled": true
}
14 changes: 0 additions & 14 deletions data/config/server-config.example.yaml

This file was deleted.

1,523 changes: 283 additions & 1,240 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@
},
"homepage": "https://github.com/runejs/server#readme",
"dependencies": {
"@runejs/core": "^1.3.2",
"@runejs/filestore": "file:../filestore",
"@runejs/login-server": "^1.1.0",
"@runejs/update-server": "^1.1.1",
"@runejs/core": "^1.5.4",
"@runejs/filestore": "^0.15.2",
"@runejs/login-server": "^1.2.2",
"@runejs/update-server": "^1.2.2",
"bigi": "^1.4.2",
"js-yaml": "^3.13.1",
"json5": "^2.1.3",
"lodash": "^4.17.21",
"quadtree-lib": "^1.0.9",
"rxjs": "^7.0.0",
"source-map-support": "^0.5.19",
"ts-node": "^9.1.1",
"tslib": "^2.1.0",
"typescript": "^4.2.3",
"uuid": "^3.3.3",
"yargs": "^15.3.1"
},
Expand All @@ -65,7 +63,6 @@
"@types/uuid": "^3.4.6",
"@types/yargs": "^13.0.4",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/eslint-plugin-tslint": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"babel-plugin-module-resolver": "^4.0.0",
"chokidar": "^3.4.3",
Expand All @@ -74,6 +71,8 @@
"mkdirp": "^1.0.4",
"nodemon": "^2.0.6",
"rimraf": "^3.0.2",
"tsconfig-paths": "^3.9.0"
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
}
}
19 changes: 2 additions & 17 deletions src/game-engine/login-server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
import { launchLoginServer } from '@runejs/login-server';
import { logger } from '@runejs/core';
import { parseServerConfig } from '@runejs/core/net';
import { ServerConfig } from '@engine/config/server-config';
import 'source-map-support/register';


const startLoginServer = (): void => {
const serverConfig = parseServerConfig<ServerConfig>();

if(!serverConfig) {
logger.error('Unable to start Login Server due to missing or invalid server configuration.');
return;
}

launchLoginServer(serverConfig.loginServerHost, serverConfig.loginServerPort,
serverConfig.rsaMod, serverConfig.rsaExp, serverConfig.checkCredentials, 'data/saves');
};

startLoginServer();
launchLoginServer();
18 changes: 9 additions & 9 deletions src/game-engine/net/server/game-server.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { logger } from '@runejs/core';
import { openServer, SocketConnectionHandler } from '@runejs/core/net';
import { ConnectionStatus, SocketServer } from '@runejs/core/net';
import { ByteBuffer } from '@runejs/core/buffer';
import { Socket } from 'net';
import { ServerGateway } from '@engine/net/server/server-gateway';
import { handlePacket, incomingPackets } from '@engine/net/inbound-packets';
import { Player } from '@engine/world/actor/player/player';


export class GameServerConnection implements SocketConnectionHandler {
export class GameServerConnection {

private activePacketId: number = null;
private activePacketSize: number = null;
private activeBuffer: ByteBuffer;

public constructor(private readonly clientSocket: Socket, private readonly player: Player) {
public constructor(private readonly clientSocket: Socket,
private readonly player: Player) {
}

public async dataReceived(buffer?: ByteBuffer): Promise<void> {
public decodeMessage(buffer?: ByteBuffer): void | Promise<void> {
if(!this.activeBuffer) {
this.activeBuffer = buffer;
} else if(buffer) {
Expand Down Expand Up @@ -98,10 +99,8 @@ export class GameServerConnection implements SocketConnectionHandler {
this.activePacketSize = null;

if(this.activeBuffer !== null && this.activeBuffer.readable > 0) {
await this.dataReceived();
this.decodeMessage();
}

return Promise.resolve();
}

public connectionDestroyed(): void {
Expand All @@ -116,5 +115,6 @@ export class GameServerConnection implements SocketConnectionHandler {
}

export const openGameServer = (host: string, port: number): void =>
openServer<ServerGateway>('Game Server', host, port,
socket => new ServerGateway(socket));
SocketServer.launch<ServerGateway>(
'Game Server',
host, port, socket => new ServerGateway(socket));
160 changes: 83 additions & 77 deletions src/game-engine/net/server/server-gateway.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logger } from '@runejs/core';
import { LoginResponseCode } from '@runejs/login-server';
import { ByteBuffer } from '@runejs/core/buffer';
import { parseServerConfig, SocketConnectionHandler } from '@runejs/core/net';
import { parseServerConfig, SocketServer } from '@runejs/core/net';
import { createConnection, Socket } from 'net';
import { GameServerConnection } from '@engine/net/server/game-server';
import { ServerConfig } from '@engine/config/server-config';
Expand All @@ -14,7 +14,7 @@ const serverConfig = parseServerConfig<ServerConfig>();
export type ServerType = 'game_server' | 'login_server' | 'update_server';


export class ServerGateway extends SocketConnectionHandler {
export class ServerGateway extends SocketServer {

private serverType: ServerType;
private gameServerConnection: GameServerConnection;
Expand All @@ -23,74 +23,112 @@ export class ServerGateway extends SocketConnectionHandler {
private serverKey: bigint;

public constructor(private readonly clientSocket: Socket) {
super();
super(clientSocket);
}

public async dataReceived(buffer: ByteBuffer): Promise<void> {
if(!this.serverType) {
buffer = this.parseInitialClientHandshake(buffer);
public initialHandshake(buffer: ByteBuffer): boolean {
if(this.serverType) {
this.decodeMessage(buffer);
return true;
}

switch(this.serverType) {
case 'login_server':
// Pass request data through to the login server
this.loginServerSocket.write(buffer);
break;
case 'update_server':
// Pass request data through to the update server
this.updateServerSocket.write(buffer);
break;
default:
if(this.gameServerConnection) {
// Use existing socket for game packets
await this.gameServerConnection.dataReceived(buffer);
break;
}
// First communication from the game client to the server gateway
// Here we find out what kind of connection the client is making - game, or update server?
// If game - they'll need to pass through the login server to authenticate first!

const packetId = buffer.get('BYTE', 'UNSIGNED');

if(packetId === 15) {
this.serverType = 'update_server';
this.updateServerSocket = createConnection({
host: serverConfig.updateServerHost,
port: serverConfig.updateServerPort
});
this.updateServerSocket.on('data', data => this.clientSocket.write(data));
this.updateServerSocket.on('end', () => {
logger.info(`Update server connection closed.`);
});
this.updateServerSocket.on('error', () => {
logger.error(`Update server error.`);
})
this.updateServerSocket.setNoDelay(true);
this.updateServerSocket.setKeepAlive(true);
this.updateServerSocket.setTimeout(30000);
} else if(packetId === 14) {
this.serverType = 'login_server';
this.loginServerSocket = createConnection({
host: serverConfig.loginServerHost,
port: serverConfig.loginServerPort
});
this.loginServerSocket.on('data', data => this.parseLoginServerResponse(new ByteBuffer(data)));
this.loginServerSocket.on('end', () => {
logger.error(`Login server error.`);
});
this.loginServerSocket.setNoDelay(true);
this.loginServerSocket.setKeepAlive(true);
this.loginServerSocket.setTimeout(30000);
} else {
logger.error(`Invalid initial client handshake packet id.`);
return false;
}

return Promise.resolve();
const data = buffer.getSlice(1, buffer.length);
const socket = this.serverType === 'login_server' ? this.loginServerSocket : this.updateServerSocket;
socket.write(data);

return true;
}

public decodeMessage(buffer: ByteBuffer): void | Promise<void> {
if(this.serverType === 'login_server') {
this.loginServerSocket.write(buffer);
} else if(this.serverType === 'update_server') {
this.updateServerSocket.write(buffer);
} else {
this.gameServerConnection?.decodeMessage(buffer);
}
}

public connectionDestroyed(): void {
this.loginServerSocket?.destroy();
this.updateServerSocket?.destroy();
this.gameServerConnection?.connectionDestroyed();
}

private parseLoginServerResponse(buffer: ByteBuffer): void {
if(!this.serverKey) {
// Login handshake response
const handshakeResponseCode = buffer.get();
const handshakeResponseCode = buffer.get('byte');

if(handshakeResponseCode === 0) {
this.serverKey = BigInt(buffer.get('LONG'));
this.serverKey = BigInt(buffer.get('long'));
}
} else {
// Login response
const loginResponseCode = buffer.get();
const loginResponseCode = buffer.get('byte');

if(loginResponseCode === LoginResponseCode.SUCCESS) {
try {
const clientKey1 = buffer.get('INT');
const clientKey2 = buffer.get('INT');
const gameClientId = buffer.get('INT');
const clientKey1 = buffer.get('int');
const clientKey2 = buffer.get('int');
const gameClientId = buffer.get('int');
const username = buffer.getString();
const passwordHash = buffer.getString();
const lowDetail = buffer.get() === 1;
const lowDetail = buffer.get('byte') === 1;

if(world.playerOnline(username)) {
// Player is already logged in!
// @TODO move to login server
buffer = new ByteBuffer(1);
buffer.put(LoginResponseCode.ALREADY_LOGGED_IN);
} else {
this.createPlayer([ clientKey1, clientKey2 ], gameClientId, username, passwordHash, lowDetail ? 'low' : 'high');
this.serverType = 'game_server';
this.createPlayer([ clientKey1, clientKey2 ], gameClientId, username, passwordHash, lowDetail ? 'low' : 'high');
return;
}
} catch(e) {
logger.error(e);
if(this.gameServerConnection) {
this.gameServerConnection.closeSocket();
}
this.gameServerConnection?.closeSocket();
}
}
}
Expand All @@ -99,7 +137,11 @@ export class ServerGateway extends SocketConnectionHandler {
this.clientSocket.write(buffer);
}

private createPlayer(clientKeys: [ number, number ], gameClientId: number, username: string, passwordHash: string, detail: 'high' | 'low'): void {
private async createPlayer(clientKeys: [ number, number ],
gameClientId: number,
username: string,
passwordHash: string,
detail: 'high' | 'low'): Promise<void> {
const sessionKey: number[] = [
Number(clientKeys[0]), Number(clientKeys[1]), Number(this.serverKey >> BigInt(32)), Number(this.serverKey)
];
Expand All @@ -119,51 +161,15 @@ export class ServerGateway extends SocketConnectionHandler {
world.registerPlayer(player);

const outputBuffer = new ByteBuffer(6);
outputBuffer.put(LoginResponseCode.SUCCESS, 'BYTE');
outputBuffer.put(player.rights.valueOf(), 'BYTE');
outputBuffer.put(0, 'BYTE'); // ???
outputBuffer.put(player.worldIndex + 1, 'SHORT');
outputBuffer.put(0, 'BYTE'); // ???
this.clientSocket.write(outputBuffer);

player.init();
}

private parseInitialClientHandshake(buffer: ByteBuffer): ByteBuffer {
// First communication from the game client to the server gateway
// Here we find out what kind of connection the client is making - game, or update server?
// If game - they'll need to pass through the login server to authenticate first!

const packetId = buffer.get('BYTE', 'UNSIGNED');

if(packetId === 15) {
this.serverType = 'update_server';
this.updateServerSocket = createConnection({ host: serverConfig.updateServerHost, port: serverConfig.updateServerPort });
this.updateServerSocket.on('data', data => this.clientSocket.write(data));
this.updateServerSocket.on('end', () => {
logger.info(`Update server connection closed.`);
});
this.updateServerSocket.on('error', () => {
logger.error(`Update server error.`);
})
this.updateServerSocket.setNoDelay(true);
this.updateServerSocket.setKeepAlive(true);
this.updateServerSocket.setTimeout(30000);
} else if(packetId === 14) {
this.serverType = 'login_server';
this.loginServerSocket = createConnection({ host: serverConfig.loginServerHost, port: serverConfig.loginServerPort });
this.loginServerSocket.on('data', data => this.parseLoginServerResponse(new ByteBuffer(data)));
this.loginServerSocket.on('end', () => {
// @TODO
});
this.loginServerSocket.setNoDelay(true);
this.loginServerSocket.setKeepAlive(true);
this.loginServerSocket.setTimeout(30000);
} else {
throw new Error(`Invalid initial client handshake packet id.`);
}
outputBuffer.put(LoginResponseCode.SUCCESS, 'byte');
outputBuffer.put(player.rights.valueOf(), 'byte');
outputBuffer.put(0, 'byte'); // ???
outputBuffer.put(player.worldIndex + 1, 'short');
outputBuffer.put(0, 'byte'); // ???
this.clientSocket.write(outputBuffer);

return buffer.getSlice(1, buffer.length);
await player.init();
}

}
Loading

0 comments on commit 6b03d2c

Please sign in to comment.