mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 23:30:41 +09:00
Use a single proxy instead?
This commit is contained in:
1 parent
fc0279ad11
commit
15fc41a801
11 files changed
+831
-71
No files matched your search
@@ -28,6 +28,8 @@ import HostConnection from "./HostConnection";
|
||||
import HostInfo from "./HostInfo";
|
||||
import BaseControllerPlugin from "./BaseControllerPlugin";
|
||||
import ControllerRouter from "./ControllerRouter";
|
||||
import PlayerRoutingStore from "./PlayerRoutingStore";
|
||||
import PlayerProxyServer from "./PlayerProxyServer";
|
||||
|
||||
const endpointDurationSummary = new Summary(
|
||||
"clusterio_controller_http_endpoint_duration_seconds",
|
||||
@@ -76,6 +78,8 @@ export default class Controller {
|
||||
trustedProxies: BlockList;
|
||||
debugEvents = new events.EventEmitter<ControllerDebugEvents>();
|
||||
private _events = new events.EventEmitter<ControllerEvents>();
|
||||
playerRoutingStore: PlayerRoutingStore;
|
||||
playerProxyServer: PlayerProxyServer | null = null;
|
||||
|
||||
/** Event subscription controller */
|
||||
subscriptions = new lib.SubscriptionController();
|
||||
@@ -207,6 +211,9 @@ export default class Controller {
|
||||
this.clusterLogger = clusterLogger;
|
||||
this.pluginInfos = pluginInfos;
|
||||
this.config = config;
|
||||
this.playerRoutingStore = new PlayerRoutingStore(
|
||||
path.join(this.config.get("controller.database_directory"), "player-routing.json"),
|
||||
);
|
||||
|
||||
this.app = express();
|
||||
this.app.locals.controller = this;
|
||||
@@ -340,6 +347,17 @@ export default class Controller {
|
||||
// Load plugins
|
||||
await this.loadPlugins();
|
||||
|
||||
await this.playerRoutingStore.load();
|
||||
const playerProxyPort = this.config.get("controller.player_proxy_port");
|
||||
if (playerProxyPort !== null) {
|
||||
this.playerProxyServer = new PlayerProxyServer(
|
||||
this,
|
||||
logger.child({ component: "player_proxy" }),
|
||||
playerProxyPort,
|
||||
);
|
||||
await this.playerProxyServer.start();
|
||||
}
|
||||
|
||||
// Log all express errors from middleware or routes (app.use is missing the type overload)
|
||||
this.app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction): void => {
|
||||
logger.log({
|
||||
@@ -482,6 +500,11 @@ export default class Controller {
|
||||
|
||||
await lib.invokeHook(this.plugins, "onShutdown");
|
||||
|
||||
if (this.playerProxyServer) {
|
||||
await this.playerProxyServer.stop();
|
||||
this.playerProxyServer = null;
|
||||
}
|
||||
|
||||
await this.wsServer.stop();
|
||||
|
||||
let stopTasks = [];
|
||||
@@ -649,6 +672,8 @@ export default class Controller {
|
||||
await this.userManager.save(path.join(this.config.get("controller.database_directory"), "users.json"));
|
||||
}
|
||||
|
||||
await this.playerRoutingStore.save();
|
||||
|
||||
await lib.invokeHook(this.plugins, "onSaveData");
|
||||
}
|
||||
|
||||
@@ -858,6 +883,87 @@ export default class Controller {
|
||||
return hosts.length ? new lib.HostUpdatesEvent(hosts) : null;
|
||||
}
|
||||
|
||||
resolvePlayerRoute(username: string): { hostId: number; instanceId: number; publicAddress: string; gamePort: number } | null {
|
||||
const key = username.toLowerCase();
|
||||
let route = this.playerRoutingStore.get(key);
|
||||
let instance: InstanceInfo | undefined;
|
||||
|
||||
if (!route) {
|
||||
const fallbackId = this.config.get("controller.player_proxy_default_instance_id");
|
||||
if (fallbackId !== null) {
|
||||
instance = this.instances.get(fallbackId);
|
||||
if (instance) {
|
||||
route = {
|
||||
name: username,
|
||||
instanceId: instance.id,
|
||||
hostId: instance.config.get("instance.assigned_host") ?? -1,
|
||||
gamePort: instance.gamePort ?? 0,
|
||||
timestampMs: Date.now(),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
instance = this._chooseFallbackInstance();
|
||||
if (instance) {
|
||||
route = {
|
||||
name: username,
|
||||
instanceId: instance.id,
|
||||
hostId: instance.config.get("instance.assigned_host") ?? -1,
|
||||
gamePort: instance.gamePort ?? 0,
|
||||
timestampMs: Date.now(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!route) {
|
||||
return null;
|
||||
}
|
||||
|
||||
instance = instance ?? this.instances.get(route.instanceId);
|
||||
if (!instance || instance.isDeleted) {
|
||||
this.playerRoutingStore.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
const hostId = instance.config.get("instance.assigned_host");
|
||||
if (hostId === null || route.hostId !== hostId) {
|
||||
this.playerRoutingStore.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
const host = this.hosts.get(hostId);
|
||||
if (!host || !host.connected) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const gamePort = instance.gamePort ?? route.gamePort;
|
||||
const publicAddress = host.publicAddress || host.remoteAddress;
|
||||
if (!gamePort || !publicAddress) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
hostId,
|
||||
instanceId: instance.id,
|
||||
publicAddress,
|
||||
gamePort,
|
||||
};
|
||||
}
|
||||
|
||||
private _chooseFallbackInstance() {
|
||||
for (const instance of this.instances.values()) {
|
||||
if (instance.status === "running" && !instance.isDeleted) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
for (const instance of this.instances.values()) {
|
||||
if (!instance.isDeleted && instance.config.get("instance.assigned_host") !== null) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get instance by ID for a request
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user