public void Connect(IPEndPoint address, string name, Guid id) { if (IsStateConnected()) { throw new InvalidOperationException($"Cannot connect when state is {State}"); } cancelToken = new CancellationTokenSource(); cName = name; disconnecting = false; cId = id; tcpSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); tcpSocket.NoDelay = true; serverAddress = address; ISLogger.Write($"ServerSocket->Connecting to {address.ToString()}"); SetState(ServerSocketState.AttemptingConnection); conId++; tcpSocket.BeginConnect(address, ConnectCallback, conId); }
public void DeleteToken(Guid token) { if (currentAccessTokens.ContainsKey(token)) { currentAccessTokens.TryGetValue(token, out AccessToken access); if (access == null) { ISLogger.Write("FileAccessController: Could not delete access token: Access token was null"); return; } access.CloseAllStreams(); } else { ISLogger.Write("FileAccessController: Could not delete access token: Key {0} not found", token); } }
public static void WriteConfig(string key, string value) { try { var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (conf.AppSettings.Settings[key] == null) { conf.AppSettings.Settings.Add(key, value); } else { conf.AppSettings.Settings[key].Value = value; } conf.Save(); ISLogger.Write($"ConfigManager->{key} = {value}"); ConfigurationManager.RefreshSection(conf.AppSettings.SectionInformation.Name); }catch (Exception ex) { ISLogger.Write($"ConfigManager->Failed to write to config: {ex.Message}"); } }
private void WndThread(LLHookCallback mouseCallback, LLHookCallback kbCallback, bool monitorDesktops, bool monitorClipboard) { procThreadId = GetCurrentThreadId(); WindowHandle = CreateMessageOnlyWindow(); windowCreatedEvent.Set(); if (mouseCallback != null) { MouseProcID = SetMouseHook(mouseCallback); } if (kbCallback != null) { KeyboardProcID = SetKeyboardHook(kbCallback); } if (monitorDesktops) { StartMonitoringDesktopSwitches(); } if (monitorClipboard) { StartMonitoringClipboard(); } if (WindowHandle == IntPtr.Zero) { ISLogger.Write($"Failed to create window: Win32 error {Marshal.GetLastWin32Error().ToString("X")}"); return; } while (!exitThread) { if (GetMessage(out MSG message, WindowHandle, 0, 0) != 0) { DispatchMessage(ref message); }
private static void WriteRecursive(ClipboardVirtualFileData.DirectoryAttributes folder, string rootDir) { foreach (var sub in folder.SubFolders) { try { ISLogger.Write("Reading folder " + sub.RelativePath); Directory.CreateDirectory(Path.Combine(rootDir, sub.RelativePath)); foreach (var file in sub.Files) { File.Create(Path.Combine(rootDir, file.RelativePath)).Dispose(); ISLogger.Write("Reading file " + file.RelativePath); } }catch (Exception ex) { ISLogger.Write("Error writing folder " + sub.RelativePath + ": " + ex.Message); } finally { WriteRecursive(sub, rootDir); } } }
public static DESKTOPOBJECT GetThreadDesktop() { IntPtr hDesk = GetThreadDesktop(GetCurrentThreadId()); GetUserObjectInformationA(hDesk, 2, null, 0, out int neededLen); IntPtr lpName = Marshal.AllocHGlobal((neededLen / 2) + 1); byte[] data = new byte[neededLen]; if (!GetUserObjectInformationA(hDesk, 2, data, neededLen, out _)) { ISLogger.Write("Failed to get current desktop name"); return(new DESKTOPOBJECT()); } string dName = Encoding.ASCII.GetString(data); Marshal.FreeHGlobal(lpName); byte[] uio = new byte[1]; GetUserObjectInformationA(hDesk, 6, uio, 1, out _); bool UIO_IO = uio[0] != 0; return(new DESKTOPOBJECT(dName, UIO_IO)); }
private void SocketReceiveThreadLoop() { try { ISLogger.Write("Socket receive thread started"); byte[] header = new byte[4]; while (!cancelToken.IsCancellationRequested) { int hRem = 4; int hPos = 0; do { int hIn = tcpSocket.Receive(header, hPos, hRem, 0); //make sure we read all 4 bytes of header hPos += hIn; hRem -= hIn; } while (hRem > 0); int pSize = BitConverter.ToInt32(header, 0); if (pSize > Settings.ClientMaxPacketSize) { OnConnectionError(new Exception("Connection error: Server sent invalid packet size of " + pSize), ServerSocketState.ConnectionError); return; } int dRem = pSize; int bPos = 4; do { int bIn = tcpSocket.Receive(socketBuffer, bPos, dRem, 0); bPos += bIn; dRem = pSize - bPos + 4; } while (dRem > 0); MessageType cmd = (MessageType)socketBuffer[4]; switch (cmd) { case MessageType.Input: { InputMessage msg = InputMessage.FromBytes(socketBuffer); InputReceived?.Invoke(this, msg.Input); break; } case MessageType.ServerOK: ISLogger.Write("Server sent OK"); SetState(ServerSocketState.Connected); MessageReceived?.Invoke(this, cmd); break; case MessageType.SetClipboardText: ProcessCbCopy(ClipboardSetTextMessage.FromBytes(socketBuffer)); break; case MessageType.FileTransferPart: FileTransferPartMessage fileMsg = FileTransferPartMessage.FromBytes(ref socketBuffer); ReadFilePart(fileMsg); break; default: MessageReceived?.Invoke(this, cmd); break; } } }catch (ObjectDisposedException) { }catch (Exception ex) { if (!disconnecting) { disconnecting = true; } else { return; } if (cancelToken.IsCancellationRequested) { return; } if (!ex.Message.Contains("WSACancelBlockingCall")) { //ISLogger.Write("Serversocket error: " + ex.Message); } OnConnectionError(ex, ServerSocketState.ConnectionError); } }
private void SocketReceiveThreadLoop() { try { ISLogger.Write("Socket receive thread started"); byte[] header = new byte[4]; while (!cancelToken.IsCancellationRequested) { tcpSocket.Receive(header, 4, 0); int pSize = BitConverter.ToInt32(header, 0); int dRem = pSize; int bPos = 4; do { int bIn = tcpSocket.Receive(socketBuffer, bPos, dRem, 0); bPos += bIn; dRem = pSize - bPos + 4; } while (dRem > 0); MessageType cmd = (MessageType)socketBuffer[4]; switch (cmd) { case MessageType.Input: { InputMessage msg = InputMessage.FromBytes(socketBuffer); InputReceived?.Invoke(this, msg.Input); break; } case MessageType.ServerOK: ISLogger.Write("Server sent OK"); SetState(ServerSocketState.Connected); MessageReceived?.Invoke(this, cmd); break; case MessageType.SetClipboardText: ProcessCbCopy(ClipboardSetTextMessage.FromBytes(socketBuffer)); break; default: MessageReceived?.Invoke(this, cmd); break; } } }catch (ObjectDisposedException) { }catch (Exception ex) { if (!disconnecting) { disconnecting = true; } else { return; } if (cancelToken.IsCancellationRequested) { return; } if (!ex.Message.Contains("WSACancelBlockingCall")) { ISLogger.Write("Serversocket error: " + ex.Message); } OnConnectionError(ex, ServerSocketState.ConnectionError); } }
private void Token_TokenClosed(object sender, Guid e) { ISLogger.Write("FileAccessController: Token {0} closed", e); currentAccessTokens.Remove(e); }