shellway 0.1.0 → 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/README.md +11 -0
- package/package.json +1 -1
- package/shellway.js +74 -4
package/README.md
CHANGED
|
@@ -22,3 +22,14 @@ Optional: `--name "Home laptop"` on `pair` sets the computer's name. `--server U
|
|
|
22
22
|
The agent makes an outbound connection to the Shellway relay. Requests from your AI app are passed to this computer and the result is passed back. File contents and command output are not stored by Shellway.
|
|
23
23
|
|
|
24
24
|
Support: support@getshellway.com
|
|
25
|
+
|
|
26
|
+
## Keep it running automatically
|
|
27
|
+
|
|
28
|
+
After pairing, run once:
|
|
29
|
+
|
|
30
|
+
npx shellway service install # add --readonly for read-only mode
|
|
31
|
+
|
|
32
|
+
Shellway then starts at login/boot and restarts itself if it stops (Linux: systemd user service, macOS: launchd, Windows: scheduled task). On Linux, if you want it to keep running after you log out: `sudo loginctl enable-linger $USER`.
|
|
33
|
+
|
|
34
|
+
npx shellway service status
|
|
35
|
+
npx shellway service uninstall
|
package/package.json
CHANGED
package/shellway.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import { spawn } from 'node:child_process';
|
|
3
3
|
import { homedir, hostname, platform, release } from 'node:os';
|
|
4
4
|
import { join, resolve } from 'node:path';
|
|
5
|
-
import { mkdirSync, readFileSync, writeFileSync, chmodSync, existsSync } from 'node:fs';
|
|
5
|
+
import { mkdirSync, readFileSync, writeFileSync, chmodSync, existsSync, copyFileSync, rmSync } from 'node:fs';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { execFileSync } from 'node:child_process';
|
|
6
8
|
import { readFile, writeFile, appendFile, readdir, stat } from 'node:fs/promises';
|
|
7
9
|
|
|
8
10
|
if (Number(process.versions.node.split('.')[0]) < 22) {
|
|
@@ -20,7 +22,7 @@ const has = (n) => args.includes(`--${n}`);
|
|
|
20
22
|
const cmd = args[0];
|
|
21
23
|
|
|
22
24
|
function loadConf() {
|
|
23
|
-
if (!existsSync(CONF)) { console.error('Not paired yet. Run: shellway pair <CODE> --server <URL>'); process.exit(
|
|
25
|
+
if (!existsSync(CONF)) { console.error('Not paired yet. Run: shellway pair <CODE> --server <URL>'); process.exit(78); }
|
|
24
26
|
return JSON.parse(readFileSync(CONF, 'utf8'));
|
|
25
27
|
}
|
|
26
28
|
|
|
@@ -97,7 +99,7 @@ function start() {
|
|
|
97
99
|
const wsUrl = conf.server.replace(/^http/, 'ws') + '/agent';
|
|
98
100
|
let delay = 1000;
|
|
99
101
|
const connect = async () => {
|
|
100
|
-
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(
|
|
102
|
+
if (!(await tokenValid(conf))) { console.error('This device was removed or its token is invalid. Pair again.'); process.exit(78); }
|
|
101
103
|
const ws = new WebSocket(wsUrl, { headers: { authorization: `Bearer ${conf.device_token}` } });
|
|
102
104
|
ws.onopen = () => { delay = 1000; console.log(`Connected as "${conf.device_name}"${readonly ? ' (read-only)' : ''}. Leave this running.`); };
|
|
103
105
|
ws.onmessage = async (ev) => {
|
|
@@ -118,6 +120,74 @@ function start() {
|
|
|
118
120
|
connect();
|
|
119
121
|
}
|
|
120
122
|
|
|
123
|
+
// ---- auto-start service (systemd user unit / launchd agent / Windows scheduled task) ----
|
|
124
|
+
const xml = (v) => String(v).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
125
|
+
function serviceSpec() {
|
|
126
|
+
// Copy ourselves to a stable path so the service survives npx cache cleanup.
|
|
127
|
+
const dest = join(CONF_DIR, 'agent.mjs');
|
|
128
|
+
const argv = [process.execPath, dest, 'start', ...(has('readonly') ? ['--readonly'] : [])];
|
|
129
|
+
return { dest, argv };
|
|
130
|
+
}
|
|
131
|
+
const run = (c, a, opts = {}) => execFileSync(c, a, { stdio: opts.quiet ? 'pipe' : 'inherit', encoding: 'utf8' });
|
|
132
|
+
|
|
133
|
+
function renderService(os, argv) {
|
|
134
|
+
const home = process.env.SHELLWAY_HOME ? `Environment=SHELLWAY_HOME=${process.env.SHELLWAY_HOME}\n` : '';
|
|
135
|
+
if (os === 'linux') return `[Unit]\nDescription=Shellway agent\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\n${home}ExecStart=${argv.map((a) => (/\s/.test(a) ? `"${a}"` : a)).join(' ')}\nRestart=always\nRestartSec=5\nRestartPreventExitStatus=78\n\n[Install]\nWantedBy=default.target\n`;
|
|
136
|
+
if (os === 'darwin') return `<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">\n<plist version="1.0"><dict>\n<key>Label</key><string>com.getshellway.agent</string>\n<key>ProgramArguments</key><array>${argv.map((a) => `<string>${xml(a)}</string>`).join('')}</array>\n${process.env.SHELLWAY_HOME ? `<key>EnvironmentVariables</key><dict><key>SHELLWAY_HOME</key><string>${xml(process.env.SHELLWAY_HOME)}</string></dict>\n` : ''}<key>RunAtLoad</key><true/>\n<key>KeepAlive</key><true/>\n<key>ThrottleInterval</key><integer>30</integer>\n<key>StandardOutPath</key><string>${xml(join(CONF_DIR, 'agent.log'))}</string>\n<key>StandardErrorPath</key><string>${xml(join(CONF_DIR, 'agent.log'))}</string>\n</dict></plist>\n`;
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function service() {
|
|
141
|
+
const sub = args[1];
|
|
142
|
+
const os = platform();
|
|
143
|
+
const { dest, argv } = serviceSpec();
|
|
144
|
+
const unitPath = os === 'linux' ? join(homedir(), '.config/systemd/user/shellway.service')
|
|
145
|
+
: os === 'darwin' ? join(homedir(), 'Library/LaunchAgents/com.getshellway.agent.plist') : null;
|
|
146
|
+
if (!['install', 'uninstall', 'status', 'print'].includes(sub)) {
|
|
147
|
+
console.log('shellway service install [--readonly] start Shellway automatically at login / boot\nshellway service uninstall\nshellway service status'); return;
|
|
148
|
+
}
|
|
149
|
+
if (sub === 'print') { console.log(renderService(os, argv) ?? '(Windows uses a scheduled task)'); return; }
|
|
150
|
+
if (!['linux', 'darwin', 'win32'].includes(os)) { console.error(`Auto-start is not supported on ${os}. Use: shellway start`); process.exit(1); }
|
|
151
|
+
if (sub === 'install') {
|
|
152
|
+
loadConf();
|
|
153
|
+
mkdirSync(CONF_DIR, { recursive: true });
|
|
154
|
+
copyFileSync(fileURLToPath(import.meta.url), dest);
|
|
155
|
+
if (os === 'linux') {
|
|
156
|
+
mkdirSync(join(homedir(), '.config/systemd/user'), { recursive: true });
|
|
157
|
+
writeFileSync(unitPath, renderService(os, argv));
|
|
158
|
+
run('systemctl', ['--user', 'daemon-reload']);
|
|
159
|
+
run('systemctl', ['--user', 'enable', '--now', 'shellway.service']);
|
|
160
|
+
try { run('loginctl', ['enable-linger', process.env.USER || ''], { quiet: true }); }
|
|
161
|
+
catch { console.log('Note: to keep running after you log out, ask an admin to run: sudo loginctl enable-linger $USER'); }
|
|
162
|
+
} else if (os === 'darwin') {
|
|
163
|
+
mkdirSync(join(homedir(), 'Library/LaunchAgents'), { recursive: true });
|
|
164
|
+
writeFileSync(unitPath, renderService(os, argv));
|
|
165
|
+
try { run('launchctl', ['unload', unitPath], { quiet: true }); } catch {}
|
|
166
|
+
run('launchctl', ['load', '-w', unitPath]);
|
|
167
|
+
} else {
|
|
168
|
+
const tr = argv.map((a) => `"${a}"`).join(' ');
|
|
169
|
+
run('schtasks', ['/Create', '/F', '/SC', 'ONLOGON', '/RL', 'LIMITED', '/TN', 'Shellway', '/TR', tr]);
|
|
170
|
+
run('schtasks', ['/Run', '/TN', 'Shellway']);
|
|
171
|
+
}
|
|
172
|
+
console.log(`Done. Shellway now starts automatically${has('readonly') ? ' (read-only)' : ''}. Check: shellway service status`);
|
|
173
|
+
} else if (sub === 'uninstall') {
|
|
174
|
+
try {
|
|
175
|
+
if (os === 'linux') { try { run('systemctl', ['--user', 'disable', '--now', 'shellway.service']); } catch {} rmSync(unitPath, { force: true }); try { run('systemctl', ['--user', 'daemon-reload'], { quiet: true }); } catch {} }
|
|
176
|
+
else if (os === 'darwin') { try { run('launchctl', ['unload', '-w', unitPath], { quiet: true }); } catch {} rmSync(unitPath, { force: true }); }
|
|
177
|
+
else { try { run('schtasks', ['/End', '/TN', 'Shellway'], { quiet: true }); } catch {} run('schtasks', ['/Delete', '/F', '/TN', 'Shellway']); }
|
|
178
|
+
} catch (e) { console.error(e.message); process.exit(1); }
|
|
179
|
+
rmSync(dest, { force: true });
|
|
180
|
+
console.log('Auto-start removed. (Your pairing is kept; run "shellway start" to run manually.)');
|
|
181
|
+
} else {
|
|
182
|
+
try {
|
|
183
|
+
if (os === 'linux') run('systemctl', ['--user', 'status', 'shellway.service', '--no-pager', '-n', '5']);
|
|
184
|
+
else if (os === 'darwin') run('launchctl', ['list', 'com.getshellway.agent']);
|
|
185
|
+
else run('schtasks', ['/Query', '/TN', 'Shellway']);
|
|
186
|
+
} catch { console.log('Auto-start is not installed (or not running).'); }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
121
190
|
if (cmd === 'pair') await pair();
|
|
122
191
|
else if (cmd === 'start') start();
|
|
123
|
-
else
|
|
192
|
+
else if (cmd === 'service') service();
|
|
193
|
+
else console.log('shellway pair <CODE> [--server URL] [--name NAME]\nshellway start [--readonly]\nshellway service install|uninstall|status [--readonly]');
|