Replace System.Remoting by named pipe, remove RIPEMD-160 support

This is a step towards full .NET Core API coverage. It makes ShareX.HelpersLib 100% covered by .NET Core + Platform Extensions, from a 99.41% previously
This commit is contained in:
Charles Milette 2020-04-21 19:03:18 -04:00
parent 84f0203e8c
commit fadc744799
No known key found for this signature in database
GPG key ID: 1A5AE81377AD973A
4 changed files with 61 additions and 111 deletions

View file

@ -23,11 +23,11 @@
#endregion License Information (GPL v3) #endregion License Information (GPL v3)
using Newtonsoft.Json;
using System; using System;
using System.Runtime.Remoting; using System.IO;
using System.Runtime.Remoting.Channels; using System.IO.Pipes;
using System.Runtime.Remoting.Channels.Ipc; using System.Text;
using System.Security.Permissions;
using System.Threading; using System.Threading;
namespace ShareX.HelpersLib namespace ShareX.HelpersLib
@ -36,15 +36,15 @@ public class ApplicationInstanceManager : IDisposable
{ {
private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC"; private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
private static readonly string AppName = "ShareX"; private static readonly string AppName = "ShareX";
private static readonly string EventName = string.Format("{0}-{1}-{2}", Environment.MachineName, Environment.UserName, AppName); private static readonly string PipeName = $"{Environment.MachineName}-{Environment.UserName}-{AppName}";
private static readonly string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore"); private static readonly string SemaphoreName = PipeName + "Semaphore";
public bool IsSingleInstance { get; private set; } public bool IsSingleInstance { get; private set; }
public bool IsFirstInstance { get; private set; } public bool IsFirstInstance { get; private set; }
private Mutex mutex; private Mutex mutex;
private Semaphore semaphore; private Semaphore semaphore;
private IpcServerChannel serverChannel; private NamedPipeServerStream pipeServer;
public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback) public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
{ {
@ -75,20 +75,9 @@ public void Dispose()
{ {
if (IsFirstInstance) if (IsFirstInstance)
{ {
if (mutex != null) mutex?.ReleaseMutex();
{ pipeServer?.Dispose();
mutex.ReleaseMutex(); semaphore?.Close();
}
if (serverChannel != null)
{
ChannelServices.UnregisterChannel(serverChannel);
}
if (semaphore != null)
{
semaphore.Close();
}
} }
} }
@ -96,23 +85,17 @@ private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callbac
{ {
try try
{ {
bool createdNew; semaphore = new Semaphore(1, 1, SemaphoreName, out var createdNew);
// Mixing single instance and multi instance (via command line parameter) copies of the program can
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew)) // result in CreateFirstInstance being called if it isn't really the first one. Make sure this is
// really first instance by detecting if the semaphore was created
if (!createdNew)
{ {
// Mixing single instance and multi instance (via command line parameter) copies of the program can return;
// result in CreateFirstInstance being called if it isn't really the first one. Make sure this is
// really first instance by detecting if EventWaitHandle was created
if (!createdNew)
{
return;
}
semaphore = new Semaphore(1, 1, SemaphoreName);
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false);
RegisterRemoteType(AppName);
} }
CreateServer(callback);
} }
catch (Exception e) catch (Exception e)
{ {
@ -124,19 +107,9 @@ private void CreateMultipleInstance(string[] args)
{ {
try try
{ {
InstanceProxy.CommandLineArgs = args; semaphore = Semaphore.OpenExisting(SemaphoreName);
semaphore.WaitOne();
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName)) SendDataToServer(args);
{
semaphore = Semaphore.OpenExisting(SemaphoreName);
semaphore.WaitOne();
UpdateRemoteObject(AppName);
if (eventWaitHandle != null)
{
eventWaitHandle.Set();
}
}
} }
catch (Exception e) catch (Exception e)
{ {
@ -146,69 +119,55 @@ private void CreateMultipleInstance(string[] args)
Environment.Exit(0); Environment.Exit(0);
} }
private void UpdateRemoteObject(string uri) private void SendDataToServer(string[] args)
{ {
IpcClientChannel clientChannel = new IpcClientChannel(); using (var pipeClient = new NamedPipeClientStream(".", PipeName, PipeDirection.Out))
ChannelServices.RegisterChannel(clientChannel, true);
InstanceProxy proxy = Activator.GetObject(typeof(InstanceProxy), string.Format("ipc://{0}{1}{2}/{2}", Environment.MachineName, Environment.UserName, uri)) as InstanceProxy;
if (proxy != null)
{ {
proxy.SetCommandLineArgs(InstanceProxy.CommandLineArgs); pipeClient.Connect();
}
ChannelServices.UnregisterChannel(clientChannel); var pipeData = new InstanceCallbackEventArgs
}
private void RegisterRemoteType(string uri)
{
serverChannel = new IpcServerChannel(Environment.MachineName + Environment.UserName + uri);
ChannelServices.RegisterChannel(serverChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
}
private void WaitOrTimerCallback(object state, bool timedOut)
{
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
if (callback != null)
{
try
{ {
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs)); CommandLineArgs = args
} };
finally
{ var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pipeData));
if (semaphore != null) pipeClient.Write(bytes, 0, bytes.Length);
{
semaphore.Release();
}
}
} }
} }
}
[Serializable] private void CreateServer(EventHandler<InstanceCallbackEventArgs> callback)
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
internal class InstanceProxy : MarshalByRefObject
{
public static string[] CommandLineArgs { get; internal set; }
public void SetCommandLineArgs(string[] commandLineArgs)
{ {
CommandLineArgs = commandLineArgs; pipeServer?.Dispose();
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
pipeServer.BeginWaitForConnection(ConnectionCallback, callback);
}
private void ConnectionCallback(IAsyncResult ar)
{
var callback = ar.AsyncState as EventHandler<InstanceCallbackEventArgs>;
var sr = new StreamReader(pipeServer, Encoding.UTF8);
try
{
pipeServer.EndWaitForConnection(ar);
if (callback != null)
{
var data = sr.ReadToEnd();
callback(this, JsonConvert.DeserializeObject<InstanceCallbackEventArgs>(data));
}
}
finally
{
semaphore?.Release();
sr.Dispose();
CreateServer(callback);
}
} }
} }
public class InstanceCallbackEventArgs : EventArgs public class InstanceCallbackEventArgs : EventArgs
{ {
public string[] CommandLineArgs { get; private set; } [JsonProperty]
public string[] CommandLineArgs { get; internal set; }
internal InstanceCallbackEventArgs(string[] commandLineArgs)
{
CommandLineArgs = commandLineArgs;
}
} }
} }

View file

@ -154,8 +154,6 @@ public static HashAlgorithm GetHashAlgorithm(HashType hashType)
return new SHA384CryptoServiceProvider(); return new SHA384CryptoServiceProvider();
case HashType.SHA512: case HashType.SHA512:
return new SHA512CryptoServiceProvider(); return new SHA512CryptoServiceProvider();
case HashType.RIPEMD160:
return new RIPEMD160Managed();
} }
return null; return null;

View file

@ -97,12 +97,9 @@ public string ASCIIText
public string SHA384 { get; private set; } public string SHA384 { get; private set; }
public string SHA512 { get; private set; } public string SHA512 { get; private set; }
// http://en.wikipedia.org/wiki/RIPEMD
public string RIPEMD160 { get; private set; }
public void Clear() public void Clear()
{ {
Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = RIPEMD160 = null; Text = Base64 = CRC32 = MD5 = SHA1 = SHA256 = SHA384 = SHA512 = null;
Binary = null; Binary = null;
Hexadecimal = null; Hexadecimal = null;
ASCII = null; ASCII = null;
@ -127,7 +124,6 @@ public bool EncodeText(string text)
SHA256 = TranslatorHelper.TextToHash(text, HashType.SHA256, true); SHA256 = TranslatorHelper.TextToHash(text, HashType.SHA256, true);
SHA384 = TranslatorHelper.TextToHash(text, HashType.SHA384, true); SHA384 = TranslatorHelper.TextToHash(text, HashType.SHA384, true);
SHA512 = TranslatorHelper.TextToHash(text, HashType.SHA512, true); SHA512 = TranslatorHelper.TextToHash(text, HashType.SHA512, true);
RIPEMD160 = TranslatorHelper.TextToHash(text, HashType.RIPEMD160, true);
return true; return true;
} }
} }
@ -207,7 +203,6 @@ public string HashToString()
sb.AppendLine($"SHA-256: {SHA256}"); sb.AppendLine($"SHA-256: {SHA256}");
sb.AppendLine($"SHA-384: {SHA384}"); sb.AppendLine($"SHA-384: {SHA384}");
sb.AppendLine($"SHA-512: {SHA512}"); sb.AppendLine($"SHA-512: {SHA512}");
sb.Append($"RIPEMD-160: {RIPEMD160}");
return sb.ToString(); return sb.ToString();
} }

View file

@ -87,9 +87,7 @@ public enum HashType
[Description("SHA-384")] [Description("SHA-384")]
SHA384, SHA384,
[Description("SHA-512")] [Description("SHA-512")]
SHA512, SHA512
[Description("RIPEMD-160")]
RIPEMD160
} }
public enum BorderType public enum BorderType