Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor import statements #2131

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test:


start:
@pnpx tsx --openssl-legacy-provider --env-file=.env server.ts
@pnpx tsx --import ./instrument.mjs --openssl-legacy-provider --env-file=.env server.ts

prod_node:
docker-compose --file docker-compose.yml up -d --build
Expand Down
18 changes: 18 additions & 0 deletions instrument.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Sentry from '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/profiling-node';

Sentry.init({
dsn: process.env['SENTRY_DSN'],
integrations: [
// Add our Profiling integration
nodeProfilingIntegration(),
],

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
profilesSampleRate: 1.0, // Profiling sample rate is relative to tracesSampleRate
debug: true,
});


5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@databases/pg": "^5.5.0",
"@dotenvx/dotenvx": "^1.10.2",
"@fastify/sensible": "^5.6.0",
"@sentry/node": "7.88",
"@sentry/profiling-node": "^1.3.2",
"@sentry/node": "8.30.0",
"@sentry/profiling-node": "8.30.0",
"adminjs": "^7.5.2",
"fastify": "^4.28.1",
"lobby": "link:packages/lobby",
Expand All @@ -48,6 +48,7 @@
"rusty-motors-login": "link:packages/login",
"rusty-motors-mcots": "link:packages/mcots",
"rusty-motors-nps": "link:packages/nps",
"rusty-motors-personas": "link:packages/persona",
"rusty-motors-sessions": "link:packages/sessions",
"rusty-motors-shared": "link:packages/shared",
"rusty-motors-shared-packets": "link:packages/shared-packets",
Expand Down
2 changes: 1 addition & 1 deletion packages/connection/test/Connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import {
createCommandEncryptionPair,
verifyLegacyCipherSupport,
} from "rusty-motors-connection";
} from "../src/Connection.js";
import { McosEncryptionPair } from "rusty-motors-shared";

vi.mock("@sentry/node", () => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/gateway/src/GatewayServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { receiveTransactionsData } from "rusty-motors-transactions";
import { onSocketConnection } from "./index.js";
import { addWebRoutes } from "./web.js";
import { populateGameMessageProcessors, populatePortToMessageTypes, portToMessageTypes, gameMessageProcessors } from "rusty-motors-nps";
import { populateServerMessageProcessors } from "rusty-motors-mcots";
import { receiveChatData } from "rusty-motors-chat";

/**
* Options for the GatewayServer.
Expand Down Expand Up @@ -251,6 +251,7 @@ export class Gateway {
let state = fetchStateFromDatabase();

state = addOnDataHandler(state, 8226, receiveLoginData);
state = addOnDataHandler(state, 8227, receiveChatData);
state = addOnDataHandler(state, 8228, receivePersonaData);
state = addOnDataHandler(state, 7003, receiveLobbyData);
state = addOnDataHandler(state, 43300, receiveTransactionsData);
Expand Down
27 changes: 20 additions & 7 deletions packages/gateway/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { randomUUID } from "node:crypto";
import {
type OnDataHandler,
SerializedBufferOld,
type ServiceResponse,
addSocket,
fetchStateFromDatabase,
Expand All @@ -32,6 +31,8 @@ import { Socket } from "node:net";
import type { Logger } from "pino";
import { getGatewayServer } from "./GatewayServer.js";
import { getPortMessageType, UserStatusManager } from "rusty-motors-nps";
import { BasePacket } from "../../shared-packets/src/BasePacket.js";
import * as Sentry from "@sentry/node";

/**
* @typedef {object} OnDataHandlerArgs
Expand Down Expand Up @@ -198,15 +199,19 @@ export function onSocketConnection({
);

// Deserialize the raw message
const rawMessage = new SerializedBufferOld()._doDeserialize(
incomingDataAsBuffer,
);
const rawMessage = new BasePacket();
rawMessage.deserialize(incomingDataAsBuffer);
drazisil marked this conversation as resolved.
Show resolved Hide resolved

// Log the raw message
log.trace(`Raw message: ${rawMessage.toString()}`);

log.debug("Calling onData handler");

Sentry.startSpan({
name: "onDataHandler",
op: "onDataHandler",
}, async (span) => {

portOnDataHandler({
connectionId: newConnectionId,
message: rawMessage,
Expand Down Expand Up @@ -235,10 +240,18 @@ export function onSocketConnection({
})
.catch((/** @type {Error} */ error: Error) => {
log.error(`Error handling data: ${String(error)}`);
Sentry.captureException(error);
Sentry.flush(2000).then(() => {
log.debug("Sentry flushed");
// Call server shutdown
getGatewayServer({}).shutdown();
}
);
}).finally(() =>
span.end(),
);

// Call server shutdown
getGatewayServer({}).shutdown();
});
});
},
);

Expand Down
8 changes: 4 additions & 4 deletions packages/lobby/src/LoginInfoMessage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ServerError } from "../../shared/src/ServerError.js";
import { deserializeString } from "../../shared/deserializeString.js";
import { LegacyMessage } from "../../shared/LegacyMessage.js";
import { serializeString } from "../../shared/serializeString.js";
import { ServerError } from "rusty-motors-shared";
import { deserializeString } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";
import { serializeString } from "rusty-motors-shared";

export class LoginInfoMessage extends LegacyMessage {
_userId: number;
Expand Down
8 changes: 4 additions & 4 deletions packages/lobby/src/UserInfoMessage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ServerError } from "../../shared/src/ServerError.js";
import { deserializeString } from "../../shared/deserializeString.js";
import { LegacyMessage } from "../../shared/LegacyMessage.js";
import { serializeString } from "../../shared/serializeString.js";
import { ServerError } from "rusty-motors-shared";
import { deserializeString } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";
import { serializeString } from "rusty-motors-shared";
// eslint-disable-next-line no-unused-vars
import { LoginInfoMessage } from "./LoginInfoMessage.js";

Expand Down
8 changes: 4 additions & 4 deletions packages/lobby/src/handlers/encryptedCommand.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getServerConfiguration } from "../../../shared/Configuration.js";
import { getServerConfiguration } from "rusty-motors-shared";
import {
fetchStateFromDatabase,
getEncryption,
updateEncryption,
} from "../../../shared/State.js";
} from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { getServerLogger } from "rusty-motors-shared";
import { MessageBufferOld } from "rusty-motors-shared";
import { SerializedBufferOld } from "../../../shared/SerializedBufferOld.js";
import { LegacyMessage } from "../../../shared/LegacyMessage.js";
import { SerializedBufferOld } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";
import { _setMyUserData } from "./_setMyUserData.js";
import { handleGetMiniUserList } from "./handleGetMiniUserList.js";
import { handleSendMiniRiffList } from "./handleSendMiniRiffList.js";
Expand Down
8 changes: 4 additions & 4 deletions packages/lobby/src/handlers/handleGetMiniUserList.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getServerConfiguration } from "../../../shared/Configuration.js";
import { getServerConfiguration } from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { getServerLogger } from "rusty-motors-shared";
import { GameMessage } from "../../../shared/GameMessage.js";
import { LegacyMessage } from "../../../shared/LegacyMessage.js";
import { serializeString } from "../../../shared/serializeString.js";
import { GameMessage } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";
import { serializeString } from "rusty-motors-shared";
import { UserInfo } from "../UserInfoMessage.js";
import { channelRecordSize, channels } from "./channels.js";

Expand Down
4 changes: 2 additions & 2 deletions packages/lobby/src/handlers/handleTrackingPing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getServerConfiguration } from "../../../shared/Configuration.js";
import { getServerConfiguration } from "rusty-motors-shared";
import { getServerLogger } from "rusty-motors-shared";
import { SerializedBufferOld } from "../../../shared/SerializedBufferOld.js";
import { SerializedBufferOld } from "rusty-motors-shared";

export async function handleTrackingPing({
connectionId,
Expand Down
10 changes: 5 additions & 5 deletions packages/lobby/src/handlers/requestConnectGameServer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { getPersonasByPersonaId } from "../../../persona/src/getPersonasByPersonaId.js";
import { getPersonasByPersonaId } from "rusty-motors-personas";
import { getServerLogger } from "rusty-motors-shared";
import { LoginInfoMessage } from "../LoginInfoMessage.js";

import {
createCommandEncryptionPair,
createDataEncryptionPair,
} from "../../../gateway/src/encryption.js";
} from "rusty-motors-gateway";
import {
McosEncryption,
addEncryption,
fetchStateFromDatabase,
getEncryption,
} from "../../../shared/State.js";
} from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { SerializedBufferOld } from "../../../shared/SerializedBufferOld.js";
import { SerializedBufferOld } from "rusty-motors-shared";
import { UserInfoMessage } from "../UserInfoMessage.js";
import { fetchSessionKeyByCustomerId } from "../../../database/index.js";
import { fetchSessionKeyByCustomerId } from "rusty-motors-database";

/**
* Convert to zero padded hex
Expand Down
22 changes: 13 additions & 9 deletions packages/lobby/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { getServerConfiguration } from "../../shared/Configuration.js";
import { getServerConfiguration } from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { getServerLogger } from "rusty-motors-shared";
import { SerializedBufferOld } from "../../shared/SerializedBufferOld.js";
import { NPSMessage } from "../../shared/NPSMessage.js";
import { LegacyMessage } from "../../shared/LegacyMessage.js";
import { SerializedBufferOld } from "rusty-motors-shared";
import { NPSMessage } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";
import { handleEncryptedNPSCommand } from "./handlers/encryptedCommand.js";
import { handleTrackingPing } from "./handlers/handleTrackingPing.js";
import { _npsRequestGameConnectServer } from "./handlers/requestConnectGameServer.js";
import type { Serializable } from "rusty-motors-shared-packets";

/**
* Array of supported message handlers
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function receiveLobbyData({
}),
}: {
connectionId: string;
message: SerializedBufferOld;
message: Serializable;
log?: import("pino").Logger;
}): Promise<{
connectionId: string;
Expand All @@ -99,7 +100,7 @@ export async function receiveLobbyData({
let inboundMessage: LegacyMessage | NPSMessage;

// Check data length
const dataLength = message.data.length;
const dataLength = message.getByteSize();

if (dataLength < 4) {
throw new ServerError(
Expand All @@ -113,9 +114,9 @@ export async function receiveLobbyData({
inboundMessage = new LegacyMessage();
}

inboundMessage._doDeserialize(message.data);
inboundMessage._doDeserialize(message.serialize());

const { data } = message;
const data = message.serialize();
log.debug(
`Received Lobby packet',
${JSON.stringify({
Expand All @@ -134,10 +135,13 @@ export async function receiveLobbyData({
);
}

const buff = new SerializedBufferOld();
buff._doDeserialize(data);

try {
const result = await supportedHandler.handler({
connectionId,
message,
message: buff,
log,
});
log.debug(`Returning with ${result.messages.length} messages`);
Expand Down
8 changes: 4 additions & 4 deletions packages/login/src/NPSUserStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { privateDecrypt } from "node:crypto";
import { readFileSync } from "node:fs";

import { Logger } from "pino";
import { Configuration } from "../../shared/Configuration.js";
import { ServerError } from "../../shared/src/ServerError.js";
import { LegacyMessage } from "../../shared/LegacyMessage.js";
import type { Logger } from "pino";
import { Configuration } from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { LegacyMessage } from "rusty-motors-shared";

/**
* @typedef {import("../../shared/Configuration.js").Configuration} Configuration
Expand Down
10 changes: 5 additions & 5 deletions packages/login/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import { getServerConfiguration } from "../../shared/Configuration.js";
import { ServerError } from "../../shared/src/ServerError.js";
import { getServerConfiguration } from "rusty-motors-shared";
import { ServerError } from "rusty-motors-shared";
import { getServerLogger } from "rusty-motors-shared";
import { SerializedBufferOld } from "../../shared/SerializedBufferOld.js";
import { NPSMessage } from "../../shared/NPSMessage.js";
import { NetworkMessage } from "../../shared/src/NetworkMessage.js";
import { SerializedBufferOld } from "rusty-motors-shared";
import { NPSMessage } from "rusty-motors-shared";
import { NetworkMessage } from "rusty-motors-shared";
import { NPSUserStatus } from "./NPSUserStatus.js";
import { updateSessionKey } from "rusty-motors-database";

Expand Down
11 changes: 7 additions & 4 deletions packages/login/src/receiveLoginData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import { getServerLogger, NPSMessage } from "rusty-motors-shared";
import { getServerLogger, NPSMessage, SerializedBufferOld, type ServiceResponse } from "rusty-motors-shared";
import { handleLoginData } from "./internal.js";
import type { Serializable } from "rusty-motors-shared-packets";

/**
* Receives login data and handles the login process.
Expand All @@ -36,14 +37,16 @@ export async function receiveLoginData({
}),
}: {
connectionId: string;
message: NPSMessage;
message: Serializable;
log?: import("pino").Logger;
}): Promise<import("../../shared/State.js").ServiceResponse> {
}): Promise<ServiceResponse> {
try {
log.debug("Entering login module");
const incomingPacket = new SerializedBufferOld();
incomingPacket._doDeserialize(message.serialize());
const response = await handleLoginData({
connectionId,
message,
message: incomingPacket,
log,
});
log.debug(`There are ${response.messages.length} messages`);
Expand Down
1 change: 1 addition & 0 deletions packages/nps/gameMessageProcessors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const portToMessageTypes = new Map<number, string>([]);
export function populatePortToMessageTypes(portMap: Map<number, string>): void {
portMap.set(7003, "Game");
portMap.set(8226, "Game");
portMap.set(8227, "Game");
portMap.set(8228, "Game");
portMap.set(43300, "Server");
}
Expand Down
1 change: 0 additions & 1 deletion packages/nps/messageStructs/SessionKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class SessionKey extends BaseSerializable {

constructor({ key, timestamp }: { key?: Buffer; timestamp?: number }) {
super();
log.debug("SessionKey.constructor");
if (isOnlyOneSet(key, timestamp)) {
throw new Error("Both key and timestamp must be set if one is set");
}
Expand Down
1 change: 1 addition & 0 deletions packages/persona/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getPersonasByPersonaId } from "./src/getPersonasByPersonaId.js";
2 changes: 1 addition & 1 deletion packages/persona/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "rusty-motors-persona",
"name": "rusty-motors-personas",
"version": "1.0.0",
"exports": {
".": {
Expand Down
6 changes: 3 additions & 3 deletions packages/persona/src/BuddyInfoMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serializeStringRaw } from "../../shared/serializeStringRaw.js";
import { NetworkMessage } from "../../shared/src/NetworkMessage.js";
import { RawMessage } from "../../shared/src/RawMessage.js";
import { serializeStringRaw } from "rusty-motors-shared";
import { NetworkMessage } from "rusty-motors-shared";
import { RawMessage } from "rusty-motors-shared";

/**
* BuddyInfoMessage
Expand Down
Loading
Loading