FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
xum/src/node/runtime/sshConfigParser.ts at main · coder/xum · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
coder
/
xum
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
2k
Code
Issues
46
Pull requests
200
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
xum
/
src
/
node
/
runtime
/
sshConfigParser.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
311 lines (259 loc) · 8.04 KB
Breadcrumbs
xum
/
src
/
node
/
runtime
/
sshConfigParser.ts
Copy path
File metadata and controls
311 lines (259 loc) · 8.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* SSH config parsing utilities (ssh-config wrapper).
*/
import
{
spawnSync
}
from
"child_process"
;
import
*
as
fs
from
"fs/promises"
;
import
*
as
os
from
"os"
;
import
*
as
path
from
"path"
;
import
SSHConfig
,
{
glob
}
from
"ssh-config"
;
import
{
log
}
from
"@/node/services/log"
;
import
{
getErrorMessage
}
from
"@/common/utils/errors"
;
interface
ParsedValueToken
{
val
:
string
;
separator
:
string
;
quoted
?:
boolean
;
}
const
DEFAULT_SSH_PORT
=
22
;
export
interface
ResolvedSSHConfig
{
host
:
string
;
hostName
:
string
;
user
?:
string
;
port
:
number
;
identityFiles
:
string
[
]
;
proxyCommand
?:
string
;
}
function
getHomeDir
(
)
:
string
{
return
process
.
env
.
USERPROFILE
??
os
.
homedir
(
)
;
}
function
getDefaultUsername
(
)
:
string
{
try
{
return
os
.
userInfo
(
)
.
username
;
}
catch
{
return
process
.
env
.
USER
??
process
.
env
.
USERNAME
??
""
;
}
}
function
expandHomePath
(
value
:
string
,
homeDir
:
string
)
:
string
{
if
(
value
===
"~"
)
{
return
homeDir
;
}
if
(
value
.
startsWith
(
"~/"
)
||
value
.
startsWith
(
"~\\"
)
)
{
return
path
.
join
(
homeDir
,
value
.
slice
(
2
)
)
;
}
return
value
;
}
function
normalizeIdentityFile
(
value
:
string
,
homeDir
:
string
)
:
string
{
const
expanded
=
expandHomePath
(
value
,
homeDir
)
;
if
(
path
.
isAbsolute
(
expanded
)
)
{
return
expanded
;
}
return
path
.
join
(
homeDir
,
expanded
)
;
}
function
parseHostAndUser
(
host
:
string
)
:
{
host
:
string
;
user
?:
string
}
{
const
trimmed
=
host
.
trim
(
)
;
const
atIndex
=
trimmed
.
lastIndexOf
(
"@"
)
;
if
(
atIndex
>
0
)
{
const
user
=
trimmed
.
slice
(
0
,
atIndex
)
.
trim
(
)
;
const
hostname
=
trimmed
.
slice
(
atIndex
+
1
)
.
trim
(
)
;
if
(
user
&&
hostname
)
{
return
{
host
:
hostname
,
user
}
;
}
}
return
{
host
:
trimmed
}
;
}
function
isParsedValueToken
(
value
:
unknown
)
:
value
is
ParsedValueToken
{
return
typeof
value
===
"object"
&&
value
!==
null
&&
"val"
in
value
&&
"separator"
in
value
;
}
function
tokensToString
(
tokens
:
ParsedValueToken
[
]
)
:
string
{
return
tokens
.
map
(
(
{
val
,
separator
,
quoted
}
)
=>
{
const
rendered
=
quoted
?
`"
${
val
}
"`
:
val
;
return
`
${
separator
}
${
rendered
}
`
;
}
)
.
join
(
""
)
.
trimStart
(
)
;
}
type
ComputedConfigValue
=
string
|
string
[
]
|
ParsedValueToken
[
]
;
function
getConfigValue
(
config
:
Record
<
string
,
ComputedConfigValue
>
,
key
:
string
)
:
ComputedConfigValue
|
undefined
{
const
match
=
Object
.
entries
(
config
)
.
find
(
(
[
configKey
]
)
=>
configKey
.
toLowerCase
(
)
===
key
.
toLowerCase
(
)
)
;
return
match
?.
[
1
]
;
}
function
toStringValue
(
value
:
ComputedConfigValue
|
undefined
)
:
string
|
undefined
{
if
(
typeof
value
===
"string"
)
{
return
value
;
}
if
(
Array
.
isArray
(
value
)
)
{
const
first
=
value
[
0
]
;
if
(
typeof
first
===
"string"
)
{
return
first
;
}
if
(
isParsedValueToken
(
first
)
)
{
return
tokensToString
(
value
as
ParsedValueToken
[
]
)
;
}
}
return
undefined
;
}
type
MatchCriteriaValue
=
string
|
Array
<
{
val
:
string
;
separator
:
string
;
quoted
?:
boolean
}
>
;
function
getCriteriaValue
(
criteria
:
Record
<
string
,
MatchCriteriaValue
>
,
key
:
string
)
:
MatchCriteriaValue
|
undefined
{
const
match
=
Object
.
entries
(
criteria
)
.
find
(
(
[
criteriaKey
]
)
=>
criteriaKey
.
toLowerCase
(
)
===
key
.
toLowerCase
(
)
)
;
return
match
?.
[
1
]
;
}
function
criteriaToString
(
value
:
MatchCriteriaValue
|
undefined
)
:
string
|
undefined
{
if
(
typeof
value
===
"string"
)
{
return
value
;
}
if
(
Array
.
isArray
(
value
)
)
{
return
value
[
0
]
?.
val
;
}
return
undefined
;
}
function
criteriaToStringArray
(
value
:
MatchCriteriaValue
|
undefined
)
:
string
[
]
{
if
(
typeof
value
===
"string"
)
{
return
[
value
]
;
}
if
(
Array
.
isArray
(
value
)
)
{
return
value
.
map
(
(
{
val
}
)
=>
val
)
;
}
return
[
]
;
}
function
expandMatchExecTokens
(
command
:
string
,
hostName
:
string
,
user
?:
string
)
:
string
{
return
command
.
replace
(
/
%
(
%
|
h
|
r
)
/
g
,
(
_match
,
token
)
=>
{
switch
(
token
)
{
case
"%"
:
return
"%"
;
case
"h"
:
return
hostName
;
case
"r"
:
return
user
??
""
;
default
:
return
_match
;
}
}
)
;
}
/**
* Handle `Match host ... !exec ...` blocks that ssh-config doesn't evaluate.
*
* Limitation: Only applies ProxyCommand from matching Match blocks. Other directives
* like User, Port, IdentityFile in the same block are ignored. This is sufficient for
* Coder configs which only set ProxyCommand in Match blocks.
*/
function
applyNegatedExecMatch
(
config
:
SSHConfig
,
hostName
:
string
,
user
:
string
|
undefined
,
computed
:
Record
<
string
,
ComputedConfigValue
>
)
:
void
{
if
(
getConfigValue
(
computed
,
"ProxyCommand"
)
)
{
return
;
}
for
(
const
line
of
config
)
{
if
(
line
.
type
!==
SSHConfig
.
DIRECTIVE
||
line
.
param
!==
"Match"
)
{
continue
;
}
if
(
!
(
"criteria"
in
line
)
)
{
continue
;
}
const
criteria
=
line
.
criteria
as
Record
<
string
,
MatchCriteriaValue
>
;
const
hostCriterion
=
getCriteriaValue
(
criteria
,
"host"
)
;
const
negatedExec
=
getCriteriaValue
(
criteria
,
"!exec"
)
;
if
(
!
hostCriterion
||
!
negatedExec
)
{
continue
;
}
const
hostPatterns
=
criteriaToStringArray
(
hostCriterion
)
;
if
(
!
glob
(
hostPatterns
,
hostName
)
)
{
continue
;
}
const
execCommand
=
criteriaToString
(
negatedExec
)
;
if
(
!
execCommand
)
{
continue
;
}
const
expandedCommand
=
expandMatchExecTokens
(
execCommand
,
hostName
,
user
)
;
const
execResult
=
spawnSync
(
expandedCommand
,
{
shell
:
true
}
)
;
if
(
execResult
.
status
===
0
)
{
continue
;
}
const
proxyLine
=
line
.
config
.
find
(
(
subline
)
=>
subline
.
type
===
SSHConfig
.
DIRECTIVE
&&
subline
.
param
.
toLowerCase
(
)
===
"proxycommand"
)
;
if
(
proxyLine
?.
type
===
SSHConfig
.
DIRECTIVE
)
{
computed
.
ProxyCommand
=
proxyLine
.
value
as
ComputedConfigValue
;
return
;
}
}
}
function
toStringArray
(
value
:
ComputedConfigValue
|
undefined
)
:
string
[
]
{
if
(
typeof
value
===
"string"
)
{
return
[
value
]
;
}
if
(
Array
.
isArray
(
value
)
)
{
const
first
=
value
[
0
]
;
if
(
typeof
first
===
"string"
)
{
return
value
as
string
[
]
;
}
if
(
isParsedValueToken
(
first
)
)
{
return
[
tokensToString
(
value
as
ParsedValueToken
[
]
)
]
;
}
}
return
[
]
;
}
async
function
loadSSHConfig
(
)
:
Promise
<
SSHConfig
|
null
>
{
const
homeDir
=
getHomeDir
(
)
;
const
configPath
=
path
.
join
(
homeDir
,
".ssh"
,
"config"
)
;
try
{
const
content
=
await
fs
.
readFile
(
configPath
,
"utf8"
)
;
const
parsed
=
SSHConfig
.
parse
(
content
)
;
return
parsed
;
}
catch
(
error
)
{
if
(
(
error
as
NodeJS
.
ErrnoException
|
undefined
)
?.
code
!==
"ENOENT"
)
{
log
.
debug
(
"Failed to read SSH config"
,
{
configPath
,
error
:
getErrorMessage
(
error
)
,
}
)
;
}
return
null
;
}
}
export
async
function
resolveSSHConfig
(
host
:
string
)
:
Promise
<
ResolvedSSHConfig
>
{
const
{
host
:
hostAlias
,
user
:
userOverride
}
=
parseHostAndUser
(
host
)
;
const
homeDir
=
getHomeDir
(
)
;
const
config
=
await
loadSSHConfig
(
)
;
const
computed
=
config
?
userOverride
?
config
.
compute
(
{
Host
:
hostAlias
,
User
:
userOverride
}
)
:
config
.
compute
(
hostAlias
)
:
{
}
;
const
hostName
=
toStringValue
(
getConfigValue
(
computed
,
"HostName"
)
)
??
hostAlias
;
const
userFromConfig
=
toStringValue
(
getConfigValue
(
computed
,
"User"
)
)
;
if
(
config
)
{
// Default to local username for %r expansion if no User is specified
const
matchExecUser
=
userOverride
??
userFromConfig
??
getDefaultUsername
(
)
;
applyNegatedExecMatch
(
config
,
hostName
,
matchExecUser
,
computed
)
;
}
const
portValue
=
toStringValue
(
getConfigValue
(
computed
,
"Port"
)
)
;
const
identityValues
=
toStringArray
(
getConfigValue
(
computed
,
"IdentityFile"
)
)
;
const
proxyCommandRaw
=
toStringValue
(
getConfigValue
(
computed
,
"ProxyCommand"
)
)
;
const
port
=
portValue
?
Number
.
parseInt
(
portValue
,
10
)
:
DEFAULT_SSH_PORT
;
const
identityFiles
=
identityValues
.
map
(
(
value
)
=>
normalizeIdentityFile
(
value
,
homeDir
)
)
;
const
proxyCommand
=
proxyCommandRaw
&&
proxyCommandRaw
.
toLowerCase
(
)
!==
"none"
?
proxyCommandRaw
.
trim
(
)
:
undefined
;
return
{
host
:
hostAlias
,
hostName
,
user
:
userOverride
??
userFromConfig
,
port
:
Number
.
isFinite
(
port
)
?
port
:
DEFAULT_SSH_PORT
,
identityFiles
,
proxyCommand
,
}
;
}
Back
|
FazBrowse Home
|
New Git URL