44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
using System.Diagnostics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace LeagueAPI.Utils;
|
|
|
|
internal class PortTokenWithLockfile : IPortTokenBehavior
|
|
{
|
|
public bool TryGet(Process process, out string remotingAuthToken, out int appPort, [NotNullWhen(false)] out Exception? exception)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo directory = new(Path.GetDirectoryName(process.MainModule?.FileName) ?? throw new FileNotFoundException("Lockfile not found.", "lockfile"));
|
|
FileInfo lockfile = new(Path.Join(directory.FullName, "lockfile"));
|
|
while (!lockfile.Exists)
|
|
{
|
|
if (directory.Root == directory)
|
|
{
|
|
break;
|
|
}
|
|
directory = directory.Parent ?? directory.Root;
|
|
lockfile = new(Path.Join(directory.FullName, "lockfile"));
|
|
}
|
|
|
|
using FileStream stream = lockfile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
using StreamReader streamReader = new(stream);
|
|
string[] array = streamReader.ReadToEnd().Split(":");
|
|
if (array is not [string clientName, string pid, string port, string secret, string protocol] || !int.TryParse(port, out appPort))
|
|
{
|
|
throw new Exception("Failed to parse lockfile.");
|
|
}
|
|
remotingAuthToken = secret;
|
|
exception = null;
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
remotingAuthToken = string.Empty;
|
|
appPort = 0;
|
|
exception = ex;
|
|
return false;
|
|
}
|
|
}
|
|
}
|