-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_google.js
159 lines (142 loc) · 5.21 KB
/
nassh_google.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
// Copyright 2020 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';
/**
* @fileoverview Misc logic for Google-specific integration.
*/
/** @const */
nassh.goog = {};
/**
* Namespace for the gnubby extension.
*
* @const
*/
nassh.goog.gnubby = {};
/**
* The default extension id for talking to the gnubby.
*
* Users can override this if needed on a per-connection basis, but probing
* allows us to easily adapt based on whatever is installed.
*
* @type {string}
*/
nassh.goog.gnubby.defaultExtension = '';
/**
* Find a usable gnubby extension.
*/
nassh.goog.gnubby.findExtension = function() {
// If we're not in an extension context, nothing to do.
if (!window.chrome || !chrome.runtime) {
return;
}
// The possible gnubby extensions.
const stableAppId = 'beknehfpfkghjoafdifaflglpjkojoco';
const stableExtId = 'lkjlajklkdhaneeelolkfgbpikkgnkpk';
// The order matches the gnubby team preferences: https://crbug.com/902588
// Prefer the extension over the app, and dev over stable.
const extensions = [
'klnjmillfildbbimkincljmfoepfhjjj', // extension (dev)
stableExtId, // extension (stable)
'dlfcjilkjfhdnfiecknlnddkmmiofjbg', // app (dev)
stableAppId, // app (stable)
'kmendfapggjehodndflmmgagdbamhnfd', // component
];
// Ping the extension to see if it's installed/enabled/alive.
const check = (id) => {
return nassh.runtimeSendMessage(id, {'type': 'HELLO'}).then((result) => {
// If the probe worked, return the id, else return nothing so we can
// clear out all the pending promises.
if (result !== undefined && result['rc'] == 0) {
return id;
}
}).catch((e) => {});
};
// Guess a reasonable default based on the OS.
nassh.goog.gnubby.defaultExtension =
(hterm.os == 'cros' ? stableAppId : stableExtId);
// We don't set a timeout here as it doesn't block overall execution.
Promise.all(extensions.map(check)).then((results) => {
console.log(`gnubby probe results: ${results}`);
for (let i = 0; i < extensions.length; ++i) {
const extId = extensions[i];
if (results.includes(extId)) {
nassh.goog.gnubby.defaultExtension = extId;
break;
}
}
});
};
/** @const */
nassh.goog.gcse = {};
/**
* The default extension id for managing certs.
*
* We default to the stable version. If the dev version is available, we'll
* switch on the fly below. We don't currently allow people to control this.
* This aligns with the gnubby team preferences: https://crbug.com/902588
*
* @type {string}
*/
nassh.goog.gcse.defaultExtension = 'cfmgaohenjcikllcgjpepfadgbflcjof';
/**
* Find a usable GCSE extension.
*/
nassh.goog.gcse.findExtension = function() {
// If we're not in an extension context, nothing to do.
if (!window.chrome || !chrome.runtime) {
return;
}
const devId = 'oncenbbimcccjedkmajnncfllmbnmbnp';
// Ping the dev extension to see if it's installed/enabled/alive.
nassh.runtimeSendMessage(devId, {'action': 'hello'}).then((result) => {
// If the probe worked, return the id, else return nothing so we can
// clear out all the pending promises. We don't check the value of the
// status field as it will be "error" which is confusing -- while the
// "hello" action is specifically reserved, it isn't handled :).
if (result !== undefined && result['status']) {
console.log(`found GCSE dev extension ${devId}`);
nassh.goog.gcse.defaultExtension = devId;
}
}).catch((e) => {});
};
/**
* Try to refresh the SSH cert if it's old.
*
* If the request works, we'll wait for it, otherwise we'll continue on even if
* we received an error. Messages will be logged, but we won't throw errors.
*
* @param {!hterm.Terminal.IO} io Handle to the terminal for showing status.
* @return {!Promise<void>} Resolve once things are in sync.
*/
nassh.goog.gcse.refresh = function(io) {
io.print(nassh.msg('SSH_CERT_CHECK_START'));
return nassh.runtimeSendMessage(nassh.goog.gcse.defaultExtension,
{'action': 'certificate_expiry'})
.then((result) => {
if (result.status === 'OK') {
// Refresh the certificate if it expires in the next hour.
const now = new Date().getTime() / 1000;
const hoursLeft = Math.floor((result.expires - now) / 60 / 60);
io.println(nassh.msg('SSH_CERT_CHECK_RESULT', [hoursLeft]));
if (hoursLeft < 1) {
io.showOverlay(nassh.msg('SSH_CERT_CHECK_REFRESH'));
return nassh.runtimeSendMessage(nassh.goog.gcse.defaultExtension,
{'action': 'request_certificate'});
}
} else {
io.println(nassh.msg('SSH_CERT_CHECK_ERROR', [result.error]));
}
})
.catch((result) => io.println(nassh.msg('SSH_CERT_CHECK_ERROR', [result])));
};
/**
* Register various Google extension probing.
*
* This could take time to resolve, so do it as part of start up.
* It resolves using promises in the background, so this is OK.
*/
lib.registerInit('goog init', () => {
nassh.goog.gnubby.findExtension();
nassh.goog.gcse.findExtension();
});