Improve CORS handling

This commit is contained in:
oleksandr-shysh
2025-06-10 17:27:03 +03:00
parent 046c9625a2
commit 5e9e937ab6
4 changed files with 27 additions and 11 deletions
+1
View File
@@ -62,6 +62,7 @@ export interface ServerConfig {
cloudflareApiToken(): string;
cloudflareConfigPath(): string;
cloudflareCredsPath(): string;
origin(): string;
}
export interface NukeMagnitude {
+12
View File
@@ -85,6 +85,18 @@ export abstract class DefaultServerConfig implements ServerConfig {
return process.env.CF_CREDS_PATH ?? "";
}
origin(): string {
const audience = this.jwtAudience();
const subdomain = this.subdomain();
if (audience === "localhost") {
return "http://localhost:9000";
}
if (subdomain === "") {
return `https://${audience}`;
}
return `https://${subdomain}.${audience}`;
}
private publicKey: JWK;
abstract jwtAudience(): string;
jwtIssuer(): string {
+11 -11
View File
@@ -1,5 +1,7 @@
import cors from "cors";
import os from "os";
import { GameEnv } from "../core/configuration/Config";
import { getServerConfigFromServer } from "../core/configuration/ConfigLoader";
function getLocalIP() {
const interfaces = os.networkInterfaces();
@@ -15,18 +17,16 @@ function getLocalIP() {
return null;
}
const allowedOrigins = [
"capacitor://localhost",
"https://localhost",
"http://localhost",
"http://localhost:9000",
"https://openfront.io",
"https://openfront.dev",
];
const config = getServerConfigFromServer();
const origin = config.origin();
const localIp = getLocalIP();
if (localIp) {
allowedOrigins.push(`http://${localIp}:9000`);
const allowedOrigins = [origin, "capacitor://localhost", "http://localhost"];
if (config.env() === GameEnv.Dev) {
const localIp = getLocalIP();
if (localIp) {
allowedOrigins.push(`http://${localIp}:9000`, `https://${localIp}:9000`);
}
}
const corsOptions = {
+3
View File
@@ -4,6 +4,9 @@ import { GameMapType } from "../../src/core/game/Game";
import { GameID } from "../../src/core/Schemas";
export class TestServerConfig implements ServerConfig {
origin(): string {
return "http://localhost:9000";
}
cloudflareConfigPath(): string {
throw new Error("Method not implemented.");
}