-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh.js
460 lines (419 loc) · 12.8 KB
/
nassh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Namespace for the whole nassh project.
*/
const nassh = {};
/**
* Non-null if nassh is running as an extension.
*/
nassh.browserAction =
window.browser && browser.browserAction ? browser.browserAction :
window.chrome && chrome.browserAction ? chrome.browserAction :
null;
lib.registerInit(
'nassh',
/**
* Register a static initializer for nassh.*.
*/
() => {
// Since our translation process only preserves \n (and discards \r), we
// have to manually insert them ourselves.
hterm.messageManager.useCrlf = true;
});
/**
* Modify if running in chrome-untrusted://. We will use
* lib.Storage.TerminalPrivate as the default storage, load messages via XHR,
* and polyfill chrome.runtime.getManifest.
*/
nassh.setupForWebApp = function() {
// Modifications if running as a web app.
if (location.href.startsWith('chrome-untrusted://')) {
lib.registerInit('terminal-private-storage', () => {
hterm.defaultStorage = new lib.Storage.TerminalPrivate();
});
lib.registerInit('messages', nassh.loadMessages);
// Polyfill chrome.runtime.getManifest since it is not available when
// We require name, version, and icons.
if (chrome && chrome.runtime && !chrome.runtime.getManifest) {
chrome.runtime.getManifest = () => {
return /** @type {!chrome.runtime.Manifest} */ ({
'name': 'Terminal',
'version': 'system',
'icons': {'192': '/images/dev/crostini-192.png'},
});
};
}
}
};
/**
* Loads messages for when chrome.i18n is not available.
*
* This should only be used in contexts outside of extensions/apps.
*/
nassh.loadMessages = async function() {
// Load hterm.messageManager from /_locales/<lang>/messages.json.
hterm.messageManager.useCrlf = true;
const url = lib.f.getURL('/_locales/$1/messages.json');
await hterm.messageManager.findAndLoadMessages(url);
};
/**
* Return a formatted message in the current locale.
*
* @param {string} name The name of the message to return.
* @param {!Array=} args The message arguments, if required.
* @return {string} The localized & formatted message.
*/
nassh.msg = function(name, args) {
return hterm.messageManager.get(name, args, name);
};
/**
* Request the persistent HTML5 filesystem for this extension.
*
* This will also create the /.ssh/ directory if it does not exits.
*
* @return {!Promise<!FileSystem>} The root filesystem handle.
*/
nassh.getFileSystem = function() {
const requestFS = window.requestFileSystem || window.webkitRequestFileSystem;
return new Promise((resolve, reject) => {
function onFileSystem(fileSystem) {
// We create /.ssh/identity/ subdir for storing keys. We need a dedicated
// subdir for users to import files to avoid collisions with standard ssh
// config files.
lib.fs.getOrCreateDirectory(fileSystem.root, '/.ssh/identity')
.then(() => resolve(fileSystem))
.catch(reject);
}
requestFS(window.PERSISTENT,
16 * 1024 * 1024,
onFileSystem,
(e) => {
console.error(`Error initializing filesystem: ${e}`);
reject(e);
});
});
};
/**
* Export the current list of nassh connections, and any hterm profiles
* they reference.
*
* This is method must be given a completion callback because the hterm
* profiles need to be loaded asynchronously.
*
* @param {function(!Object)} onComplete Callback to be invoked when export is
* complete.
* The callback will receive a plain JS object representing the state of
* nassh preferences. The object can be passed back to
* nassh.importPreferences.
*/
nassh.exportPreferences = function(onComplete) {
let pendingReads = 0;
const rv = {};
const onReadStorage = function(profile, prefs) {
rv.hterm[profile] = prefs.exportAsJson();
if (--pendingReads < 1) {
onComplete(rv);
}
};
rv.magic = 'nassh-prefs';
rv.version = 1;
const nasshPrefs = new nassh.PreferenceManager();
nasshPrefs.readStorage(function() {
// Export all the connection settings.
rv.nassh = nasshPrefs.exportAsJson();
// Save all the profiles.
rv.hterm = {};
hterm.PreferenceManager.listProfiles(nasshPrefs.storage, (profiles) => {
profiles.forEach((profile) => {
rv.hterm[profile] = null;
const prefs = new hterm.PreferenceManager(profile);
prefs.readStorage(onReadStorage.bind(null, profile, prefs));
pendingReads++;
});
if (profiles.length == 0) {
onComplete(rv);
}
});
});
};
/**
* Import a preferences object.
*
* This will not overwrite any existing preferences.
*
* @param {!Object} prefsObject A preferences object created with
* nassh.exportPreferences.
* @param {function()=} onComplete A callback to be invoked when the import is
* complete.
*/
nassh.importPreferences = function(prefsObject, onComplete) {
let pendingReads = 0;
const onReadStorage = function(terminalProfile, prefs) {
prefs.importFromJson(prefsObject.hterm[terminalProfile]);
if (--pendingReads < 1 && onComplete) {
onComplete();
}
};
if (prefsObject.magic != 'nassh-prefs') {
throw new Error('Not a JSON object or bad value for \'magic\'.');
}
if (prefsObject.version != 1) {
throw new Error('Bad version, expected 1, got: ' + prefsObject.version);
}
const nasshPrefs = new nassh.PreferenceManager();
nasshPrefs.importFromJson(prefsObject.nassh, () => {
for (const terminalProfile in prefsObject.hterm) {
const prefs = new hterm.PreferenceManager(terminalProfile);
prefs.readStorage(onReadStorage.bind(null, terminalProfile, prefs));
pendingReads++;
}
});
};
/**
* Create a new window to the options page for customizing preferences.
*
* @param {string=} page The specific options page to navigate to.
*/
nassh.openOptionsPage = function(page = '') {
const fallback = () => {
lib.f.openWindow(`/html/nassh_preferences_editor.html#${page}`);
};
if (!page && window.chrome && chrome.runtime &&
chrome.runtime.openOptionsPage) {
// This is a bit convoluted because, in some scenarios (e.g. crosh), the
// openOptionsPage helper might fail. If it does, fallback to a tab.
chrome.runtime.openOptionsPage(() => {
const err = lib.f.lastError();
if (err) {
console.warn(err);
fallback();
}
});
} else {
fallback();
}
};
/**
* Trigger the flow for sending feedback.
*/
nassh.sendFeedback = function() {
lib.f.openWindow('https://goo.gl/vb94JY');
};
/**
* Register this extension to handle URIs like ssh://.
*
* The protocol should be one allowed by the specifications:
* https://html.spec.whatwg.org/multipage/webappapis.html#webappapis
* https://chromium.googlesource.com/chromium/src/+blame/HEAD/third_party/WebKit/Source/modules/navigatorcontentutils/NavigatorContentUtils.cpp
* https://www.iana.org/assignments/uri-schemes/prov/sftp
*
* @param {string} proto The protocol name to register.
*/
nassh.registerProtocolHandler = function(proto) {
try {
navigator.registerProtocolHandler(
proto,
chrome.runtime.getURL('/html/nassh.html#uri:%s'),
chrome.runtime.getManifest().name);
} catch (e) {
console.error(`Unable to register '${proto}' handler:`, e);
}
// Not all runtimes allow direct registration, so also register with the
// 'web+' prefix just in case.
if (!proto.startsWith('web+')) {
nassh.registerProtocolHandler(`web+${proto}`);
}
};
/**
* Disable automatic tab discarding for our windows.
*
* Newer versions of Chrome are a bit more proactive in discarding tabs. Signal
* that we shouldn't be discarded as restarting crosh/ssh sessions is not easy
* for users.
* https://crbug.com/868155
*
* Note: This code updates tab properties asynchronously, but that should be
* fine for our usage as we don't generally create windows/tabs on the fly.
*/
nassh.disableTabDiscarding = function() {
if (window.chrome && chrome.tabs) {
chrome.tabs.getCurrent((tab) => {
chrome.tabs.update(tab.id, {autoDiscardable: false});
});
}
};
/**
* Convert a base64url encoded string to the base64 encoding.
*
* The difference here is in the last two characters of the alphabet.
* So converting between them is easy.
*
* base64: https://tools.ietf.org/html/rfc4648#section-4
* 62 +
* 63 /
* base64url: https://tools.ietf.org/html/rfc4648#section-5
* 62 -
* 63 _
*
* We re-add any trailing = padding characters.
*
* @param {string} data The base64url encoded data.
* @return {string} The data in base64 encoding.
*/
nassh.base64UrlToBase64 = function(data) {
const replacements = {'-': '+', '_': '/'};
let ret = data.replace(/[-_]/g, (ch) => replacements[ch]);
switch (ret.length % 4) {
case 1:
throw new Error(`Invalid base64url length: ${ret.length}`);
case 2:
ret += '==';
break;
case 3:
ret += '=';
break;
}
return ret;
};
/**
* Convert a base64 encoded string to the base64url encoding.
*
* This is the inverse of nassh.base64UrlToBase64.
*
* We strip off any = padding characters too.
*
* @param {string} data The base64 encoded data.
* @return {string} The data in base64url encoding.
*/
nassh.base64ToBase64Url = function(data) {
const replacements = {'+': '-', '/': '_', '=': ''};
return data.replace(/[+/=]/g, (ch) => replacements[ch]);
};
/**
* Generate an SGR escape sequence.
*
* @param {!Object=} settings
* @return {string} The SGR escape sequence.
*/
nassh.sgrSequence = function(
{bold, faint, italic, underline, blink, fg, bg} = {}) {
const parts = [];
if (bold) {
parts.push('1');
}
if (faint) {
parts.push('2');
}
if (italic) {
parts.push('3');
}
if (underline) {
if (underline === true) {
parts.push('4');
} else {
parts.push(`4:${underline}`);
}
}
if (blink) {
parts.push('5');
}
if (fg) {
parts.push(fg);
}
if (bg) {
parts.push(bg);
}
return `\x1b[${parts.join(';')}m`;
};
/**
* Apply SGR styling to text.
*
* This will reset the SGR style to the default.
*
* @param {string} text The text to be stylized.
* @param {!Object=} settings The SGR settings to apply.
* @return {string} The text wrapped in SGR escape sequences.
*/
nassh.sgrText = function(text, settings) {
return nassh.sgrSequence(settings) + text + nassh.sgrSequence();
};
/**
* Generate a hyperlink using OSC-8 escape sequence.
*
* @param {string} url The link target.
* @param {string=} text The user visible text.
* @return {string} The hyperlink with OSC-8 escape sequences.
*/
nassh.osc8Link = function(url, text = url) {
if (url.startsWith('/')) {
url = lib.f.getURL(url);
}
return `\x1b]8;;${url}\x07${text}\x1b]8;;\x07`;
};
/**
* @typedef {{
* name: string,
* isWebFont: boolean,
* }}
*/
nassh.Font;
/** @type {!Array<!nassh.Font>} */
nassh.FONTS = [
{name: 'Noto Sans Mono', isWebFont: false},
{name: 'Cousine', isWebFont: true},
{name: 'Inconsolata', isWebFont: true},
{name: 'Roboto Mono', isWebFont: true},
{name: 'Source Code Pro', isWebFont: true},
];
/**
* Add css to load web fonts from fonts.googleapis.com.
*
* @param {!Document} document The document to load into.
*/
nassh.loadWebFonts = function(document) {
const imports = [];
const fontFaces = [];
for (const font of nassh.FONTS) {
if (font.isWebFont) {
// Load normal (400) and bold (700).
imports.push(`@import url('https://fonts.googleapis.com/css2?family=` +
`${encodeURIComponent(font.name)}:wght@400;700&display=swap');`);
}
fontFaces.push(`
@font-face {
font-family: 'Powerline For ${font.name}';
src: url('../fonts/PowerlineFor${font.name.replace(/\s/g, '')}.woff2')
format('woff2');
font-weight: normal bold;
unicode-range:
U+2693,U+26A1,U+2699,U+270E,U+2714,U+2718,U+273C,U+279C,U+27A6,
U+2B06-2B07,U+E0A0-E0D4;
}`);
}
const style = document.createElement('style');
style.textContent = imports.join('\n') + fontFaces.join('');
document.head.appendChild(style);
};
/**
* A Promise wrapper for the chrome.runtime.sendMessage API.
*
* @param {*} args The arguments to sendMessage.
* @return {!Promise<*>} A promise to resolve with the remote's response.
*/
nassh.runtimeSendMessage = function(...args) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(...args, (response) => {
// If the remote side doesn't exist (which is normal), Chrome complains
// if we don't read the lastError. Clear that here.
const err = lib.f.lastError();
if (err) {
reject(err);
} else {
resolve(response);
}
});
});
};