mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-23 13:49:09 +00:00
Improve CORS handling
This commit is contained in:
@@ -62,6 +62,7 @@ export interface ServerConfig {
|
||||
cloudflareApiToken(): string;
|
||||
cloudflareConfigPath(): string;
|
||||
cloudflareCredsPath(): string;
|
||||
origin(): string;
|
||||
}
|
||||
|
||||
export interface NukeMagnitude {
|
||||
|
||||
@@ -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
@@ -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 = {
|
||||
|
||||
@@ -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.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user