using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Net; using System.Net.Sockets; using System.Reflection; using System.Security.Cryptography; using System.Threading; namespace ScreenConnect; [SingletonType("ScreenConnect.WindowsToolkit", "ScreenConnect.Windows, Version={0}, PublicKeyToken={1}", EnvironmentFlags.FullMicrosoftClr, EnvironmentFlags.UnknownRuntime)] [SingletonType("ScreenConnect.MonoTouchToolkit", "MonoTouchCore, Version={0}, PublicKeyToken={1}", EnvironmentFlags.Mono | EnvironmentFlags.IOS, EnvironmentFlags.UnknownRuntime)] [SingletonType("ScreenConnect.WindowsAppToolkit", "ScreenConnect.WindowsApp, Version={0}, PublicKeyToken={1}", EnvironmentFlags.PortableMicrosoftClr, EnvironmentFlags.UnknownRuntime)] [SingletonType("ScreenConnect.NonWindowsToolkit", "ScreenConnect.NonWindowsCore, Version={0}, PublicKeyToken={1}", EnvironmentFlags.UnknownRuntime, EnvironmentFlags.UnknownRuntime)] public class Toolkit : Singleton { private class FrameworkThread : IThread { private Thread thread; public bool IsCompleted => thread.ThreadState == System.Threading.ThreadState.Stopped; public FrameworkThread(Thread thread) { this.thread = thread; } public void Abort() { thread.Abort(); } public void Join() { thread.Join(); } } public virtual ICryptoTransform CreateEncryptor(byte[] key, byte[] iv) { throw new NotSupportedException(); } public virtual ICryptoTransform CreateDecryptor(byte[] key, byte[] iv) { throw new NotSupportedException(); } public virtual long GetMillisecondCount() { return DateTime.UtcNow.Ticks / 10000; } public IThread StartThread(string name, CorePriority priority, Proc threadProc) { return StartThread(name, priority, delegate { threadProc(); }, null); } public virtual IThread StartThread(string name, CorePriority priority, Proc threadProc, T threadState) { return new Thread((ParameterizedThreadStart)delegate { threadProc(threadState); }) { Name = name, Priority = (ThreadPriority)priority }.Mutate(delegate(Thread it) { it.Start(); }).Pipe((Thread it) => new FrameworkThread(it)); } public virtual ICursorMessageProcessor CreateCursorMessageProcessor() { throw new NotSupportedException(); } public virtual ITraceSource GetTraceSource(string name) { return new DebugTraceSource(name); } public virtual IEnumerable GetTraceSources() { return Extensions.EmptyArray(); } public virtual void TryFreezeStackForRethrow(Exception ex) { } public virtual Guid GetMachineGuid() { return default(Guid); } public virtual bool IsArmInstructionSet() { return false; } public virtual void SetPlatformSocketOptions(Socket socket, bool willBeAsync, int keepAliveTimeMillis, int keepAliveIntervalMillis, int unacknowledgedDataTimeoutMillis) { socket.AssertArgumentNonNull("socket"); ulong num = (ulong)keepAliveTimeMillis; ulong num2 = (ulong)keepAliveIntervalMillis; byte[] array = new byte[12]; ulong[] array2 = new ulong[3]; if (num == 0L || num2 == 0L) { array2[0] = 0uL; } else { array2[0] = 1uL; } array2[1] = num; array2[2] = num2; for (int i = 0; i < array2.Length; i++) { array[i * 4 + 3] = (byte)((array2[i] >> 24) & 0xFF); array[i * 4 + 2] = (byte)((array2[i] >> 16) & 0xFF); array[i * 4 + 1] = (byte)((array2[i] >> 8) & 0xFF); array[i * 4] = (byte)(array2[i] & 0xFF); } byte[] bytes = BitConverter.GetBytes(0); socket.IOControl(IOControlCode.KeepAliveValues, array, bytes); } public virtual void LaunchUrl(string url) { throw new NotSupportedException(); } public virtual ArraySegment EncodeFileIconAsImage(string path, bool isDirectory) { throw new NotSupportedException(); } public virtual ArraySegment EncodeTheoreticalDirectoryIconAsImage() { throw new NotSupportedException(); } public virtual ArraySegment EncodeTheoreticalFileIconAsImage(string fileExtension) { throw new NotSupportedException(); } public virtual bool CanConfigureUnobtrusiveValidationMode() { return true; } public virtual bool ValidateSystemCredentials(NetworkCredential networkCredential) { return false; } public virtual INativeLibrary LoadNativeLibraryFromResourceStream(string resourceName, Stream resourceStream) { throw new NotSupportedException(); } public virtual INativeLibrary LoadNativeLibraryFromDisk(string libraryPath) { throw new NotSupportedException(); } public virtual string FindNativeLibrary(string libraryName) { return null; } public virtual bool SupportsReflectionEmit() { return false; } public virtual object BuildEmitCalliProxy(Type type, INativeLibrary nativeLibrary) { return ProxyBuilder.GetNativeLibraryProxy(type, nativeLibrary); } public virtual object BuildEmitPlatformInvokeProxy(Type type, string libraryNameOrPath) { return ProxyBuilder.GetPlatformInvokeProxy(type, libraryNameOrPath); } [return: NotNullIfNotNull("unprotectedBytes")] public virtual byte[]? ProtectBytes(byte[]? unprotectedBytes, Guid entropy = default(Guid)) { throw new NotSupportedException(); } public virtual byte[]? TryUnprotectBytes(byte[]? protectedBytes, Guid entropy = default(Guid)) { throw new NotSupportedException(); } public virtual bool WaitSafe(object syncObject, long waitMilliseconds) { return Monitor.Wait(syncObject, Extensions.ConvertWaitMilliseconds(waitMilliseconds), exitContext: false); } public virtual bool WaitOneSafe(WaitHandle waitHandle, long waitMilliseconds) { return waitHandle.WaitOne(Extensions.ConvertWaitMilliseconds(waitMilliseconds), exitContext: false); } public virtual IEnumerable GetPluginAssemblies() { return PluginAttribute.GetPluginAssemblies(); } public virtual string[] GetPrivateAssemblyFilePaths() { return Directory.GetFiles(Path.GetDirectoryName(GetType().Assembly.Location), "*.dll"); } public virtual string GetCurrentProcessMainModuleFileName() { using Process process = Process.GetCurrentProcess(); using ProcessModule processModule = process.MainModule; return processModule.FileName; } public virtual CultureInfo GetCultureInfo(string name) { return CultureInfo.GetCultureInfo(name); } public virtual ArraySegment GetAllBytes(MemoryStream memoryStream) { return new ArraySegment(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); } protected virtual ScreenCodecFactory GetScreenCodecFactory() { return new ScreenCodecFactory(); } public ScreenCodec CreateScreenDecoder(ScreenCodecID codecID) { return GetScreenCodecFactory().CreateDecoder(codecID); } public ScreenCodec CreateScreenEncoder(ScreenCodecID codecID) { return GetScreenCodecFactory().CreateEncoder(codecID); } public virtual byte[] GenerateEncryptionBytes(int count) { byte[] array = new byte[count]; new RNGCryptoServiceProvider().GetBytes(array); return array; } public virtual byte[] ComputeMD5Hash(byte[] bytes) { using MD5CryptoServiceProvider mD5CryptoServiceProvider = Extensions.CreateCryptographyAlgorithm(); return mD5CryptoServiceProvider.ComputeHash(bytes); } public virtual byte[] ComputeHMACSHA256Hash(byte[] key, byte[] bytes) { using HMACSHA256 hMACSHA = Extensions.CreateCryptographyAlgorithm(); hMACSHA.Key = key; return hMACSHA.ComputeHash(bytes); } }