This is the eleventh part of the Availability Anywhere series. For your convenience you can find other parts in the table of contents in Part 1 – Connecting to SSH tunnel automatically in Windows
We already have our Channel Bonder for aggregating links. However, if all of them go down briefly we end up with a lag. Ideally, we’d like to refresh the connection as soon as possible.
There are 2 network stack registry settings controlling how fast the connection is dropped or restored. They are Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxConnectRetransmissions
and Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\TcpMaxDataRetransmissions
. By setting them you can control how many retries for keep alive packets are allowed. However, to change them we need to restart the machine. What’s worse, we can’t tune them easily.
What we can do instead is we can track if our SSH connection is alive and then restore it easily if needed. This won’t solve all the issues as mstsc will keep its own reconnection time but will help significantly to keep the channel alive.
First, you start with configuring your kitty to authenticate automatically, reconnect on failures etc. You also configure it to run the following command after connecting to the server:
1 |
bash -c "while :; do k; sleep 1; done" |
This will effectively print something like bash: k: command not found
. It doesn’t matter what you print as long as we get some visible output.
Next, you configure kitty to write the output to a log file. We want to check that file often to detect when the tunnel got lag. In that case we kill kitty and restart it to refresh the port forwarding as early as possible. This will briefly stop RDP and other services going through the channel but that doesn’t make things worse as they wouldn’t work anyway. Let’s use PowerShell
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 |
while ($true) { taskkill /F /IM kitty.exe rm kitty.log touch kitty.log rm kitty.old.log touch kitty.old.log .\kitty.exe -load channel_bonder -log kitty.log sleep -s 5 $retries = 0 while($retries -lt 1){ sleep -s 3 copy -force kitty.log kitty.temp.log if ((get-filehash kitty.temp.log).Hash -eq (get-filehash kitty.old.log).Hash){ $retries = $retries + 1 }else { $retries = 0 } cp -force kitty.temp.log kitty.old.log } } |
That’s it. Run this script and your kitty will get restarted as soon as we detect the lag. Tune your retries count and sleep times as you wish.