Skip to content

Commit

Permalink
Merge pull request #484 from nblair2/rdp-wait
Browse files Browse the repository at this point in the history
RDP Wait exponential back off
  • Loading branch information
sei-dupdyke authored Jan 15, 2025
2 parents b8e2b9e + 3ca35c8 commit e9b80c9
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/Ghosts.Client/Handlers/Rdp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,15 @@ public void RdpEx(TimelineHandler handler, TimelineEvent timelineEvent, string c
process.StandardInput.Close(); // line added to stop process from hanging on ReadToEnd()
Thread.Sleep(2000); //give time for response
checkPasswordPrompt(au, password, username, usePasswordOnly);
//wait 3 minutes for connection window
if (!findRdpWindow(target, 3))
//wait 15 seconds for connection window
if (!findRdpWindow(target, 15))
{
//may have a certificate problem
Log.Trace("RDP:: Unable to find a RDP window, attempting to accept an untrusted certificate");
checkGenericPrompt(au, "{TAB}{TAB}{TAB}{ENTER}"); //this is for a certificate prompt
}
//try again for another 3 minutes to find
if (findRdpWindow(target, 3))
//try again for another 4.25 minutes to find
if (findRdpWindow(target, 255))
{
doMouseLoop(caption, target, au, timelineEvent);
}
Expand Down Expand Up @@ -278,18 +279,18 @@ public void RdpEx(TimelineHandler handler, TimelineEvent timelineEvent, string c
}
}

public bool findRdpWindow(string target,int minutes)
public bool findRdpWindow(string target,int timeout)
{
var caption = $"{target} - Remote Desktop Connection";
var winHandle = Winuser.FindWindow("TscShellContainerClass", caption);
int count = 0;
int wait = 1; //time to wait for this loop (exponential back off: 2^n starting with n=0)
//wait for window to appear
while (winHandle == IntPtr.Zero)
{
Log.Trace($"RDP:: Unable to find desktop window for {target}, sleeping 1 minute");
Thread.Sleep(60 * 1000); //sleep 1 minute, then try again
count++;
if (count >= minutes) break;
Log.Trace($"RDP:: Unable to find desktop window for {target}, sleeping {wait} seconds");
Thread.Sleep(wait * 1000); //sleep for exponential back off amount of seconds
if (2 * wait - 1 >= timeout) break; // sum(2^n) from n=0 -> n=x is 2 * 2^x - 1
wait = 2 * wait; //double the time to wait next loop (2^n)
winHandle = Winuser.FindWindow("TscShellContainerClass", caption);
}
return winHandle != IntPtr.Zero;
Expand Down

0 comments on commit e9b80c9

Please sign in to comment.