shellway 0.6.2 → 0.6.4
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/package.json +1 -1
- package/shellway.js +51 -3
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
7
7
|
import { execFileSync, execFile } from 'node:child_process';
|
|
8
8
|
import { readFile, writeFile, appendFile, readdir, stat, lstat, mkdir, rename, cp, rm } from 'node:fs/promises';
|
|
9
9
|
|
|
10
|
-
const AGENT_VERSION = '0.6.
|
|
10
|
+
const AGENT_VERSION = '0.6.4'; // keep equal to agent/package.json
|
|
11
11
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
12
12
|
console.error(`Shellway needs Node.js 22 or newer (you have ${process.versions.node}). Get it at https://nodejs.org`);
|
|
13
13
|
process.exit(1);
|
|
@@ -563,7 +563,35 @@ function blockedBy(text, pol) {
|
|
|
563
563
|
return pol.blocked_commands.find((b) => new RegExp(`(^|[^\\w-])${escRe(b.trim()).replace(/\s+/g, '\\s+')}($|[^\\w-])`, 'i').test(text));
|
|
564
564
|
}
|
|
565
565
|
// Throws when the owner's rules forbid this call. May fill in a default cwd inside an allowed folder.
|
|
566
|
+
// Always on (with or without rules): the AI cannot read or change Shellway's own folder (pairing token, safety
|
|
567
|
+
// rules, the agent itself) with file tools, and cannot run "shellway policy" or touch that folder with commands.
|
|
568
|
+
const PROTECT_MSG = "Shellway's own settings (the .shellway folder and \"shellway policy\") can only be changed by the owner directly on the computer, not through the AI. Nothing was done.";
|
|
569
|
+
function protectSelf(method, params) {
|
|
570
|
+
if (PATH_PARAMS[method]) {
|
|
571
|
+
const root = normCase(realLocation(CONF_DIR));
|
|
572
|
+
for (const k of PATH_PARAMS[method]) {
|
|
573
|
+
if (k === 'cwd') continue;
|
|
574
|
+
const vals = k === 'paths' ? (Array.isArray(params[k]) ? params[k] : []) : (params[k] === undefined ? [] : [params[k]]);
|
|
575
|
+
for (const v of vals) {
|
|
576
|
+
if (method === 'open_file' && /^https?:\/\//.test(String(v))) continue;
|
|
577
|
+
const real = normCase(realLocation(String(v)));
|
|
578
|
+
if (real === root || real.startsWith(root + sep)) {
|
|
579
|
+
// backups stay readable so the AI can help restore an older copy of a file
|
|
580
|
+
const backups = root + sep + 'backups';
|
|
581
|
+
const readOnly = ['read_file', 'read_multiple_files', 'list_directory', 'get_file_info', 'search_files', 'start_search'].includes(method);
|
|
582
|
+
if (!(readOnly && (real === backups || real.startsWith(backups + sep)))) throw new Error(PROTECT_MSG);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
if (COMMAND_METHODS.has(method)) {
|
|
588
|
+
const text = method === 'interact_with_process' ? String(params.input ?? '') : String(params.command ?? '');
|
|
589
|
+
if (/[\\/]\.shellway\b|%USERPROFILE%[\\/]\.shellway|~[\\/]\.shellway|\$HOME[\\/]\.shellway|\$env:USERPROFILE[\\/]\.shellway/i.test(text) || /\bshellway(?:@\S+)?(?:\.(?:js|mjs))?["']?\s+policy\b/i.test(text)) throw new Error(PROTECT_MSG);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
export { protectSelf };
|
|
566
593
|
function checkPolicy(method, params) {
|
|
594
|
+
protectSelf(method, params);
|
|
567
595
|
const pol = loadPolicy();
|
|
568
596
|
if (!pol) return params;
|
|
569
597
|
const fix = 'The owner of this computer set this rule; it can only be changed on the computer (shellway policy).';
|
|
@@ -613,7 +641,7 @@ function policyCli() {
|
|
|
613
641
|
console.log(`Blocked commands: ${q.blocked_commands.length ? q.blocked_commands.join(', ') : 'none'}`);
|
|
614
642
|
console.log(`Backups before a file is changed: ${q.backups ? `on (${join(CONF_DIR, 'backups')})` : 'off'}`);
|
|
615
643
|
console.log(`Keep awake while Shellway runs: ${q.keep_awake ? 'on' : 'off'}`);
|
|
616
|
-
console.log(`Screen, apps, mouse and keyboard: ${q.desktop ? 'ON (the AI can see the screen and
|
|
644
|
+
console.log(`Screen, apps, mouse and keyboard: ${q.desktop ? (platform() === 'win32' ? 'ON (the AI can open, switch and close apps, click and type; no screenshots on Windows yet)' : 'ON (the AI can see the screen; apps, mouse and keyboard on Windows)') : 'off'}`);
|
|
617
645
|
if (q.allowed_folders.length && q.commands) console.log('Note: commands start inside an allowed folder, but a command can still reach other folders. For a strict lock, also run: shellway policy commands off');
|
|
618
646
|
console.log('Rules apply right away; no restart needed.');
|
|
619
647
|
};
|
|
@@ -854,9 +882,29 @@ function start() {
|
|
|
854
882
|
setInterval(keepAwakeTick, 60_000);
|
|
855
883
|
const wsUrl = conf.server.replace(/^http/, 'ws') + '/agent';
|
|
856
884
|
let delay = 1000;
|
|
885
|
+
// 0.6.4 (owner PC 15 Nov 09:13): after sleep or a network change the socket can stay "open" on this side while the
|
|
886
|
+
// server has already dropped it (heartbeat timeout) — the agent then never reconnects. Watchdog: a tiny keepalive
|
|
887
|
+
// every 15 s (the relay ignores messages without an id) makes a dead connection fail fast, a clock jump means the
|
|
888
|
+
// computer slept, and data stuck in the send buffer for ~1 minute means the link is gone. Any of these: reconnect now.
|
|
889
|
+
let current = null, lastTick = Date.now();
|
|
890
|
+
const reconnectNow = (why) => {
|
|
891
|
+
const w = current; if (!w) return;
|
|
892
|
+
current = null; w.onclose = null; w.onmessage = null;
|
|
893
|
+
try { w.close(); } catch {}
|
|
894
|
+
console.log(`${why} — reconnecting…`);
|
|
895
|
+
delay = 1000; setTimeout(connect, 500);
|
|
896
|
+
};
|
|
897
|
+
setInterval(() => {
|
|
898
|
+
const t = Date.now(), slept = t - lastTick > 45_000; lastTick = t;
|
|
899
|
+
const w = current; if (!w || w.readyState !== 1) return;
|
|
900
|
+
if (slept) return reconnectNow('Computer woke up');
|
|
901
|
+
try { w.send('{"type":"keepalive"}'); } catch { return reconnectNow('Connection lost'); }
|
|
902
|
+
if (w.bufferedAmount > 0) { w._stuck = (w._stuck || 0) + 1; if (w._stuck >= 4) reconnectNow('Connection stuck'); } else w._stuck = 0;
|
|
903
|
+
}, 15_000);
|
|
857
904
|
const connect = async () => {
|
|
858
905
|
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
859
906
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}`, 'x-shellway-version': AGENT_VERSION } });
|
|
907
|
+
current = ws;
|
|
860
908
|
ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
861
909
|
ws.onmessage = async (ev) => {
|
|
862
910
|
let m; try { m = JSON.parse(String(ev.data)); } catch { return; }
|
|
@@ -867,7 +915,7 @@ function start() {
|
|
|
867
915
|
ws.send(JSON.stringify({ id: m.id, error: e.message }));
|
|
868
916
|
}
|
|
869
917
|
};
|
|
870
|
-
ws.onclose = () => { console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
918
|
+
ws.onclose = () => { if (current === ws) current = null; console.log(`Disconnected. Retrying in ${Math.round(delay / 1000)}s…`); setTimeout(connect, delay); delay = Math.min(delay * 2, 30_000); };
|
|
871
919
|
ws.onerror = () => {};
|
|
872
920
|
};
|
|
873
921
|
connect();
|