-
Notifications
You must be signed in to change notification settings - Fork 11
/
extension.js
345 lines (325 loc) · 15.8 KB
/
extension.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
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
var vscode = require('vscode');
var commandExistsSync = require('command-exists').sync;
var upath = require("upath");
var moment = require("moment");
var isPathInside = require('is-path-inside');
var configLoader = require('./adapters/config-loader');
var outputChannel = null;
var fastOpenConnectionButton = null;
var fastOpenConnectionServerName = null;
var fastOpenConnectionProjectPath = null;
var servers = [];
var terminals = [];
// Shows a list of servers and returns a promise for what to do with the server
function selectServer() {
if (!servers.length) {
vscode.window.showInformationMessage("You don't have any servers");
return;
}
// Show Command Palette with server list of servers
// Return promise to allow for .then(...)
return vscode.window.showQuickPick(servers.map(s => s.name), 'Select the server to connect...');
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
initExtension();
// Command palette 'Open SSH Connection'
context.subscriptions.push(vscode.commands.registerCommand('sshextension.openConnection', function () {
selectServer().then(s => openSSHConnection(s, false));
}));
// Command palette 'SSH Port Forwarding'
context.subscriptions.push(vscode.commands.registerCommand('sshextension.portForwarding', function () {
selectServer().then(s => createForwarding(s));
}));
// Launch reload configs if config files has been changed
configLoader.startWatchers();
configLoader.watcher.on('change', function(file) {
loadServerList(loadServerConfigs());
});
vscode.workspace.onDidChangeConfiguration(function (event) {
loadServerList(loadServerConfigs());
});
// If terminal closed
context.subscriptions.push(vscode.window.onDidCloseTerminal(function (event) {
var terminal = terminals.find(function (element, index, array) {
return element.terminal._id == this._id
}, event);
if (terminal === undefined) return;
terminals.shift(terminal);
outputChannel.appendLine("A terminal with a session for '" + terminal.host + "' has been killed.");
}));
// If the edited file is in the project directory
context.subscriptions.push(vscode.window.onDidChangeActiveTextEditor(function (event) {
manageFastOpenConnectionButtonState();
}));
context.subscriptions.push(vscode.commands.registerCommand('sshextension.fastOpenConnection', function (c) {
openSSHConnection(fastOpenConnectionServerName, true);
}));
var api = {
connections(host = '', username = '') {
return terminals.filter(function (element, index, array) {
return (element.host == host || !host.length) && (element.username == username || !username.length)
})
}
}
return api;
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
// Loads an object that contains a list of servers in JSON format
function loadServerConfigs() {
var result = true;
var { merged_configs, messages } = configLoader.getConfigContents();
messages.forEach(function(message){
outputChannel.appendLine(message);
});
return { "result": result, "json": merged_configs };
}
// Function initializes an array of servers from a string or JSON object
function loadServerList(source) {
var serversConfig = null;
if (typeof (source) === "string") { // If the parameter is a string
var parsedSource = JSON.parse(source);
serversConfig = { "result": parsedSource !== undefined, "json": parsedSource };
}
else { // If the parameter is a JSON object
serversConfig = source;
}
if (serversConfig.result) {
servers = [];
serversConfig.json.forEach(function (element) {
var server = { "name": element.name, "configuration": element };
servers.push(server);
}, this);
}
else {
outputChannel.appendLine("Unable to load server list, check configuration files.");
return false;
}
return true;
}
// Function checks for ssh utilities
function checkSSHExecutable() {
var checkResult = commandExistsSync('ssh');
if (checkResult) {
outputChannel.appendLine("Find ssh on your system.");
} else {
outputChannel.appendLine("Did not find ssh on your system.");
}
outputChannel.appendLine("If you use a third-party terminal, then make sure that there is an SSH utility.");
return checkResult;
}
// This method creates a terminal for the server by its name
function openSSHConnection(serverName, isFastConnection, forwardingArgs = null) {
if (serverName === undefined) return false;
var server = servers.find(function (element, index, array) { return element.name == this }, serverName);
var terminal = terminals.find(function (element, index, array) {
return element.name == this.terminalName && element.isForwarding == this.isForwarding
}, {"terminalName" : serverName, "isForwarding": (forwardingArgs != null)});
var terminalIsNew = true;
var hasErrors = false;
if (terminal === undefined || (vscode.workspace.getConfiguration('sshextension').allowMultipleConnections && forwardingArgs == null)) { // If the terminal does not exist
outputChannel.appendLine("New terminal session initialization for '" + server.configuration.host + "'...");
if (server.configuration.host === undefined || server.configuration.username === undefined) {
outputChannel.appendLine("Check host or username for '" + server.configuration.host + "'");
hasErrors = true;
}
var sshCommand = 'ssh ' + ((forwardingArgs != null) ? forwardingArgs + " " : "") + server.configuration.host + ' -l ' + server.configuration.username;
// Add port if different from default port
if (server.configuration.port !== undefined && server.configuration.port && server.configuration.port !== 22)
sshCommand += ' -p ' + server.configuration.port;
var sshAuthorizationMethod = "byPass";
// Authorization through an agent
if (server.configuration.agent !== undefined && server.configuration.agent)
sshAuthorizationMethod = "agent";
// Authorization by private key
if (server.configuration.privateKey !== undefined && server.configuration.privateKey) {
sshCommand += ' -i "' + server.configuration.privateKey + '"';
sshAuthorizationMethod = "byPrivateKey";
}
if (!hasErrors) {
terminal = vscode.window.createTerminal(serverName + ((forwardingArgs != null) ? " (Forwarding)" : ""));
terminals.push({ "name": serverName, "username": server.configuration.username, "host": server.configuration.host, "terminal": terminal, "isForwarding": (forwardingArgs != null) });
terminal.sendText(sshCommand);
if (sshAuthorizationMethod == "byPass") {
terminal.sendText(server.configuration.password);
}
if (vscode.workspace.getConfiguration('sshextension').openProjectCatalog) {
if (isFastConnection)
terminal.sendText("cd " + fastOpenConnectionProjectPath);
else if (server.configuration.path !== undefined) {
terminal.sendText("cd " + server.configuration.path);
}
}
// If custom commands defined send it to terminal
if (server.configuration.customCommands !== undefined && server.configuration.customCommands.length) {
terminal.sendText(server.configuration.customCommands.join(' && '));
}
else if (vscode.workspace.getConfiguration('sshextension').customCommands.length) {
terminal.sendText(vscode.workspace.getConfiguration('sshextension').customCommands.join(' && '));
}
}
}
else { // If the terminal instance was found
terminal = terminal.terminal;
terminalIsNew = false;
}
if (!hasErrors) {
terminal.show();
outputChannel.appendLine("A terminal with a session for '" + server.configuration.host + "' has been " + ((terminalIsNew) ? "created and displayed" : "displayed."));
}
else {
outputChannel.appendLine("A terminal with a session for '" + server.configuration.host + "' has been not started, because errors were found.");
vscode.window.showErrorMessage("Terminal has been not started, check output for more info.", "Check output").then(function(button){
outputChannel.show();
});
}
return hasErrors;
}
function createForwarding(serverName){
function validateHostPort(port, domainReq = false){
var portRegex = /^(?:(?:\S|[^:])+:{1})?(?:[0-5]?\d{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2]\d|6553[0-6]){1}$/;
if (domainReq) portRegex = /^(?:(?:\S|[^:])+:{1}){1}(?:[0-5]?\d{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2]\d|6553[0-6]){1}$/;
return (portRegex.test(port)) ? null : "Please enter a domain " + (!domainReq ? "(optional) ": "") + "and port in range 0 - 65535 (e. g. localhost:9000" + (!domainReq ? " or 9000": "")+ ")";
}
function createForwardingArgs(option, firstAddress, secondAddress = null) {
var forwardingArgs = option + " " + firstAddress;
if (secondAddress != null)
forwardingArgs += ":" + secondAddress;
return forwardingArgs;
}
function toRecentlyUsed(recentlyUsedForwardings, forwardingArgs){
if (recentlyUsedForwardings.indexOf(forwardingArgs) == -1) {
vscode.window.showInformationMessage("Want to save this forwarding in recently used?", "Yes").then(function(button){
if (button == "Yes"){
recentlyUsedForwardings.push(forwardingArgs);
vscode.workspace.getConfiguration("sshextension").update("recentlyUsedForwardings", recentlyUsedForwardings, true)
}
});
}
}
var types = {
'Local to remote': {
'firstAdressPrompt': "Type local address/port (e. g. localhost:9000 or 9000)",
'secondAddressPrompt': "Type remote address (e. g. localhost:9000)",
"firstDomainReq": false,
'secondDomainReq': true,
"option": "-L"
},
'Remote to local': {
'firstAdressPrompt': "Type remote address/port (e. g. localhost:9000 or 9000)",
'secondAddressPrompt': "Type local address (e. g. localhost:9000)",
"firstDomainReq": false,
'secondDomainReq': true,
"option": "-R"
},
'SOCKS': {
'firstAdressPrompt': "Type address for SOCKS (e. g. localhost:9000)",
"firstDomainReq": true,
"option": "-D"
}
}
var recentlyUsedForwardings = vscode.workspace.getConfiguration("sshextension").recentlyUsedForwardings;
if (recentlyUsedForwardings.length) {
types['Recently used'] = {};
}
// Show select of types
vscode.window.showQuickPick(Object.keys(types), 'Select forwarding type...').then(function (type) {
if (type === undefined) return;
// Show input for first address
if (type != "Recently used"){
vscode.window.showInputBox({"validateInput": (s) => {
return validateHostPort(s, types[type].firstDomainReq)
},"prompt": types[type].firstAdressPrompt, "ignoreFocusOut" : true}).then(function (firstAddress) {
if (firstAddress === undefined || !firstAddress.length) return;
if (type != "SOCKS"){
// Show input for second address
vscode.window.showInputBox({"validateInput": (s) => {
return validateHostPort(s, types[type].secondDomainReq)
}, "prompt": types[type].secondAddressPrompt, "ignoreFocusOut" : true}).then(function (secondAddress) {
if (secondAddress === undefined || !secondAddress.length) return;
var forwardingArgs = createForwardingArgs(types[type].option, firstAddress, secondAddress);
openSSHConnection(serverName, false, forwardingArgs);
toRecentlyUsed(recentlyUsedForwardings, forwardingArgs);
});
}
else {
var forwardingArgs = createForwardingArgs(types[type].option, firstAddress);
openSSHConnection(serverName, false, forwardingArgs);
toRecentlyUsed(recentlyUsedForwardings, forwardingArgs);
}
});
}
else {
vscode.window.showQuickPick(recentlyUsedForwardings, 'Select forwarding arguments from recently used...').then(function (forwardingArgs) {
if (forwardingArgs === undefined) return;
openSSHConnection(serverName, false, forwardingArgs);
});
}
});
}
// This method try to find server with project that contains file
function getProjectByFilePath(filePath) {
var projectPath = null;
// Get path to edited file with fixed drive letter case
var openedFileName = upath.normalize(filePath);
openedFileName = openedFileName.replace(/\w:/g, function (g) { return g.toLowerCase() })
// Find the server that has the project containing this file
var server = servers.find(function (element, index, array) {
// If the server does not have any projects, go to the next
if (element.configuration.project === undefined) return false;
var thisServerMapped = false;
Object.keys(element.configuration.project).forEach(function (item) {
// Get project path with fixed drive letter case
var serverProjectPath = upath.normalize(item);
serverProjectPath = serverProjectPath.replace(/\w:/g, function (g) { return g.toLowerCase() });
thisServerMapped = isPathInside(openedFileName, serverProjectPath);
if (thisServerMapped) {
projectPath = element.configuration.project[item];
}
}, this);
return thisServerMapped;
}, openedFileName);
return { "server" : server, "projectPath" : projectPath };
}
function manageFastOpenConnectionButtonState() {
var mappedServer = undefined;
if (vscode.window.activeTextEditor != undefined) {
var project = getProjectByFilePath(vscode.window.activeTextEditor.document.fileName);
fastOpenConnectionProjectPath = project.projectPath;
mappedServer = project.server;
}
// If the server is found then show the button and save the server name
if (mappedServer !== undefined) {
fastOpenConnectionButton.text = "$(terminal) Open SSH on " + mappedServer.configuration.name;
fastOpenConnectionButton.show();
fastOpenConnectionServerName = mappedServer.configuration.name;
}
// Otherwise hide the button
else {
fastOpenConnectionButton.hide();
}
}
// Initialize extension
function initExtension() {
outputChannel = vscode.window.createOutputChannel("ssh-extension");
outputChannel.appendLine = (function(_super) {
return function() {
var now_formatted = moment().format("YYYY-MM-DD HH:mm:ss");
arguments[0] = "[" + now_formatted + "] " + arguments[0];
return _super.apply(this, arguments);
};
})(outputChannel.appendLine);
checkSSHExecutable();
loadServerList(loadServerConfigs());
fastOpenConnectionButton = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
fastOpenConnectionButton.command = "sshextension.fastOpenConnection";
manageFastOpenConnectionButtonState();
return true;
}
exports.deactivate = deactivate;