diff --git a/Agent/Services/DeviceSocket.cs b/Agent/Services/DeviceSocket.cs index d99559b0..e5c7133e 100644 --- a/Agent/Services/DeviceSocket.cs +++ b/Agent/Services/DeviceSocket.cs @@ -263,18 +263,32 @@ namespace Remotely.Agent.Services } else if (OSUtils.IsLinux) { - var users = OSUtils.StartProcessWithResults("users", ""); - var username = users?.Split()?.FirstOrDefault()?.Trim(); - var homeDir = OSUtils.StartProcessWithResults("sudo", $"-u {username} env | grep HOME")?.Split('=')?.Last(); + //var users = OSUtils.StartProcessWithResults("users", ""); + //var username = users?.Split()?.FirstOrDefault()?.Trim(); + var xauthority = OSUtils.StartProcessWithResults("find", $"/ -name Xauthority").Split('\n', StringSplitOptions.RemoveEmptyEntries).First(); + var display = ":0"; + var whoString = OSUtils.StartProcessWithResults("who", "")?.Trim(); + var username = string.Empty; + + var args = $"{rcBinaryPath} -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} & disown"; + if (!string.IsNullOrWhiteSpace(whoString)) + { + var whoLine = whoString.Split('\n', StringSplitOptions.RemoveEmptyEntries).First(); + var whoSplit = whoLine.Split(' ', StringSplitOptions.RemoveEmptyEntries); + username = whoSplit[0]; + display = whoSplit[1]; + args = $"-u {username} {args}"; + } var psi = new ProcessStartInfo() { FileName = "sudo", - Arguments = $"-u {username} {rcBinaryPath} -mode Unattended -requester {requesterID} -serviceid {serviceID} -deviceid {ConnectionInfo.DeviceID} -host {ConfigService.GetConnectionInfo().Host} & disown" + Arguments = args }; - psi.Environment.Add("DISPLAY", ":0"); - psi.Environment.Add("XAUTHORITY", $"{homeDir}/.Xauthority"); + psi.Environment.Add("DISPLAY", display); + psi.Environment.Add("XAUTHORITY", xauthority); + Logger.Write($"Attempting to launch screen caster with username {username}, xauthority {xauthority}, and display {display}."); var casterProc = Process.Start(psi); - casterProc.WaitForExit(); + await Task.Run(() => { casterProc.WaitForExit(); }); } } catch (Exception ex) diff --git a/Agent/Services/Logger.cs b/Agent/Services/Logger.cs index 57babd31..f10caed8 100644 --- a/Agent/Services/Logger.cs +++ b/Agent/Services/Logger.cs @@ -12,33 +12,31 @@ namespace Remotely.Agent.Services { public static class Logger { - public static object WriteLock { get; } = new object(); + private static string LogPath => Path.Combine(Path.GetTempPath(), "Remotely_Logs.log"); + private static object WriteLock { get; } = new object(); + public static void Debug(string message) + { + lock (WriteLock) + { +#if DEBUG + CheckLogFileExists(); + + File.AppendAllText(LogPath, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t[DEBUG]\t{message}{Environment.NewLine}"); + +#endif + System.Diagnostics.Debug.WriteLine(message); + } + + } + public static void Write(string message) { try { lock (WriteLock) { - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.log"); - if (!File.Exists(path)) - { - File.Create(path).Close(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); - } - } - if (File.Exists(path)) - { - var fi = new FileInfo(path); - while (fi.Length > 1000000) - { - var content = File.ReadAllLines(path); - File.WriteAllLines(path, content.Skip(10)); - fi = new FileInfo(path); - } - } - File.AppendAllText(path, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t[INFO]\t{message}{Environment.NewLine}"); + CheckLogFileExists(); + File.AppendAllText(LogPath, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t[INFO]\t{message}{Environment.NewLine}"); Console.WriteLine(message); } } @@ -51,39 +49,13 @@ namespace Remotely.Agent.Services { try { - var exception = ex; - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.log"); + CheckLogFileExists(); - if (!File.Exists(path)) - { - File.Create(path).Close(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); - } - } + var exception = ex; while (exception != null) { - var jsonError = new - { - Type = "Error", - Timestamp = DateTime.Now.ToString(), - Message = exception?.Message, - Source = exception?.Source, - StackTrace = exception?.StackTrace, - }; - if (File.Exists(path)) - { - var fi = new FileInfo(path); - while (fi.Length > 1000000) - { - var content = File.ReadAllLines(path); - File.WriteAllLines(path, content.Skip(10)); - fi = new FileInfo(path); - } - } - File.AppendAllText(path, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t[ERROR]\t{exception?.Message}\t{exception?.StackTrace}\t{exception?.Source}{Environment.NewLine}"); + File.AppendAllText(LogPath, $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}\t[ERROR]\t{exception?.Message}\t{exception?.StackTrace}\t{exception?.Source}{Environment.NewLine}"); Console.WriteLine(exception.Message); exception = exception.InnerException; } @@ -91,5 +63,27 @@ namespace Remotely.Agent.Services catch { } } } + + private static void CheckLogFileExists() + { + if (!File.Exists(LogPath)) + { + File.Create(LogPath).Close(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("sudo", $"chmod 666 {LogPath}").WaitForExit(); + } + } + if (File.Exists(LogPath)) + { + var fi = new FileInfo(LogPath); + while (fi.Length > 1000000) + { + var content = File.ReadAllLines(LogPath); + File.WriteAllLines(LogPath, content.Skip(10)); + fi = new FileInfo(LogPath); + } + } + } } } diff --git a/README.md b/README.md index 0dfcd593..5cca5c3a 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,6 @@ The following steps will configure your Windows 10 machine for building the Remo * Requires .NET Core Desktop Runtime. * Windows 2016/2019 should work as well, but isn't tested regularly. * Linux: Only Ubuntu 18.04+ is tested. - * Your account must be set to auto login for unattended remote control to work. ## Session Recording * You can turn on session recording in appsettings.json. diff --git a/ScreenCast.Linux/ScreenCast.Linux.csproj b/ScreenCast.Linux/ScreenCast.Linux.csproj index c9bf2e0b..6ab5e943 100644 --- a/ScreenCast.Linux/ScreenCast.Linux.csproj +++ b/ScreenCast.Linux/ScreenCast.Linux.csproj @@ -34,6 +34,12 @@ true + + + + + + @@ -43,8 +49,4 @@ - - - -