@@ -3,46 +3,68 @@ Function Log($msg) {
3
3
$host.ui.WriteErrorLine (" [W] $date $msg " )
4
4
}
5
5
6
- Function RelayMessage ($from , $to , $buf , $arrow ) {
6
+ Function ReadMessage ($stream ) {
7
+ $buf = New-Object byte[] 4
7
8
$offset = 0
8
9
while ($offset -lt 4 ) {
9
- $n = $from.Read ($buf , $offset , 4 - $offset );
10
- if ($n -eq 0 ) { exit }
10
+ $n = $stream.Read ($buf , $offset , 4 - $offset );
11
+ if ($n -eq 0 ) {
12
+ break
13
+ }
11
14
$offset += $n ;
12
15
}
13
- $len = (( $buf [ 0 ] * 256 + $buf [ 1 ]) * 256 + $buf [ 2 ]) * 256 + $buf [ 3 ] + 4
14
- Log " [L] $arrow [W] $arrow ssh-agent.exe ( $len B) "
15
- $ len
16
- while ($offset -lt $len ) {
17
- $n = $from .Read ($buf , $offset , [ Math ]::Min( $len , $ buf.Length ) - $offset )
18
- if ($n -eq 0 ) { exit }
19
- $offset += $n
20
- $to .Write ( $buf , 0 , $offset )
21
- $len - = $offset
22
- $offset = 0
16
+ if ( $offset -eq 4 ) {
17
+ $len = (( $buf [ 0 ] * 256 + $buf [ 1 ]) * 256 + $buf [ 2 ]) * 256 + $buf [ 3 ] + 4
18
+ [ Array ]::Resize([ ref ] $buf , $ len)
19
+ while ($offset -lt $buf .Length ) {
20
+ $n = $stream .Read ($buf , $offset , $ buf.Length - $offset )
21
+ if ($n -eq 0 ) {
22
+ break
23
+ }
24
+ $offset + = $n
25
+ }
23
26
}
27
+ [Array ]::Resize([ref ]$buf , $offset )
28
+ return $buf
24
29
}
25
30
26
31
Function MainLoop {
27
32
Try {
28
- $buf = New-Object byte[] 8192
33
+ $ignoreOpenSSHExtensions = $false
34
+ Try {
35
+ $sshAgentVersion = (Get-Command - CommandType Application ssh-agent.exe - ErrorAction Stop)[0 ].Version
36
+ $ignoreOpenSSHExtensions = ($sshAgentVersion.Major -le 8 -and $sshAgentVersion.Minor -lt 9 )
37
+ Log " ssh-agent.exe version: $ ( $sshAgentVersion.ToString ()) (ignoreOpenSSHExtensions: $ignoreOpenSSHExtensions )"
38
+ }
39
+ Catch {
40
+ $ignoreOpenSSHExtensions = $true
41
+ }
42
+
29
43
$ssh_client_in = [console ]::OpenStandardInput()
30
44
$ssh_client_out = [console ]::OpenStandardOutput()
31
45
32
46
$ver = $PSVersionTable [" PSVersion" ]
47
+ $ssh_client_out.WriteByte (0xff )
33
48
Log " ready: PSVersion $ver "
34
49
35
- $buf [0 ] = 0xff
36
- $ssh_client_out.Write ($buf , 0 , 1 )
37
-
38
50
while ($true ) {
39
51
Try {
40
52
$null = $ssh_client_in.Read ((New-Object byte[] 1 ), 0 , 0 )
53
+ $buf = ReadMessage $ssh_client_in
54
+ if ($ignoreOpenSSHExtensions -and $buf.Length -gt 4 -and $buf [4 ] -eq 0x1b ) {
55
+ $buf = [byte []](0 , 0 , 0 , 1 , 6 )
56
+ $ssh_client_out.Write ($buf , 0 , $buf.Length )
57
+ Log " [W] return dummy for OpenSSH ext."
58
+ Continue
59
+ }
41
60
$ssh_agent = New-Object System.IO.Pipes.NamedPipeClientStream " ." , " openssh-ssh-agent" , InOut
42
61
$ssh_agent.Connect ()
43
62
Log " [W] named pipe: connected"
44
- $len = RelayMessage $ssh_client_in $ssh_agent $buf " ->"
45
- $len = RelayMessage $ssh_agent $ssh_client_out $buf " <-"
63
+ $ssh_agent.Write ($buf , 0 , $buf.Length )
64
+ Log " [L] -> [W] -> ssh-agent.exe ($ ( $buf.Length ) B)"
65
+ $buf = ReadMessage $ssh_agent
66
+ $ssh_client_out.Write ($buf , 0 , $buf.Length )
67
+ Log " [L] <- [W] <- ssh-agent.exe ($ ( $buf.Length ) B)"
46
68
}
47
69
Finally {
48
70
if ($null -ne $ssh_agent ) {
0 commit comments