Skip to content

Commit

Permalink
Ubuntu desktop app requires sudo but will install dependencies. Creat…
Browse files Browse the repository at this point in the history
…ed config service to replace static class.
  • Loading branch information
bitbound committed Jan 21, 2021
1 parent 052001f commit 005e556
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 145 deletions.
15 changes: 15 additions & 0 deletions Desktop.Core/Interfaces/IConfigService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Remotely.Desktop.Core.Interfaces
{
public interface IConfigService
{
DesktopAppConfig GetConfig();
void Save(DesktopAppConfig config);
}
}
1 change: 1 addition & 0 deletions Desktop.Core/Services/Viewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public void Dispose()
RtcSession,
Capturer
});
GC.SuppressFinalize(this);
}

public async Task InitializeWebRtc()
Expand Down
1 change: 1 addition & 0 deletions Desktop.Linux/Desktop.Linux.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<PackageReference Include="Avalonia" Version="0.9.12" />
<PackageReference Include="Avalonia.Desktop" Version="0.9.12" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.9.12" />
<PackageReference Include="Mono.Posix.NETStandard" Version="5.20.1-preview" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Desktop.Core\Desktop.Core.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ in this Software without prior written authorization from The Open Group.
using System;
using System.Runtime.InteropServices;

namespace Remotely.Desktop.Linux.X11Interop
namespace Remotely.Desktop.Linux.Native
{
public static unsafe class LibX11
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.InteropServices;

namespace Remotely.Desktop.Linux.X11Interop
namespace Remotely.Desktop.Linux.Native
{
public class LibXtst
{
Expand Down
15 changes: 15 additions & 0 deletions Desktop.Linux/Native/Libc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Remotely.Desktop.Linux.Native
{
public class Libc
{
[DllImport("libc", SetLastError = true)]
public static extern uint geteuid();
}
}
1 change: 1 addition & 0 deletions Desktop.Linux/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private static void BuildServices()
serviceCollection.AddSingleton<IShutdownService, ShutdownServiceLinux>();
serviceCollection.AddScoped<IDtoMessageHandler, DtoMessageHandler>();
serviceCollection.AddScoped<IRemoteControlAccessService, RemoteControlAccessServiceLinux>();
serviceCollection.AddScoped<IConfigService, ConfigServiceLinux>();

ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
}
Expand Down
8 changes: 3 additions & 5 deletions Desktop.Linux/Services/ClipboardServiceLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void BeginWatching()
finally
{
cancelTokenSource = new CancellationTokenSource();
_ = Task.Run(() => WatchClipboard(cancelTokenSource.Token));
_ = Task.Run(async () => await WatchClipboard(cancelTokenSource.Token));
}
}

Expand Down Expand Up @@ -52,16 +52,14 @@ public void StopWatching()
cancelTokenSource?.Dispose();
}

private void WatchClipboard(CancellationToken cancelToken)
private async Task WatchClipboard(CancellationToken cancelToken)
{
while (!cancelToken.IsCancellationRequested &&
!Environment.HasShutdownStarted)
{
try
{
var currentText = EnvironmentHelper.StartProcessWithResults("xclip", "-o");
// TODO: Switch back when fixed.
//var currentText = await App.Current.Clipboard.GetTextAsync();
var currentText = await App.Current.Clipboard.GetTextAsync();
if (!string.IsNullOrEmpty(currentText) && currentText != ClipboardText)
{
ClipboardText = currentText;
Expand Down
43 changes: 0 additions & 43 deletions Desktop.Linux/Services/Config.cs

This file was deleted.

48 changes: 48 additions & 0 deletions Desktop.Linux/Services/ConfigServiceLinux.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Remotely.Desktop.Core.Interfaces;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
using System;
using System.IO;
using System.Text.Json;

namespace Remotely.Desktop.Linux.Services
{
public class ConfigServiceLinux : IConfigService
{
private static string _configFile => Path.Combine(_configFolder, "Config.json");
private static string _configFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "remotely.json");

public DesktopAppConfig GetConfig()
{
var config = new DesktopAppConfig();

if (string.IsNullOrWhiteSpace(config.Host) &&
File.Exists(_configFile))
{
try
{
config = JsonSerializer.Deserialize<DesktopAppConfig>(File.ReadAllText(_configFile));
}
catch (Exception ex)
{
Logger.Write(ex);
}
}

return config;
}

public void Save(DesktopAppConfig config)
{
try
{
Directory.CreateDirectory(_configFolder);
File.WriteAllText(_configFile, JsonSerializer.Serialize(config));
}
catch (Exception ex)
{
Logger.Write(ex);
}
}
}
}
2 changes: 1 addition & 1 deletion Desktop.Linux/Services/KeyboardMouseInputLinux.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Remotely.Desktop.Core.Enums;
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Core.Services;
using Remotely.Desktop.Linux.X11Interop;
using Remotely.Desktop.Linux.Native;
using Remotely.Shared.Utilities;
using System;

Expand Down
3 changes: 2 additions & 1 deletion Desktop.Linux/Services/ScreenCapturerLinux.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Linux.X11Interop;
using Remotely.Desktop.Linux.Native;
using Remotely.Shared.Utilities;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -31,6 +31,7 @@ public ScreenCapturerLinux()
public void Dispose()
{
LibX11.XCloseDisplay(Display);
GC.SuppressFinalize(this);
}

public IEnumerable<string> GetDisplayNames() => _x11Screens.Keys;
Expand Down
72 changes: 28 additions & 44 deletions Desktop.Linux/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Remotely.Desktop.Core.Interfaces;
using Remotely.Desktop.Core.Services;
using Remotely.Desktop.Linux.Controls;
using Remotely.Desktop.Linux.Native;
using Remotely.Desktop.Linux.Services;
using Remotely.Desktop.Linux.Views;
using Remotely.Shared.Models;
Expand Down Expand Up @@ -38,6 +39,7 @@ public MainWindowViewModel()
Services.GetRequiredService<IClipboardService>().BeginWatching();
Services.GetRequiredService<IKeyboardMouseInput>().Init();

ConfigService = Services.GetRequiredService<IConfigService>();
Conductor = Services.GetRequiredService<Conductor>();
CasterSocket = Services.GetRequiredService<ICasterSocket>();

Expand Down Expand Up @@ -126,6 +128,7 @@ public string SessionID
public ObservableCollection<Viewer> Viewers { get; } = new ObservableCollection<Viewer>();
private static IServiceProvider Services => ServiceContainer.Instance;
private ICasterSocket CasterSocket { get; }
private IConfigService ConfigService { get; }
private Conductor Conductor { get; }
public async Task GetSessionID()
{
Expand All @@ -137,13 +140,19 @@ public async Task Init()
{
try
{
if (Libc.geteuid() != 0)
{
await MessageBox.Show("Please run with sudo.", "Sudo Required", MessageBoxType.OK);
Environment.Exit(0);
}

SessionID = "Retrieving...";
SessionID = "Initializing...";

await CheckDependencies();
await InstallDependencies();

SessionID = "Retrieving...";

Host = Config.GetConfig().Host;
Host = ConfigService.GetConfig().Host;

while (string.IsNullOrWhiteSpace(Host))
{
Expand Down Expand Up @@ -211,59 +220,34 @@ public async Task PromptForHostName()
if (result != Host)
{
Host = result.TrimEnd('/');
var config = Config.GetConfig();
var config = ConfigService.GetConfig();
config.Host = Host;
config.Save();
ConfigService.Save(config);
}
}


private async Task CheckDependencies()
private async Task InstallDependencies()
{
try
{
var dependencies = new string[]
var psi = new ProcessStartInfo()
{
"libx11-dev",
"libc6-dev",
"libgdiplus",
"libxtst-dev",
"xclip"
FileName = "sudo",
Arguments = "bash -c \"apt-get -y install libx11-dev ; " +
"apt-get -y install libc6-dev ; " +
"apt-get -y install libgdiplus ; " +
"apt-get -y install libxtst-dev ; " +
"apt-get -y install xclip\"",
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true
};

foreach (var dependency in dependencies)
{
var proc = Process.Start("dpkg", $"-s {dependency}");
proc.WaitForExit();
if (proc.ExitCode != 0)
{
var commands = "sudo apt-get -y install libx11-dev ; " +
"sudo apt-get -y install libc6-dev ; " +
"sudo apt-get -y install libgdiplus ; " +
"sudo apt-get -y install libxtst-dev ; " +
"sudo apt-get -y install xclip";

await App.Current.Clipboard.SetTextAsync(commands);

var message = "The following dependencies are required. Install commands have been copied to your clipboard." +
Environment.NewLine + Environment.NewLine +
"Please paste them into a terminal and run, then try opening Remotely again." +
Environment.NewLine + Environment.NewLine +
"libx11-dev" + Environment.NewLine +
"libc6-dev" + Environment.NewLine +
"libgdiplus" + Environment.NewLine +
"libxtst-dev" + Environment.NewLine +
"xclip";

await MessageBox.Show(message, "Dependencies Required", MessageBoxType.OK);

Environment.Exit(0);
}
}
await Task.Run(() => Process.Start(psi).WaitForExit());
}
catch
{
Logger.Write("Unable to check dependencies.", Shared.Enums.EventType.Warning);
Logger.Write("Failed to install dependencies.", Shared.Enums.EventType.Error);
}

}
Expand Down Expand Up @@ -303,9 +287,9 @@ private void ViewerAdded(object sender, Viewer viewer)
});
}

private void ViewerRemoved(object sender, string viewerID)
private async void ViewerRemoved(object sender, string viewerID)
{
Dispatcher.UIThread.InvokeAsync(() =>
await Dispatcher.UIThread.InvokeAsync(() =>
{
var viewer = Viewers.FirstOrDefault(x => x.ViewerConnectionID == viewerID);
if (viewer != null)
Expand Down
1 change: 1 addition & 0 deletions Desktop.Win/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private static void BuildServices()
serviceCollection.AddSingleton<IShutdownService, ShutdownServiceWin>();
serviceCollection.AddScoped<IDtoMessageHandler, DtoMessageHandler>();
serviceCollection.AddScoped<IRemoteControlAccessService, RemoteControlAccessServiceWin>();
serviceCollection.AddScoped<IConfigService, ConfigServiceWin>();

BackgroundForm = new Form()
{
Expand Down
Loading

0 comments on commit 005e556

Please sign in to comment.