@lethevimlet/sshift 1.7.9 → 1.8.1

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.
@@ -0,0 +1,314 @@
1
+ /**
2
+ * Local certificate authority + CA-signed server certificate.
3
+ *
4
+ * Why a CA instead of a plain self-signed leaf: Android (and Brave/Chrome
5
+ * on Android) will only install a certificate as a trust anchor when it
6
+ * is a real CA certificate (basicConstraints CA:TRUE, keyCertSign). A
7
+ * self-signed leaf without those extensions is rejected or silently
8
+ * ignored by the "Install a certificate → CA certificate" flow, so the
9
+ * "Not Secure" warning never goes away on the phone.
10
+ *
11
+ * Layout (all files live in the user-space data directory, which
12
+ * survives `npm install -g` updates and Docker container recreation when
13
+ * /data is a volume):
14
+ *
15
+ * ssl-ca-cert.pem the local CA, valid 10 years → install THIS on devices
16
+ * ssl-ca-key.pem the CA private key (0600)
17
+ * ssl-cert.pem the server certificate signed by the CA
18
+ * ssl-key.pem the server private key (0600)
19
+ *
20
+ * The CA is generated once and never rotated automatically, so devices
21
+ * only need to trust it once. The server certificate is re-issued from
22
+ * the same CA whenever it is missing, not signed by the CA (old
23
+ * self-signed installs), close to expiry, or no longer lists the
24
+ * machine's current hostname / IPs in its SANs. A re-issued server
25
+ * certificate keeps working on every device that already trusts the CA.
26
+ */
27
+
28
+ const fs = require('fs');
29
+ const path = require('path');
30
+ const os = require('os');
31
+ const { X509Certificate } = require('crypto');
32
+ const selfsigned = require('selfsigned');
33
+
34
+ const SSL_CERT_FILE = 'ssl-cert.pem';
35
+ const SSL_KEY_FILE = 'ssl-key.pem';
36
+ const SSL_CA_CERT_FILE = 'ssl-ca-cert.pem';
37
+ const SSL_CA_KEY_FILE = 'ssl-ca-key.pem';
38
+
39
+ const CA_VALID_DAYS = 3650;
40
+ // Apple (and, following it, Chrome) reject leaf certificates valid for
41
+ // longer than 825 days.
42
+ const SERVER_VALID_DAYS = 825;
43
+ // Re-issue the server certificate when it has less than this left.
44
+ const RENEW_BEFORE_DAYS = 30;
45
+ const ORG_NAME = 'sshift';
46
+
47
+ const DAY_MS = 24 * 60 * 60 * 1000;
48
+
49
+ /**
50
+ * Collect the names the server certificate must be valid for.
51
+ * @returns {{hostname: string, ips: string[]}}
52
+ */
53
+ function collectLocalNames() {
54
+ const ips = ['127.0.0.1'];
55
+ const interfaces = os.networkInterfaces();
56
+ Object.values(interfaces).forEach(iface => {
57
+ (iface || []).forEach(addr => {
58
+ if (addr.family === 'IPv4' && !addr.internal && !ips.includes(addr.address)) {
59
+ ips.push(addr.address);
60
+ }
61
+ });
62
+ });
63
+ const hostname = os.hostname() || 'localhost';
64
+ return { hostname, ips };
65
+ }
66
+
67
+ function daysUntil(dateStr) {
68
+ return (new Date(dateStr).getTime() - Date.now()) / DAY_MS;
69
+ }
70
+
71
+ function parseCert(pem) {
72
+ try {
73
+ return new X509Certificate(pem);
74
+ } catch {
75
+ return null;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Returns null when the CA certificate/key pair is usable, otherwise a
81
+ * human-readable reason it must be regenerated.
82
+ * @param {string} caCertPem
83
+ * @param {string} caKeyPem
84
+ * @returns {string|null}
85
+ */
86
+ function caProblem(caCertPem, caKeyPem) {
87
+ const ca = parseCert(caCertPem);
88
+ if (!ca) return 'CA certificate is not parseable';
89
+ if (!ca.ca) return 'CA certificate is not marked as a CA (basicConstraints CA:TRUE)';
90
+ if (daysUntil(ca.validTo) < RENEW_BEFORE_DAYS) return 'CA certificate has expired or expires soon';
91
+ try {
92
+ const { createPrivateKey } = require('crypto');
93
+ const key = createPrivateKey(caKeyPem);
94
+ if (!ca.checkPrivateKey(key)) return 'CA private key does not match the CA certificate';
95
+ } catch (err) {
96
+ return `CA private key is not usable: ${err.message}`;
97
+ }
98
+ return null;
99
+ }
100
+
101
+ /**
102
+ * Returns null when the server certificate is usable with this CA and
103
+ * these names, otherwise a human-readable reason it must be re-issued.
104
+ * @param {string} certPem
105
+ * @param {string} keyPem
106
+ * @param {string} caCertPem
107
+ * @param {{hostname: string, ips: string[]}} names
108
+ * @returns {string|null}
109
+ */
110
+ function serverCertProblem(certPem, keyPem, caCertPem, names) {
111
+ const cert = parseCert(certPem);
112
+ if (!cert) return 'server certificate is not parseable';
113
+ const ca = parseCert(caCertPem);
114
+ if (!ca) return 'CA certificate is not parseable';
115
+
116
+ let issuedByCa = false;
117
+ try {
118
+ issuedByCa = cert.checkIssued(ca) && cert.verify(ca.publicKey);
119
+ } catch {
120
+ issuedByCa = false;
121
+ }
122
+ if (!issuedByCa) return 'server certificate is not signed by the local CA (old self-signed certificate)';
123
+ if (cert.ca) return 'server certificate is marked as a CA';
124
+ if (daysUntil(cert.validTo) < RENEW_BEFORE_DAYS) return 'server certificate has expired or expires soon';
125
+
126
+ try {
127
+ const { createPrivateKey } = require('crypto');
128
+ if (!cert.checkPrivateKey(createPrivateKey(keyPem))) {
129
+ return 'server private key does not match the server certificate';
130
+ }
131
+ } catch (err) {
132
+ return `server private key is not usable: ${err.message}`;
133
+ }
134
+
135
+ // subjectAltName looks like "DNS:localhost, DNS:host, IP Address:127.0.0.1"
136
+ const san = cert.subjectAltName || '';
137
+ const missing = [];
138
+ if (!new RegExp(`DNS:${escapeRegExp(names.hostname)}(,|$)`).test(san)) missing.push(names.hostname);
139
+ names.ips.forEach(ip => {
140
+ if (!new RegExp(`IP Address:${escapeRegExp(ip)}(,|$)`).test(san)) missing.push(ip);
141
+ });
142
+ if (missing.length) return `server certificate does not cover: ${missing.join(', ')}`;
143
+
144
+ return null;
145
+ }
146
+
147
+ function escapeRegExp(s) {
148
+ return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
149
+ }
150
+
151
+ /**
152
+ * Generate the local CA (10 years, RSA 2048, SHA-256).
153
+ * @param {string} hostname - shown in the CA name so several sshift
154
+ * hosts can be told apart in a device's credential list
155
+ * @returns {Promise<{cert: string, key: string}>}
156
+ */
157
+ async function generateCA(hostname) {
158
+ const notBefore = new Date();
159
+ const notAfter = new Date(notBefore.getTime() + CA_VALID_DAYS * DAY_MS);
160
+ const pems = await selfsigned.generate(
161
+ [
162
+ { name: 'commonName', value: `sshift Local CA (${hostname})` },
163
+ { name: 'organizationName', value: ORG_NAME }
164
+ ],
165
+ {
166
+ keySize: 2048,
167
+ algorithm: 'sha256',
168
+ notBeforeDate: notBefore,
169
+ notAfterDate: notAfter,
170
+ extensions: [
171
+ { name: 'basicConstraints', cA: true, critical: true },
172
+ { name: 'keyUsage', keyCertSign: true, cRLSign: true, critical: true }
173
+ ]
174
+ }
175
+ );
176
+ return { cert: pems.cert, key: pems.private };
177
+ }
178
+
179
+ /**
180
+ * Issue a server certificate signed by the local CA.
181
+ * @param {{cert: string, key: string}} ca
182
+ * @param {{hostname: string, ips: string[]}} names
183
+ * @returns {Promise<{cert: string, key: string}>}
184
+ */
185
+ async function generateServerCert(ca, names) {
186
+ const notBefore = new Date();
187
+ const notAfter = new Date(notBefore.getTime() + SERVER_VALID_DAYS * DAY_MS);
188
+ const dnsNames = ['localhost'];
189
+ if (names.hostname && names.hostname !== 'localhost') dnsNames.push(names.hostname);
190
+ const altNames = [
191
+ ...dnsNames.map(value => ({ type: 2, value })),
192
+ ...names.ips.map(ip => ({ type: 7, ip }))
193
+ ];
194
+ const pems = await selfsigned.generate(
195
+ [
196
+ { name: 'commonName', value: names.hostname },
197
+ { name: 'organizationName', value: ORG_NAME }
198
+ ],
199
+ {
200
+ keySize: 2048,
201
+ algorithm: 'sha256',
202
+ notBeforeDate: notBefore,
203
+ notAfterDate: notAfter,
204
+ ca: { cert: ca.cert, key: ca.key },
205
+ extensions: [
206
+ { name: 'basicConstraints', cA: false, critical: true },
207
+ { name: 'keyUsage', digitalSignature: true, keyEncipherment: true, critical: true },
208
+ { name: 'extKeyUsage', serverAuth: true },
209
+ { name: 'subjectAltName', altNames }
210
+ ]
211
+ }
212
+ );
213
+ return { cert: pems.cert, key: pems.private };
214
+ }
215
+
216
+ function readPair(certPath, keyPath) {
217
+ if (!fs.existsSync(certPath) || !fs.existsSync(keyPath)) return null;
218
+ try {
219
+ return {
220
+ cert: fs.readFileSync(certPath, 'utf8'),
221
+ key: fs.readFileSync(keyPath, 'utf8')
222
+ };
223
+ } catch {
224
+ return null;
225
+ }
226
+ }
227
+
228
+ function writePair(certPath, keyPath, pair) {
229
+ fs.mkdirSync(path.dirname(certPath), { recursive: true });
230
+ // Certificates are public (they are served by /api/cert); keys are not.
231
+ fs.writeFileSync(certPath, pair.cert, { mode: 0o644 });
232
+ fs.writeFileSync(keyPath, pair.key, { mode: 0o600 });
233
+ }
234
+
235
+ /**
236
+ * Make sure a usable local CA and a CA-signed server certificate exist in
237
+ * dataDir, creating or re-issuing whatever is missing or stale.
238
+ *
239
+ * @param {string} dataDir
240
+ * @param {object} [opts]
241
+ * @param {{hostname: string, ips: string[]}} [opts.names] - override the
242
+ * names the certificate must cover (tests)
243
+ * @param {(msg: string) => void} [opts.log]
244
+ * @returns {Promise<{cert: string, key: string, caCert: string,
245
+ * caCreated: boolean, certIssued: boolean}>}
246
+ * `cert` is the full chain (server certificate followed by the CA) so
247
+ * TLS clients receive the issuer too.
248
+ */
249
+ async function ensureLocalCertificates(dataDir, opts = {}) {
250
+ const log = opts.log || (() => {});
251
+ const names = opts.names || collectLocalNames();
252
+
253
+ const caCertPath = path.join(dataDir, SSL_CA_CERT_FILE);
254
+ const caKeyPath = path.join(dataDir, SSL_CA_KEY_FILE);
255
+ const certPath = path.join(dataDir, SSL_CERT_FILE);
256
+ const keyPath = path.join(dataDir, SSL_KEY_FILE);
257
+
258
+ let ca = readPair(caCertPath, caKeyPath);
259
+ let caCreated = false;
260
+ if (ca) {
261
+ const problem = caProblem(ca.cert, ca.key);
262
+ if (problem) {
263
+ log(`Local CA in ${dataDir} is unusable (${problem}); generating a new one`);
264
+ ca = null;
265
+ }
266
+ }
267
+ if (!ca) {
268
+ log('Generating local certificate authority (valid 10 years)...');
269
+ ca = await generateCA(names.hostname);
270
+ writePair(caCertPath, caKeyPath, ca);
271
+ caCreated = true;
272
+ log(`Local CA saved to ${caCertPath} — install this file on your devices (also served at /api/cert)`);
273
+ }
274
+
275
+ let server = readPair(certPath, keyPath);
276
+ let certIssued = false;
277
+ if (server) {
278
+ const problem = serverCertProblem(server.cert, server.key, ca.cert, names);
279
+ if (problem) {
280
+ log(`Re-issuing server certificate: ${problem}`);
281
+ server = null;
282
+ }
283
+ }
284
+ if (!server) {
285
+ log(`Issuing server certificate for ${names.hostname}, ${names.ips.join(', ')} (signed by the local CA)`);
286
+ server = await generateServerCert(ca, names);
287
+ writePair(certPath, keyPath, server);
288
+ certIssued = true;
289
+ if (!caCreated) {
290
+ log('The local CA is unchanged: devices that already trust it need no action');
291
+ }
292
+ }
293
+
294
+ return {
295
+ cert: `${server.cert.trim()}\n${ca.cert.trim()}\n`,
296
+ key: server.key,
297
+ caCert: ca.cert,
298
+ caCreated,
299
+ certIssued
300
+ };
301
+ }
302
+
303
+ module.exports = {
304
+ SSL_CERT_FILE,
305
+ SSL_KEY_FILE,
306
+ SSL_CA_CERT_FILE,
307
+ SSL_CA_KEY_FILE,
308
+ collectLocalNames,
309
+ caProblem,
310
+ serverCertProblem,
311
+ generateCA,
312
+ generateServerCert,
313
+ ensureLocalCertificates
314
+ };
@@ -372,18 +372,14 @@ body {
372
372
  }
373
373
 
374
374
  .overflow-toggle {
375
- background: none;
376
- border: none;
377
- color: var(--text-secondary);
378
- padding: 8px;
379
- border-radius: var(--radius-sm);
380
375
  cursor: pointer;
381
- transition: var(--transition-fast);
382
376
  }
383
377
 
384
- .overflow-toggle:hover {
385
- background: var(--bg-hover);
386
- color: var(--text-primary);
378
+ /* Mobile-only controls in the tabs bar / sidebar (the mobile header is gone) */
379
+ /* (.btn sets display later in the file; keep specificity above it) */
380
+ .btn.mobile-bookmarks-btn,
381
+ .btn.sidebar-close-btn {
382
+ display: none;
387
383
  }
388
384
 
389
385
  .overflow-dropdown {
@@ -3453,7 +3449,9 @@ body.dragging-tabs .tabs-container {
3453
3449
  @media (max-width: 768px) {
3454
3450
  :root {
3455
3451
  --sidebar-width: 100%;
3456
- --header-height: 43px;
3452
+ /* No header on mobile (see .header below); everything that offsets
3453
+ by the header height (tabs bar, sidebar, keyboard math) gets 0. */
3454
+ --header-height: 0px;
3457
3455
  }
3458
3456
 
3459
3457
  /* Thinner xterm scrollbar on mobile */
@@ -3519,9 +3517,39 @@ body.dragging-tabs .tabs-container {
3519
3517
  display: none;
3520
3518
  }
3521
3519
 
3522
- /* Show mobile overflow menu on mobile */
3520
+ /* Show mobile overflow menu on mobile (lives in the tabs bar) */
3523
3521
  .mobile-overflow-menu {
3524
3522
  display: block;
3523
+ flex-shrink: 0;
3524
+ }
3525
+
3526
+ .tabs-actions .overflow-toggle {
3527
+ min-width: 28px;
3528
+ min-height: 28px;
3529
+ padding: 4px 6px;
3530
+ font-size: 12px;
3531
+ }
3532
+
3533
+ /* Mobile header is removed: the tabs bar is the only top bar. It holds
3534
+ bookmarks | tabs dropdown | resize | font -/+ | keys | mic | ... */
3535
+ .header {
3536
+ display: none;
3537
+ }
3538
+
3539
+ .btn.mobile-bookmarks-btn {
3540
+ display: flex;
3541
+ align-items: center;
3542
+ justify-content: center;
3543
+ min-width: 28px;
3544
+ min-height: 28px;
3545
+ padding: 4px 6px;
3546
+ margin-left: 4px;
3547
+ font-size: 12px;
3548
+ flex-shrink: 0;
3549
+ }
3550
+
3551
+ .btn.sidebar-close-btn {
3552
+ display: inline-flex;
3525
3553
  }
3526
3554
 
3527
3555
  /* Hide desktop tabs on mobile */
@@ -3529,10 +3557,12 @@ body.dragging-tabs .tabs-container {
3529
3557
  display: none;
3530
3558
  }
3531
3559
 
3532
- /* Show mobile tabs dropdown on mobile */
3560
+ /* Show mobile tabs dropdown on mobile. min-width: 0 lets it shrink
3561
+ (label ellipsis) so the bookmarks and overflow buttons fit. */
3533
3562
  .mobile-tabs-dropdown {
3534
3563
  display: flex;
3535
3564
  margin-left: 4px;
3565
+ min-width: 0;
3536
3566
  }
3537
3567
 
3538
3568
  .mobile-force-resize-btn {
@@ -3545,7 +3575,9 @@ body.dragging-tabs .tabs-container {
3545
3575
 
3546
3576
  .mobile-tabs-toggle {
3547
3577
  min-height: 28px;
3578
+ min-width: 0;
3548
3579
  padding: 4px 6px;
3580
+ gap: 6px;
3549
3581
  font-size: 12px;
3550
3582
  }
3551
3583
 
@@ -3694,7 +3726,8 @@ body.dragging-tabs .tabs-container {
3694
3726
  }
3695
3727
 
3696
3728
  /* Keep tabs-actions buttons at 32px for consistency */
3697
- .tabs-actions .btn-sm {
3729
+ .tabs-actions .btn-sm,
3730
+ .mobile-bookmarks-btn {
3698
3731
  min-height: 28px;
3699
3732
  min-width: 28px;
3700
3733
  padding: 4px 6px;
@@ -4493,10 +4526,11 @@ body.dragging-tabs .tabs-container {
4493
4526
  transform: none !important;
4494
4527
  }
4495
4528
 
4496
- /* Add padding to main-content to account for fixed header and tabs */
4529
+ /* Add padding to main-content to account for the fixed tabs bar
4530
+ (there is no header on mobile any more) */
4497
4531
  .main-content {
4498
- /* Header: 43px + 1px border, Tabs: 35px + 1px border = 80px total */
4499
- padding-top: 80px;
4532
+ /* Tabs bar: 37px incl. border */
4533
+ padding-top: 37px;
4500
4534
  /* Account for mobile keys bar at bottom - use padding-bottom */
4501
4535
  padding-bottom: 75px;
4502
4536
  }
@@ -4572,8 +4606,8 @@ body.dragging-tabs .tabs-container {
4572
4606
  }
4573
4607
 
4574
4608
  body.keyboard-visible .main-content {
4575
- /* Header and tabs stay fixed at top when keyboard is open */
4576
- padding-top: 80px !important;
4609
+ /* Tabs bar stays fixed at top when keyboard is open (no header) */
4610
+ padding-top: 37px !important;
4577
4611
  /* Remove bottom padding since mobile keys bar is positioned above keyboard */
4578
4612
  padding-bottom: 0 !important;
4579
4613
  }