mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-02 18:26:27 +13:00
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:
parent
84f0203e8c
commit
fadc744799
4 changed files with 61 additions and 111 deletions
|
@ -23,11 +23,11 @@ You should have received a copy of the GNU General Public License
|
||||||
|
|
||||||
#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);
|
||||||
|
|
||||||
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
|
|
||||||
{
|
|
||||||
// Mixing single instance and multi instance (via command line parameter) copies of the program can
|
// Mixing single instance and multi instance (via command line parameter) copies of the program can
|
||||||
// result in CreateFirstInstance being called if it isn't really the first one. Make sure this is
|
// 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
|
// really first instance by detecting if the semaphore was created
|
||||||
if (!createdNew)
|
if (!createdNew)
|
||||||
{
|
{
|
||||||
return;
|
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)
|
||||||
{
|
{
|
||||||
|
@ -123,20 +106,10 @@ private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callbac
|
||||||
private void CreateMultipleInstance(string[] args)
|
private void CreateMultipleInstance(string[] args)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
|
||||||
InstanceProxy.CommandLineArgs = args;
|
|
||||||
|
|
||||||
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
|
|
||||||
{
|
{
|
||||||
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
semaphore = Semaphore.OpenExisting(SemaphoreName);
|
||||||
semaphore.WaitOne();
|
semaphore.WaitOne();
|
||||||
UpdateRemoteObject(AppName);
|
SendDataToServer(args);
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
var pipeData = new InstanceCallbackEventArgs
|
||||||
|
{
|
||||||
|
CommandLineArgs = args
|
||||||
|
};
|
||||||
|
|
||||||
|
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(pipeData));
|
||||||
|
pipeClient.Write(bytes, 0, bytes.Length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ChannelServices.UnregisterChannel(clientChannel);
|
private void CreateServer(EventHandler<InstanceCallbackEventArgs> callback)
|
||||||
|
{
|
||||||
|
pipeServer?.Dispose();
|
||||||
|
pipeServer = new NamedPipeServerStream(PipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
|
||||||
|
pipeServer.BeginWaitForConnection(ConnectionCallback, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterRemoteType(string uri)
|
private void ConnectionCallback(IAsyncResult ar)
|
||||||
{
|
{
|
||||||
serverChannel = new IpcServerChannel(Environment.MachineName + Environment.UserName + uri);
|
var callback = ar.AsyncState as EventHandler<InstanceCallbackEventArgs>;
|
||||||
ChannelServices.RegisterChannel(serverChannel, true);
|
var sr = new StreamReader(pipeServer, Encoding.UTF8);
|
||||||
|
try
|
||||||
RemotingConfiguration.RegisterWellKnownServiceType(typeof(InstanceProxy), uri, WellKnownObjectMode.Singleton);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WaitOrTimerCallback(object state, bool timedOut)
|
|
||||||
{
|
{
|
||||||
EventHandler<InstanceCallbackEventArgs> callback = state as EventHandler<InstanceCallbackEventArgs>;
|
pipeServer.EndWaitForConnection(ar);
|
||||||
|
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
{
|
{
|
||||||
try
|
var data = sr.ReadToEnd();
|
||||||
{
|
callback(this, JsonConvert.DeserializeObject<InstanceCallbackEventArgs>(data));
|
||||||
callback(state, new InstanceCallbackEventArgs(InstanceProxy.CommandLineArgs));
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (semaphore != null)
|
semaphore?.Release();
|
||||||
{
|
sr.Dispose();
|
||||||
semaphore.Release();
|
CreateServer(callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
|
|
||||||
internal class InstanceProxy : MarshalByRefObject
|
|
||||||
{
|
|
||||||
public static string[] CommandLineArgs { get; internal set; }
|
|
||||||
|
|
||||||
public void SetCommandLineArgs(string[] commandLineArgs)
|
|
||||||
{
|
|
||||||
CommandLineArgs = commandLineArgs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue