@fyresmith/hive-server 1.0.1-3
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/README.md +143 -0
- package/bin/hive.js +4 -0
- package/cli/checks.js +28 -0
- package/cli/config.js +33 -0
- package/cli/constants.js +45 -0
- package/cli/env-file.js +141 -0
- package/cli/errors.js +11 -0
- package/cli/exec.js +12 -0
- package/cli/main.js +730 -0
- package/cli/output.js +21 -0
- package/cli/service.js +360 -0
- package/cli/tunnel.js +238 -0
- package/index.js +129 -0
- package/lib/auth.js +50 -0
- package/lib/socketHandler.js +226 -0
- package/lib/vaultManager.js +258 -0
- package/lib/yjsServer.js +277 -0
- package/package.json +52 -0
- package/routes/auth.js +99 -0
package/cli/main.js
ADDED
|
@@ -0,0 +1,730 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { access } from 'fs/promises';
|
|
3
|
+
import { constants as fsConstants } from 'fs';
|
|
4
|
+
import process from 'process';
|
|
5
|
+
import { Command, CommanderError } from 'commander';
|
|
6
|
+
import prompts from 'prompts';
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_ENV_FILE,
|
|
9
|
+
DEFAULT_CLOUDFLARED_CERT,
|
|
10
|
+
DEFAULT_CLOUDFLARED_CONFIG,
|
|
11
|
+
DEFAULT_TUNNEL_NAME,
|
|
12
|
+
EXIT,
|
|
13
|
+
HIVE_CONFIG_FILE,
|
|
14
|
+
LEGACY_ENV_FILE,
|
|
15
|
+
} from './constants.js';
|
|
16
|
+
import { CliError } from './errors.js';
|
|
17
|
+
import { section, info, success, warn, fail } from './output.js';
|
|
18
|
+
import { loadHiveConfig, updateHiveConfig } from './config.js';
|
|
19
|
+
import {
|
|
20
|
+
inferDomainFromRedirect,
|
|
21
|
+
loadEnvFile,
|
|
22
|
+
normalizeEnv,
|
|
23
|
+
promptForEnv,
|
|
24
|
+
redactEnv,
|
|
25
|
+
validateEnvValues,
|
|
26
|
+
writeEnvFile,
|
|
27
|
+
} from './env-file.js';
|
|
28
|
+
import { isPortAvailable, pathExists, validateDomain } from './checks.js';
|
|
29
|
+
import { run } from './exec.js';
|
|
30
|
+
import {
|
|
31
|
+
cloudflaredServiceStatus,
|
|
32
|
+
installCloudflaredService,
|
|
33
|
+
runTunnelForeground,
|
|
34
|
+
setupTunnel,
|
|
35
|
+
tunnelStatus,
|
|
36
|
+
getCloudflaredPath,
|
|
37
|
+
} from './tunnel.js';
|
|
38
|
+
import {
|
|
39
|
+
getServiceDefaults,
|
|
40
|
+
getHiveServiceStatus,
|
|
41
|
+
installHiveService,
|
|
42
|
+
restartHiveService,
|
|
43
|
+
startHiveService,
|
|
44
|
+
stopHiveService,
|
|
45
|
+
streamHiveServiceLogs,
|
|
46
|
+
uninstallHiveService,
|
|
47
|
+
} from './service.js';
|
|
48
|
+
import { startHiveServer } from '../index.js';
|
|
49
|
+
|
|
50
|
+
function parseInteger(value, key) {
|
|
51
|
+
const parsed = parseInt(String(value ?? '').trim(), 10);
|
|
52
|
+
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
53
|
+
throw new CliError(`${key} must be a positive integer`);
|
|
54
|
+
}
|
|
55
|
+
return parsed;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function requiredOrFallback(value, fallback) {
|
|
59
|
+
const trimmed = String(value ?? '').trim();
|
|
60
|
+
return trimmed || fallback;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function promptConfirm(message, yes = false, initial = true) {
|
|
64
|
+
if (yes) return true;
|
|
65
|
+
const answer = await prompts({
|
|
66
|
+
type: 'confirm',
|
|
67
|
+
name: 'ok',
|
|
68
|
+
message,
|
|
69
|
+
initial,
|
|
70
|
+
});
|
|
71
|
+
return Boolean(answer.ok);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function resolveContext(options = {}) {
|
|
75
|
+
const config = await loadHiveConfig();
|
|
76
|
+
const envFile = options.envFile || config.envFile || DEFAULT_ENV_FILE;
|
|
77
|
+
return { config, envFile };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function resolveServiceConfig(config) {
|
|
81
|
+
const defaults = getServiceDefaults();
|
|
82
|
+
return {
|
|
83
|
+
servicePlatform: config.servicePlatform || defaults.servicePlatform,
|
|
84
|
+
serviceName: config.serviceName || defaults.serviceName,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function loadValidatedEnv(envFile, { requireFile = true } = {}) {
|
|
89
|
+
if (requireFile && !existsSync(envFile)) {
|
|
90
|
+
throw new CliError(`Env file not found: ${envFile}. Run: hive env init`, EXIT.FAIL);
|
|
91
|
+
}
|
|
92
|
+
const raw = await loadEnvFile(envFile);
|
|
93
|
+
const env = normalizeEnv(raw);
|
|
94
|
+
const issues = validateEnvValues(env);
|
|
95
|
+
return { env, issues };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
async function setRedirectUriForDomain({ envFile, env, domain, yes = false }) {
|
|
99
|
+
const expected = `https://${domain}/auth/callback`;
|
|
100
|
+
if (env.DISCORD_REDIRECT_URI === expected) return env;
|
|
101
|
+
|
|
102
|
+
const shouldUpdate = await promptConfirm(
|
|
103
|
+
`Set DISCORD_REDIRECT_URI to ${expected}?`,
|
|
104
|
+
yes,
|
|
105
|
+
true
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
if (!shouldUpdate) return env;
|
|
109
|
+
|
|
110
|
+
const next = { ...env, DISCORD_REDIRECT_URI: expected };
|
|
111
|
+
await writeEnvFile(envFile, next);
|
|
112
|
+
success(`Updated DISCORD_REDIRECT_URI -> ${expected}`);
|
|
113
|
+
return next;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function runDoctorChecks({ envFile, includeCloudflared = true }) {
|
|
117
|
+
section('Hive Doctor');
|
|
118
|
+
|
|
119
|
+
let prereqFailures = 0;
|
|
120
|
+
let failures = 0;
|
|
121
|
+
|
|
122
|
+
const major = parseInt(process.versions.node.split('.')[0], 10);
|
|
123
|
+
if (major >= 18) {
|
|
124
|
+
success(`Node version OK (${process.versions.node})`);
|
|
125
|
+
} else {
|
|
126
|
+
fail(`Node >= 18 is required (current: ${process.versions.node})`);
|
|
127
|
+
prereqFailures += 1;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (includeCloudflared) {
|
|
131
|
+
const cloudflaredPath = getCloudflaredPath();
|
|
132
|
+
if (!cloudflaredPath) {
|
|
133
|
+
fail('cloudflared is not installed or not on PATH');
|
|
134
|
+
prereqFailures += 1;
|
|
135
|
+
} else {
|
|
136
|
+
const versionOutput = await run('cloudflared', ['--version']).catch(() => ({ stdout: '' }));
|
|
137
|
+
success(`cloudflared found (${cloudflaredPath}) ${versionOutput.stdout.trim()}`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (!existsSync(envFile)) {
|
|
142
|
+
fail(`Env file missing: ${envFile}`);
|
|
143
|
+
failures += 1;
|
|
144
|
+
} else {
|
|
145
|
+
success(`Env file found: ${envFile}`);
|
|
146
|
+
const { env, issues } = await loadValidatedEnv(envFile, { requireFile: true });
|
|
147
|
+
if (issues.length > 0) {
|
|
148
|
+
for (const issue of issues) fail(issue);
|
|
149
|
+
failures += issues.length;
|
|
150
|
+
} else {
|
|
151
|
+
success('Env values look valid');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (env.VAULT_PATH) {
|
|
155
|
+
const vaultExists = await pathExists(env.VAULT_PATH);
|
|
156
|
+
if (!vaultExists) {
|
|
157
|
+
fail(`VAULT_PATH does not exist: ${env.VAULT_PATH}`);
|
|
158
|
+
failures += 1;
|
|
159
|
+
} else {
|
|
160
|
+
try {
|
|
161
|
+
await access(env.VAULT_PATH, fsConstants.R_OK | fsConstants.W_OK);
|
|
162
|
+
success(`VAULT_PATH is readable/writable: ${env.VAULT_PATH}`);
|
|
163
|
+
} catch {
|
|
164
|
+
fail(`VAULT_PATH is not readable/writable: ${env.VAULT_PATH}`);
|
|
165
|
+
failures += 1;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const port = parseInt(env.PORT, 10);
|
|
171
|
+
const yjsPort = parseInt(env.YJS_PORT, 10);
|
|
172
|
+
if (Number.isInteger(port) && port > 0) {
|
|
173
|
+
const portFree = await isPortAvailable(port);
|
|
174
|
+
if (portFree) info(`PORT ${port} is available`);
|
|
175
|
+
else warn(`PORT ${port} is in use`);
|
|
176
|
+
}
|
|
177
|
+
if (Number.isInteger(yjsPort) && yjsPort > 0) {
|
|
178
|
+
const yjsFree = await isPortAvailable(yjsPort);
|
|
179
|
+
if (yjsFree) info(`YJS_PORT ${yjsPort} is available`);
|
|
180
|
+
else warn(`YJS_PORT ${yjsPort} is in use`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (Number.isInteger(port) && port > 0) {
|
|
184
|
+
const health = await fetch(`http://127.0.0.1:${port}/health`)
|
|
185
|
+
.then((res) => ({ ok: res.ok, status: res.status }))
|
|
186
|
+
.catch(() => null);
|
|
187
|
+
if (health?.ok) {
|
|
188
|
+
success(`Health endpoint reachable on :${port}`);
|
|
189
|
+
} else if (health) {
|
|
190
|
+
warn(`Health endpoint returned HTTP ${health.status}`);
|
|
191
|
+
} else {
|
|
192
|
+
info(`Health endpoint not reachable on :${port} (server may not be running)`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (prereqFailures > 0) {
|
|
198
|
+
throw new CliError('Doctor found missing prerequisites', EXIT.PREREQ);
|
|
199
|
+
}
|
|
200
|
+
if (failures > 0) {
|
|
201
|
+
throw new CliError('Doctor found configuration issues', EXIT.FAIL);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
success('Doctor checks passed');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function runSetupWizard(options) {
|
|
208
|
+
const yes = Boolean(options.yes);
|
|
209
|
+
|
|
210
|
+
section('Hive Setup');
|
|
211
|
+
|
|
212
|
+
const { config, envFile } = await resolveContext(options);
|
|
213
|
+
let nextConfig = { ...config, envFile };
|
|
214
|
+
let importedLegacy = false;
|
|
215
|
+
|
|
216
|
+
if (!existsSync(HIVE_CONFIG_FILE) && existsSync(LEGACY_ENV_FILE)) {
|
|
217
|
+
const shouldImportLegacy = await promptConfirm(
|
|
218
|
+
`Import existing legacy env from ${LEGACY_ENV_FILE}?`,
|
|
219
|
+
yes,
|
|
220
|
+
true
|
|
221
|
+
);
|
|
222
|
+
if (shouldImportLegacy) {
|
|
223
|
+
const legacyValues = await loadEnvFile(LEGACY_ENV_FILE);
|
|
224
|
+
await writeEnvFile(envFile, legacyValues);
|
|
225
|
+
importedLegacy = true;
|
|
226
|
+
success(`Imported legacy env into ${envFile}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const envExists = existsSync(envFile);
|
|
231
|
+
let envValues;
|
|
232
|
+
if (!envExists || importedLegacy) {
|
|
233
|
+
info(`Initializing env file at ${envFile}`);
|
|
234
|
+
const existing = await loadEnvFile(envFile);
|
|
235
|
+
envValues = await promptForEnv({ envFile, existing, yes });
|
|
236
|
+
} else {
|
|
237
|
+
const edit = await promptConfirm('Env file exists. Edit it now?', yes, false);
|
|
238
|
+
if (edit) {
|
|
239
|
+
const existing = await loadEnvFile(envFile);
|
|
240
|
+
envValues = await promptForEnv({ envFile, existing, yes });
|
|
241
|
+
} else {
|
|
242
|
+
envValues = normalizeEnv(await loadEnvFile(envFile));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const envIssues = validateEnvValues(envValues);
|
|
247
|
+
if (envIssues.length > 0) {
|
|
248
|
+
for (const issue of envIssues) fail(issue);
|
|
249
|
+
throw new CliError('Env configuration is invalid', EXIT.FAIL);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
let domain = requiredOrFallback(
|
|
253
|
+
options.domain,
|
|
254
|
+
inferDomainFromRedirect(envValues.DISCORD_REDIRECT_URI) || nextConfig.domain
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
if (!yes) {
|
|
258
|
+
const response = await prompts({
|
|
259
|
+
type: 'text',
|
|
260
|
+
name: 'domain',
|
|
261
|
+
message: 'Public domain for Hive server',
|
|
262
|
+
initial: domain,
|
|
263
|
+
});
|
|
264
|
+
if (response.domain !== undefined) {
|
|
265
|
+
domain = String(response.domain).trim();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (!validateDomain(domain)) {
|
|
270
|
+
throw new CliError(`Invalid domain: ${domain}`);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
envValues = await setRedirectUriForDomain({ envFile, env: envValues, domain, yes });
|
|
274
|
+
|
|
275
|
+
const shouldSetupTunnel = await promptConfirm('Configure Cloudflare Tunnel now?', yes, true);
|
|
276
|
+
if (shouldSetupTunnel) {
|
|
277
|
+
const port = parseInteger(envValues.PORT, 'PORT');
|
|
278
|
+
const yjsPort = parseInteger(envValues.YJS_PORT, 'YJS_PORT');
|
|
279
|
+
const tunnelName = requiredOrFallback(options.tunnelName, nextConfig.tunnelName || DEFAULT_TUNNEL_NAME);
|
|
280
|
+
const cloudflaredConfigFile = requiredOrFallback(
|
|
281
|
+
options.cloudflaredConfigFile,
|
|
282
|
+
nextConfig.cloudflaredConfigFile || DEFAULT_CLOUDFLARED_CONFIG
|
|
283
|
+
);
|
|
284
|
+
const tunnelService = await promptConfirm('Install cloudflared as a service?', yes, true);
|
|
285
|
+
|
|
286
|
+
const tunnelResult = await setupTunnel({
|
|
287
|
+
tunnelName,
|
|
288
|
+
domain,
|
|
289
|
+
configFile: cloudflaredConfigFile,
|
|
290
|
+
certPath: DEFAULT_CLOUDFLARED_CERT,
|
|
291
|
+
port,
|
|
292
|
+
yjsPort,
|
|
293
|
+
yes,
|
|
294
|
+
installService: tunnelService,
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
nextConfig = {
|
|
298
|
+
...nextConfig,
|
|
299
|
+
domain,
|
|
300
|
+
tunnelName,
|
|
301
|
+
tunnelId: tunnelResult.tunnelId,
|
|
302
|
+
tunnelCredentialsFile: tunnelResult.credentialsFile,
|
|
303
|
+
cloudflaredConfigFile: tunnelResult.configFile,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const shouldInstallService = await promptConfirm('Install Hive server as an OS service?', yes, true);
|
|
308
|
+
if (shouldInstallService) {
|
|
309
|
+
const serviceInfo = await installHiveService({ envFile, yes });
|
|
310
|
+
nextConfig = {
|
|
311
|
+
...nextConfig,
|
|
312
|
+
servicePlatform: serviceInfo.servicePlatform,
|
|
313
|
+
serviceName: serviceInfo.serviceName,
|
|
314
|
+
};
|
|
315
|
+
success(`Installed service ${serviceInfo.serviceName}`);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
await updateHiveConfig(nextConfig);
|
|
319
|
+
success(`Saved config: ${HIVE_CONFIG_FILE}`);
|
|
320
|
+
|
|
321
|
+
await runDoctorChecks({ envFile, includeCloudflared: shouldSetupTunnel });
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function registerEnvCommands(program) {
|
|
325
|
+
const env = program.command('env').description('Manage Hive .env configuration');
|
|
326
|
+
|
|
327
|
+
env
|
|
328
|
+
.command('init')
|
|
329
|
+
.description('Create or update env file from prompts')
|
|
330
|
+
.option('--env-file <path>', 'env file path')
|
|
331
|
+
.option('--yes', 'accept defaults where possible', false)
|
|
332
|
+
.action(async (options) => {
|
|
333
|
+
section('Env Init');
|
|
334
|
+
const { config, envFile } = await resolveContext(options);
|
|
335
|
+
const existing = await loadEnvFile(envFile);
|
|
336
|
+
const values = await promptForEnv({ envFile, existing, yes: options.yes });
|
|
337
|
+
const issues = validateEnvValues(values);
|
|
338
|
+
if (issues.length > 0) {
|
|
339
|
+
for (const issue of issues) fail(issue);
|
|
340
|
+
throw new CliError('Env file has validation issues', EXIT.FAIL);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const domain = inferDomainFromRedirect(values.DISCORD_REDIRECT_URI) || config.domain;
|
|
344
|
+
await updateHiveConfig({ envFile, domain });
|
|
345
|
+
success(`Env file ready at ${envFile}`);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
env
|
|
349
|
+
.command('edit')
|
|
350
|
+
.description('Edit env values interactively')
|
|
351
|
+
.option('--env-file <path>', 'env file path')
|
|
352
|
+
.option('--yes', 'accept defaults where possible', false)
|
|
353
|
+
.action(async (options) => {
|
|
354
|
+
section('Env Edit');
|
|
355
|
+
const { envFile } = await resolveContext(options);
|
|
356
|
+
if (!existsSync(envFile)) {
|
|
357
|
+
throw new CliError(`Env file not found: ${envFile}. Run: hive env init`, EXIT.FAIL);
|
|
358
|
+
}
|
|
359
|
+
const existing = await loadEnvFile(envFile);
|
|
360
|
+
const values = await promptForEnv({ envFile, existing, yes: options.yes });
|
|
361
|
+
const issues = validateEnvValues(values);
|
|
362
|
+
if (issues.length > 0) {
|
|
363
|
+
for (const issue of issues) fail(issue);
|
|
364
|
+
throw new CliError('Env file has validation issues', EXIT.FAIL);
|
|
365
|
+
}
|
|
366
|
+
success(`Env file updated: ${envFile}`);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
env
|
|
370
|
+
.command('check')
|
|
371
|
+
.description('Validate env file')
|
|
372
|
+
.option('--env-file <path>', 'env file path')
|
|
373
|
+
.action(async (options) => {
|
|
374
|
+
section('Env Check');
|
|
375
|
+
const { envFile } = await resolveContext(options);
|
|
376
|
+
const { issues } = await loadValidatedEnv(envFile, { requireFile: true });
|
|
377
|
+
if (issues.length > 0) {
|
|
378
|
+
for (const issue of issues) fail(issue);
|
|
379
|
+
throw new CliError('Env validation failed', EXIT.FAIL);
|
|
380
|
+
}
|
|
381
|
+
success('Env validation passed');
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
env
|
|
385
|
+
.command('print')
|
|
386
|
+
.description('Print redacted env values')
|
|
387
|
+
.option('--env-file <path>', 'env file path')
|
|
388
|
+
.action(async (options) => {
|
|
389
|
+
const { envFile } = await resolveContext(options);
|
|
390
|
+
const values = normalizeEnv(await loadEnvFile(envFile));
|
|
391
|
+
const redacted = redactEnv(values);
|
|
392
|
+
section(`Env (${envFile})`);
|
|
393
|
+
for (const [key, value] of Object.entries(redacted)) {
|
|
394
|
+
console.log(`${key}=${value}`);
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function registerTunnelCommands(program) {
|
|
400
|
+
const tunnel = program.command('tunnel').description('Manage Cloudflare tunnel');
|
|
401
|
+
|
|
402
|
+
tunnel
|
|
403
|
+
.command('setup')
|
|
404
|
+
.description('Run full tunnel lifecycle setup')
|
|
405
|
+
.option('--env-file <path>', 'env file path')
|
|
406
|
+
.option('--domain <domain>', 'public domain')
|
|
407
|
+
.option('--tunnel-name <name>', 'tunnel name')
|
|
408
|
+
.option('--cloudflared-config-file <path>', 'cloudflared config file')
|
|
409
|
+
.option('--install-service', 'install cloudflared service', false)
|
|
410
|
+
.option('--yes', 'non-interactive mode', false)
|
|
411
|
+
.action(async (options) => {
|
|
412
|
+
section('Tunnel Setup');
|
|
413
|
+
const { config, envFile } = await resolveContext(options);
|
|
414
|
+
const { env, issues } = await loadValidatedEnv(envFile, { requireFile: true });
|
|
415
|
+
if (issues.length > 0) {
|
|
416
|
+
for (const issue of issues) fail(issue);
|
|
417
|
+
throw new CliError('Fix env file first (hive env check)', EXIT.FAIL);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const domain = requiredOrFallback(
|
|
421
|
+
options.domain,
|
|
422
|
+
inferDomainFromRedirect(env.DISCORD_REDIRECT_URI) || config.domain
|
|
423
|
+
);
|
|
424
|
+
if (!validateDomain(domain)) {
|
|
425
|
+
throw new CliError(`Invalid domain: ${domain}`);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const tunnelName = requiredOrFallback(options.tunnelName, config.tunnelName || DEFAULT_TUNNEL_NAME);
|
|
429
|
+
const cloudflaredConfigFile = requiredOrFallback(
|
|
430
|
+
options.cloudflaredConfigFile,
|
|
431
|
+
config.cloudflaredConfigFile || DEFAULT_CLOUDFLARED_CONFIG
|
|
432
|
+
);
|
|
433
|
+
|
|
434
|
+
const tunnelResult = await setupTunnel({
|
|
435
|
+
tunnelName,
|
|
436
|
+
domain,
|
|
437
|
+
configFile: cloudflaredConfigFile,
|
|
438
|
+
certPath: DEFAULT_CLOUDFLARED_CERT,
|
|
439
|
+
port: parseInteger(env.PORT, 'PORT'),
|
|
440
|
+
yjsPort: parseInteger(env.YJS_PORT, 'YJS_PORT'),
|
|
441
|
+
yes: Boolean(options.yes),
|
|
442
|
+
installService: Boolean(options.installService),
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
const nextEnv = await setRedirectUriForDomain({
|
|
446
|
+
envFile,
|
|
447
|
+
env,
|
|
448
|
+
domain,
|
|
449
|
+
yes: Boolean(options.yes),
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
await updateHiveConfig({
|
|
453
|
+
envFile,
|
|
454
|
+
domain,
|
|
455
|
+
tunnelName,
|
|
456
|
+
tunnelId: tunnelResult.tunnelId,
|
|
457
|
+
tunnelCredentialsFile: tunnelResult.credentialsFile,
|
|
458
|
+
cloudflaredConfigFile: cloudflaredConfigFile,
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
if (nextEnv.DISCORD_REDIRECT_URI !== env.DISCORD_REDIRECT_URI) {
|
|
462
|
+
success('Redirect URI synced for tunnel domain');
|
|
463
|
+
}
|
|
464
|
+
success('Tunnel setup complete');
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
tunnel
|
|
468
|
+
.command('status')
|
|
469
|
+
.description('Show tunnel status and config')
|
|
470
|
+
.option('--tunnel-name <name>', 'tunnel name')
|
|
471
|
+
.option('--cloudflared-config-file <path>', 'cloudflared config path')
|
|
472
|
+
.action(async (options) => {
|
|
473
|
+
const config = await loadHiveConfig();
|
|
474
|
+
const tunnelName = requiredOrFallback(options.tunnelName, config.tunnelName || DEFAULT_TUNNEL_NAME);
|
|
475
|
+
const cloudflaredConfigFile = requiredOrFallback(
|
|
476
|
+
options.cloudflaredConfigFile,
|
|
477
|
+
config.cloudflaredConfigFile || DEFAULT_CLOUDFLARED_CONFIG
|
|
478
|
+
);
|
|
479
|
+
const status = await tunnelStatus({ tunnelName, configFile: cloudflaredConfigFile });
|
|
480
|
+
section('Tunnel Status');
|
|
481
|
+
console.log(`Name: ${tunnelName}`);
|
|
482
|
+
console.log(`Tunnel ID: ${status.tunnel?.id || '(not found)'}`);
|
|
483
|
+
console.log(`Config file: ${status.configFile} ${status.configExists ? '' : '(missing)'}`);
|
|
484
|
+
if (config.domain) {
|
|
485
|
+
console.log(`Domain: ${config.domain}`);
|
|
486
|
+
}
|
|
487
|
+
const svc = await cloudflaredServiceStatus().catch(() => false);
|
|
488
|
+
console.log(`cloudflared service: ${svc ? 'active' : 'inactive or unknown'}`);
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
tunnel
|
|
492
|
+
.command('run')
|
|
493
|
+
.description('Run tunnel in foreground')
|
|
494
|
+
.option('--tunnel-name <name>', 'tunnel name')
|
|
495
|
+
.action(async (options) => {
|
|
496
|
+
const config = await loadHiveConfig();
|
|
497
|
+
const tunnelName = requiredOrFallback(options.tunnelName, config.tunnelName || DEFAULT_TUNNEL_NAME);
|
|
498
|
+
await runTunnelForeground({ tunnelName });
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
tunnel
|
|
502
|
+
.command('service-install')
|
|
503
|
+
.description('Install cloudflared as a system service')
|
|
504
|
+
.action(async () => {
|
|
505
|
+
section('Tunnel Service Install');
|
|
506
|
+
await installCloudflaredService();
|
|
507
|
+
success('cloudflared service installed');
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
tunnel
|
|
511
|
+
.command('service-status')
|
|
512
|
+
.description('Show cloudflared service status')
|
|
513
|
+
.action(async () => {
|
|
514
|
+
const active = await cloudflaredServiceStatus();
|
|
515
|
+
section('Tunnel Service Status');
|
|
516
|
+
console.log(active ? 'active' : 'inactive');
|
|
517
|
+
if (process.platform === 'darwin') {
|
|
518
|
+
const listing = await run('launchctl', ['list']).catch(() => ({ stdout: '' }));
|
|
519
|
+
const row = listing.stdout
|
|
520
|
+
.split('\n')
|
|
521
|
+
.find((line) => line.toLowerCase().includes('cloudflared'));
|
|
522
|
+
if (row) {
|
|
523
|
+
console.log('');
|
|
524
|
+
console.log(row.trim());
|
|
525
|
+
}
|
|
526
|
+
} else if (process.platform === 'linux') {
|
|
527
|
+
const status = await run('sudo', ['systemctl', 'status', 'cloudflared', '--no-pager', '--lines', '20'])
|
|
528
|
+
.catch(() => ({ stdout: '' }));
|
|
529
|
+
if (status.stdout) {
|
|
530
|
+
console.log('');
|
|
531
|
+
console.log(status.stdout);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function registerServiceCommands(program) {
|
|
538
|
+
const service = program.command('service').description('Manage Hive OS service');
|
|
539
|
+
|
|
540
|
+
service
|
|
541
|
+
.command('install')
|
|
542
|
+
.description('Install Hive as launchd/systemd service')
|
|
543
|
+
.option('--env-file <path>', 'env file path')
|
|
544
|
+
.option('--yes', 'non-interactive mode', false)
|
|
545
|
+
.action(async (options) => {
|
|
546
|
+
const { config, envFile } = await resolveContext(options);
|
|
547
|
+
const infoOut = await installHiveService({
|
|
548
|
+
envFile,
|
|
549
|
+
yes: Boolean(options.yes),
|
|
550
|
+
serviceName: config.serviceName,
|
|
551
|
+
});
|
|
552
|
+
await updateHiveConfig({
|
|
553
|
+
envFile,
|
|
554
|
+
servicePlatform: infoOut.servicePlatform,
|
|
555
|
+
serviceName: infoOut.serviceName,
|
|
556
|
+
});
|
|
557
|
+
success(`Service installed: ${infoOut.serviceName}`);
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
service
|
|
561
|
+
.command('start')
|
|
562
|
+
.description('Start Hive service')
|
|
563
|
+
.action(async () => {
|
|
564
|
+
const config = await loadHiveConfig();
|
|
565
|
+
const svc = resolveServiceConfig(config);
|
|
566
|
+
await startHiveService(svc);
|
|
567
|
+
success('Hive service started');
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
service
|
|
571
|
+
.command('stop')
|
|
572
|
+
.description('Stop Hive service')
|
|
573
|
+
.action(async () => {
|
|
574
|
+
const config = await loadHiveConfig();
|
|
575
|
+
const svc = resolveServiceConfig(config);
|
|
576
|
+
await stopHiveService(svc);
|
|
577
|
+
success('Hive service stopped');
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
service
|
|
581
|
+
.command('restart')
|
|
582
|
+
.description('Restart Hive service')
|
|
583
|
+
.action(async () => {
|
|
584
|
+
const config = await loadHiveConfig();
|
|
585
|
+
const svc = resolveServiceConfig(config);
|
|
586
|
+
await restartHiveService(svc);
|
|
587
|
+
success('Hive service restarted');
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
service
|
|
591
|
+
.command('status')
|
|
592
|
+
.description('Show Hive service status')
|
|
593
|
+
.action(async () => {
|
|
594
|
+
const config = await loadHiveConfig();
|
|
595
|
+
const svc = resolveServiceConfig(config);
|
|
596
|
+
const status = await getHiveServiceStatus(svc);
|
|
597
|
+
section('Hive Service Status');
|
|
598
|
+
console.log(`Service: ${svc.serviceName} (${svc.servicePlatform})`);
|
|
599
|
+
console.log(`Active: ${status.active ? 'yes' : 'no'}`);
|
|
600
|
+
if (status.detail) {
|
|
601
|
+
console.log('');
|
|
602
|
+
console.log(status.detail);
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
service
|
|
607
|
+
.command('logs')
|
|
608
|
+
.description('Stream service logs')
|
|
609
|
+
.option('-n, --lines <n>', 'lines to show', '80')
|
|
610
|
+
.option('--no-follow', 'do not follow logs')
|
|
611
|
+
.action(async (options) => {
|
|
612
|
+
const config = await loadHiveConfig();
|
|
613
|
+
const svc = resolveServiceConfig(config);
|
|
614
|
+
const lines = parseInteger(options.lines, 'lines');
|
|
615
|
+
await streamHiveServiceLogs({
|
|
616
|
+
...svc,
|
|
617
|
+
follow: Boolean(options.follow),
|
|
618
|
+
lines,
|
|
619
|
+
});
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
service
|
|
623
|
+
.command('uninstall')
|
|
624
|
+
.description('Uninstall Hive service')
|
|
625
|
+
.option('--yes', 'skip confirmation', false)
|
|
626
|
+
.action(async (options) => {
|
|
627
|
+
const config = await loadHiveConfig();
|
|
628
|
+
const svc = resolveServiceConfig(config);
|
|
629
|
+
await uninstallHiveService({
|
|
630
|
+
...svc,
|
|
631
|
+
yes: Boolean(options.yes),
|
|
632
|
+
});
|
|
633
|
+
success(`Service removed: ${svc.serviceName}`);
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function registerRootCommands(program) {
|
|
638
|
+
program
|
|
639
|
+
.command('setup')
|
|
640
|
+
.description('Run guided setup for env, tunnel, and service')
|
|
641
|
+
.option('--env-file <path>', 'env file path')
|
|
642
|
+
.option('--domain <domain>', 'public domain')
|
|
643
|
+
.option('--tunnel-name <name>', 'tunnel name')
|
|
644
|
+
.option('--cloudflared-config-file <path>', 'cloudflared config file')
|
|
645
|
+
.option('--yes', 'non-interactive mode', false)
|
|
646
|
+
.action(runSetupWizard);
|
|
647
|
+
|
|
648
|
+
program
|
|
649
|
+
.command('doctor')
|
|
650
|
+
.description('Run prerequisite and configuration checks')
|
|
651
|
+
.option('--env-file <path>', 'env file path')
|
|
652
|
+
.action(async (options) => {
|
|
653
|
+
const { envFile } = await resolveContext(options);
|
|
654
|
+
await runDoctorChecks({ envFile, includeCloudflared: true });
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
program
|
|
658
|
+
.command('run')
|
|
659
|
+
.description('Start Hive server immediately')
|
|
660
|
+
.option('--env-file <path>', 'env file path')
|
|
661
|
+
.option('--quiet', 'reduce startup logs', false)
|
|
662
|
+
.action(async (options) => {
|
|
663
|
+
const { envFile } = await resolveContext(options);
|
|
664
|
+
if (!existsSync(envFile)) {
|
|
665
|
+
throw new CliError(`Env file not found: ${envFile}. Run: hive env init`, EXIT.FAIL);
|
|
666
|
+
}
|
|
667
|
+
await startHiveServer({ envFile, quiet: Boolean(options.quiet) });
|
|
668
|
+
info(`Hive server started using env: ${envFile}`);
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
program
|
|
672
|
+
.command('status')
|
|
673
|
+
.description('Quick status summary (service + tunnel + doctor-lite)')
|
|
674
|
+
.option('--env-file <path>', 'env file path')
|
|
675
|
+
.action(async (options) => {
|
|
676
|
+
const { config, envFile } = await resolveContext(options);
|
|
677
|
+
section('Hive Status');
|
|
678
|
+
console.log(`Config: ${HIVE_CONFIG_FILE}`);
|
|
679
|
+
console.log(`Env: ${envFile} ${existsSync(envFile) ? '' : '(missing)'}`);
|
|
680
|
+
if (config.domain) console.log(`Domain: ${config.domain}`);
|
|
681
|
+
if (config.tunnelName) console.log(`Tunnel: ${config.tunnelName}`);
|
|
682
|
+
|
|
683
|
+
const svc = resolveServiceConfig(config);
|
|
684
|
+
const serviceStatus = await getHiveServiceStatus(svc).catch(() => ({ active: false, detail: 'not installed' }));
|
|
685
|
+
console.log(`Service ${svc.serviceName}: ${serviceStatus.active ? 'active' : 'inactive'}`);
|
|
686
|
+
|
|
687
|
+
const tunnelSvc = await cloudflaredServiceStatus().catch(() => false);
|
|
688
|
+
console.log(`cloudflared service: ${tunnelSvc ? 'active' : 'inactive or unknown'}`);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
export async function runCli(argv = process.argv) {
|
|
693
|
+
const program = new Command();
|
|
694
|
+
|
|
695
|
+
program
|
|
696
|
+
.name('hive')
|
|
697
|
+
.description('Hive server operations CLI')
|
|
698
|
+
.showHelpAfterError()
|
|
699
|
+
.version('1.0.0');
|
|
700
|
+
|
|
701
|
+
registerRootCommands(program);
|
|
702
|
+
registerEnvCommands(program);
|
|
703
|
+
registerTunnelCommands(program);
|
|
704
|
+
registerServiceCommands(program);
|
|
705
|
+
|
|
706
|
+
program.exitOverride();
|
|
707
|
+
|
|
708
|
+
try {
|
|
709
|
+
await program.parseAsync(argv);
|
|
710
|
+
return EXIT.OK;
|
|
711
|
+
} catch (err) {
|
|
712
|
+
if (err instanceof CommanderError) {
|
|
713
|
+
if (err.code === 'commander.helpDisplayed') return EXIT.OK;
|
|
714
|
+
throw new CliError(err.message, err.exitCode ?? EXIT.FAIL);
|
|
715
|
+
}
|
|
716
|
+
throw err;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
export async function runCliOrExit(argv = process.argv) {
|
|
721
|
+
try {
|
|
722
|
+
const code = await runCli(argv);
|
|
723
|
+
process.exit(code);
|
|
724
|
+
} catch (err) {
|
|
725
|
+
const exitCode = err instanceof CliError ? err.exitCode : EXIT.FAIL;
|
|
726
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
727
|
+
fail(message);
|
|
728
|
+
process.exit(exitCode);
|
|
729
|
+
}
|
|
730
|
+
}
|