Skip to content

Commit a8a8397

Browse files
committed
removed servererror, improved logging
1 parent 77da40a commit a8a8397

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+339
-487
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test:
3535

3636

3737
start:
38-
@pnpx tsx --import ./instrument.mjs --openssl-legacy-provider --env-file=.env server.ts
38+
@pnpx tsx --import ./instrument.mjs --openssl-legacy-provider --env-file=.env server.ts | pnpx pino-pretty
3939

4040
prod_node:
4141
docker-compose --file docker-compose.yml up -d --build

instrument.mjs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Sentry.init({
1212
// for finer control
1313
tracesSampleRate: 1.0,
1414
profilesSampleRate: 1.0, // Profiling sample rate is relative to tracesSampleRate
15-
debug: true,
1615
});
1716

1817

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"lobby": "link:packages/lobby",
4040
"moment": "^2.29.4",
4141
"pino": "^9.4.0",
42+
"pino-pretty": "^11.2.2",
4243
"rusty-motors-chat": "link:src/chat",
4344
"rusty-motors-cli": "link:packages/cli",
4445
"rusty-motors-connection": "link:packages/connection",
@@ -48,8 +49,10 @@
4849
"rusty-motors-login": "link:packages/login",
4950
"rusty-motors-mcots": "link:packages/mcots",
5051
"rusty-motors-nps": "link:packages/nps",
52+
"rusty-motors-patch": "link:packages/patch",
5153
"rusty-motors-personas": "link:packages/persona",
5254
"rusty-motors-sessions": "link:packages/sessions",
55+
"rusty-motors-shard": "link:packages/shard",
5356
"rusty-motors-shared": "link:packages/shared",
5457
"rusty-motors-shared-packets": "link:packages/shared-packets",
5558
"rusty-motors-transactions": "link:packages/transactions",

packages/cli/ConsoleThread.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { emitKeypressEvents } from "node:readline";
22
// eslint-disable-next-line no-unused-vars
33
import { Gateway } from "rusty-motors-gateway";
44
import { SubThread, type ServerLogger } from "rusty-motors-shared";
5-
import { ServerError } from "rusty-motors-shared";
65

76
/**
87
* @module ConsoleThread
@@ -27,7 +26,7 @@ export class ConsoleThread extends SubThread {
2726
}) {
2827
super("ReadInput", log, 100);
2928
if (parentThread === undefined) {
30-
throw new ServerError(
29+
throw Error(
3130
"parentThread is undefined when creating ReadInput",
3231
);
3332
}

packages/gateway/src/GatewayServer.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import fastify, { type FastifyInstance } from "fastify";
44
import { ConsoleThread } from "rusty-motors-cli";
55
import { receiveLobbyData } from "rusty-motors-lobby";
66
import { receiveLoginData } from "rusty-motors-login";
7-
import { receivePersonaData } from "../../persona/src/internal.js";
7+
import { receivePersonaData } from "rusty-motors-personas";
88
import { Configuration, getServerConfiguration, type ServerLogger } from "rusty-motors-shared";
99
import {
1010
addOnDataHandler,
1111
createInitialState,
1212
fetchStateFromDatabase,
1313
} from "rusty-motors-shared";
14-
import { ServerError } from "rusty-motors-shared";
1514
import { getServerLogger } from "rusty-motors-shared";
1615
import { receiveTransactionsData } from "rusty-motors-transactions";
1716
import { onSocketConnection } from "./index.js";
@@ -97,7 +96,7 @@ export class Gateway {
9796
*/
9897
getWebServer(): FastifyInstance {
9998
if (this.webServer === undefined) {
100-
throw new ServerError("webServer is undefined");
99+
throw Error("webServer is undefined");
101100
}
102101
return this.webServer;
103102
}
@@ -108,7 +107,7 @@ export class Gateway {
108107

109108
// Check if there are any listening ports specified
110109
if (this.listeningPortList.length === 0) {
111-
throw new ServerError("No listening ports specified");
110+
throw Error("No listening ports specified");
112111
}
113112

114113
// Mark the GatewayServer as running
@@ -137,7 +136,7 @@ export class Gateway {
137136
});
138137

139138
if (this.webServer === undefined) {
140-
throw new ServerError("webServer is undefined");
139+
throw Error("webServer is undefined");
141140
}
142141

143142
// Start the web server
@@ -192,7 +191,7 @@ export class Gateway {
192191
}
193192

194193
if (this.webServer === undefined) {
195-
throw new ServerError("webServer is undefined");
194+
throw Error("webServer is undefined");
196195
}
197196
await this.webServer.close();
198197

@@ -234,7 +233,7 @@ export class Gateway {
234233

235234
// Register the read thread events
236235
if (this.readThread === undefined) {
237-
throw new ServerError("readThread is undefined");
236+
throw Error("readThread is undefined");
238237
}
239238
this.consoleEvents.forEach((event) => {
240239
this.readThread?.on(event, () => {

packages/gateway/src/encryption.ts

+27-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import { createCipheriv, createDecipheriv, getCiphers } from "node:crypto";
1818
import { McosEncryptionPair } from "rusty-motors-shared";
19-
import { ServerError } from "rusty-motors-shared";
2019

2120
/**
2221
* This function creates a new encryption pair for use with the game server
@@ -25,27 +24,26 @@ import { ServerError } from "rusty-motors-shared";
2524
* @returns {McosEncryptionPair} The encryption pair
2625
*/
2726
export function createCommandEncryptionPair(key: string): McosEncryptionPair {
28-
if (key.length < 16) {
29-
const err = new ServerError("Key too short");
30-
throw err;
31-
}
27+
if (key.length < 16) {
28+
throw Error("Key too short");
29+
}
3230

33-
const sKey = key.slice(0, 16);
31+
const sKey = key.slice(0, 16);
3432

35-
// Deepcode ignore HardcodedSecret: This uses an empty IV
36-
const desIV = Buffer.alloc(8);
33+
// Deepcode ignore HardcodedSecret: This uses an empty IV
34+
const desIV = Buffer.alloc(8);
3735

38-
const gsCipher = createCipheriv("des-cbc", Buffer.from(sKey, "hex"), desIV);
39-
gsCipher.setAutoPadding(false);
36+
const gsCipher = createCipheriv("des-cbc", Buffer.from(sKey, "hex"), desIV);
37+
gsCipher.setAutoPadding(false);
4038

41-
const gsDecipher = createDecipheriv(
42-
"des-cbc",
43-
Buffer.from(sKey, "hex"),
44-
desIV,
45-
);
46-
gsDecipher.setAutoPadding(false);
39+
const gsDecipher = createDecipheriv(
40+
"des-cbc",
41+
Buffer.from(sKey, "hex"),
42+
desIV,
43+
);
44+
gsDecipher.setAutoPadding(false);
4745

48-
return new McosEncryptionPair(gsCipher, gsDecipher);
46+
return new McosEncryptionPair(gsCipher, gsDecipher);
4947
}
5048

5149
/**
@@ -56,18 +54,17 @@ export function createCommandEncryptionPair(key: string): McosEncryptionPair {
5654
* @throws Error if the key is too short
5755
*/
5856
export function createDataEncryptionPair(key: string): McosEncryptionPair {
59-
if (key.length < 16) {
60-
const err = new ServerError("Key too short");
61-
throw err;
62-
}
57+
if (key.length < 16) {
58+
throw Error("Key too short");
59+
}
6360

64-
const stringKey = Buffer.from(key, "hex");
61+
const stringKey = Buffer.from(key, "hex");
6562

66-
// File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
67-
const tsCipher = createCipheriv("rc4", stringKey.subarray(0, 16), "");
68-
const tsDecipher = createDecipheriv("rc4", stringKey.subarray(0, 16), "");
63+
// File deepcode ignore InsecureCipher: RC4 is the encryption algorithum used here, file deepcode ignore HardcodedSecret: A blank IV is used here
64+
const tsCipher = createCipheriv("rc4", stringKey.subarray(0, 16), "");
65+
const tsDecipher = createDecipheriv("rc4", stringKey.subarray(0, 16), "");
6966

70-
return new McosEncryptionPair(tsCipher, tsDecipher);
67+
return new McosEncryptionPair(tsCipher, tsDecipher);
7168
}
7269

7370
/**
@@ -77,13 +74,8 @@ export function createDataEncryptionPair(key: string): McosEncryptionPair {
7774
* @throws Error if the server does not support the legacy ciphers
7875
*/
7976
export function verifyLegacyCipherSupport() {
80-
const cipherList = getCiphers();
81-
if (!cipherList.includes("des-cbc")) {
82-
const err = new ServerError("DES-CBC cipher not available");
83-
throw err;
84-
}
85-
if (!cipherList.includes("rc4")) {
86-
const err = new ServerError("RC4 cipher not available");
87-
throw err;
88-
}
77+
const cipherList = getCiphers();
78+
if (!cipherList.includes("des-cbc") || !cipherList.includes("rc4")) {
79+
throw Error("Legacy ciphers not available");
80+
}
8981
}

packages/gateway/src/index.ts

+52-48
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ import {
2525
removeSocket,
2626
wrapSocket,
2727
} from "rusty-motors-shared";
28-
import { ServerError } from "rusty-motors-shared";
2928
import { getServerLogger } from "rusty-motors-shared";
3029

3130
import { Socket } from "node:net";
3231
import { getGatewayServer } from "./GatewayServer.js";
3332
import { getPortMessageType, UserStatusManager } from "rusty-motors-nps";
34-
import { BasePacket } from "../../shared-packets/src/BasePacket.js";
33+
import { BasePacket } from "rusty-motors-shared-packets";
3534
import * as Sentry from "@sentry/node";
3635

3736
/**
@@ -74,9 +73,7 @@ export function socketErrorHandler({
7473
log.debug(`Connection ${connectionId} reset`);
7574
return;
7675
}
77-
throw new ServerError(
78-
`Socket error: ${error.message} on connection ${connectionId}`,
79-
);
76+
throw Error(`Socket error: ${error.message} on connection ${connectionId}`);
8077
}
8178

8279
/**
@@ -122,7 +119,7 @@ export function onSocketConnection({
122119
const { localPort, remoteAddress } = incomingSocket;
123120

124121
if (localPort === undefined || remoteAddress === undefined) {
125-
throw new ServerError("localPort or remoteAddress is undefined");
122+
throw Error("localPort or remoteAddress is undefined");
126123
}
127124

128125
// This is a new connection so generate a new connection ID
@@ -207,49 +204,56 @@ export function onSocketConnection({
207204

208205
log.debug("Calling onData handler");
209206

210-
Sentry.startSpan({
211-
name: "onDataHandler",
212-
op: "onDataHandler",
213-
}, async () => {
214-
215-
portOnDataHandler({
216-
connectionId: newConnectionId,
217-
message: rawMessage,
218-
})
219-
.then((response: ServiceResponse) => {
220-
log.debug("onData handler returned");
221-
const { messages } = response;
222-
223-
// Log the messages
224-
log.trace(`Messages: ${messages.map((m) => m.toString())}`);
225-
226-
// Serialize the messages
227-
const serializedMessages = messages.map((m) =>
228-
m.serialize(),
229-
);
230-
231-
try {
232-
// Send the messages
233-
serializedMessages.forEach((m) => {
234-
incomingSocket.write(m);
235-
log.trace(`Sent data: ${m.toString("hex")}`);
207+
Sentry.startSpan(
208+
{
209+
name: "onDataHandler",
210+
op: "onDataHandler",
211+
},
212+
async () => {
213+
portOnDataHandler({
214+
connectionId: newConnectionId,
215+
message: rawMessage,
216+
})
217+
.then((response: ServiceResponse) => {
218+
log.debug("onData handler returned");
219+
const { messages } = response;
220+
221+
// Log the messages
222+
log.trace(
223+
`Messages: ${messages.map((m) => m.toString())}`,
224+
);
225+
226+
// Serialize the messages
227+
const serializedMessages = messages.map((m) =>
228+
m.serialize(),
229+
);
230+
231+
try {
232+
// Send the messages
233+
serializedMessages.forEach((m) => {
234+
incomingSocket.write(m);
235+
log.trace(
236+
`Sent data: ${m.toString("hex")}`,
237+
);
238+
});
239+
} catch (error) {
240+
log.error(
241+
`Error sending data: ${String(error)}`,
242+
);
243+
}
244+
})
245+
.catch((error: Error) => {
246+
log.fatal(`Error handling data: ${String(error)}`);
247+
Sentry.captureException(error);
248+
void getGatewayServer({}).stop();
249+
Sentry.flush(200).then(() => {
250+
log.debug("Sentry flushed");
251+
// Call server shutdown
252+
void getGatewayServer({}).shutdown();
253+
});
236254
});
237-
} catch (error) {
238-
log.error(`Error sending data: ${String(error)}`);
239-
}
240-
})
241-
.catch((/** @type {Error} */ error: Error) => {
242-
log.error(`Error handling data: ${String(error)}`);
243-
Sentry.captureException(error);
244-
Sentry.flush(2000).then(() => {
245-
log.debug("Sentry flushed");
246-
// Call server shutdown
247-
getGatewayServer({}).shutdown();
248-
}
249-
);
250-
});
251-
252-
});
255+
},
256+
);
253257
},
254258
);
255259

packages/gateway/src/web.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17-
import { CastanetResponse } from "../../patch/src/PatchServer.js";
18-
import { generateShardList } from "../../shard/src/ShardServer.js";
17+
import { CastanetResponse } from "rusty-motors-patch";
18+
import { generateShardList } from "rusty-motors-shard";
1919
import {
2020
handleGetCert,
2121
handleGetKey,
2222
handleGetRegistry,
23-
} from "../../shard/src/index.js";
23+
} from "rusty-motors-shard";
2424
import { getServerConfiguration } from "rusty-motors-shared";
25-
import { ServerError } from "rusty-motors-shared";
2625

2726
/**
2827
* Add web routes to the web server
@@ -94,33 +93,21 @@ export function addWebRoutes(webServer: import("fastify").FastifyInstance) {
9493

9594
webServer.get("/ShardList/", (_request, reply) => {
9695
const config = getServerConfiguration({});
97-
if (typeof config.host === "undefined") {
98-
throw new ServerError("No host defined in config");
99-
}
10096
return reply.send(generateShardList(config.host));
10197
});
10298

10399
webServer.get("/cert", (_request, reply) => {
104100
const config = getServerConfiguration({});
105-
if (typeof config.host === "undefined") {
106-
throw new ServerError("No host defined in config");
107-
}
108101
return reply.send(handleGetCert(config));
109102
});
110103

111104
webServer.get("/key", (_request, reply) => {
112105
const config = getServerConfiguration({});
113-
if (typeof config.host === "undefined") {
114-
throw new ServerError("No host defined in config");
115-
}
116106
return reply.send(handleGetKey(config));
117107
});
118108

119109
webServer.get("/registry", (_request, reply) => {
120110
const config = getServerConfiguration({});
121-
if (typeof config.host === "undefined") {
122-
throw new ServerError("No host defined in config");
123-
}
124111
return reply.send(handleGetRegistry(config));
125112
});
126113
}

0 commit comments

Comments
 (0)