forked from Eugeny/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profiles.ts
157 lines (147 loc) · 5.17 KB
/
profiles.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
import { Injectable, InjectFlags, Injector } from '@angular/core'
import { NewTabParameters, PartialProfile, TranslateService, ConnectableProfileProvider } from 'tabby-core'
import * as ALGORITHMS from 'ssh2/lib/protocol/constants'
import { SSHProfileSettingsComponent } from './components/sshProfileSettings.component'
import { SSHTabComponent } from './components/sshTab.component'
import { PasswordStorageService } from './services/passwordStorage.service'
import { ALGORITHM_BLACKLIST, SSHAlgorithmType, SSHProfile } from './api'
import { SSHProfileImporter } from './api/importer'
@Injectable({ providedIn: 'root' })
export class SSHProfilesService extends ConnectableProfileProvider<SSHProfile> {
id = 'ssh'
name = 'SSH'
settingsComponent = SSHProfileSettingsComponent
configDefaults = {
options: {
host: null,
port: 22,
user: 'root',
auth: null,
password: null,
privateKeys: [],
keepaliveInterval: 5000,
keepaliveCountMax: 10,
readyTimeout: null,
x11: false,
skipBanner: false,
jumpHost: null,
agentForward: false,
warnOnClose: null,
algorithms: {
hmac: [],
kex: [],
cipher: [],
serverHostKey: [],
},
proxyCommand: null,
forwardedPorts: [],
scripts: [],
socksProxyHost: null,
socksProxyPort: null,
httpProxyHost: null,
httpProxyPort: null,
reuseSession: true,
input: { backspace: 'backspace' },
},
clearServiceMessagesOnConnect: true,
}
constructor (
private passwordStorage: PasswordStorageService,
private translate: TranslateService,
private injector: Injector,
) {
super()
for (const k of Object.values(SSHAlgorithmType)) {
const defaultAlg = {
[SSHAlgorithmType.KEX]: 'DEFAULT_KEX',
[SSHAlgorithmType.HOSTKEY]: 'DEFAULT_SERVER_HOST_KEY',
[SSHAlgorithmType.CIPHER]: 'DEFAULT_CIPHER',
[SSHAlgorithmType.HMAC]: 'DEFAULT_MAC',
}[k]
this.configDefaults.options.algorithms[k] = ALGORITHMS[defaultAlg].filter(x => !ALGORITHM_BLACKLIST.includes(x))
this.configDefaults.options.algorithms[k].sort()
}
}
async getBuiltinProfiles (): Promise<PartialProfile<SSHProfile>[]> {
const importers = this.injector.get<SSHProfileImporter[]>(SSHProfileImporter as any, [], InjectFlags.Optional)
let imported: PartialProfile<SSHProfile>[] = []
for (const importer of importers) {
try {
imported = imported.concat(await importer.getProfiles())
} catch (e) {
console.warn('Could not import SSH profiles:', e)
}
}
return [
{
id: `ssh:template`,
type: 'ssh',
name: this.translate.instant('SSH connection'),
icon: 'fas fa-desktop',
options: {
host: '',
port: 22,
user: 'root',
},
isBuiltin: true,
isTemplate: true,
weight: -1,
},
...imported.map(p => ({
...p,
isBuiltin: true,
})),
]
}
async getNewTabParameters (profile: SSHProfile): Promise<NewTabParameters<SSHTabComponent>> {
return {
type: SSHTabComponent,
inputs: { profile },
}
}
getSuggestedName (profile: SSHProfile): string {
return `${profile.options.user}@${profile.options.host}:${profile.options.port}`
}
getDescription (profile: PartialProfile<SSHProfile>): string {
return profile.options?.host ?? ''
}
deleteProfile (profile: SSHProfile): void {
this.passwordStorage.deletePassword(profile)
}
quickConnect (query: string): PartialProfile<SSHProfile> {
let user: string|undefined = undefined
let host = query
let port = 22
if (host.includes('@')) {
const parts = host.split(/@/g)
host = parts[parts.length - 1]
user = parts.slice(0, parts.length - 1).join('@')
}
if (host.includes('[')) {
port = parseInt(host.split(']')[1].substring(1))
host = host.split(']')[0].substring(1)
} else if (host.includes(':')) {
port = parseInt(host.split(/:/g)[1])
host = host.split(':')[0]
}
return {
name: query,
type: 'ssh',
options: {
host,
user,
port,
},
}
}
intoQuickConnectString (profile: SSHProfile): string|null {
let s = profile.options.host
if (profile.options.user !== 'root') {
s = `${profile.options.user}@${s}`
}
if (profile.options.port !== 22) {
s = `${s}:${profile.options.port}`
}
return s
}
}