mirror of
https://github.com/spacebarchat/server.git
synced 2026-11-09 06:08:17 +00:00
Use EnvConfig for more logging options
This commit is contained in:
1 parent
b7f1c9e483
commit
ce7103e80f
4 files changed
+19
-18
No files matched your search
@@ -27,7 +27,7 @@ import { Close } from "./Close";
|
||||
import { Message } from "./Message";
|
||||
import { Deflate, Inflate } from "fast-zlib";
|
||||
import { URL } from "node:url";
|
||||
import { Config } from "@spacebar/util";
|
||||
import { Config, EnvConfig } from "@spacebar/util";
|
||||
import { Decoder, Encoder } from "@toondepauw/node-zstd";
|
||||
import { ProcessLifecycle } from "@spacebar/util/util/ProcessLifecycle";
|
||||
import { Monitoring } from "../../util/monitoring/Monitoring";
|
||||
@@ -113,7 +113,7 @@ export async function Connection(this: WS.Server, rawSocket: WS, request: Incomi
|
||||
|
||||
console.log(`[Gateway] New connection from ${ipAddress}, total ${this.clients.size}`);
|
||||
|
||||
if (process.env.WS_LOGEVENTS)
|
||||
if (EnvConfig.logging.logGatewayEvents)
|
||||
[
|
||||
"close",
|
||||
"error",
|
||||
|
||||
+13
-13
@@ -16,7 +16,7 @@
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { Config } from "@spacebar/util";
|
||||
import { Config, EnvConfig } from "@spacebar/util";
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import ms, { StringValue } from "ms";
|
||||
import { TextDecoder } from "node:util";
|
||||
@@ -45,7 +45,7 @@ export class NewUrlSignatureData extends NewUrlUserSignatureData {
|
||||
throw new Error("Either path or url must be provided for URL signing");
|
||||
}
|
||||
if (this.path && this.url) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.warn("[Signing] Both path and url are provided, using path for signing", this, new Error().stack);
|
||||
if (EnvConfig.logging.logCdnSignatures) console.warn("[Signing] Both path and url are provided, using path for signing", this, new Error().stack);
|
||||
}
|
||||
if (this.url) {
|
||||
try {
|
||||
@@ -94,10 +94,10 @@ export class UrlSignResult {
|
||||
|
||||
static fromUrl(url: URL | string): UrlSignResult {
|
||||
if (typeof url === "string") {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.debug("[Signing] Parsing URL from string:", url);
|
||||
if (EnvConfig.logging.logCdnSignatures) console.debug("[Signing] Parsing URL from string:", url);
|
||||
url = new URL(url);
|
||||
}
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.debug("[Signing] Parsing URL from URL object:", url.toString());
|
||||
if (EnvConfig.logging.logCdnSignatures) console.debug("[Signing] Parsing URL from URL object:", url.toString());
|
||||
const ex = url.searchParams.get("ex");
|
||||
const is = url.searchParams.get("is");
|
||||
const hm = url.searchParams.get("hm");
|
||||
@@ -149,7 +149,7 @@ function calculateHash(request: UrlSignatureData): UrlSignResult {
|
||||
"[Signing] CDN Signature IP is enabled but we couldn't find the IP field in the request. This may cause issues with signature validation. Please report this to the Spacebar team!",
|
||||
);
|
||||
else {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.log("[Signing] CDN Signature IP is enabled, adding IP to hash:", request.ip);
|
||||
if (EnvConfig.logging.logCdnSignatures) console.log("[Signing] CDN Signature IP is enabled, adding IP to hash:", request.ip);
|
||||
data.update(request.ip!);
|
||||
ipData = new Int8Array(new TextEncoder().encode(request.ip!));
|
||||
}
|
||||
@@ -162,7 +162,7 @@ function calculateHash(request: UrlSignatureData): UrlSignResult {
|
||||
"[Signing] CDN Signature User-Agent is enabled but we couldn't find the user-agent header in the request. This may cause issues with signature validation. Please report this to the Spacebar team!",
|
||||
);
|
||||
else {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.log("[Signing] CDN Signature User-Agent is enabled, adding User-Agent to hash:", request.userAgent);
|
||||
if (EnvConfig.logging.logCdnSignatures) console.log("[Signing] CDN Signature User-Agent is enabled, adding User-Agent to hash:", request.userAgent);
|
||||
data.update(request.userAgent!);
|
||||
userAgentData = new Int8Array(new TextEncoder().encode(request.userAgent!));
|
||||
}
|
||||
@@ -193,7 +193,7 @@ function calculateHash(request: UrlSignatureData): UrlSignResult {
|
||||
expiresAt: request.expiresAt,
|
||||
hash,
|
||||
});
|
||||
if (process.env["LOG_CDN_SIGNATURES"])
|
||||
if (EnvConfig.logging.logCdnSignatures)
|
||||
console.log(
|
||||
"[Signing]",
|
||||
{
|
||||
@@ -214,7 +214,7 @@ export const isExpired = (data: UrlSignResult | UrlSignatureData) => {
|
||||
const expiresAt = parseInt(data.expiresAt, 16);
|
||||
|
||||
if (Number.isNaN(issuedAt) || Number.isNaN(expiresAt)) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.debug("[Signing] Invalid timestamps in query");
|
||||
if (EnvConfig.logging.logCdnSignatures) console.debug("[Signing] Invalid timestamps in query");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -222,13 +222,13 @@ export const isExpired = (data: UrlSignResult | UrlSignatureData) => {
|
||||
|
||||
const isExpired = expiresAt < currentTime;
|
||||
if (isExpired) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.debug("[Signing] Signature expired");
|
||||
if (EnvConfig.logging.logCdnSignatures) console.debug("[Signing] Signature expired");
|
||||
return true;
|
||||
}
|
||||
|
||||
const isValidIssuedAt = issuedAt < currentTime;
|
||||
if (!isValidIssuedAt) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.debug("[Signing] Signature issued in the future");
|
||||
if (EnvConfig.logging.logCdnSignatures) console.debug("[Signing] Signature issued in the future");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -238,13 +238,13 @@ export const isExpired = (data: UrlSignResult | UrlSignatureData) => {
|
||||
export const hasValidSignature = (req: NewUrlUserSignatureData, sig: UrlSignResult) => {
|
||||
// if the required query parameters are not present, return false
|
||||
if (!sig.expiresAt || !sig.issuedAt || !sig.hash) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.warn("[Signing] Missing required query parameters for signature validation");
|
||||
if (EnvConfig.logging.logCdnSignatures) console.warn("[Signing] Missing required query parameters for signature validation");
|
||||
return false;
|
||||
}
|
||||
|
||||
// check if the signature is expired
|
||||
if (isExpired(sig)) {
|
||||
if (process.env["LOG_CDN_SIGNATURES"]) console.warn("[Signing] Signature is expired");
|
||||
if (EnvConfig.logging.logCdnSignatures) console.warn("[Signing] Signature is expired");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -279,7 +279,7 @@ export const hasValidSignature = (req: NewUrlUserSignatureData, sig: UrlSignResu
|
||||
const isHashValid = calculated.length === received.length && timingSafeEqual(calculated, received);
|
||||
|
||||
if (!isHashValid)
|
||||
if (process.env["LOG_CDN_SIGNATURES"])
|
||||
if (EnvConfig.logging.logCdnSignatures)
|
||||
console.warn(`Signature validation for ${sig.path} (is=${sig.issuedAt}, ex=${sig.expiresAt}) failed: calculated: ${calcd}, received: ${sig.hash}`);
|
||||
|
||||
return isHashValid;
|
||||
|
||||
@@ -25,7 +25,7 @@ import { HTTPError } from "lambert-server/HTTPError";
|
||||
import { InstanceBan, Session, User } from "@spacebar/database";
|
||||
import { Random, sleep, Stopwatch, TimeSpan } from "@spacebar/extensions";
|
||||
import { Config } from "./Config";
|
||||
import { OrmUtils } from "@spacebar/util";
|
||||
import { OrmUtils, EnvConfig } from "@spacebar/util";
|
||||
import { clearInterval, setInterval } from "node:timers";
|
||||
import { ProcessLifecycle } from "@spacebar/util/util/ProcessLifecycle";
|
||||
|
||||
@@ -52,7 +52,7 @@ export type UserTokenData = {
|
||||
};
|
||||
|
||||
function logAuth(text: string) {
|
||||
if (process.env.LOG_AUTH !== "true") return;
|
||||
if (!EnvConfig.logging.logAuthentication) return;
|
||||
console.log(`[AUTH] ${text}`);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import WS from "ws";
|
||||
import { VoiceOPCodes, WebRtcWebSocket, Send } from "../util";
|
||||
import { onClose } from "./Close";
|
||||
import { onMessage } from "./Message";
|
||||
import { EnvConfig } from "@spacebar/util";
|
||||
|
||||
// TODO: check rate limit
|
||||
// TODO: specify rate limit in config
|
||||
@@ -35,7 +36,7 @@ export async function Connection(this: WS.Server, rawSocket: WS, request: Incomi
|
||||
socket.rawSocket.on("message", (data, isBinary) => onMessage(socket, data as Buffer));
|
||||
console.log("[WebRTC] new connection", request.url);
|
||||
|
||||
if (process.env.WS_LOGEVENTS) {
|
||||
if (EnvConfig.logging.logGatewayEvents) {
|
||||
[
|
||||
"close",
|
||||
"error",
|
||||
|
||||
Reference in new issue
Block a user