-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathremoteConnector.ts
300 lines (247 loc) · 8.7 KB
/
remoteConnector.ts
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
import * as os from 'os'
import * as fs from 'fs'
import dayjs from 'dayjs'
import * as vscode from 'vscode'
import SSHConfig from 'ssh-config'
import {
defaultSSHConfigPath,
defaultDevboxSSHConfigPath,
defaultSSHKeyPath,
} from '../constant/file'
import { Logger } from '../common/logger'
import { Disposable } from '../common/dispose'
import { convertSSHConfigToVersion2 } from '../utils/sshConfig'
import { GlobalStateManager } from '../utils/globalStateManager'
import { ensureFileAccessPermission, ensureFileExists } from '../utils/file'
import { modifiedRemoteSSHConfig } from '../utils/remoteSSHConfig'
const message = {
FailedToWriteSSHConfig: vscode.l10n.t('Failed to write SSH configuration'),
FailedToWriteSSHPrivateKey: vscode.l10n.t('Failed to write SSH private key'),
PleaseInstallRemoteSSH: vscode.l10n.t(
'Please install "Remote - SSH" extension to connect to a devbox workspace.'
),
Install: vscode.l10n.t('Install'),
Cancel: vscode.l10n.t('Cancel'),
}
export class RemoteSSHConnector extends Disposable {
constructor(context: vscode.ExtensionContext) {
super()
if (context.extension.extensionKind === vscode.ExtensionKind.UI) {
this._register(
vscode.commands.registerCommand('devbox.connectRemoteSSH', (args) =>
this.connectRemoteSSH(args)
)
)
}
}
private replaceHomePathInConfig(content: string): string {
const includePattern = new RegExp(
`Include ${os.homedir()}/.ssh/sealos/devbox_config`,
'g'
)
const includePattern2 = new RegExp(
`Include "${os.homedir()}/.ssh/sealos/devbox_config"`,
'g'
)
const includeLine = `Include ~/.ssh/sealos/devbox_config`
if (includePattern.test(content)) {
return content.replace(includePattern, '')
}
if (includePattern2.test(content)) {
return content.replace(includePattern2, '')
}
if (content.includes(includeLine)) {
return content
}
return `${includeLine}\n${content}`
}
private sshConfigPreProcess() {
Logger.info('SSH config pre-processing')
// 1. ensure .ssh/config exists
ensureFileExists(defaultSSHConfigPath, '.ssh')
// 2. ensure .ssh/sealos/devbox_config exists
ensureFileExists(defaultDevboxSSHConfigPath, '.ssh/sealos')
const customConfigFile = vscode.workspace
.getConfiguration('remote.SSH')
.get<string>('configFile', '')
if (customConfigFile) {
const resolvedPath = customConfigFile.replace(/^~/, os.homedir())
try {
const existingSSHConfig = fs.readFileSync(resolvedPath, 'utf8')
const updatedConfig = this.replaceHomePathInConfig(existingSSHConfig)
if (updatedConfig !== existingSSHConfig) {
fs.writeFileSync(resolvedPath, updatedConfig)
}
} catch (error) {
console.error(`Error reading/writing SSH config: ${error}`)
this.handleDefaultSSHConfig()
}
} else {
this.handleDefaultSSHConfig()
}
// 4. ensure sshConfig from version1 to version2
convertSSHConfigToVersion2(defaultDevboxSSHConfigPath)
Logger.info('SSH config pre-processing completed')
}
// backup the devbox ssh config
private sshConfigPostProcess() {
Logger.info('SSH config post-processing')
try {
const devboxSSHConfig = fs.readFileSync(
defaultDevboxSSHConfigPath,
'utf8'
)
const backupFolderPath = defaultSSHKeyPath + '/backup/devbox_config'
if (!fs.existsSync(backupFolderPath)) {
fs.mkdirSync(backupFolderPath, { recursive: true })
}
const backFileName = dayjs().format('YYYY-MM-DD_HH-mm-ss')
const backupFilePath = `${backupFolderPath}/${backFileName}`
fs.writeFileSync(backupFilePath, devboxSSHConfig)
Logger.info(`SSH config backed up to ${backupFilePath}`)
} catch (error) {
Logger.error(`Failed to backup SSH config: ${error}`)
}
}
private handleDefaultSSHConfig() {
const existingSSHConfig = fs.readFileSync(defaultSSHConfigPath, 'utf8')
const updatedConfig = this.replaceHomePathInConfig(existingSSHConfig)
if (updatedConfig !== existingSSHConfig) {
fs.writeFileSync(defaultSSHConfigPath, updatedConfig)
}
}
private async connectRemoteSSH(args: {
sshDomain: string
sshPort: string
base64PrivateKey: string
sshHostLabel: string
workingDir: string
}) {
Logger.info(`Connecting to remote SSH: ${args.sshHostLabel}`)
await this.ensureRemoteSSHExtInstalled()
const { sshDomain, sshPort, base64PrivateKey, sshHostLabel, workingDir } =
args
const sshUser = sshDomain.split('@')[0]
const sshHost = sshDomain.split('@')[1]
await modifiedRemoteSSHConfig(sshHostLabel)
// sshHostLabel: usw.sailos.io_ns-admin_devbox-1
const normalPrivateKey = Buffer.from(base64PrivateKey, 'base64')
const sshConfig = new SSHConfig().append({
Host: sshHostLabel,
HostName: sshHost,
User: sshUser,
Port: sshPort,
IdentityFile: `~/.ssh/sealos/${sshHostLabel}`,
IdentitiesOnly: 'yes',
StrictHostKeyChecking: 'no',
})
const sshConfigString = SSHConfig.stringify(sshConfig)
GlobalStateManager.addApiRegion(sshHost)
this.sshConfigPreProcess()
try {
Logger.info('Writing SSH config to .ssh/sealos/devbox_config')
const existingDevboxConfigLines = fs
.readFileSync(defaultDevboxSSHConfigPath, 'utf8')
.split('\n')
// replace the existing ssh config item
const newDevboxConfigLines = []
let skipLines = false
for (let i = 0; i < existingDevboxConfigLines.length; i++) {
const line = existingDevboxConfigLines[i].trim()
if (
line.startsWith('Host ') &&
line.substring(5).trim().startsWith(sshHostLabel)
) {
skipLines = true
continue
}
if (skipLines) {
if (
line.startsWith('Host ') ||
i === existingDevboxConfigLines.length
) {
skipLines = false
}
}
if (!skipLines) {
newDevboxConfigLines.push(existingDevboxConfigLines[i])
}
}
fs.writeFileSync(
defaultDevboxSSHConfigPath,
newDevboxConfigLines.join('\n')
)
// 5. write new ssh config to .ssh/sealos/devbox_config
fs.appendFileSync(defaultDevboxSSHConfigPath, `\n${sshConfigString}\n`)
Logger.info('SSH config written to .ssh/sealos/devbox_config')
} catch (error) {
Logger.error(`Failed to write SSH configuration: ${error}`)
vscode.window.showErrorMessage(
`${message.FailedToWriteSSHConfig}: ${error}`
)
}
// 6. create sealos privateKey file in .ssh/sealos
try {
Logger.info('Creating sealos privateKey file in .ssh/sealos')
const sshKeyPath = defaultSSHKeyPath + `/${sshHostLabel}`
fs.writeFileSync(sshKeyPath, normalPrivateKey)
ensureFileAccessPermission(sshKeyPath)
Logger.info('Sealos privateKey file created in .ssh/sealos')
} catch (error) {
Logger.error(`Failed to write SSH private key: ${error}`)
vscode.window.showErrorMessage(
`${message.FailedToWriteSSHPrivateKey}: ${error}`
)
}
Logger.info('Opening Devbox in VSCode')
await vscode.commands.executeCommand(
'vscode.openFolder',
vscode.Uri.parse(
`vscode-remote://ssh-remote+${sshHostLabel}${workingDir}`
),
{
forceNewWindow: true,
}
)
Logger.info('Devbox opened in VSCode')
// refresh devboxList
await vscode.commands.executeCommand('devboxDashboard.refresh')
this.sshConfigPostProcess()
}
private async ensureRemoteSSHExtInstalled(): Promise<boolean> {
const isOfficialVscode =
vscode.env.uriScheme === 'vscode' ||
vscode.env.uriScheme === 'vscode-insiders' ||
vscode.env.uriScheme === 'cursor'
const isTrae = vscode.env.uriScheme === 'trae'
// windsurf has remote-ssh inside already
if (!isOfficialVscode && !isTrae) {
return true
}
const remoteSSHId = isOfficialVscode
? 'ms-vscode-remote.remote-ssh'
: 'labring.open-remote-ssh-for-trae'
const msVscodeRemoteExt = vscode.extensions.getExtension(remoteSSHId)
if (msVscodeRemoteExt) {
return true
}
const install = message.Install
const cancel = message.Cancel
const action = await vscode.window.showInformationMessage(
message.PleaseInstallRemoteSSH,
{ modal: true },
install,
cancel
)
if (action === cancel) {
return false
}
await vscode.commands.executeCommand('extension.open', remoteSSHId)
await vscode.commands.executeCommand(
'workbench.extensions.installExtension',
remoteSSHId
)
Logger.info(`"${remoteSSHId}" extension is installed`)
return true
}
}