nc-agent 0.1.1 → 0.1.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/bin/nc.mjs +100 -31
- package/lib/diagnostics-bundle.mjs +39 -11
- package/lib/setup.mjs +22 -0
- package/package.json +6 -19
- package/LICENSE +0 -176
- package/README.md +0 -45
package/bin/nc.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import child_process from 'node:child_process';
|
|
|
10
10
|
import readline from 'node:readline';
|
|
11
11
|
import { fileURLToPath } from 'node:url';
|
|
12
12
|
import {
|
|
13
|
+
canElevate,
|
|
13
14
|
decideInstallMode,
|
|
14
15
|
findRemovalTargets,
|
|
15
16
|
getNcRootForMode,
|
|
@@ -218,7 +219,7 @@ export function buildDeviceDownloadHeaders({ hostId, hostToken, privateKey }, ab
|
|
|
218
219
|
* @param {Record<string, string>} [extraHeaders]
|
|
219
220
|
* @returns {Promise<import('node:http').IncomingMessage>}
|
|
220
221
|
*/
|
|
221
|
-
function httpGet(targetUrl, maxRedirects = 5, extraHeaders = {}) {
|
|
222
|
+
export function httpGet(targetUrl, maxRedirects = 5, extraHeaders = {}, options = {}) {
|
|
222
223
|
if (typeof maxRedirects === 'object' && maxRedirects !== null) {
|
|
223
224
|
extraHeaders = /** @type {Record<string, string>} */ (maxRedirects);
|
|
224
225
|
maxRedirects = 5;
|
|
@@ -229,10 +230,13 @@ function httpGet(targetUrl, maxRedirects = 5, extraHeaders = {}) {
|
|
|
229
230
|
}
|
|
230
231
|
const u = new URL(targetUrl);
|
|
231
232
|
const client = u.protocol === 'http:' ? http : https;
|
|
233
|
+
const timeoutMs = options.timeoutMs ?? getCliRequestTimeoutMs();
|
|
234
|
+
const signal = options.signal ?? (timeoutMs > 0 ? AbortSignal.timeout(timeoutMs) : undefined);
|
|
232
235
|
|
|
233
236
|
const req = client.get(
|
|
234
237
|
u,
|
|
235
238
|
{
|
|
239
|
+
signal,
|
|
236
240
|
headers: {
|
|
237
241
|
'User-Agent': 'nc-cli/0.1.0',
|
|
238
242
|
Accept: '*/*',
|
|
@@ -249,7 +253,7 @@ function httpGet(targetUrl, maxRedirects = 5, extraHeaders = {}) {
|
|
|
249
253
|
const redirectUrl = new URL(res.headers.location, targetUrl).toString();
|
|
250
254
|
res.resume();
|
|
251
255
|
const forwarded = isSameOrigin(targetUrl, redirectUrl) ? extraHeaders : {};
|
|
252
|
-
return resolve(httpGet(redirectUrl, maxRedirects - 1, forwarded));
|
|
256
|
+
return resolve(httpGet(redirectUrl, maxRedirects - 1, forwarded, { signal, timeoutMs }));
|
|
253
257
|
}
|
|
254
258
|
|
|
255
259
|
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
|
|
@@ -269,27 +273,17 @@ function httpGet(targetUrl, maxRedirects = 5, extraHeaders = {}) {
|
|
|
269
273
|
* Fetches and parses a JSON endpoint. The update manifest (`/api/updates`)
|
|
270
274
|
* is admin-only and never takes device headers, so this stays anonymous;
|
|
271
275
|
* device identity is only attached to per-client package downloads.
|
|
276
|
+
* Delegates to requestJson to enforce request timeout and response byte limits.
|
|
272
277
|
* @param {string} targetUrl
|
|
273
278
|
* @param {Record<string, string>} [extraHeaders]
|
|
274
279
|
* @returns {Promise<any>}
|
|
275
280
|
*/
|
|
276
|
-
async function fetchJson(targetUrl, extraHeaders = {}) {
|
|
277
|
-
const res = await
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
raw += chunk;
|
|
283
|
-
});
|
|
284
|
-
res.on('end', () => {
|
|
285
|
-
try {
|
|
286
|
-
resolve(JSON.parse(raw));
|
|
287
|
-
} catch (err) {
|
|
288
|
-
reject(new Error(`Failed to parse JSON from ${targetUrl}: ${err.message}`));
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
res.on('error', reject);
|
|
292
|
-
});
|
|
281
|
+
export async function fetchJson(targetUrl, extraHeaders = {}) {
|
|
282
|
+
const res = await requestJson('GET', targetUrl, null, extraHeaders);
|
|
283
|
+
if (res.status < 200 || res.status >= 300) {
|
|
284
|
+
throw new Error(`Request to ${targetUrl} failed with status ${res.status}`);
|
|
285
|
+
}
|
|
286
|
+
return res.data;
|
|
293
287
|
}
|
|
294
288
|
|
|
295
289
|
/**
|
|
@@ -297,10 +291,11 @@ async function fetchJson(targetUrl, extraHeaders = {}) {
|
|
|
297
291
|
* @param {string} targetUrl
|
|
298
292
|
* @param {string} destinationPath
|
|
299
293
|
* @param {Record<string, string>} [extraHeaders] Device headers for `/api/clients/...` package URLs; empty for public `/releases/...` URLs.
|
|
294
|
+
* @param {{ timeoutMs?: number, signal?: AbortSignal }} [options]
|
|
300
295
|
* @returns {Promise<string>}
|
|
301
296
|
*/
|
|
302
|
-
async function downloadToFile(targetUrl, destinationPath, extraHeaders = {}) {
|
|
303
|
-
const res = await httpGet(targetUrl, 5, extraHeaders);
|
|
297
|
+
export async function downloadToFile(targetUrl, destinationPath, extraHeaders = {}, options = {}) {
|
|
298
|
+
const res = await httpGet(targetUrl, 5, extraHeaders, options);
|
|
304
299
|
return new Promise((resolve, reject) => {
|
|
305
300
|
const fileStream = fs.createWriteStream(destinationPath);
|
|
306
301
|
let resolved = false;
|
|
@@ -960,8 +955,13 @@ export async function verifyHostRegistration({ baseUrl: baseUrlOpt = undefined,
|
|
|
960
955
|
* admin-only GET /api/updates manifest is NEVER fetched: the release already
|
|
961
956
|
* carries concrete per-client bootstrap/package URLs plus integrity pins,
|
|
962
957
|
* and the CLI is not an admin so the manifest would answer 401 anyway.
|
|
963
|
-
* Relative URLs resolve against `serverUrl`/`NC_SERVER_URL`.
|
|
964
|
-
*
|
|
958
|
+
* Relative URLs resolve against `serverUrl`/`NC_SERVER_URL`. When no release
|
|
959
|
+
* is present but host identity (`hostId` + `hostToken` + `privateKey`) is
|
|
960
|
+
* available — the resume and already-redeemed paths — the release is
|
|
961
|
+
* resolved from the client-authenticated per-host manifest
|
|
962
|
+
* (GET /api/clients/<id>/update/manifest with NC-Device + Device Proof v1,
|
|
963
|
+
* the same identity the agent updater sends); /api/updates stays reserved
|
|
964
|
+
* for callers with no device identity at all, and an admin-auth refusal
|
|
965
965
|
* surfaces an explanatory error pointing at enrollment-based setup.
|
|
966
966
|
* The /api/updates endpoint stays admin-only (Caddy and control-plane are
|
|
967
967
|
* intentionally untouched): opening it on the agent edge would only trade a
|
|
@@ -974,11 +974,12 @@ export async function verifyHostRegistration({ baseUrl: baseUrlOpt = undefined,
|
|
|
974
974
|
* client token, exactly like redeemEnrollment.
|
|
975
975
|
* @param {{ ncRoot?: string, updatesUrl?: string, serverUrl?: string, release?: Record<string, unknown>|null, installMode?: 'system'|'user', runInstaller?: (scriptPath: string, ncRoot: string) => void, hostId?: string, hostToken?: string, privateKey?: import('node:crypto').KeyObject }} options
|
|
976
976
|
*/
|
|
977
|
-
export async function downloadAndInstallAgent({ ncRoot =
|
|
977
|
+
export async function downloadAndInstallAgent({ ncRoot = undefined, updatesUrl = undefined, serverUrl = undefined, release = undefined, installMode = undefined, runInstaller = undefined, hostId = undefined, hostToken = undefined, privateKey = undefined } = {}) {
|
|
978
|
+
ncRoot = ncRoot || (installMode ? getNcRootForMode(installMode) : getNcRoot());
|
|
978
979
|
const modeEnv = installMode ? installerEnvForMode(installMode) : {};
|
|
979
980
|
const invokeInstaller =
|
|
980
981
|
runInstaller || ((scriptPath, root) => runInstallerScript(scriptPath, root, modeEnv));
|
|
981
|
-
const root = ncRoot
|
|
982
|
+
const root = ncRoot;
|
|
982
983
|
console.log(`📍 Installation root: ${root}`);
|
|
983
984
|
|
|
984
985
|
// Package/release resolution stays on the agent edge: an explicit
|
|
@@ -990,11 +991,39 @@ export async function downloadAndInstallAgent({ ncRoot = getNcRoot(), updatesUrl
|
|
|
990
991
|
/** @type {Record<string, any>} */
|
|
991
992
|
let activeRelease;
|
|
992
993
|
if (release && typeof release === 'object') {
|
|
993
|
-
// Redemption release wins: skip the
|
|
994
|
-
//
|
|
994
|
+
// Redemption release wins: skip the manifest entirely, so its URL is
|
|
995
|
+
// never logged as a misleading signal.
|
|
995
996
|
activeRelease = /** @type {Record<string, any>} */ (release);
|
|
996
997
|
const releaseVersion = activeRelease.version ? ` ${activeRelease.version}` : '';
|
|
997
998
|
console.log(`📦 Using release${releaseVersion} from enrollment redemption; skipping update manifest.`);
|
|
999
|
+
} else if (
|
|
1000
|
+
typeof hostId === 'string' &&
|
|
1001
|
+
hostId &&
|
|
1002
|
+
typeof hostToken === 'string' &&
|
|
1003
|
+
hostToken &&
|
|
1004
|
+
privateKey
|
|
1005
|
+
) {
|
|
1006
|
+
// Resume paths (verified stored credentials, already-redeemed enrollment)
|
|
1007
|
+
// carry no redemption release, but they DO hold device identity. Resolve
|
|
1008
|
+
// the release from the client-authenticated per-host manifest the agent
|
|
1009
|
+
// updater itself uses, never from the admin-only /api/updates manifest
|
|
1010
|
+
// (which can only answer 401/404 to a non-admin CLI).
|
|
1011
|
+
const clientManifestUrl = `${defaultBaseUrl}/api/clients/${encodeURIComponent(hostId)}/update/manifest`;
|
|
1012
|
+
console.log(`📡 Fetching client update manifest from ${clientManifestUrl}...`);
|
|
1013
|
+
let manifestData;
|
|
1014
|
+
try {
|
|
1015
|
+
manifestData = await fetchJson(
|
|
1016
|
+
clientManifestUrl,
|
|
1017
|
+
buildDeviceDownloadHeaders({ hostId, hostToken, privateKey }, clientManifestUrl)
|
|
1018
|
+
);
|
|
1019
|
+
} catch (err) {
|
|
1020
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
1021
|
+
throw new PairingError(
|
|
1022
|
+
'INSTALL_MANIFEST_ERROR',
|
|
1023
|
+
`Could not fetch the client update manifest: ${detail}`
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
activeRelease = manifestData?.release || manifestData || {};
|
|
998
1027
|
} else {
|
|
999
1028
|
const manifestOrigin = String(process.env.NC_API_URL || apiBase).replace(/\/$/, '');
|
|
1000
1029
|
const manifestUrl = updatesUrl || process.env.NC_UPDATES_URL || `${manifestOrigin}/api/updates`;
|
|
@@ -1220,7 +1249,12 @@ export async function pairDeviceAndInstall({
|
|
|
1220
1249
|
writePairState(statePath, { host_id: localEnv.NC_CLIENT_ID, phase: 'verified' });
|
|
1221
1250
|
return { hostId: localEnv.NC_CLIENT_ID, serverUrl: localEnv.NC_SERVER_URL, resumed: true };
|
|
1222
1251
|
} catch (err) {
|
|
1223
|
-
|
|
1252
|
+
// Only a definitive 404 (VERIFY_FAILED) falls through to a fresh
|
|
1253
|
+
// enrollment. A timeout (VERIFY_TIMEOUT) rethrows with the timeout
|
|
1254
|
+
// guidance: wiping stored credentials on a transient stall would
|
|
1255
|
+
// orphan a healthy host identity. The caller reports the stored-
|
|
1256
|
+
// credentials connectivity hint via suggestedActionFor.
|
|
1257
|
+
if (err instanceof PairingError && err.code === 'VERIFY_FAILED') {
|
|
1224
1258
|
// Stored credentials no longer authenticate (rotated, revoked, or
|
|
1225
1259
|
// server reset): fall through to a fresh enrollment, which
|
|
1226
1260
|
// overwrites the stale files below.
|
|
@@ -1527,7 +1561,21 @@ export async function runSetup(args = []) {
|
|
|
1527
1561
|
console.error('suggested_action: Re-run as `nodecommand setup` or `nodecommand setup <host_id> <token>`.');
|
|
1528
1562
|
process.exit(1);
|
|
1529
1563
|
}
|
|
1530
|
-
|
|
1564
|
+
let installMode = decideInstallMode();
|
|
1565
|
+
// A non-root admin must not silently fall back to a per-user install: the
|
|
1566
|
+
// install root, service scope and privileged helper all differ. Request sudo
|
|
1567
|
+
// (the password prompt IS the admin request) and re-run elevated so the
|
|
1568
|
+
// install lands machine-wide at the canonical system root.
|
|
1569
|
+
if (installMode === 'user' && canElevate() && !process.env.NC_ELEVATED) {
|
|
1570
|
+
console.log('Admin rights required for a system-wide install. Requesting sudo…');
|
|
1571
|
+
const elevated = child_process.spawnSync(
|
|
1572
|
+
'sudo',
|
|
1573
|
+
['-E', process.execPath, ...process.argv.slice(1)],
|
|
1574
|
+
{ stdio: 'inherit', env: { ...process.env, NC_ELEVATED: '1' } }
|
|
1575
|
+
);
|
|
1576
|
+
if (elevated && elevated.status === 0) process.exit(0);
|
|
1577
|
+
console.warn('Elevation unavailable or declined; continuing with a per-user install.');
|
|
1578
|
+
}
|
|
1531
1579
|
// Mode-aware root: on win32 a system install lives in %ProgramData%\NC,
|
|
1532
1580
|
// not the %LOCALAPPDATA% tree getNcRoot() returns, so credentials must be
|
|
1533
1581
|
// written where the system installer will actually read them.
|
|
@@ -1712,7 +1760,8 @@ async function runDeviceFlow({ installMode = undefined, ncRoot = undefined } = {
|
|
|
1712
1760
|
user_code,
|
|
1713
1761
|
verification_uri_complete,
|
|
1714
1762
|
verification_uri,
|
|
1715
|
-
interval = 3
|
|
1763
|
+
interval = 3,
|
|
1764
|
+
expires_in = 600
|
|
1716
1765
|
} = initRes.data;
|
|
1717
1766
|
|
|
1718
1767
|
const bold = '\x1b[1m';
|
|
@@ -1745,9 +1794,20 @@ async function runDeviceFlow({ installMode = undefined, ncRoot = undefined } = {
|
|
|
1745
1794
|
|
|
1746
1795
|
let isPolling = false;
|
|
1747
1796
|
let currentIntervalMs = pollIntervalMs;
|
|
1797
|
+
// Poll bounds (RFC 8628 §3.5): the device_code lives ~expires_in seconds
|
|
1798
|
+
// server-side, and consecutive network failures must not spin forever.
|
|
1799
|
+
const lifetimeSec = Number(expires_in) > 0 ? Number(expires_in) : 600;
|
|
1800
|
+
const flowDeadline = Date.now() + lifetimeSec * 1000;
|
|
1801
|
+
let consecutiveNetErrors = 0;
|
|
1802
|
+
const MAX_NET_ERROR_RETRIES = 10;
|
|
1748
1803
|
|
|
1749
1804
|
const poll = async () => {
|
|
1750
1805
|
if (isPolling) return;
|
|
1806
|
+
if (Date.now() >= flowDeadline) {
|
|
1807
|
+
console.error(`\nerror: [DEVICE_FLOW_CONNECT] Device authorization window expired before approval (no approval within ~${lifetimeSec}s).`);
|
|
1808
|
+
console.error('suggested_action: Re-run the command and approve the prompt in the browser within the time window.');
|
|
1809
|
+
process.exit(1);
|
|
1810
|
+
}
|
|
1751
1811
|
isPolling = true;
|
|
1752
1812
|
|
|
1753
1813
|
try {
|
|
@@ -1757,6 +1817,7 @@ async function runDeviceFlow({ installMode = undefined, ncRoot = undefined } = {
|
|
|
1757
1817
|
client_id: clientId,
|
|
1758
1818
|
code_verifier: deviceCodeVerifier
|
|
1759
1819
|
});
|
|
1820
|
+
consecutiveNetErrors = 0;
|
|
1760
1821
|
|
|
1761
1822
|
if (tokenRes.status === 200 && tokenRes.data?.access_token) {
|
|
1762
1823
|
// The OAuth token authorizes enrollment ONLY. It stays in memory:
|
|
@@ -1806,7 +1867,15 @@ async function runDeviceFlow({ installMode = undefined, ncRoot = undefined } = {
|
|
|
1806
1867
|
process.exit(1);
|
|
1807
1868
|
}
|
|
1808
1869
|
} catch (pollErr) {
|
|
1809
|
-
// Ephemeral network glitch during poll
|
|
1870
|
+
// Ephemeral network glitch during poll: retry a bounded number of
|
|
1871
|
+
// times, then fail loudly instead of polling forever.
|
|
1872
|
+
consecutiveNetErrors += 1;
|
|
1873
|
+
if (consecutiveNetErrors >= MAX_NET_ERROR_RETRIES) {
|
|
1874
|
+
const detail = pollErr instanceof Error ? pollErr.message : String(pollErr);
|
|
1875
|
+
console.error(`\nerror: [DEVICE_FLOW_CONNECT] Could not reach ${tokenEndpoint} after ${consecutiveNetErrors} attempts: ${detail}`);
|
|
1876
|
+
console.error('suggested_action: Check network access to the control plane (or NC_BASE_URL), then re-run.');
|
|
1877
|
+
process.exit(1);
|
|
1878
|
+
}
|
|
1810
1879
|
} finally {
|
|
1811
1880
|
isPolling = false;
|
|
1812
1881
|
setTimeout(poll, currentIntervalMs);
|
|
@@ -17,6 +17,7 @@ import os from 'node:os';
|
|
|
17
17
|
import path from 'node:path';
|
|
18
18
|
import zlib from 'node:zlib';
|
|
19
19
|
import child_process from 'node:child_process';
|
|
20
|
+
import { candidateInstallRoots, decideInstallMode, getNcRootForMode, probeInstallRoot } from './setup.mjs';
|
|
20
21
|
|
|
21
22
|
export const CLI_DIAGNOSTICS_VERSION = '1.0.0';
|
|
22
23
|
export const CLI_DIAGNOSTICS_MAX_LOG_BYTES = 65536;
|
|
@@ -485,21 +486,48 @@ export function renderCliSummary(bundle, { wrote = null, dryRun = true } = {}) {
|
|
|
485
486
|
* @param {(file: string, data: Uint8Array) => void} [options.writeFile]
|
|
486
487
|
* @param {string} [options.cwd]
|
|
487
488
|
*/
|
|
489
|
+
/**
|
|
490
|
+
* Default agent root for diagnostics. Explicit roots win; otherwise the
|
|
491
|
+
* shared installer layout table is probed so machine-wide installs
|
|
492
|
+
* (`/opt/nc`, `%ProgramData%\\NC`) resolve instead of silently falling back
|
|
493
|
+
* to the per-user tree the agent never reads. `mode` overrides the
|
|
494
|
+
* privilege probe (deterministic tests); callers without one get the live
|
|
495
|
+
* `decideInstallMode()` fallback for the nothing-installed case.
|
|
496
|
+
* @param {{ env?: Record<string, string|undefined>, platform?: string, homedir?: string, mode?: 'system'|'user' }} [options]
|
|
497
|
+
*/
|
|
498
|
+
export function resolveDefaultCliRoot({ env = process.env, platform = os.platform(), homedir = undefined, mode = undefined } = {}) {
|
|
499
|
+
const e = env || {};
|
|
500
|
+
if (e.NC_ROOT) return e.NC_ROOT;
|
|
501
|
+
const base = { env: e, platform };
|
|
502
|
+
if (homedir !== undefined) base.homedir = homedir;
|
|
503
|
+
let roots = [];
|
|
504
|
+
try {
|
|
505
|
+
roots = candidateInstallRoots(base);
|
|
506
|
+
} catch {
|
|
507
|
+
roots = [];
|
|
508
|
+
}
|
|
509
|
+
for (const r of roots) {
|
|
510
|
+
let status = '';
|
|
511
|
+
try {
|
|
512
|
+
status = probeInstallRoot(r);
|
|
513
|
+
} catch {
|
|
514
|
+
status = '';
|
|
515
|
+
}
|
|
516
|
+
if (status === 'install' || status === 'unreadable') return r;
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
const fallbackMode = mode === 'system' || mode === 'user' ? mode : decideInstallMode({ platform });
|
|
520
|
+
return getNcRootForMode(fallbackMode, base);
|
|
521
|
+
} catch {
|
|
522
|
+
return roots.length ? roots[roots.length - 1] : null;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
488
526
|
export function runCliDiagnostics(argv = [], { ncRoot = null, env = process.env, cliVersion = '0.1.0', baseUrl = null, writeFile = null, cwd = null } = {}) {
|
|
489
527
|
const parsed = parseCliDiagnosticsArgs(argv);
|
|
490
528
|
if (!parsed.enabled) return { handled: false, ok: false, summary: '', outputPath: null };
|
|
491
529
|
try {
|
|
492
|
-
|
|
493
|
-
if (!root) {
|
|
494
|
-
const e = env || {};
|
|
495
|
-
if (e.NC_ROOT) root = e.NC_ROOT;
|
|
496
|
-
else {
|
|
497
|
-
const plat = os.platform();
|
|
498
|
-
if (plat === 'win32') root = path.join(e.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'), 'nc');
|
|
499
|
-
else if (plat === 'darwin') root = path.join(os.homedir(), 'Library', 'Application Support', 'nc');
|
|
500
|
-
else root = path.join(os.homedir(), '.local', 'share', 'nc');
|
|
501
|
-
}
|
|
502
|
-
}
|
|
530
|
+
const root = ncRoot || resolveDefaultCliRoot({ env });
|
|
503
531
|
const bundle = collectCliDiagnostics({ ncRoot: root, env, cliVersion, baseUrl });
|
|
504
532
|
if (!parsed.apply) {
|
|
505
533
|
return { handled: true, ok: true, dryRun: true, outputPath: null, bundle, summary: renderCliSummary(bundle, { dryRun: true }) };
|
package/lib/setup.mjs
CHANGED
|
@@ -72,6 +72,28 @@ export function decideInstallMode(options = {}) {
|
|
|
72
72
|
return isElevated(options) ? 'system' : 'user';
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
/**
|
|
76
|
+
* True when the current user CAN elevate to a machine-wide install even when
|
|
77
|
+
* the process is not root yet: POSIX admin groups (sudo/admin/wheel) or an
|
|
78
|
+
* already-elevated process. Used so a non-root admin is asked for sudo instead
|
|
79
|
+
* of silently degrading to a per-user install (different root, service scope
|
|
80
|
+
* and no privileged helper).
|
|
81
|
+
* @param {{ platform?: string, getuid?: (() => number)|null, isAdmin?: boolean|null, runAdminCheck?: (() => boolean)|null }} [options]
|
|
82
|
+
*/
|
|
83
|
+
export function canElevate(options = {}) {
|
|
84
|
+
if (isElevated(options)) return true;
|
|
85
|
+
const platform = options.platform || os.platform();
|
|
86
|
+
// Windows: isElevated already answers "is administrator".
|
|
87
|
+
if (platform === 'win32') return false;
|
|
88
|
+
try {
|
|
89
|
+
const r = child_process.spawnSync('id', ['-nG'], { encoding: 'utf8' });
|
|
90
|
+
const groups = r && typeof r.stdout === 'string' ? r.stdout : '';
|
|
91
|
+
return /\b(sudo|admin|wheel)\b/.test(groups);
|
|
92
|
+
} catch {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
75
97
|
/**
|
|
76
98
|
* Warning text for the given mode: a single line for user mode, null for
|
|
77
99
|
* system mode (callers assert presence/absence on this, not on stdout).
|
package/package.json
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nc-agent",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Node Command device pairing and agent installer. Run npx nc-agent to pair this machine with your Node Command workspace.",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"bin",
|
|
11
|
-
"lib",
|
|
12
|
-
"README.md",
|
|
13
|
-
"LICENSE"
|
|
14
|
-
],
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=24"
|
|
17
|
-
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"start": "node src/client/launcher.mjs",
|
|
20
|
-
"test": "node --test tests/*.mjs"
|
|
21
|
-
},
|
|
22
|
-
"description": "Node Command device pairing and agent installer. Run npx nc-agent to pair this machine via the browser device flow; the agent is downloaded from the control plane."
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"engines": { "node": ">=22.5.0" },
|
|
8
|
+
"bin": { "nc-agent": "./bin/nc.mjs" },
|
|
9
|
+
"files": ["bin/", "lib/"]
|
|
23
10
|
}
|
package/LICENSE
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
Apache License
|
|
2
|
-
Version 2.0, January 2004
|
|
3
|
-
http://www.apache.org/licenses/
|
|
4
|
-
|
|
5
|
-
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
-
|
|
7
|
-
1. Definitions.
|
|
8
|
-
|
|
9
|
-
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
-
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
-
|
|
12
|
-
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
-
the copyright owner that is granting the License.
|
|
14
|
-
|
|
15
|
-
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
-
other entities that control, are controlled by, or are under common
|
|
17
|
-
control with that entity. For the purposes of this definition,
|
|
18
|
-
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
-
direction or management of such entity, whether by contract or
|
|
20
|
-
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
-
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for reasonable and customary use in describing the
|
|
141
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
package/README.md
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# nc-agent
|
|
2
|
-
|
|
3
|
-
Node Command device pairing and agent installer.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npx nc-agent
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
This starts the browser device authorization flow (RFC 8628). It prints a
|
|
12
|
-
short confirmation code and a URL — open the URL in your browser, approve the
|
|
13
|
-
prompt, and the agent is enrolled, downloaded from the control plane,
|
|
14
|
-
installed, and verified automatically.
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npx nc-agent setup # explicit setup (auto system/user mode)
|
|
18
|
-
npx nc-agent setup --system # force system-level service install
|
|
19
|
-
npx nc-agent setup --user # force non-elevated user install
|
|
20
|
-
npx nc-agent remove # uninstall the agent
|
|
21
|
-
npx nc-agent --diagnostics # preview a redacted support bundle
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
The agent runtime is **not** bundled in this package. It is downloaded from
|
|
25
|
-
the control plane at install time, so a fresh `npx nc-agent` always installs
|
|
26
|
-
the latest release we serve. Updates after install flow through the Node
|
|
27
|
-
Command OTA channel, not npm.
|
|
28
|
-
|
|
29
|
-
## Environment
|
|
30
|
-
|
|
31
|
-
- `NC_BASE_URL` — auth host for the browser device flow (default `https://auth.nodecommand.app`)
|
|
32
|
-
- `NC_API_URL` — agent API host for device endpoints and package downloads (default `https://agent.nodecommand.app`)
|
|
33
|
-
- `NC_SERVER_URL` — agent server address written to the client env
|
|
34
|
-
|
|
35
|
-
## Manual install (fallback)
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
git clone https://github.com/huseyincig/nc-agent.git
|
|
39
|
-
cd nc-agent
|
|
40
|
-
npm start
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## License
|
|
44
|
-
|
|
45
|
-
MIT — see [LICENSE](LICENSE).
|