mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 16:13:12 +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
@@ -694,7 +694,7 @@ export default class Host extends lib.Link {
|
||||
instanceId,
|
||||
instanceConnection ? instanceConnection.instance.status : "stopped",
|
||||
instanceConnection
|
||||
? instanceConnection.instance.config.get("factorio.proxy_port") ?? instanceConnection.instance.server.gamePort
|
||||
? instanceConnection.instance.server.gamePort
|
||||
: this.publicGamePort(instanceId, instanceInfo),
|
||||
instanceConnection
|
||||
? instanceConnection.instance.server.version
|
||||
@@ -749,17 +749,13 @@ export default class Host extends lib.Link {
|
||||
/**
|
||||
* Retrieved public port for clients to connect to for the given instance.
|
||||
* @param instanceId - ID of the instance to get the port for.
|
||||
* @returns Proxy port if configured, otherwise the game port.
|
||||
* @returns Game port for the instance.
|
||||
*/
|
||||
publicGamePort(instanceId: number, instanceInfo?: { config: lib.InstanceConfig }) {
|
||||
instanceInfo = instanceInfo ?? this.discoveredInstanceInfos.get(instanceId);
|
||||
if (!instanceInfo) {
|
||||
return undefined;
|
||||
}
|
||||
const proxyPort = instanceInfo.config.get("factorio.proxy_port");
|
||||
if (proxyPort !== null && proxyPort !== undefined) {
|
||||
return proxyPort;
|
||||
}
|
||||
return (
|
||||
instanceInfo.config.get("factorio.game_port")
|
||||
?? instanceInfo.config.get("factorio.host_assigned_game_port")
|
||||
@@ -859,66 +855,6 @@ export default class Host extends lib.Link {
|
||||
return promise;
|
||||
}
|
||||
|
||||
private async _ensureFactorioProxy(instanceId: number, instanceInfo: { config: lib.InstanceConfig }) {
|
||||
const proxyPort = instanceInfo.config.get("factorio.proxy_port");
|
||||
const existingProxy = this.factorioProxies.get(instanceId);
|
||||
if (proxyPort === null) {
|
||||
if (existingProxy) {
|
||||
await existingProxy.stop();
|
||||
this.factorioProxies.delete(instanceId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const beforeAssigned = instanceInfo.config.get("factorio.host_assigned_game_port");
|
||||
const gamePort = this.ensureGamePortAssigned(instanceId, instanceInfo);
|
||||
if (beforeAssigned === null && instanceInfo.config.get("factorio.host_assigned_game_port") !== null) {
|
||||
await instanceInfo.config.save();
|
||||
}
|
||||
|
||||
if (proxyPort === gamePort) {
|
||||
logger.warn(
|
||||
`Proxy port ${proxyPort} matches game port for instance, proxy disabled.`,
|
||||
this.instanceLogMeta(instanceId, instanceInfo),
|
||||
);
|
||||
if (existingProxy) {
|
||||
await existingProxy.stop();
|
||||
this.factorioProxies.delete(instanceId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (existingProxy) {
|
||||
await existingProxy.updatePorts(proxyPort, gamePort);
|
||||
return;
|
||||
}
|
||||
|
||||
const proxyLogger = logger.child(this.instanceLogMeta(instanceId, instanceInfo));
|
||||
const proxy = new FactorioProxy(this, instanceId, proxyPort, gamePort, proxyLogger);
|
||||
this.factorioProxies.set(instanceId, proxy);
|
||||
try {
|
||||
await proxy.start();
|
||||
} catch (err: any) {
|
||||
logger.error(
|
||||
`Failed to start proxy on UDP ${proxyPort}: ${err.message ?? err}`,
|
||||
this.instanceLogMeta(instanceId, instanceInfo),
|
||||
);
|
||||
this.factorioProxies.delete(instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
private async _syncFactorioProxies() {
|
||||
for (const [instanceId] of this.factorioProxies) {
|
||||
if (!this.instanceInfos.has(instanceId)) {
|
||||
await this.factorioProxies.get(instanceId)!.stop();
|
||||
this.factorioProxies.delete(instanceId);
|
||||
}
|
||||
}
|
||||
for (const [instanceId, instanceInfo] of this.instanceInfos) {
|
||||
await this._ensureFactorioProxy(instanceId, instanceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize and connect an unloaded instance
|
||||
*
|
||||
@@ -1287,7 +1223,7 @@ export default class Host extends lib.Link {
|
||||
instanceInfo.config.toRemote("controller"),
|
||||
instanceConnection ? instanceConnection.instance.status : "stopped",
|
||||
instanceConnection
|
||||
? instanceConnection.instance.config.get("factorio.proxy_port") ?? instanceConnection.instance.server.gamePort
|
||||
? instanceConnection.instance.server.gamePort
|
||||
: this.publicGamePort(instanceId, instanceInfo),
|
||||
instanceConnection ? instanceConnection.instance.server.version : undefined,
|
||||
));
|
||||
@@ -1317,7 +1253,6 @@ export default class Host extends lib.Link {
|
||||
}
|
||||
}
|
||||
|
||||
await this._syncFactorioProxies();
|
||||
}
|
||||
|
||||
async prepareDisconnect() {
|
||||
|
||||
@@ -398,6 +398,13 @@ export default class Instance extends lib.Link {
|
||||
stats,
|
||||
};
|
||||
this.sendTo("controller", new lib.InstancePlayerUpdateEvent("join", name, stats));
|
||||
const hostId = this.config.get("instance.assigned_host");
|
||||
if (hostId !== null) {
|
||||
this.sendTo(
|
||||
"controller",
|
||||
new lib.InstancePlayerRouteUpdateEvent(name, this.id, hostId, this.server.gamePort, Date.now()),
|
||||
);
|
||||
}
|
||||
lib.invokeHook(this.plugins, "onPlayerEvent", event);
|
||||
}
|
||||
|
||||
@@ -548,7 +555,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
|
||||
notifyStatus(status: Instance["_status"]) {
|
||||
this._status = status;
|
||||
const publicPort = this.config.get("factorio.proxy_port") ?? this.server.gamePort;
|
||||
const publicPort = this.server.gamePort;
|
||||
this.sendTo(
|
||||
"controller",
|
||||
new lib.InstanceStatusChangedEvent(
|
||||
|
||||
@@ -96,6 +96,30 @@ async function findVersion(factorioDir: string, targetVersion: lib.TargetVersion
|
||||
);
|
||||
}
|
||||
|
||||
const binEntries = ["factorio-run", "factorio-run.exe", "factorio", "factorio.exe"];
|
||||
const binChecks = await Promise.all(
|
||||
binEntries.map(entry => fs.pathExists(path.join(factorioDir, entry)))
|
||||
);
|
||||
if (binChecks.some(Boolean)) {
|
||||
const dataCandidates = [
|
||||
path.resolve(factorioDir, "..", "..", "data"),
|
||||
path.resolve(factorioDir, "..", "data"),
|
||||
];
|
||||
for (let dataDir of dataCandidates) {
|
||||
let version = await getVersion(path.join(dataDir, "changelog.txt"));
|
||||
if (version === null) {
|
||||
continue;
|
||||
}
|
||||
if (version === targetVersion || targetVersion === "latest") {
|
||||
return [dataDir, version];
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Factorio version ${targetVersion} was requested, but install directory contains ${version}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let versions = new Map();
|
||||
for (let entry of await fs.readdir(factorioDir, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory()) {
|
||||
@@ -1353,7 +1377,40 @@ export class FactorioServer extends events.EventEmitter<FactorioServerEvents> {
|
||||
if (process.platform === "darwin") {
|
||||
return this.dataPath("..", "MacOS", "factorio");
|
||||
}
|
||||
return this.dataPath("..", "bin", "x64", "factorio");
|
||||
const binDir = this.dataPath("..", "bin", "x64");
|
||||
const runPath = path.join(binDir, "factorio-run");
|
||||
if (fs.existsSync(runPath)) {
|
||||
return runPath;
|
||||
}
|
||||
const runPathExe = `${runPath}.exe`;
|
||||
if (fs.existsSync(runPathExe)) {
|
||||
return runPathExe;
|
||||
}
|
||||
const factorioPath = path.join(binDir, "factorio");
|
||||
if (fs.existsSync(factorioPath)) {
|
||||
return factorioPath;
|
||||
}
|
||||
const factorioPathExe = `${factorioPath}.exe`;
|
||||
if (fs.existsSync(factorioPathExe)) {
|
||||
return factorioPathExe;
|
||||
}
|
||||
const directRunPath = path.join(this._factorioDir, "factorio-run");
|
||||
if (fs.existsSync(directRunPath)) {
|
||||
return directRunPath;
|
||||
}
|
||||
const directRunPathExe = `${directRunPath}.exe`;
|
||||
if (fs.existsSync(directRunPathExe)) {
|
||||
return directRunPathExe;
|
||||
}
|
||||
const directFactorioPath = path.join(this._factorioDir, "factorio");
|
||||
if (fs.existsSync(directFactorioPath)) {
|
||||
return directFactorioPath;
|
||||
}
|
||||
const directFactorioPathExe = `${directFactorioPath}.exe`;
|
||||
if (fs.existsSync(directFactorioPathExe)) {
|
||||
return directFactorioPathExe;
|
||||
}
|
||||
return factorioPath;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user