Skip to content

Commit

Permalink
Desktop app fixes. Screen capture tweaks. Installer tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 6, 2020
1 parent 950f8db commit ab09c81
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 69 deletions.
45 changes: 22 additions & 23 deletions Agent.Installer.Win/Services/InstallerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@ public class InstallerService

}

private void CreateSupportShortcut(string serverUrl, string deviceUuid, bool createSupportShortcut)
{
var shell = new WshShell();
var shortcutLocation = Path.Combine(InstallPath, "Get Support.lnk");
var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Get IT support";
shortcut.IconLocation = Path.Combine(InstallPath, "Remotely_Agent.exe");
shortcut.TargetPath = serverUrl.TrimEnd('/') + $"/GetSupport?deviceID={deviceUuid}";
shortcut.Save();

if (createSupportShortcut)
{
var systemRoot = Path.GetPathRoot(Environment.SystemDirectory);
var publicDesktop = Path.Combine(systemRoot, "Users", "Public", "Desktop", "Get Support.lnk");
FileIO.Copy(shortcutLocation, publicDesktop, true);
}
}

public async Task<bool> Uninstall()
{
try
Expand All @@ -117,7 +99,7 @@ public async Task<bool> Uninstall()
ClearInstallDirectory();
ProcessEx.StartHidden("cmd.exe", $"/c timeout 5 & rd /s /q \"{InstallPath}\"");

ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop\"").WaitForExit();
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop Unattended\"").WaitForExit();

GetRegistryBaseKey().DeleteSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Remotely", false);

Expand All @@ -132,9 +114,9 @@ public async Task<bool> Uninstall()

private void AddFirewallRule()
{
var screenCastPath = Path.Combine(InstallPath, "Desktop", "Remotely_Desktop.exe");
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop\"").WaitForExit();
ProcessEx.StartHidden("netsh", $"advfirewall firewall add rule name=\"Remotely Desktop\" program=\"{screenCastPath}\" protocol=any dir=in enable=yes action=allow profile=Private,Domain description=\"The agent that allows screen sharing and remote control for Remotely.\"").WaitForExit();
var desktopExePath = Path.Combine(InstallPath, "Desktop", "Remotely_Desktop.exe");
ProcessEx.StartHidden("netsh", "advfirewall firewall delete rule name=\"Remotely Desktop Unattended\"").WaitForExit();
ProcessEx.StartHidden("netsh", $"advfirewall firewall add rule name=\"Remotely Desktop Unattended\" program=\"{desktopExePath}\" protocol=any dir=in enable=yes action=allow description=\"The agent that allows screen sharing and remote control for Remotely.\"").WaitForExit();
}

private void BackupDirectory()
Expand Down Expand Up @@ -204,6 +186,23 @@ private void CreateDeviceSetupOptions(string deviceGroup, string deviceAlias)
}
}

private void CreateSupportShortcut(string serverUrl, string deviceUuid, bool createSupportShortcut)
{
var shell = new WshShell();
var shortcutLocation = Path.Combine(InstallPath, "Get Support.lnk");
var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Get IT support";
shortcut.IconLocation = Path.Combine(InstallPath, "Remotely_Agent.exe");
shortcut.TargetPath = serverUrl.TrimEnd('/') + $"/GetSupport?deviceID={deviceUuid}";
shortcut.Save();

if (createSupportShortcut)
{
var systemRoot = Path.GetPathRoot(Environment.SystemDirectory);
var publicDesktop = Path.Combine(systemRoot, "Users", "Public", "Desktop", "Get Support.lnk");
FileIO.Copy(shortcutLocation, publicDesktop, true);
}
}
private void CreateUninstallKey()
{
var version = FileVersionInfo.GetVersionInfo(Path.Combine(InstallPath, "Remotely_Agent.exe"));
Expand Down Expand Up @@ -350,7 +349,7 @@ private void InstallService()
Logger.Write("Service installed.");
serv = ServiceController.GetServices().FirstOrDefault(ser => ser.ServiceName == "Remotely_Service");

ProcessEx.StartHidden("cmd.exe", "/c sc.exe failure \"Remotely_Service\" reset=5 actions=restart/5000");
ProcessEx.StartHidden("cmd.exe", "/c sc.exe failure \"Remotely_Service\" reset= 5 actions= restart/5000");
}
if (serv.Status != ServiceControllerStatus.Running)
{
Expand Down
6 changes: 3 additions & 3 deletions Desktop.Core/Models/Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public async Task SendWindowsSessions()
}
}

public async Task ThrottleIfNeeded()
public void ThrottleIfNeeded()
{
if (AutoAdjustQuality && DateTimeOffset.Now - lastQualityAdjustment > TimeSpan.FromSeconds(2))
{
Expand All @@ -189,8 +189,8 @@ public async Task ThrottleIfNeeded()
ImageQuality = defaultImageQuality;
}
}

await TaskHelper.DelayUntil(() => PendingSentFrames.Count < 5 &&
TaskHelper.DelayUntil(() => PendingSentFrames.Count < 5 &&
(
!PendingSentFrames.TryPeek(out var result) || DateTimeOffset.Now - result < TimeSpan.FromSeconds(1)
),
Expand Down
2 changes: 1 addition & 1 deletion Desktop.Core/Services/ScreenCaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public async Task BeginScreenCasting(ScreenCastRequest screenCastRequest)
Debug.WriteLine($"Capture FPS: {fpsQueue.Count}");
}

await viewer.ThrottleIfNeeded();
viewer.ThrottleIfNeeded();

viewer.Capturer.GetNextFrame();

Expand Down
2 changes: 1 addition & 1 deletion Desktop.Win.Wrapper/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void AddFirewallRule()

Process.Start(psi).WaitForExit();

psi.Arguments = $"advfirewall firewall add rule name=\"Remotely Desktop\" program=\"{Path.Combine(currentVersionDir, remotelyDesktopFilename)}\" protocol=any dir=in enable=yes action=allow profile=Private,Domain description=\"The agent that allows screen sharing and remote control for Remotely.\"";
psi.Arguments = $"advfirewall firewall add rule name=\"Remotely Desktop\" program=\"{Path.Combine(currentVersionDir, remotelyDesktopFilename)}\" protocol=any dir=in enable=yes action=allow description=\"The agent that allows screen sharing and remote control for Remotely.\"";

Process.Start(psi).WaitForExit();
}
Expand Down
31 changes: 22 additions & 9 deletions Desktop.Win/Services/ScreenCapturerWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ public void GetNextFrame()
PreviousFrame?.Dispose();
PreviousFrame = (Bitmap)CurrentFrame.Clone();

if (directxScreens.ContainsKey(SelectedScreen))
{
GetDirectXFrame();
}
else
// Sometimes DX will result in a timeout, even when there are changes
// on the screen. I've observed this when a laptop lid is closed, or
// on some machines that aren't connected to a monitor. This will
// have it fall back to BitBlt in those cases.
if (!directxScreens.ContainsKey(SelectedScreen) ||
GetDirectXFrame() == GetDirectXFrameResult.Timeout)
{
GetBitBltFrame();
}

}
catch (Exception e)
{
Expand Down Expand Up @@ -172,7 +174,14 @@ private void GetBitBltFrame()
}
}

private void GetDirectXFrame()
private enum GetDirectXFrameResult
{
Success,
Failure,
Timeout,
}

private GetDirectXFrameResult GetDirectXFrame()
{
try
{
Expand All @@ -189,13 +198,13 @@ private void GetDirectXFrame()
{
if (result.Code == SharpDX.DXGI.ResultCode.WaitTimeout.Code)
{
return;
return GetDirectXFrameResult.Timeout;
}
else
{
Logger.Write($"TryAcquireFrame error. Code: {result.Code}");
NeedsInit = true;
return;
return GetDirectXFrameResult.Failure;
}
}

Expand All @@ -206,7 +215,7 @@ private void GetDirectXFrame()
duplicatedOutput.ReleaseFrame();
}
catch { }
return;
return GetDirectXFrameResult.Failure;
}

// Copy resource into memory that can be accessed by the CPU
Expand Down Expand Up @@ -240,14 +249,18 @@ private void GetDirectXFrame()

screenResource.Dispose();
duplicatedOutput.ReleaseFrame();

return GetDirectXFrameResult.Success;
}
catch (SharpDXException e)
{
if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
{
Logger.Write(e);
NeedsInit = true;
return GetDirectXFrameResult.Failure;
}
return GetDirectXFrameResult.Timeout;
}
}

Expand Down
25 changes: 13 additions & 12 deletions Desktop.Win/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public class MainWindowViewModel : ViewModelBase
public MainWindowViewModel()
{
Current = this;

Application.Current.Exit += Application_Exit;

CursorIconWatcher = Services?.GetRequiredService<ICursorIconWatcher>();
CursorIconWatcher.OnChange += CursorIconWatcher_OnChange;
Services?.GetRequiredService<IClipboardService>().BeginWatching();
Conductor = Services?.GetRequiredService<Conductor>();
CasterSocket = Services?.GetRequiredService<CasterSocket>();
Conductor.SessionIDChanged += SessionIDChanged;
Conductor.ViewerRemoved += ViewerRemoved;
Conductor.ViewerAdded += ViewerAdded;
Conductor.ScreenCastRequested += ScreenCastRequested;
}

public static IServiceProvider Services => ServiceContainer.Instance;
Expand Down Expand Up @@ -175,18 +187,7 @@ public async Task GetSessionID()

public async Task Init()
{
Application.Current.Exit += Application_Exit;

CursorIconWatcher = Services?.GetRequiredService<ICursorIconWatcher>();
CursorIconWatcher.OnChange += CursorIconWatcher_OnChange;
Services?.GetRequiredService<IClipboardService>().BeginWatching();
Conductor = Services?.GetRequiredService<Conductor>();
CasterSocket = Services?.GetRequiredService<CasterSocket>();
Conductor.SessionIDChanged += SessionIDChanged;
Conductor.ViewerRemoved += ViewerRemoved;
Conductor.ViewerAdded += ViewerAdded;
Conductor.ScreenCastRequested += ScreenCastRequested;


SessionID = "Retrieving...";

Host = Config.GetConfig().Host;
Expand Down
2 changes: 1 addition & 1 deletion Server/API/RemoteControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ bool remoteControlStarted()
.All(x => existingSessions.Contains(x.CasterSocketID));
};

if (!await TaskHelper.DelayUntil(remoteControlStarted, TimeSpan.FromSeconds(30)))
if (!await TaskHelper.DelayUntilAsync(remoteControlStarted, TimeSpan.FromSeconds(30)))
{
return StatusCode(408, "The remote control process failed to start in time on the remote device.");
}
Expand Down
2 changes: 1 addition & 1 deletion Server/API/ScriptingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task<ActionResult<CommandResult>> ExecuteCommand(string mode, strin
DataService.AddOrUpdateCommandResult(commandResult);
var requestID = Guid.NewGuid().ToString();
await AgentHubContext.Clients.Client(connection.Key).SendAsync("ExecuteCommandFromApi", mode, requestID, command, commandResult.ID, Guid.NewGuid().ToString());
var success = await TaskHelper.DelayUntil(() => AgentHub.ApiScriptResults.TryGetValue(requestID, out _), TimeSpan.FromSeconds(30));
var success = await TaskHelper.DelayUntilAsync(() => AgentHub.ApiScriptResults.TryGetValue(requestID, out _), TimeSpan.FromSeconds(30));
if (!success)
{
return commandResult;
Expand Down
13 changes: 12 additions & 1 deletion Shared/Helpers/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace Remotely.Shared.Helpers
{
public static class TaskHelper
{
public static async Task<bool> DelayUntil(Func<bool> condition, TimeSpan timeout, int pollingMs = 10)
public static bool DelayUntil(Func<bool> condition, TimeSpan timeout, int pollingMs = 10)
{
var sw = Stopwatch.StartNew();
while (!condition() && sw.Elapsed < timeout)
{
Thread.Sleep(pollingMs);
}
return condition();
}

public static async Task<bool> DelayUntilAsync(Func<bool> condition, TimeSpan timeout, int pollingMs = 10)
{
var sw = Stopwatch.StartNew();
while (!condition() && sw.Elapsed < timeout)
Expand Down
35 changes: 20 additions & 15 deletions Utilities/Publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,37 @@ param (
[string]$RID = "",
[string]$Hostname = "",
[string]$CertificatePath = "",
[string]$CertificatePassword = ""
[string]$CertificatePassword = "",
[string]$CurrentVersion = ""
)



$ErrorActionPreference = "Stop"
$Year = ([DateTime]::UtcNow).Year.ToString()
$Month = ([DateTime]::UtcNow).Month.ToString().PadLeft(2, "0")
$Day = ([DateTime]::UtcNow).Day.ToString().PadLeft(2, "0")
$Hour = ([DateTime]::UtcNow).Hour.ToString().PadLeft(2, "0")
$Minute = ([DateTime]::UtcNow).Minute.ToString().PadLeft(2, "0")
$CurrentVersion = "$Year.$Month.$Day.$Hour$Minute"
$MSBuildPath = (Get-ChildItem -Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio\" -Recurse -Filter "MSBuild.exe" -File | ForEach-Object {
[System.Diagnostics.FileVersionInfo]::GetVersionInfo($_.FullName)
} | Sort-Object -Property FileVersion -Descending | Select-Object -First 1).FileName
$Root = (Get-Item -Path $PSScriptRoot).Parent.FullName
$SignAssemblies = $false

if (!$CurrentVersion) {
$Year = ([DateTime]::UtcNow).Year.ToString()
$Month = ([DateTime]::UtcNow).Month.ToString().PadLeft(2, "0")
$Day = ([DateTime]::UtcNow).Day.ToString().PadLeft(2, "0")
$Hour = ([DateTime]::UtcNow).Hour.ToString().PadLeft(2, "0")
$Minute = ([DateTime]::UtcNow).Minute.ToString().PadLeft(2, "0")
$CurrentVersion = "$Year.$Month.$Day.$Hour$Minute"
}

if ($CertificatePath.Length -gt 0 -and
(Test-Path -Path $CertificatePath) -eq $true -and
$CertificatePassword.Length -gt 0)
{
$SignAssemblies = $true
}



Set-Location -Path $Root

#region Functions
Expand Down Expand Up @@ -65,14 +78,6 @@ if ([string]::IsNullOrWhiteSpace($MSBuildPath) -or !(Test-Path -Path $MSBuildPat
return
}

if ($CertificatePath.Length -gt 0 -and
(Test-Path -Path $CertificatePath) -eq $true -and
$CertificatePassword.Length -gt 0)
{
$SignAssemblies = $true
}


# Add Current Version file to root content folder for client update checks.
# TODO: Remove after a few releases.
Set-Content -Path "$Root\Server\CurrentVersion.txt" -Value $CurrentVersion.Trim() -Encoding UTF8 -Force
Expand Down
2 changes: 1 addition & 1 deletion Utilities/Remotely_CentOS_Install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

# Install Certbot and get SSL cert.
yum -y install certbot python2-certbot-nginx
yum -y install certbot python3-certbot-nginx

certbot --nginx
2 changes: 1 addition & 1 deletion Utilities/Remotely_Server_Install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ systemctl restart remotely.service


# Install Certbot and get SSL cert.
apt-get -y install certbot python-certbot-nginx
apt-get -y install certbot python3-certbot-nginx

certbot --nginx

0 comments on commit ab09c81

Please sign in to comment.