@remoteshell/device 0.0.0-stage → 0.1.0-beta.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 +26 -0
- package/NOTICE +20 -0
- package/bin/dev-agent-client.mjs +228 -0
- package/bin/dev-agent-runner.mjs +60 -0
- package/bin/dev-api-client.mjs +31 -0
- package/bin/dev-auto-worker.mjs +25 -0
- package/bin/dev-browser-open.mjs +12 -0
- package/bin/dev-dogfood-read.mjs +3 -0
- package/bin/dev-heartbeat-service.mjs +77 -0
- package/bin/dev-pairing-client.mjs +63 -0
- package/bin/dev-secrets.mjs +33 -0
- package/bin/dev-setup-recovery.mjs +34 -0
- package/bin/dev-signer-pin.mjs +22 -0
- package/bin/device.mjs +363 -0
- package/bin/enrollment-client.mjs +105 -0
- package/bin/install-acquire.mjs +89 -0
- package/bin/install-integrity.mjs +74 -0
- package/bin/install-plan.mjs +43 -0
- package/bin/lifecycle.mjs +172 -0
- package/bin/native-path-contract.mjs +23 -0
- package/bin/remoteshell-device-install.cmd +17 -0
- package/bin/remoteshell-device-install.ps1 +17 -0
- package/bin/remoteshell-device-install.sh +15 -0
- package/bin/remoteshell-device-preflight.cmd +10 -0
- package/bin/remoteshell-device-preflight.ps1 +10 -0
- package/bin/remoteshell-easy-diagnose.ps1 +32 -0
- package/bin/remoteshell-easy-install.ps1 +289 -0
- package/bin/remoteshell-easy-install.sh +71 -0
- package/bin/remoteshell-easy-uninstall.ps1 +124 -0
- package/bin/windows-credential-boundary.mjs +106 -0
- package/bin/windows-install-layout.mjs +28 -0
- package/bin/windows-native-installer.mjs +88 -0
- package/bin/windows-private-store.mjs +163 -0
- package/bin/windows-user-lifecycle.mjs +34 -0
- package/npm-shrinkwrap.json +8591 -0
- package/package.json +27 -4
- package/shared/lib/bounded-search-contract.mjs +5 -0
- package/shared/lib/bounded-search-policy.mjs +24 -0
- package/shared/lib/config.mjs +14 -0
- package/shared/lib/contracts.mjs +26 -0
- package/shared/lib/dc-generic-policy.mjs +102 -0
- package/shared/lib/dc-relay-contract.mjs +42 -0
- package/shared/lib/dc-tool-catalog.mjs +67 -0
- package/shared/lib/dev-dc-relay-contract.mjs +20 -0
- package/shared/lib/dev-paged-output.mjs +32 -0
- package/shared/lib/dev-root-exclusions.mjs +56 -0
- package/shared/lib/errors.mjs +9 -0
- package/shared/lib/events.mjs +5 -0
- package/shared/lib/execution-policy.mjs +41 -0
- package/shared/lib/isolated-read-boundary.mjs +50 -0
- package/shared/lib/isolated-read-policy.mjs +29 -0
- package/shared/lib/lifecycle-events.mjs +13 -0
- package/shared/lib/runtime-timing.mjs +9 -0
- package/shared/lib/synthetic-auxiliary-policy.mjs +21 -0
- package/shared/lib/synthetic-write-policy.mjs +52 -0
- package/shared/lib/tool-registry.mjs +58 -0
- package/src/config/l2-dev-oci-credential.mjs +39 -0
- package/src/config/p2-private-credential.mjs +15 -0
- package/src/connection/dev-dogfood-processes.mjs +25 -0
- package/src/connection/dev-dogfood-read.mjs +64 -0
- package/src/connection/dev-dogfood-task.mjs +113 -0
- package/src/connection/dev-dogfood-write.mjs +62 -0
- package/src/connection/dev-file-task.mjs +119 -0
- package/src/connection/dev-r4-file-task.mjs +56 -0
- package/src/connection/dev-read-tools.mjs +134 -0
- package/src/connection/dev-root-scopes.mjs +82 -0
- package/src/core/c1-dc-transport.mjs +26 -0
- package/src/core/c1-startup.mjs +22 -0
- package/src/core/c1-supervisor.mjs +111 -0
- package/src/core/c1-worker.mjs +88 -0
- package/src/core/fixture.txt +2 -0
- package/src/dc/dc-access-readiness.mjs +31 -0
- package/src/dc/dev-dc-relay.mjs +47 -0
- package/src/device-agent/main.mjs +1 -0
- package/src/gateway-keepalive.mjs +73 -0
- package/src/main.mjs +434 -0
- package/src/policy/bounded-search-adapter.mjs +115 -0
- package/src/policy/isolated-read-adapter.mjs +50 -0
- package/src/policy/isolated-read-mount.mjs +27 -0
- package/src/policy/synthetic-auxiliary-adapter.mjs +75 -0
- package/src/policy/synthetic-file-adapter.mjs +26 -0
- package/src/policy/synthetic-file-transaction.mjs +87 -0
- package/src/policy/synthetic-write-adapter.mjs +151 -0
- package/src/policy/synthetic-write-mount.mjs +23 -0
- package/src/policy/verified-bounded-search.mjs +12 -0
- package/src/policy/verified-isolated-read.mjs +15 -0
- package/src/policy/verified-synthetic-write.mjs +14 -0
- package/README.md +0 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// #310: non-executing installer preflight aligned with the existing DEV
|
|
2
|
+
// artifact-delivery schema v2 and the independently pinned Ed25519 key.
|
|
3
|
+
// Trust anchors come from the installer, never from a downloaded manifest.
|
|
4
|
+
import {createHash,createPublicKey,verify as verifySignature,timingSafeEqual} from 'node:crypto';
|
|
5
|
+
import {openSync,closeSync,readSync,lstatSync,fstatSync,constants} from 'node:fs';
|
|
6
|
+
import {basename} from 'node:path';
|
|
7
|
+
const MAX_BYTES=128*1024*1024;
|
|
8
|
+
const fail=code=>{throw Object.assign(new Error(code),{code});};
|
|
9
|
+
const versionOk=v=>typeof v==='string'&&/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(v);
|
|
10
|
+
const hashOk=x=>typeof x==='string'&&/^[0-9a-f]{64}$/.test(x);
|
|
11
|
+
const nameOk=x=>typeof x==='string'&&/^[a-zA-Z0-9][a-zA-Z0-9._-]{0,127}\.tgz$/.test(x)&&!x.includes('..');
|
|
12
|
+
export function installPreflight({os,arch,nodeVersion,wsl=false}={}){
|
|
13
|
+
const supported=(os==='win32'||os==='linux')&&['x64','arm64'].includes(arch);
|
|
14
|
+
const major=Number(/^v?(\d+)(?:\.|$)/.exec(nodeVersion||'')?.[1]);
|
|
15
|
+
if(!supported)fail('DEVICE_INSTALL_OS_UNSUPPORTED');
|
|
16
|
+
if(wsl)fail('DEVICE_INSTALL_WSL_BETA_EXCLUDED');
|
|
17
|
+
if(!Number.isInteger(major)||major<22)fail('DEVICE_INSTALL_NODE_UNSUPPORTED');
|
|
18
|
+
return Object.freeze({preflight:'PASS',os,arch,nodeMajor:major,automaticInstall:false,
|
|
19
|
+
installationReady:false,requiresOwnerConsent:true});
|
|
20
|
+
}
|
|
21
|
+
export function verifySignedInstallArchive({artifactPath,manifest,trustedKeys,os,arch,nodeVersion,wsl=false}={}){
|
|
22
|
+
installPreflight({os,arch,nodeVersion,wsl});
|
|
23
|
+
const fields=manifest?.signed_fields;
|
|
24
|
+
if(!manifest||typeof manifest!=='object'||Array.isArray(manifest)||
|
|
25
|
+
Object.keys(manifest).sort().join(',')!=='algorithm,public_key_sha256,signature,signed_fields'||
|
|
26
|
+
manifest.algorithm!=='ed25519'||!hashOk(manifest.public_key_sha256)||
|
|
27
|
+
typeof manifest.signature!=='string'||!/^[A-Za-z0-9_-]{86}$/.test(manifest.signature)||
|
|
28
|
+
!fields||typeof fields!=='object'||Array.isArray(fields)||
|
|
29
|
+
fields.schema_version!==2||fields.channel!=='private-dev'||fields.public_release!==false||
|
|
30
|
+
fields.package!=='@remoteshell/device'||!versionOk(fields.version)||
|
|
31
|
+
fields.os!==os||fields.arch!==arch||!nameOk(fields.artifact)||
|
|
32
|
+
fields.artifact!==basename(artifactPath||'')||!Number.isSafeInteger(fields.bytes)||
|
|
33
|
+
fields.bytes<1||fields.bytes>MAX_BYTES||!hashOk(fields.sha256)||
|
|
34
|
+
typeof fields.source_head!=='string'||!/^[a-f0-9]{40}$/.test(fields.source_head)||
|
|
35
|
+
typeof fields.feature!=='string'||fields.feature.length>150)
|
|
36
|
+
fail('DEVICE_INSTALL_MANIFEST_DENIED');
|
|
37
|
+
const trusted=trustedKeys&&Object.hasOwn(trustedKeys,manifest.public_key_sha256)?
|
|
38
|
+
trustedKeys[manifest.public_key_sha256]:null;
|
|
39
|
+
if(!trusted)fail('DEVICE_INSTALL_TRUST_ANCHOR_MISSING');
|
|
40
|
+
let key;
|
|
41
|
+
try{key=trusted?.type==='public'&&trusted?.asymmetricKeyType==='ed25519'?trusted:createPublicKey(trusted);}
|
|
42
|
+
catch{fail('DEVICE_INSTALL_TRUST_ANCHOR_INVALID');}
|
|
43
|
+
if(key.asymmetricKeyType!=='ed25519'||key.type!=='public')fail('DEVICE_INSTALL_TRUST_ANCHOR_INVALID');
|
|
44
|
+
const fingerprint=createHash('sha256').update(key.export({type:'spki',format:'der'})).digest('hex');
|
|
45
|
+
if(fingerprint!==manifest.public_key_sha256)fail('DEVICE_INSTALL_TRUST_ANCHOR_INVALID');
|
|
46
|
+
const signature=Buffer.from(manifest.signature,'base64url');
|
|
47
|
+
if(signature.length!==64||!verifySignature(null,Buffer.from(JSON.stringify(fields),'utf8'),key,signature))
|
|
48
|
+
fail('DEVICE_INSTALL_SIGNATURE_DENIED');
|
|
49
|
+
let fd;
|
|
50
|
+
try{
|
|
51
|
+
const st=lstatSync(artifactPath);
|
|
52
|
+
if(!st.isFile()||st.isSymbolicLink()||st.size!==fields.bytes)fail('DEVICE_INSTALL_ARTIFACT_DENIED');
|
|
53
|
+
fd=openSync(artifactPath,constants.O_RDONLY | (constants.O_NOFOLLOW||0));
|
|
54
|
+
const first=fstatSync(fd);
|
|
55
|
+
if(!first.isFile()||first.dev!==st.dev||first.ino!==st.ino||first.size!==st.size)
|
|
56
|
+
fail('DEVICE_INSTALL_ARTIFACT_DENIED');
|
|
57
|
+
const hash=createHash('sha256'),buffer=Buffer.alloc(65536);
|
|
58
|
+
let remaining=st.size;
|
|
59
|
+
while(remaining>0){
|
|
60
|
+
const count=readSync(fd,buffer,0,Math.min(buffer.length,remaining),null);
|
|
61
|
+
if(count<=0)fail('DEVICE_INSTALL_ARTIFACT_DENIED');
|
|
62
|
+
hash.update(buffer.subarray(0,count));remaining-=count;
|
|
63
|
+
}
|
|
64
|
+
const after=fstatSync(fd);
|
|
65
|
+
if(after.size!==first.size||after.mtimeMs!==first.mtimeMs||after.ino!==first.ino)
|
|
66
|
+
fail('DEVICE_INSTALL_ARTIFACT_DENIED');
|
|
67
|
+
const actual=Buffer.from(hash.digest('hex'),'hex');
|
|
68
|
+
if(!timingSafeEqual(actual,Buffer.from(fields.sha256,'hex')))
|
|
69
|
+
fail('DEVICE_INSTALL_CHECKSUM_DENIED');
|
|
70
|
+
}catch(e){if(e?.code?.startsWith('DEVICE_INSTALL_'))throw e;fail('DEVICE_INSTALL_ARTIFACT_DENIED');}
|
|
71
|
+
finally{if(fd!==undefined)closeSync(fd);}
|
|
72
|
+
return Object.freeze({verified:true,version:fields.version,os,arch,sha256:fields.sha256,
|
|
73
|
+
signingKeyFingerprint:fingerprint,channel:fields.channel,installed:false,registered:false});
|
|
74
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// #310 Shared, non-executing installation decision. Both OS entrypoints must
|
|
2
|
+
// consume one verified artifact; this module never runs it or changes credentials.
|
|
3
|
+
import {verifySignedInstallArchive} from './install-integrity.mjs';
|
|
4
|
+
const fail=code=>{throw Error(code);};
|
|
5
|
+
function semver(value){
|
|
6
|
+
if(typeof value!=='string'||!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value))
|
|
7
|
+
fail('DEVICE_INSTALL_VERSION_DENIED');
|
|
8
|
+
return value.split('-')[0].split('.').map(Number);
|
|
9
|
+
}
|
|
10
|
+
function compare(a,b){
|
|
11
|
+
const av=semver(a),bv=semver(b);
|
|
12
|
+
for(let i=0;i<3;i++){if(av[i]>bv[i])return 1;if(av[i]<bv[i])return -1;}
|
|
13
|
+
// Never infer prerelease ordering or treat a prerelease as a final release.
|
|
14
|
+
if(a===b)return 0;
|
|
15
|
+
fail('DEVICE_INSTALL_VERSION_REVIEW_REQUIRED');
|
|
16
|
+
}
|
|
17
|
+
export function planVerifiedInstall({
|
|
18
|
+
artifactPath,manifest,trustedKeys,os,arch,nodeVersion,wsl=false,
|
|
19
|
+
installedVersion=null,existingCredentials=false,existingService=false,
|
|
20
|
+
consent=false
|
|
21
|
+
}={}){
|
|
22
|
+
// Validate before making a version or installation decision, even on no-op.
|
|
23
|
+
const verified=verifySignedInstallArchive({
|
|
24
|
+
artifactPath,manifest,trustedKeys,os,arch,nodeVersion,wsl
|
|
25
|
+
});
|
|
26
|
+
if(typeof existingCredentials!=='boolean'||typeof existingService!=='boolean'||
|
|
27
|
+
typeof consent!=='boolean')fail('DEVICE_INSTALL_STATE_DENIED');
|
|
28
|
+
const result=installedVersion===null?'fresh':
|
|
29
|
+
compare(verified.version,installedVersion)===0?'already-installed':
|
|
30
|
+
compare(verified.version,installedVersion)>0?'upgrade':'downgrade';
|
|
31
|
+
if(result==='downgrade')fail('DEVICE_INSTALL_DOWNGRADE_DENIED');
|
|
32
|
+
if(existingService&&installedVersion===null)fail('DEVICE_INSTALL_STATE_DENIED');
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
verified:true,os,arch,version:verified.version,
|
|
35
|
+
signingKeyFingerprint:verified.signingKeyFingerprint,
|
|
36
|
+
artifactSha256:verified.sha256,action:result,
|
|
37
|
+
// Installing, replacing, service enabling and re-enrollment are separate
|
|
38
|
+
// later steps and require an explicit consent/restore plan.
|
|
39
|
+
next:consent?'OS_INSTALL_APPROVAL_REQUIRED':'USER_CONSENT_REQUIRED',
|
|
40
|
+
approvedToExecute:false,installed:false,deviceRegistered:false,
|
|
41
|
+
credentialsPreserved:true,existingCredentials,existingService
|
|
42
|
+
});
|
|
43
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// #126 device-local lifecycle. Does not implement CP credential issuance.
|
|
2
|
+
// Commands are limited to the current user's dedicated private installation.
|
|
3
|
+
import {existsSync,lstatSync,mkdirSync,readFileSync,writeFileSync,rmSync} from 'node:fs';
|
|
4
|
+
import {homedir} from 'node:os';
|
|
5
|
+
import {dirname,join,resolve} from 'node:path';
|
|
6
|
+
import {spawnSync,spawn} from 'node:child_process';
|
|
7
|
+
import https from 'node:https';
|
|
8
|
+
import {readDeviceCredential} from './enrollment-client.mjs';
|
|
9
|
+
|
|
10
|
+
const ERROR='DEVICE_LIFECYCLE_DENIED';
|
|
11
|
+
const fail=()=>{throw Error(ERROR);};
|
|
12
|
+
const SERVICE='remoteshell-device.service';
|
|
13
|
+
function requireDir(path,mode=null){
|
|
14
|
+
const s=lstatSync(path);
|
|
15
|
+
if(!s.isDirectory()||s.isSymbolicLink()||s.uid!==process.getuid?.()||(mode!==null&&(s.mode&0o777)!==mode))fail();
|
|
16
|
+
}
|
|
17
|
+
function requireFile(path,mode=null){
|
|
18
|
+
const s=lstatSync(path);
|
|
19
|
+
if(!s.isFile()||s.isSymbolicLink()||s.uid!==process.getuid?.()||(mode!==null&&(s.mode&0o777)!==mode))fail();
|
|
20
|
+
}
|
|
21
|
+
function safeGateway({gatewayUrl,caFile}={}){
|
|
22
|
+
try{
|
|
23
|
+
const u=new URL(gatewayUrl);
|
|
24
|
+
if(u.pathname!=='/'||u.username||u.password||u.hash||u.search)fail();
|
|
25
|
+
if(!['http:','https:'].includes(u.protocol)||u.href!==u.protocol+'//'+u.host+'/'||u.host.length>255||[...u.host].some(c=>!'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:-[]'.includes(c)))fail();
|
|
26
|
+
const local=['127.0.0.1','localhost','[::1]'].includes(u.hostname);
|
|
27
|
+
if(!local&&u.protocol!=='https:')fail();
|
|
28
|
+
if(u.protocol==='https:'){
|
|
29
|
+
if(typeof caFile!=='string'||!/^\/[A-Za-z0-9._/@+-]+$/.test(caFile))fail();
|
|
30
|
+
requireFile(caFile,0o600);requireDir(dirname(caFile),0o700);
|
|
31
|
+
}else if(caFile)fail();
|
|
32
|
+
return {origin:u.href,caFile:u.protocol==='https:'?caFile:null,remote:!local};
|
|
33
|
+
}catch{fail();}
|
|
34
|
+
}
|
|
35
|
+
function paths(home){
|
|
36
|
+
if(typeof home!=='string'||!home.startsWith('/')||resolve(home)==='/')fail();
|
|
37
|
+
requireDir(home);
|
|
38
|
+
const base=join(home,'.local/share/remoteshell-device'),install=join(base,'install');
|
|
39
|
+
requireDir(base,0o700);requireDir(install,0o700);
|
|
40
|
+
const runtime=join(install,'node_modules/@remoteshell/device/src/device-agent/main.mjs');
|
|
41
|
+
requireFile(runtime);
|
|
42
|
+
const credentials=join(base,'credentials');
|
|
43
|
+
const unitDir=join(home,'.config/systemd/user');
|
|
44
|
+
const unit=join(unitDir,SERVICE);
|
|
45
|
+
return {base,install,runtime,credentials,unitDir,unit};
|
|
46
|
+
}
|
|
47
|
+
function credentialPath(p){
|
|
48
|
+
const credential=readDeviceCredential({storageDir:p.credentials});
|
|
49
|
+
if(!credential?.device_id)fail();
|
|
50
|
+
return join(p.credentials,'device.json');
|
|
51
|
+
}
|
|
52
|
+
function checkUnitFile(file){
|
|
53
|
+
if(existsSync(file))requireFile(file,0o600);
|
|
54
|
+
}
|
|
55
|
+
function unitContent(p,gateway,nodeBin){
|
|
56
|
+
// No shell expansion, secrets, or environment from untrusted callers.
|
|
57
|
+
for(const path of [p.runtime,p.credentials,nodeBin,p.install]){
|
|
58
|
+
if(!/^\/[A-Za-z0-9._/@+-]+$/.test(path))fail();
|
|
59
|
+
}
|
|
60
|
+
return [
|
|
61
|
+
'[Unit]','Description=RemoteShell Device Agent (user)','After=network-online.target',
|
|
62
|
+
'Wants=network-online.target','','[Service]','Type=simple',
|
|
63
|
+
'ExecStart='+nodeBin+' '+p.runtime,'Environment=RS_P2_MODE=true',
|
|
64
|
+
'Environment=RS_P2_DEVICE_CREDENTIAL_FILE='+join(p.credentials,'device.json'),
|
|
65
|
+
'Environment=RS_GATEWAY_URL='+gateway.origin,
|
|
66
|
+
...(gateway.caFile?['Environment=RS_CA_FILE='+gateway.caFile]:[]),
|
|
67
|
+
...(gateway.remote?['Environment=RS_ENABLE_REMOTE=true']:[]),
|
|
68
|
+
'WorkingDirectory='+p.install,
|
|
69
|
+
'Restart=on-failure','RestartSec=5','UMask=0077','NoNewPrivileges=yes',
|
|
70
|
+
'PrivateTmp=yes','','[Install]','WantedBy=default.target',''
|
|
71
|
+
].join('\n');
|
|
72
|
+
}
|
|
73
|
+
function systemctl(args,run=spawnSync){
|
|
74
|
+
const result=run('systemctl',['--user',...args],{encoding:'utf8',timeout:5000,maxBuffer:16384,stdio:'pipe'});
|
|
75
|
+
if(result.error||result.signal||result.status!==0)fail();
|
|
76
|
+
return String(result.stdout||'');
|
|
77
|
+
}
|
|
78
|
+
export function installUserUnit({home=homedir(),gatewayUrl,caFile,nodeBin=process.execPath,run=spawnSync}={}){
|
|
79
|
+
const p=paths(home);credentialPath(p);const gateway=safeGateway({gatewayUrl,caFile});
|
|
80
|
+
if(existsSync(p.unit))fail();
|
|
81
|
+
const config=join(home,'.config'),systemd=join(config,'systemd');
|
|
82
|
+
for(const path of [config,systemd,p.unitDir]){
|
|
83
|
+
if(!existsSync(path))mkdirSync(path,{mode:0o700});
|
|
84
|
+
requireDir(path);
|
|
85
|
+
}
|
|
86
|
+
writeFileSync(p.unit,unitContent(p,gateway,nodeBin),{flag:'wx',mode:0o600});
|
|
87
|
+
try{systemctl(['daemon-reload'],run);}catch(e){rmSync(p.unit);throw e;}
|
|
88
|
+
return {unitInstalled:true,enabled:false,started:false,connected:false};
|
|
89
|
+
}
|
|
90
|
+
export function userService({action,home=homedir(),run=spawnSync}={}){
|
|
91
|
+
const p=paths(home);
|
|
92
|
+
if(action==='status'){
|
|
93
|
+
if(!existsSync(p.unit))return {unitInstalled:false,connected:false};
|
|
94
|
+
checkUnitFile(p.unit);
|
|
95
|
+
const r=run('systemctl',['--user','is-active',SERVICE],{encoding:'utf8',timeout:3000,stdio:'pipe'});
|
|
96
|
+
const enabled=run('systemctl',['--user','is-enabled',SERVICE],{encoding:'utf8',timeout:3000,stdio:'pipe'});
|
|
97
|
+
return {unitInstalled:true,active:r.status===0,enabled:enabled.status===0,connected:false};
|
|
98
|
+
}
|
|
99
|
+
checkUnitFile(p.unit);
|
|
100
|
+
if(!existsSync(p.unit))fail();
|
|
101
|
+
if(action==='start'){credentialPath(p);systemctl(['start',SERVICE],run);return {started:true,connected:false};}
|
|
102
|
+
if(action==='stop'){systemctl(['stop',SERVICE],run);return {stopped:true,connected:false};}
|
|
103
|
+
if(action==='restart'){credentialPath(p);systemctl(['restart',SERVICE],run);return {restarted:true,connected:false};}
|
|
104
|
+
if(action==='enable'){credentialPath(p);systemctl(['enable',SERVICE],run);return {enabled:true,connected:false};}
|
|
105
|
+
if(action==='disable'){systemctl(['disable',SERVICE],run);return {enabled:false,connected:false};}
|
|
106
|
+
if(action==='uninstall'){
|
|
107
|
+
// Stop/disable first, fail closed if either operation fails.
|
|
108
|
+
systemctl(['stop',SERVICE],run);systemctl(['disable',SERVICE],run);
|
|
109
|
+
const state=run('systemctl',['--user','is-active',SERVICE],{encoding:'utf8',timeout:3000,stdio:'pipe'});
|
|
110
|
+
if(state.status===0)fail();
|
|
111
|
+
rmSync(p.unit);systemctl(['daemon-reload'],run);
|
|
112
|
+
return {removed:true,credentialsPreserved:true,connected:false};
|
|
113
|
+
}
|
|
114
|
+
fail();
|
|
115
|
+
}
|
|
116
|
+
export async function foreground({home=homedir(),gatewayUrl,caFile,spawnImpl=spawn}={}){
|
|
117
|
+
const p=paths(home),file=credentialPath(p),gateway=safeGateway({gatewayUrl,caFile});
|
|
118
|
+
const child=spawnImpl(process.execPath,[p.runtime],{
|
|
119
|
+
cwd:p.install,stdio:['ignore','ignore','ignore'],
|
|
120
|
+
env:{PATH:process.env.PATH||'/usr/bin:/bin',HOME:home,
|
|
121
|
+
RS_P2_MODE:'true',RS_GATEWAY_URL:gateway.origin,RS_P2_DEVICE_CREDENTIAL_FILE:file,
|
|
122
|
+
...(gateway.caFile?{RS_CA_FILE:gateway.caFile}:{}),
|
|
123
|
+
...(gateway.remote?{RS_ENABLE_REMOTE:'true'}:{}),
|
|
124
|
+
RS_55C1_TEST_MODE:'false',RS_55D_TEST_MODE:'false'}
|
|
125
|
+
});
|
|
126
|
+
return await new Promise((done,reject)=>{
|
|
127
|
+
let exited=false;
|
|
128
|
+
child.once('error',reject);
|
|
129
|
+
child.once('exit',(code,signal)=>{exited=true;done({exited:true,code,signal,connected:false});});
|
|
130
|
+
const stop=()=>{if(!exited)child.kill('SIGTERM');};
|
|
131
|
+
process.once('SIGINT',stop);process.once('SIGTERM',stop);
|
|
132
|
+
child.once('exit',()=>{process.removeListener('SIGINT',stop);process.removeListener('SIGTERM',stop);});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export async function gatewayStatus({home=homedir(),gatewayUrl,caFile,timeoutMs=3000}={}){
|
|
137
|
+
const p=paths(home);
|
|
138
|
+
const credential=readDeviceCredential({storageDir:p.credentials});
|
|
139
|
+
const gateway=safeGateway({gatewayUrl,caFile});
|
|
140
|
+
const payload=JSON.stringify({device_id:credential.device_id});
|
|
141
|
+
const headers={authorization:'Bearer '+credential.credential,'content-type':'application/json'};
|
|
142
|
+
let response;
|
|
143
|
+
if(gateway.origin.startsWith('https:')){
|
|
144
|
+
response=await new Promise((done,reject)=>{
|
|
145
|
+
const request=https.request(new URL('/device/status',gateway.origin),{
|
|
146
|
+
method:'POST',ca:readFileSync(gateway.caFile),
|
|
147
|
+
headers:{...headers,'content-length':Buffer.byteLength(payload)},
|
|
148
|
+
timeout:timeoutMs
|
|
149
|
+
},res=>{
|
|
150
|
+
let text='';
|
|
151
|
+
res.on('data',chunk=>{text+=chunk;if(text.length>2048)request.destroy(new Error(ERROR));});
|
|
152
|
+
res.on('end',()=>done({status:res.statusCode,text}));
|
|
153
|
+
});
|
|
154
|
+
request.on('error',reject);
|
|
155
|
+
request.on('timeout',()=>request.destroy(new Error(ERROR)));
|
|
156
|
+
request.end(payload);
|
|
157
|
+
});
|
|
158
|
+
}else{
|
|
159
|
+
const r=await fetch(new URL('/device/status',gateway.origin),{
|
|
160
|
+
method:'POST',headers,body:payload,signal:AbortSignal.timeout(timeoutMs)
|
|
161
|
+
});
|
|
162
|
+
const text=await r.text();
|
|
163
|
+
if(text.length>2048)fail();
|
|
164
|
+
response={status:r.status,text};
|
|
165
|
+
}
|
|
166
|
+
if(response.status!==200)fail();
|
|
167
|
+
let data;
|
|
168
|
+
try{data=JSON.parse(response.text);}catch{fail();}
|
|
169
|
+
if(data?.device_id!==credential.device_id||typeof data.connected!=='boolean'||
|
|
170
|
+
Object.keys(data).sort().join(',')!=='connected,device_id')fail();
|
|
171
|
+
return {connected:data.connected,gatewayVerified:true};
|
|
172
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// #310 Windows lexical path boundary for offline planning only. This is NOT
|
|
2
|
+
// proof of NTFS ownership/ACLs, reparse-point freedom, or an approved Root.
|
|
3
|
+
import {win32} from 'node:path';
|
|
4
|
+
const deny=()=>{throw Error('DEVICE_WINDOWS_PATH_DENIED');};
|
|
5
|
+
const reserved=/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i;
|
|
6
|
+
export function canonicalWindowsLocalFile(path){
|
|
7
|
+
if(typeof path!=='string'||path.length<4||path.length>1024||!(/^[A-Za-z]:\\/.test(path)))
|
|
8
|
+
deny();
|
|
9
|
+
// Reject UNC, device namespaces, forward slashes, ADS, env interpolation and
|
|
10
|
+
// shell metacharacters before a name can be used in a task argument.
|
|
11
|
+
if(/[\x00-\x1f\x7f"<>|?*\/&^%]/.test(path)||path.slice(2).includes(':'))deny();
|
|
12
|
+
const segments=path.slice(3).split('\\');
|
|
13
|
+
if(segments.length===0||segments.some(x=>!x||x==='.'||x==='..'||/[. ]$/.test(x)||reserved.test(x)))deny();
|
|
14
|
+
const normalized=win32.normalize(path);
|
|
15
|
+
if(normalized!==path)deny(); // no invisible normalization changes
|
|
16
|
+
return path;
|
|
17
|
+
}
|
|
18
|
+
export function isWindowsChildPath(path,root){
|
|
19
|
+
const child=canonicalWindowsLocalFile(path),parent=canonicalWindowsLocalFile(root);
|
|
20
|
+
const rel=win32.relative(parent,child);
|
|
21
|
+
if(!rel||rel==='..'||rel.startsWith('..\\')||win32.isAbsolute(rel))deny();
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
setlocal DisableDelayedExpansion
|
|
3
|
+
rem #310: package-local Windows bootstrap; no network download or privileged task.
|
|
4
|
+
rem Only run from an independently verified private package within this user profile.
|
|
5
|
+
if not "%~1"=="" (
|
|
6
|
+
echo DEVICE_INSTALL_ARGS_DENIED 1>&2
|
|
7
|
+
exit /b 2
|
|
8
|
+
)
|
|
9
|
+
where node >nul 2>nul
|
|
10
|
+
if errorlevel 1 (
|
|
11
|
+
echo DEVICE_INSTALL_NODE_UNSUPPORTED 1>&2
|
|
12
|
+
exit /b 1
|
|
13
|
+
)
|
|
14
|
+
node "%~dp0device.mjs" install-preflight
|
|
15
|
+
if errorlevel 1 exit /b %errorlevel%
|
|
16
|
+
node "%~dp0device.mjs" install
|
|
17
|
+
exit /b %errorlevel%
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# #310: package-local Windows bootstrap. Does not download or elevate.
|
|
2
|
+
# Run only from the verified private package in the current user's profile.
|
|
3
|
+
$ErrorActionPreference = 'Stop'
|
|
4
|
+
if ($args.Count -ne 0) {
|
|
5
|
+
[Console]::Error.WriteLine('DEVICE_INSTALL_ARGS_DENIED')
|
|
6
|
+
exit 2
|
|
7
|
+
}
|
|
8
|
+
$node = Get-Command node -CommandType Application -ErrorAction SilentlyContinue
|
|
9
|
+
if ($null -eq $node) {
|
|
10
|
+
[Console]::Error.WriteLine('DEVICE_INSTALL_NODE_UNSUPPORTED')
|
|
11
|
+
exit 1
|
|
12
|
+
}
|
|
13
|
+
$entry = Join-Path $PSScriptRoot 'device.mjs'
|
|
14
|
+
& $node.Source $entry install-preflight
|
|
15
|
+
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
|
16
|
+
& $node.Source $entry install
|
|
17
|
+
exit $LASTEXITCODE
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# #310: package-local Linux bootstrap; never executes downloaded shell text.
|
|
3
|
+
# Download/verify/signature trust must be handled before invoking this script.
|
|
4
|
+
set -eu
|
|
5
|
+
if [ "$#" -ne 0 ]; then
|
|
6
|
+
echo DEVICE_INSTALL_ARGS_DENIED >&2
|
|
7
|
+
exit 2
|
|
8
|
+
fi
|
|
9
|
+
if ! command -v node >/dev/null 2>&1; then
|
|
10
|
+
echo DEVICE_INSTALL_NODE_UNSUPPORTED >&2
|
|
11
|
+
exit 1
|
|
12
|
+
fi
|
|
13
|
+
entry_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)
|
|
14
|
+
node "$entry_dir/device.mjs" install-preflight
|
|
15
|
+
node "$entry_dir/device.mjs" install
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
@echo off
|
|
2
|
+
setlocal DisableDelayedExpansion
|
|
3
|
+
rem Windows native preflight only. No download, enrollment or service mutation.
|
|
4
|
+
where node >nul 2>nul
|
|
5
|
+
if errorlevel 1 (
|
|
6
|
+
echo DEVICE_INSTALL_NODE_UNSUPPORTED 1>&2
|
|
7
|
+
exit /b 1
|
|
8
|
+
)
|
|
9
|
+
node "%~dp0device.mjs" install-preflight
|
|
10
|
+
exit /b %errorlevel%
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Windows native preflight only. No download, enrollment or service mutation.
|
|
2
|
+
$ErrorActionPreference = 'Stop'
|
|
3
|
+
$node = Get-Command node -CommandType Application -ErrorAction SilentlyContinue
|
|
4
|
+
if ($null -eq $node) {
|
|
5
|
+
[Console]::Error.WriteLine('DEVICE_INSTALL_NODE_UNSUPPORTED')
|
|
6
|
+
exit 1
|
|
7
|
+
}
|
|
8
|
+
$entry = Join-Path $PSScriptRoot 'device.mjs'
|
|
9
|
+
& $node.Source $entry install-preflight
|
|
10
|
+
exit $LASTEXITCODE
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Read-only DEV installer diagnostics. Never output device keys, poll tokens or raw pair.out.
|
|
2
|
+
$ErrorActionPreference='Stop'
|
|
3
|
+
$identity=[Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
4
|
+
$local=[Environment]::GetFolderPath('LocalApplicationData')
|
|
5
|
+
$root=Join-Path $local 'RemoteShell'
|
|
6
|
+
$logDir=Join-Path $root 'EasyInstall\logs'
|
|
7
|
+
$install=Join-Path $root 'DeviceAgent\install'
|
|
8
|
+
$credentials=Join-Path $root 'DeviceAgent\dev-credentials'
|
|
9
|
+
Write-Output ('DIAG_AT='+((Get-Date).ToString('o')))
|
|
10
|
+
Write-Output ('WINDOWS_USER='+$identity)
|
|
11
|
+
Write-Output ('AGENT_INSTALLED='+(Test-Path -LiteralPath $install))
|
|
12
|
+
Write-Output ('LOCAL_CREDENTIAL_FILE_PRESENT='+(Test-Path -LiteralPath (Join-Path $credentials 'device.json')))
|
|
13
|
+
foreach($n in @('result.txt','pair.err','install.err','stage-agent.err','status.err')) {
|
|
14
|
+
$p=Join-Path $logDir $n
|
|
15
|
+
if(Test-Path -LiteralPath $p -PathType Leaf) {
|
|
16
|
+
Write-Output ('LOG_FILE='+$n)
|
|
17
|
+
foreach($line in (Get-Content -LiteralPath $p -Tail 90)) {
|
|
18
|
+
if($line -match '^(\xFEFF)?(RemoteShell EasyInstall DEV|AGENT_INSTALLED=|PAIR_|RESULT=|USER_|NEXT=|DEVICE_[A-Z0-9_]+$)' -or ($n -ne 'result.txt' -and $line -match '^[A-Z][A-Z0-9_]{4,90}$')) {
|
|
19
|
+
# Only predefined status lines and status codes, never JSON response bodies or URLs.
|
|
20
|
+
$clean=$line -replace 'https?://\S+','[URL]' -replace '(?i)(poll_token|capability|nonce|private_key)=\S+','$1=[REDACTED]'
|
|
21
|
+
Write-Output $clean
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
} else { Write-Output ('LOG_MISSING='+$n) }
|
|
25
|
+
}
|
|
26
|
+
$uninstall=Join-Path $root 'UninstallLogs\easy-uninstall-result.txt'
|
|
27
|
+
if(Test-Path -LiteralPath $uninstall -PathType Leaf) {
|
|
28
|
+
Write-Output 'UNINSTALL_RESULT'
|
|
29
|
+
Get-Content -LiteralPath $uninstall -Tail 25 | Where-Object {$_ -match '^(RemoteShell easy uninstall |AGENT_|USER_|LOCAL_|PRIVATE_|RESULT=)'}
|
|
30
|
+
}
|
|
31
|
+
Write-Output 'NOTE: Browser HTTP status and Google identity are NOT available from Agent logs.'
|
|
32
|
+
Write-Output 'NOTE: Pairing IDs are non-secret request correlations; never share raw pair.out.'
|