Added ability to capture logon screen in Linux.

This commit is contained in:
Jared Goodwin 2020-01-19 12:02:01 -08:00
parent abd8839084
commit 8b849a5079
4 changed files with 71 additions and 62 deletions

View File

@ -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)

View File

@ -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);
}
}
}
}
}

View File

@ -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.

View File

@ -34,6 +34,12 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Input\**" />
<EmbeddedResource Remove="Input\**" />
<None Remove="Input\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup>
@ -43,8 +49,4 @@
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Input\" />
</ItemGroup>
</Project>