@edgegap/mcp 0.1.4 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/README.md +341 -230
- package/dist/client.js +31 -0
- package/dist/dockerfile.js +185 -0
- package/dist/index.js +6 -25
- package/dist/matchmaker.js +101 -0
- package/dist/serverconfig.js +255 -0
- package/dist/tools.js +541 -5
- package/package.json +3 -3
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a game server Dockerfile for a specific project.
|
|
3
|
+
*
|
|
4
|
+
* The validator in serverconfig.ts tells an agent what is wrong with a
|
|
5
|
+
* Dockerfile; this writes one that is right to begin with, for the project's
|
|
6
|
+
* actual build folder, binary name, ports and launch arguments, so the agent
|
|
7
|
+
* does not have to adapt a fixed template by trial and error.
|
|
8
|
+
*
|
|
9
|
+
* Unity and Unreal output follows the Dockerfiles Edgegap ships in its engine
|
|
10
|
+
* plugins. Godot and "other" follow the same conventions. Every template must
|
|
11
|
+
* pass validateServerConfig; newtools.mjs enforces that.
|
|
12
|
+
*/
|
|
13
|
+
import { protocolForNetcode } from './serverconfig.js';
|
|
14
|
+
// Every value below is interpolated into Dockerfile lines, and launch_args into
|
|
15
|
+
// a shell command for Unity, so each is restricted to characters that cannot
|
|
16
|
+
// start a new instruction, quote out of an argument, or run a subshell.
|
|
17
|
+
const PATH_RE = /^[A-Za-z0-9._\-/]+$/;
|
|
18
|
+
const FILE_RE = /^[A-Za-z0-9._-]+$/;
|
|
19
|
+
const ARG_RE = /^[A-Za-z0-9._\-=:/,+@]+$/;
|
|
20
|
+
const IMAGE_RE = /^[a-z0-9][a-z0-9._\-/:@]*$/;
|
|
21
|
+
const DEFAULTS = {
|
|
22
|
+
unity: { build_path: 'Builds/EdgegapServer', executable: 'ServerBuild' },
|
|
23
|
+
unreal: { build_path: '.', executable: 'StartServer.sh' },
|
|
24
|
+
godot: { build_path: 'build', executable: 'server.x86_64' },
|
|
25
|
+
other: { build_path: 'build' },
|
|
26
|
+
};
|
|
27
|
+
export class DockerfileInputError extends Error {
|
|
28
|
+
}
|
|
29
|
+
function exposeLines(ports) {
|
|
30
|
+
const lines = [];
|
|
31
|
+
for (const p of ports) {
|
|
32
|
+
const proto = p.protocol.toUpperCase();
|
|
33
|
+
if (proto === 'TCP/UDP')
|
|
34
|
+
lines.push(`EXPOSE ${p.port}/udp`, `EXPOSE ${p.port}/tcp`);
|
|
35
|
+
else
|
|
36
|
+
lines.push(`EXPOSE ${p.port}/${proto === 'UDP' ? 'udp' : 'tcp'}`);
|
|
37
|
+
}
|
|
38
|
+
return lines;
|
|
39
|
+
}
|
|
40
|
+
/** Linux packages every template installs: CA certificates, so the server can
|
|
41
|
+
* call HTTPS APIs (Edgegap's context API, backends, analytics). */
|
|
42
|
+
const APT = [
|
|
43
|
+
'RUN apt-get update && \\',
|
|
44
|
+
' apt-get install -y --no-install-recommends ca-certificates && \\',
|
|
45
|
+
' apt-get clean && rm -rf /var/lib/apt/lists/*',
|
|
46
|
+
];
|
|
47
|
+
/** Unprivileged user. Required for Unreal, good practice everywhere else. */
|
|
48
|
+
const USER_SETUP = 'RUN useradd -m -u 1000 server';
|
|
49
|
+
export function generateDockerfile(input) {
|
|
50
|
+
const engine = input.engine;
|
|
51
|
+
const defaults = DEFAULTS[engine];
|
|
52
|
+
const assumptions = [];
|
|
53
|
+
const buildPath = input.build_path ?? defaults.build_path;
|
|
54
|
+
const executable = input.executable ?? defaults.executable;
|
|
55
|
+
const base = input.base_image ?? 'ubuntu:22.04';
|
|
56
|
+
const args = input.launch_args ?? [];
|
|
57
|
+
if (!executable) {
|
|
58
|
+
throw new DockerfileInputError('executable is required for engine "other": the file name of the server binary or start script inside build_path.');
|
|
59
|
+
}
|
|
60
|
+
if (!PATH_RE.test(buildPath) || buildPath.split('/').includes('..') || buildPath.startsWith('/')) {
|
|
61
|
+
throw new DockerfileInputError(`build_path "${buildPath}" must be a relative path inside the Docker build context (letters, digits, ".", "_", "-", "/"; no "..").`);
|
|
62
|
+
}
|
|
63
|
+
if (!FILE_RE.test(executable)) {
|
|
64
|
+
throw new DockerfileInputError(`executable "${executable}" must be a plain file name (letters, digits, ".", "_", "-").`);
|
|
65
|
+
}
|
|
66
|
+
for (const a of args) {
|
|
67
|
+
if (!ARG_RE.test(a)) {
|
|
68
|
+
throw new DockerfileInputError(`launch argument "${a}" contains characters that are not allowed (spaces, quotes, $, ;, |, &, backticks). Pass each argument separately.`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!IMAGE_RE.test(base)) {
|
|
72
|
+
throw new DockerfileInputError(`base_image "${base}" is not a valid image reference.`);
|
|
73
|
+
}
|
|
74
|
+
// ------------------------------------------------------------- ports ----
|
|
75
|
+
let ports;
|
|
76
|
+
if (input.ports && input.ports.length > 0) {
|
|
77
|
+
ports = input.ports.map((p, i) => ({
|
|
78
|
+
port: p.port,
|
|
79
|
+
protocol: p.protocol.toUpperCase(),
|
|
80
|
+
name: p.name ?? (i === 0 ? 'gameport' : `port${i + 1}`),
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const protocol = (input.netcode && protocolForNetcode(input.netcode)) || 'UDP';
|
|
85
|
+
ports = [{ port: 7777, protocol, name: 'gameport' }];
|
|
86
|
+
assumptions.push(`Assumed the server listens on 7777/${protocol}${input.netcode ? ` (the protocol ${input.netcode} uses)` : ''}. ` +
|
|
87
|
+
'Confirm the port in the server\'s network/transport settings and pass ports if it differs.');
|
|
88
|
+
}
|
|
89
|
+
// --------------------------------------------------------- templates ----
|
|
90
|
+
const expose = exposeLines(ports);
|
|
91
|
+
let lines;
|
|
92
|
+
if (engine === 'unity') {
|
|
93
|
+
// Shell form so $UNITY_COMMANDLINE_ARGS (settable per deployment) expands.
|
|
94
|
+
// Edgegap's plugin template also prints `env` first; that is left out on
|
|
95
|
+
// purpose, since it writes every environment variable, hidden ones
|
|
96
|
+
// included, into the container logs.
|
|
97
|
+
const command = [`/root/build/${executable}`, '-batchmode', '-nographics', ...args, '$UNITY_COMMANDLINE_ARGS'].join(' ');
|
|
98
|
+
lines = [
|
|
99
|
+
`FROM ${base}`,
|
|
100
|
+
'',
|
|
101
|
+
'ARG DEBIAN_FRONTEND=noninteractive',
|
|
102
|
+
...APT,
|
|
103
|
+
'',
|
|
104
|
+
`# Linux Dedicated Server build folder (${executable}, *_Data, UnityPlayer.so).`,
|
|
105
|
+
`COPY ${buildPath} /root/build/`,
|
|
106
|
+
'WORKDIR /root/',
|
|
107
|
+
`RUN chmod +x /root/build/${executable}`,
|
|
108
|
+
'',
|
|
109
|
+
'# Documentation only: the port Edgegap exposes is the one on the app version.',
|
|
110
|
+
...expose,
|
|
111
|
+
'',
|
|
112
|
+
`CMD ["/bin/bash", "-c", "${command}"]`,
|
|
113
|
+
];
|
|
114
|
+
if (input.executable === undefined) {
|
|
115
|
+
assumptions.push('Assumed the server binary is named "ServerBuild". Unity names it after the build file you chose; check the build folder and pass executable if it differs.');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
else if (engine === 'unreal') {
|
|
119
|
+
const isScript = executable.endsWith('.sh');
|
|
120
|
+
lines = [
|
|
121
|
+
`FROM ${base}`,
|
|
122
|
+
'',
|
|
123
|
+
'ARG DEBIAN_FRONTEND=noninteractive',
|
|
124
|
+
...APT,
|
|
125
|
+
'',
|
|
126
|
+
'# Unreal refuses to start as root.',
|
|
127
|
+
USER_SETUP,
|
|
128
|
+
'WORKDIR /app',
|
|
129
|
+
`COPY --chown=server:server ${buildPath} /app/`,
|
|
130
|
+
isScript
|
|
131
|
+
? `# Scripts saved on Windows carry CRLF endings, which break the shebang.\nRUN sed -i 's/\\r$//' /app/${executable} && chmod +x /app/${executable}`
|
|
132
|
+
: `RUN chmod +x /app/${executable}`,
|
|
133
|
+
'USER server',
|
|
134
|
+
'',
|
|
135
|
+
'# Documentation only: the port Edgegap exposes is the one on the app version.',
|
|
136
|
+
...expose,
|
|
137
|
+
'',
|
|
138
|
+
`CMD ${JSON.stringify([`/app/${executable}`, ...args])}`,
|
|
139
|
+
];
|
|
140
|
+
if (input.executable === undefined) {
|
|
141
|
+
assumptions.push('Assumed StartServer.sh, which the Edgegap Unreal plugin generates. Without the plugin, use the <Project>Server.sh from the packaged LinuxServer folder.');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
// Godot and other engines: same shape as Unreal, minus the CRLF fix
|
|
146
|
+
// unless the entry point is a script.
|
|
147
|
+
const isScript = executable.endsWith('.sh');
|
|
148
|
+
const launch = engine === 'godot' ? [`/app/${executable}`, '--headless', ...args] : [`/app/${executable}`, ...args];
|
|
149
|
+
lines = [
|
|
150
|
+
`FROM ${base}`,
|
|
151
|
+
'',
|
|
152
|
+
'ARG DEBIAN_FRONTEND=noninteractive',
|
|
153
|
+
...APT,
|
|
154
|
+
'',
|
|
155
|
+
USER_SETUP,
|
|
156
|
+
'WORKDIR /app',
|
|
157
|
+
`COPY --chown=server:server ${buildPath} /app/`,
|
|
158
|
+
isScript
|
|
159
|
+
? `RUN sed -i 's/\\r$//' /app/${executable} && chmod +x /app/${executable}`
|
|
160
|
+
: `RUN chmod +x /app/${executable}`,
|
|
161
|
+
'USER server',
|
|
162
|
+
'',
|
|
163
|
+
'# Documentation only: the port Edgegap exposes is the one on the app version.',
|
|
164
|
+
...expose,
|
|
165
|
+
'',
|
|
166
|
+
`CMD ${JSON.stringify(launch)}`,
|
|
167
|
+
];
|
|
168
|
+
if (engine === 'godot') {
|
|
169
|
+
assumptions.push('Export with the Linux x86_64 preset. If the .pck is exported separately, keep it next to the binary in the build folder.');
|
|
170
|
+
if (input.executable === undefined) {
|
|
171
|
+
assumptions.push('Assumed the exported binary is named "server.x86_64"; pass executable if yours differs.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
assumptions.push(buildPath === '.'
|
|
176
|
+
? 'Run docker build from inside the server build folder (the folder itself is the build context).'
|
|
177
|
+
: `Run docker build from the folder that contains ${buildPath}.`);
|
|
178
|
+
return {
|
|
179
|
+
dockerfile: lines.join('\n') + '\n',
|
|
180
|
+
ports,
|
|
181
|
+
assumptions,
|
|
182
|
+
build_path: buildPath,
|
|
183
|
+
executable,
|
|
184
|
+
};
|
|
185
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* Edgegap MCP server.
|
|
4
4
|
*
|
|
5
|
-
* Exposes
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Exposes curated tools covering the path from "I have a headless server
|
|
6
|
+
* build" to "players are connected to it" — Dockerfile checks, registry push,
|
|
7
|
+
* deploy, relays for peer-to-peer games, and a matchmaker config — so a coding
|
|
8
|
+
* agent can take a developer through it without reading the API reference.
|
|
8
9
|
*
|
|
9
10
|
* Transport is stdio, which is what Claude Code, Cursor, Codex and VS Code use
|
|
10
11
|
* for locally configured servers.
|
|
@@ -13,7 +14,7 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
|
13
14
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
14
15
|
import { loadConfig, ConfigError } from './config.js';
|
|
15
16
|
import { EdgegapClient } from './client.js';
|
|
16
|
-
import { registerTools } from './tools.js';
|
|
17
|
+
import { registerTools, serverInstructions } from './tools.js';
|
|
17
18
|
import { TokenProvider, warnIfTokenInArgv } from './auth.js';
|
|
18
19
|
async function main() {
|
|
19
20
|
let config;
|
|
@@ -28,27 +29,7 @@ async function main() {
|
|
|
28
29
|
}
|
|
29
30
|
throw err;
|
|
30
31
|
}
|
|
31
|
-
const server = new McpServer({ name: 'edgegap', version: '0.
|
|
32
|
-
instructions: 'Deploy and operate game servers on Edgegap.\n\n' +
|
|
33
|
-
'Golden path for a first deployment:\n' +
|
|
34
|
-
'1. edgegap_list_apps to see what already exists\n' +
|
|
35
|
-
'2. edgegap_create_app if no suitable application is there\n' +
|
|
36
|
-
'3. edgegap_create_app_version to register the container image\n' +
|
|
37
|
-
'4. edgegap_deploy to start an instance near the players\n' +
|
|
38
|
-
'5. edgegap_wait_for_deployment to get the connection address\n' +
|
|
39
|
-
'6. edgegap_stop_deployment when finished\n\n' +
|
|
40
|
-
'Deployments cost money while running. Tag test deployments and stop ' +
|
|
41
|
-
'them before ending the task. If a deployment errors, read the container ' +
|
|
42
|
-
'logs before redeploying.\n\n' +
|
|
43
|
-
'Credentials: if no token was configured, the first tool call asks the ' +
|
|
44
|
-
'developer for one. That token is org-wide and cannot be scoped by ' +
|
|
45
|
-
'Edgegap, so it authorises far more than any single task needs. Treat it ' +
|
|
46
|
-
'as a supervised credential: do not use it for work the developer did ' +
|
|
47
|
-
'not ask for, do not enumerate or modify unrelated applications, and do ' +
|
|
48
|
-
'not repeat operations against it to explore what is possible. If the ' +
|
|
49
|
-
'developer declines to provide a token, stop and report which operation ' +
|
|
50
|
-
'needed it — do not retry.',
|
|
51
|
-
});
|
|
32
|
+
const server = new McpServer({ name: 'edgegap', version: '0.2.2' }, { instructions: serverInstructions('local') });
|
|
52
33
|
warnIfTokenInArgv();
|
|
53
34
|
const auth = new TokenProvider(config);
|
|
54
35
|
auth.attach(server.server);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds and checks a basic Edgegap matchmaker configuration.
|
|
3
|
+
*
|
|
4
|
+
* Edgegap has no public API for creating a matchmaker: it is created in the
|
|
5
|
+
* dashboard by uploading a JSON configuration. What an agent can usefully do is
|
|
6
|
+
* produce that JSON correctly the first time, pointed at an application version
|
|
7
|
+
* that actually exists, so the developer's only step is the upload.
|
|
8
|
+
*
|
|
9
|
+
* Format reference: https://docs.edgegap.com/learn/matchmaking/matchmaker-in-depth
|
|
10
|
+
*/
|
|
11
|
+
/** Config schema version the docs currently publish. Overridable per call. */
|
|
12
|
+
export const MATCHMAKER_CONFIG_VERSION = '3.3.2';
|
|
13
|
+
export const DASHBOARD_URL = 'https://app.edgegap.com';
|
|
14
|
+
const DURATION = /^\d+(ms|s|m|h)$/;
|
|
15
|
+
function seconds(d) {
|
|
16
|
+
const m = d.match(/^(\d+)(ms|s|m|h)$/);
|
|
17
|
+
if (!m)
|
|
18
|
+
return NaN;
|
|
19
|
+
const n = Number(m[1]);
|
|
20
|
+
return m[2] === 'ms' ? n / 1000 : m[2] === 's' ? n : m[2] === 'm' ? n * 60 : n * 3600;
|
|
21
|
+
}
|
|
22
|
+
export function buildMatchmakerConfig(input) {
|
|
23
|
+
const problems = [];
|
|
24
|
+
const cautions = [];
|
|
25
|
+
if (input.min_team_size > input.max_team_size) {
|
|
26
|
+
problems.push(`min_team_size (${input.min_team_size}) is greater than max_team_size (${input.max_team_size}).`);
|
|
27
|
+
}
|
|
28
|
+
if (!/^[a-z0-9][a-z0-9-_]*$/i.test(input.profile_name)) {
|
|
29
|
+
problems.push(`profile_name "${input.profile_name}" should be letters, digits, "-" or "_". Game clients send it on every ticket.`);
|
|
30
|
+
}
|
|
31
|
+
const expiration = input.ticket_expiration ?? '5m';
|
|
32
|
+
const removal = input.ticket_removal ?? '1m';
|
|
33
|
+
for (const [field, value] of [['ticket_expiration', expiration], ['ticket_removal', removal]]) {
|
|
34
|
+
if (!DURATION.test(value))
|
|
35
|
+
problems.push(`${field} "${value}" is not a duration like "30s", "5m" or "1h".`);
|
|
36
|
+
}
|
|
37
|
+
if (seconds(expiration) < 60) {
|
|
38
|
+
cautions.push(`ticket_expiration ${expiration} is short. Tickets must outlive the queue wait plus server start-up, or players are dropped before the match is ready.`);
|
|
39
|
+
}
|
|
40
|
+
const rules = {
|
|
41
|
+
match_size: {
|
|
42
|
+
type: 'player_count',
|
|
43
|
+
attributes: {
|
|
44
|
+
team_count: input.team_count,
|
|
45
|
+
min_team_size: input.min_team_size,
|
|
46
|
+
max_team_size: input.max_team_size,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
const useLatency = input.max_latency_ms !== undefined || input.latency_difference_ms !== undefined;
|
|
51
|
+
if (useLatency) {
|
|
52
|
+
rules.beacons = {
|
|
53
|
+
type: 'latencies',
|
|
54
|
+
attributes: {
|
|
55
|
+
difference: input.latency_difference_ms ?? 100,
|
|
56
|
+
max_latency: input.max_latency_ms ?? 200,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const expansions = {};
|
|
61
|
+
let lastAfter = 0;
|
|
62
|
+
for (const e of [...(input.expansions ?? [])].sort((a, b) => a.after_seconds - b.after_seconds)) {
|
|
63
|
+
const step = {};
|
|
64
|
+
if (e.min_team_size !== undefined) {
|
|
65
|
+
if (e.min_team_size > input.max_team_size) {
|
|
66
|
+
problems.push(`expansion at ${e.after_seconds}s sets min_team_size ${e.min_team_size} above max_team_size ${input.max_team_size}.`);
|
|
67
|
+
}
|
|
68
|
+
step.match_size = { min_team_size: e.min_team_size };
|
|
69
|
+
}
|
|
70
|
+
if (e.max_latency_ms !== undefined) {
|
|
71
|
+
if (!useLatency) {
|
|
72
|
+
problems.push(`expansion at ${e.after_seconds}s relaxes max_latency_ms, but no latency rule is configured. Set max_latency_ms on the profile too.`);
|
|
73
|
+
}
|
|
74
|
+
step.beacons = { max_latency: e.max_latency_ms };
|
|
75
|
+
}
|
|
76
|
+
if (Object.keys(step).length === 0)
|
|
77
|
+
continue;
|
|
78
|
+
if (e.after_seconds === lastAfter)
|
|
79
|
+
problems.push(`two expansions share after_seconds ${e.after_seconds}.`);
|
|
80
|
+
lastAfter = e.after_seconds;
|
|
81
|
+
expansions[String(e.after_seconds)] = step;
|
|
82
|
+
}
|
|
83
|
+
if (input.team_count * input.min_team_size === 1) {
|
|
84
|
+
cautions.push('A match of one player starts a server per ticket. Fine for testing, costly in production.');
|
|
85
|
+
}
|
|
86
|
+
const config = {
|
|
87
|
+
version: input.config_version ?? MATCHMAKER_CONFIG_VERSION,
|
|
88
|
+
inspect: input.inspect ?? true,
|
|
89
|
+
max_deployment_retry_count: 3,
|
|
90
|
+
profiles: {
|
|
91
|
+
[input.profile_name]: {
|
|
92
|
+
ticket_expiration_period: expiration,
|
|
93
|
+
ticket_removal_period: removal,
|
|
94
|
+
group_inactivity_removal_period: '5m',
|
|
95
|
+
application: { name: input.application, version: input.version },
|
|
96
|
+
rules: { initial: rules, expansions },
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
return { config, problems, cautions };
|
|
101
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static checks for a game server Dockerfile and its Edgegap port config.
|
|
3
|
+
*
|
|
4
|
+
* This is the step an agent gets wrong most often and learns about latest:
|
|
5
|
+
* a Dockerfile that builds and runs locally, then fails on Edgegap because the
|
|
6
|
+
* image is arm64, the server runs as root under Unreal, the port the server
|
|
7
|
+
* listens on is not the port the version exposes, or the protocol is TCP where
|
|
8
|
+
* the transport speaks UDP. Every one of those costs a build, a push, a version
|
|
9
|
+
* and a deployment to discover. Checking the text first costs nothing.
|
|
10
|
+
*
|
|
11
|
+
* No network, no Docker daemon. The rules come from the Dockerfiles Edgegap
|
|
12
|
+
* ships in its Unity and Unreal plugins, and from the constraints the API
|
|
13
|
+
* enforces on app versions.
|
|
14
|
+
*/
|
|
15
|
+
export const PROTOCOLS = ['UDP', 'TCP', 'TCP/UDP', 'HTTP', 'HTTPS', 'WS', 'WSS'];
|
|
16
|
+
/** Transport → protocol it needs on the wire. Keys are lowercase. */
|
|
17
|
+
const NETCODE_PROTOCOL = {
|
|
18
|
+
'mirror-kcp': 'UDP',
|
|
19
|
+
kcp: 'UDP',
|
|
20
|
+
'mirror-telepathy': 'TCP',
|
|
21
|
+
telepathy: 'TCP',
|
|
22
|
+
'mirror-simpleweb': 'WS',
|
|
23
|
+
simpleweb: 'WS',
|
|
24
|
+
websocket: 'WS',
|
|
25
|
+
'fishnet-tugboat': 'UDP',
|
|
26
|
+
tugboat: 'UDP',
|
|
27
|
+
'netcode-for-gameobjects': 'UDP',
|
|
28
|
+
ngo: 'UDP',
|
|
29
|
+
'unity-transport': 'UDP',
|
|
30
|
+
utp: 'UDP',
|
|
31
|
+
'photon-fusion': 'UDP',
|
|
32
|
+
litenetlib: 'UDP',
|
|
33
|
+
enet: 'UDP',
|
|
34
|
+
'unreal-netdriver': 'UDP',
|
|
35
|
+
'godot-enet': 'UDP',
|
|
36
|
+
'godot-websocket': 'WS',
|
|
37
|
+
};
|
|
38
|
+
export const NETCODE_NAMES = Object.keys(NETCODE_PROTOCOL);
|
|
39
|
+
/** Protocol a netcode transport needs on the wire, or undefined if unknown. */
|
|
40
|
+
export function protocolForNetcode(netcode) {
|
|
41
|
+
return NETCODE_PROTOCOL[netcode.toLowerCase()];
|
|
42
|
+
}
|
|
43
|
+
/** Joins backslash continuations and drops comments, keeping the start line. */
|
|
44
|
+
function parseDockerfile(text) {
|
|
45
|
+
const out = [];
|
|
46
|
+
const lines = text.replace(/\r\n?/g, '\n').split('\n');
|
|
47
|
+
let buf = '';
|
|
48
|
+
let start = 0;
|
|
49
|
+
for (let i = 0; i < lines.length; i++) {
|
|
50
|
+
const raw = lines[i];
|
|
51
|
+
const trimmed = raw.trim();
|
|
52
|
+
if (!buf && (trimmed === '' || trimmed.startsWith('#')))
|
|
53
|
+
continue;
|
|
54
|
+
if (!buf)
|
|
55
|
+
start = i + 1;
|
|
56
|
+
if (trimmed.endsWith('\\')) {
|
|
57
|
+
buf += trimmed.slice(0, -1) + ' ';
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
buf += trimmed;
|
|
61
|
+
const m = buf.match(/^(\w+)\s*(.*)$/s);
|
|
62
|
+
if (m)
|
|
63
|
+
out.push({ line: start, op: m[1].toUpperCase(), args: m[2].trim() });
|
|
64
|
+
buf = '';
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function detectEngine(text) {
|
|
69
|
+
if (/-batchmode|-nographics|UNITY_|UnityPlayer|\.x86_64\b/i.test(text))
|
|
70
|
+
return 'unity';
|
|
71
|
+
if (/StartServer\.sh|UnrealServer|Binaries\/Linux|LinuxServer|Server\.sh\b/i.test(text))
|
|
72
|
+
return 'unreal';
|
|
73
|
+
if (/godot|--headless|\.pck\b/i.test(text))
|
|
74
|
+
return 'godot';
|
|
75
|
+
return 'other';
|
|
76
|
+
}
|
|
77
|
+
function parseExpose(args) {
|
|
78
|
+
return args
|
|
79
|
+
.split(/\s+/)
|
|
80
|
+
.filter(Boolean)
|
|
81
|
+
.map((tok) => {
|
|
82
|
+
const [p, proto] = tok.split('/');
|
|
83
|
+
return { port: Number.parseInt(p, 10), protocol: (proto ?? 'tcp').toUpperCase() };
|
|
84
|
+
})
|
|
85
|
+
.filter((e) => Number.isFinite(e.port));
|
|
86
|
+
}
|
|
87
|
+
/** Does an Edgegap port protocol carry this EXPOSE protocol (tcp|udp)? */
|
|
88
|
+
function carries(edgegapProtocol, exposeProtocol) {
|
|
89
|
+
const p = edgegapProtocol.toUpperCase();
|
|
90
|
+
if (p === 'TCP/UDP')
|
|
91
|
+
return true;
|
|
92
|
+
if (exposeProtocol === 'UDP')
|
|
93
|
+
return p === 'UDP';
|
|
94
|
+
return p !== 'UDP'; // TCP, HTTP, HTTPS, WS, WSS all ride on TCP
|
|
95
|
+
}
|
|
96
|
+
export function validateServerConfig(input) {
|
|
97
|
+
const findings = [];
|
|
98
|
+
const err = (code, message, fix, line) => findings.push({ severity: 'error', code, message, fix, line });
|
|
99
|
+
const warn = (code, message, fix, line) => findings.push({ severity: 'warning', code, message, fix, line });
|
|
100
|
+
let engine = input.engine ?? 'other';
|
|
101
|
+
const exposed = [];
|
|
102
|
+
// ------------------------------------------------------------ Dockerfile --
|
|
103
|
+
if (input.dockerfile !== undefined) {
|
|
104
|
+
const text = input.dockerfile;
|
|
105
|
+
const ins = parseDockerfile(text);
|
|
106
|
+
if (!input.engine)
|
|
107
|
+
engine = detectEngine(text);
|
|
108
|
+
const froms = ins.filter((i) => i.op === 'FROM');
|
|
109
|
+
const finalFrom = froms[froms.length - 1];
|
|
110
|
+
if (!finalFrom) {
|
|
111
|
+
err('no-from', 'The Dockerfile has no FROM instruction.', 'Start from a Linux base image, e.g. "FROM ubuntu:22.04".');
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
const platform = finalFrom.args.match(/--platform=(\S+)/)?.[1];
|
|
115
|
+
const image = finalFrom.args.replace(/--\S+/g, '').trim().split(/\s+/)[0] ?? '';
|
|
116
|
+
if (platform && platform.toLowerCase() !== 'linux/amd64' && !platform.includes('$')) {
|
|
117
|
+
err('wrong-platform', `Final stage is pinned to ${platform}. Edgegap runs linux/amd64 only.`, 'Use "FROM --platform=linux/amd64 ..." or drop the flag and build with "docker build --platform linux/amd64".', finalFrom.line);
|
|
118
|
+
}
|
|
119
|
+
if (/(^|\/)(arm64v8|arm32v7|arm32v6)\//i.test(image) || /[-:]arm64\b/i.test(image)) {
|
|
120
|
+
err('arm-base-image', `Base image "${image}" is an ARM image. Edgegap runs linux/amd64 only.`, 'Use the multi-arch or amd64 variant of the image.', finalFrom.line);
|
|
121
|
+
}
|
|
122
|
+
if (/windows|nanoserver|servercore/i.test(image)) {
|
|
123
|
+
err('windows-base-image', `Base image "${image}" is a Windows container. Edgegap runs Linux containers.`, 'Build a Linux dedicated server and use a Linux base such as ubuntu:22.04.', finalFrom.line);
|
|
124
|
+
}
|
|
125
|
+
if (image && image !== 'scratch' && !image.includes('$') && (!/[:@]/.test(image.split('/').pop() ?? '') || image.endsWith(':latest'))) {
|
|
126
|
+
warn('unpinned-base', `Base image "${image}" is unpinned or uses "latest", so rebuilds are not reproducible.`, 'Pin a version, e.g. ubuntu:22.04.', finalFrom.line);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const run = [...ins].reverse().find((i) => i.op === 'CMD' || i.op === 'ENTRYPOINT');
|
|
130
|
+
if (!run) {
|
|
131
|
+
err('no-cmd', 'No CMD or ENTRYPOINT: the container will exit as soon as it starts and the deployment will error.', 'Add a CMD that launches the server binary in the foreground.');
|
|
132
|
+
}
|
|
133
|
+
const launch = ins.filter((i) => i.op === 'CMD' || i.op === 'ENTRYPOINT').map((i) => i.args).join(' ');
|
|
134
|
+
if (/\.exe\b/i.test(ins.filter((i) => ['COPY', 'ADD', 'CMD', 'ENTRYPOINT'].includes(i.op)).map((i) => i.args).join(' '))) {
|
|
135
|
+
err('windows-binary', 'The image copies or launches a .exe. Edgegap runs Linux containers, so a Windows build cannot start.', 'Build the Linux dedicated server target (Unity: Linux Dedicated Server; Unreal: LinuxServer).');
|
|
136
|
+
}
|
|
137
|
+
if (/\b(127\.0\.0\.1|localhost)\b/.test(launch)) {
|
|
138
|
+
warn('loopback-bind', 'The launch command mentions 127.0.0.1/localhost. A server bound to loopback is unreachable from outside the container.', 'Bind to 0.0.0.0 (or omit the address so the server listens on all interfaces).', run?.line);
|
|
139
|
+
}
|
|
140
|
+
for (const e of ins.filter((i) => i.op === 'EXPOSE')) {
|
|
141
|
+
for (const p of parseExpose(e.args)) {
|
|
142
|
+
exposed.push({ ...p, line: e.line });
|
|
143
|
+
if (p.port === 22) {
|
|
144
|
+
warn('ssh-exposed', 'Port 22 (SSH) is exposed. Edgegap advises never exposing SSH in production images.', 'Remove EXPOSE 22 and the sshd start-up from the production image.', e.line);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const users = ins.filter((i) => i.op === 'USER');
|
|
149
|
+
const finalUser = users[users.length - 1]?.args.split(':')[0];
|
|
150
|
+
const runsAsRoot = !finalUser || finalUser === 'root' || finalUser === '0';
|
|
151
|
+
if (engine === 'unity') {
|
|
152
|
+
if (run && !/-batchmode/.test(launch)) {
|
|
153
|
+
warn('unity-no-batchmode', 'Unity server launched without -batchmode.', 'Append "-batchmode -nographics" to the server command.', run.line);
|
|
154
|
+
}
|
|
155
|
+
if (run && !/-nographics/.test(launch)) {
|
|
156
|
+
warn('unity-no-nographics', 'Unity server launched without -nographics; it may try to initialise a GPU the container does not have.', 'Append "-nographics" to the server command.', run.line);
|
|
157
|
+
}
|
|
158
|
+
if (!/chmod\s+(\+x|[0-7]*[157][0-7]{0,2})/.test(text)) {
|
|
159
|
+
warn('no-chmod', 'No "chmod +x" on the server binary. Builds copied from Windows often lose the executable bit, giving "permission denied" at start.', 'Add "RUN chmod +x /path/to/ServerBuild".');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (engine === 'unreal') {
|
|
163
|
+
if (runsAsRoot) {
|
|
164
|
+
err('unreal-root', 'The container runs as root. Unreal Engine servers refuse to start as root and exit immediately.', 'Create a user (useradd ... -u 1000 m) and add "USER m" before CMD.', users[users.length - 1]?.line);
|
|
165
|
+
}
|
|
166
|
+
if (/\.sh\b/.test(launch) && !/sed\s+-i\s+['"]?s\/\\r\$\/\/|dos2unix/.test(text)) {
|
|
167
|
+
warn('crlf-script', 'The start script is not normalised to LF line endings. Scripts saved on Windows fail with "bad interpreter" or "not found".', "Add \"RUN sed -i 's/\\r$//' /app/StartServer.sh\" after the COPY.");
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (engine === 'godot' && run && !/--headless/.test(launch)) {
|
|
171
|
+
warn('godot-no-headless', 'Godot server launched without --headless.', 'Add "--headless" to the server command.', run.line);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
// ----------------------------------------------------------------- Ports --
|
|
175
|
+
const ports = input.ports;
|
|
176
|
+
if (ports !== undefined) {
|
|
177
|
+
if (ports.length === 0) {
|
|
178
|
+
err('no-ports', 'No ports configured. Players cannot connect to a version with no ports.', 'Add the port the server listens on, e.g. {port: 7777, protocol: "UDP"}.');
|
|
179
|
+
}
|
|
180
|
+
const seenPorts = new Map();
|
|
181
|
+
const seenNames = new Set();
|
|
182
|
+
ports.forEach((p, idx) => {
|
|
183
|
+
const proto = p.protocol.toUpperCase();
|
|
184
|
+
if (!PROTOCOLS.includes(proto)) {
|
|
185
|
+
err('bad-protocol', `ports[${idx}].protocol "${p.protocol}" is not one Edgegap accepts.`, `Use one of: ${PROTOCOLS.join(', ')}.`);
|
|
186
|
+
}
|
|
187
|
+
if (!Number.isInteger(p.port) || p.port < 1 || p.port > 59999) {
|
|
188
|
+
err('port-range', `ports[${idx}].port ${p.port} is outside 1-59999.`, 'Use the internal port the server listens on inside the container.');
|
|
189
|
+
}
|
|
190
|
+
if (seenPorts.has(p.port)) {
|
|
191
|
+
err('duplicate-port', `Port ${p.port} is listed twice.`, 'Use one entry with protocol "TCP/UDP" if the server needs both.');
|
|
192
|
+
}
|
|
193
|
+
seenPorts.set(p.port, idx);
|
|
194
|
+
const name = p.name ?? 'gameport';
|
|
195
|
+
if (seenNames.has(name)) {
|
|
196
|
+
err('duplicate-port-name', `Port name "${name}" is used twice. Deployment ports are keyed by name, so one would hide the other.`, 'Give every port a distinct name, e.g. "gameport" and "webport".');
|
|
197
|
+
}
|
|
198
|
+
seenNames.add(name);
|
|
199
|
+
});
|
|
200
|
+
if (input.netcode) {
|
|
201
|
+
const expected = NETCODE_PROTOCOL[input.netcode.toLowerCase()];
|
|
202
|
+
if (!expected) {
|
|
203
|
+
warn('unknown-netcode', `Netcode "${input.netcode}" is not one this check knows.`, `Known values: ${NETCODE_NAMES.join(', ')}.`);
|
|
204
|
+
}
|
|
205
|
+
else if (ports.length > 0 && !ports.some((p) => {
|
|
206
|
+
const proto = p.protocol.toUpperCase();
|
|
207
|
+
return proto === expected || (proto === 'TCP/UDP' && (expected === 'UDP' || expected === 'TCP')) || (expected === 'WS' && proto === 'WSS');
|
|
208
|
+
})) {
|
|
209
|
+
err('netcode-protocol', `${input.netcode} speaks ${expected}, but no configured port uses ${expected}. Clients will time out connecting.`, `Set the game port's protocol to ${expected}.`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Cross-check EXPOSE against the version ports. EXPOSE is documentation,
|
|
213
|
+
// so a mismatch is not fatal on its own, but it is the best signal we have
|
|
214
|
+
// for "the server listens on a different port than the version exposes".
|
|
215
|
+
if (exposed.length > 0) {
|
|
216
|
+
for (const p of ports) {
|
|
217
|
+
const match = exposed.find((e) => e.port === p.port);
|
|
218
|
+
if (!match) {
|
|
219
|
+
warn('port-not-exposed', `Port ${p.port} is configured on the version but not EXPOSEd in the Dockerfile, which suggests the server listens elsewhere.`, `Confirm the server listens on ${p.port}, and add "EXPOSE ${p.port}/${p.protocol.toUpperCase() === 'UDP' ? 'udp' : 'tcp'}".`);
|
|
220
|
+
}
|
|
221
|
+
else if (!carries(p.protocol, match.protocol)) {
|
|
222
|
+
warn('protocol-mismatch', `Dockerfile exposes ${p.port}/${match.protocol.toLowerCase()} but the version configures it as ${p.protocol}.`, 'Make the two agree; the version setting is the one Edgegap uses.', match.line);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
for (const e of exposed) {
|
|
226
|
+
if (e.port !== 22 && !ports.some((p) => p.port === e.port)) {
|
|
227
|
+
warn('exposed-not-configured', `Dockerfile exposes ${e.port}/${e.protocol.toLowerCase()} but no version port maps it, so players cannot reach it.`, `Add {port: ${e.port}, protocol: "${e.protocol}"} to the version ports if clients need it.`, e.line);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// ------------------------------------------------------------- Resources --
|
|
233
|
+
if (input.cpu_units !== undefined && input.cpu_units < 256) {
|
|
234
|
+
err('cpu-too-low', `cpu_units ${input.cpu_units} is below the 256 minimum.`, 'Use at least 256 (1024 = 1 vCPU).');
|
|
235
|
+
}
|
|
236
|
+
if (input.memory_mb !== undefined && input.memory_mb < 256) {
|
|
237
|
+
err('memory-too-low', `memory_mb ${input.memory_mb} is below the 256 minimum.`, 'Use at least 256.');
|
|
238
|
+
}
|
|
239
|
+
if (input.cpu_units !== undefined && input.memory_mb !== undefined && input.memory_mb > input.cpu_units * 2) {
|
|
240
|
+
err('memory-ratio', `memory_mb (${input.memory_mb}) exceeds twice cpu_units (${input.cpu_units}). Edgegap rejects this.`, `Lower memory_mb to ${input.cpu_units * 2} or raise cpu_units.`);
|
|
241
|
+
}
|
|
242
|
+
// ----------------------------------------------------------------- Image --
|
|
243
|
+
if (input.docker_tag !== undefined && (input.docker_tag === '' || input.docker_tag === 'latest')) {
|
|
244
|
+
err('latest-tag', `docker_tag "${input.docker_tag || '(empty)'}" is not reproducible, and Edgegap caches images by tag, so a re-pushed "latest" can deploy a stale build.`, 'Tag every build uniquely, e.g. a build ID or timestamp.');
|
|
245
|
+
}
|
|
246
|
+
if (input.docker_repository?.includes('registry.edgegap.com') && input.docker_image && !input.docker_image.includes('/')) {
|
|
247
|
+
err('missing-project', `docker_image "${input.docker_image}" has no project prefix. Images on registry.edgegap.com live under your project.`, 'Use "<project>/<image>"; edgegap_get_registry_credentials returns the project name.');
|
|
248
|
+
}
|
|
249
|
+
if (input.docker_repository && /^https?:\/\//.test(input.docker_repository)) {
|
|
250
|
+
err('repository-scheme', `docker_repository "${input.docker_repository}" includes a URL scheme.`, 'Use the bare host, e.g. "registry.edgegap.com".');
|
|
251
|
+
}
|
|
252
|
+
const errors = findings.filter((f) => f.severity === 'error');
|
|
253
|
+
const warnings = findings.filter((f) => f.severity === 'warning');
|
|
254
|
+
return { engine, errors, warnings };
|
|
255
|
+
}
|