|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Windows; |
| 5 | +using System.Linq; |
| 6 | +using System.Security.Principal; |
| 7 | + |
| 8 | +namespace LogOut { |
| 9 | + /// <summary> |
| 10 | + /// Interaction logic for MainWindow.xaml |
| 11 | + /// </summary> |
| 12 | + public partial class MainWindow : Window { |
| 13 | + private EventHandler eventHandler; |
| 14 | + private IntPtr client_hWnd; |
| 15 | + private uint processId; |
| 16 | + private const string GAME_WINDOW_TITLE = "Path of Exile"; |
| 17 | + private const string APP_WINDOW_TITLE = "TCP Disconnect v0.1"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Utility methods for getting handles and stuffs |
| 21 | + /// </summary> |
| 22 | + sealed class Win128 { |
| 23 | + [DllImport("user32.dll", SetLastError = true)] |
| 24 | + static public extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); |
| 25 | + |
| 26 | + [DllImport("user32.dll")] |
| 27 | + private static extern IntPtr GetForegroundWindow(); |
| 28 | + |
| 29 | + [DllImport("user32.dll")] |
| 30 | + private static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count); |
| 31 | + |
| 32 | + public static string GetTopmostWindowTitle() { |
| 33 | + int nChars = 256; |
| 34 | + |
| 35 | + System.Text.StringBuilder Buff = new System.Text.StringBuilder(nChars); |
| 36 | + IntPtr handle = GetForegroundWindow(); |
| 37 | + |
| 38 | + if (GetWindowText(handle, Buff, nChars) > 0) |
| 39 | + return Buff.ToString(); |
| 40 | + else |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + public static bool checkElevation() { |
| 45 | + using (WindowsIdentity identity = WindowsIdentity.GetCurrent()) { |
| 46 | + WindowsPrincipal principal = new WindowsPrincipal(identity); |
| 47 | + return principal.IsInRole(WindowsBuiltInRole.Administrator); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Made by from /u/Umocrajen |
| 54 | + /// </summary> |
| 55 | + sealed class KillTCP { |
| 56 | + [DllImport("iphlpapi.dll", SetLastError = true)] |
| 57 | + private static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TcpTableClass tblClass, uint reserved = 0); |
| 58 | + |
| 59 | + [DllImport("iphlpapi.dll")] |
| 60 | + private static extern int SetTcpEntry(IntPtr pTcprow); |
| 61 | + |
| 62 | + [StructLayout(LayoutKind.Sequential)] |
| 63 | + public struct MibTcprowOwnerPid { |
| 64 | + public uint state; |
| 65 | + public uint localAddr; |
| 66 | + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] localPort; |
| 67 | + public uint remoteAddr; |
| 68 | + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] public byte[] remotePort; |
| 69 | + public uint owningPid; |
| 70 | + } |
| 71 | + |
| 72 | + [StructLayout(LayoutKind.Sequential)] |
| 73 | + public struct MibTcptableOwnerPid { |
| 74 | + public uint dwNumEntries; |
| 75 | + private readonly MibTcprowOwnerPid table; |
| 76 | + } |
| 77 | + |
| 78 | + private enum TcpTableClass { |
| 79 | + TcpTableBasicListener, |
| 80 | + TcpTableBasicConnections, |
| 81 | + TcpTableBasicAll, |
| 82 | + TcpTableOwnerPidListener, |
| 83 | + TcpTableOwnerPidConnections, |
| 84 | + TcpTableOwnerPidAll, |
| 85 | + TcpTableOwnerModuleListener, |
| 86 | + TcpTableOwnerModuleConnections, |
| 87 | + TcpTableOwnerModuleAll |
| 88 | + } |
| 89 | + |
| 90 | + public static void KillTCPConnectionForProcess(uint ProcessId) { |
| 91 | + MibTcprowOwnerPid[] table; |
| 92 | + var afInet = 2; |
| 93 | + var buffSize = 0; |
| 94 | + var ret = GetExtendedTcpTable(IntPtr.Zero, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); |
| 95 | + var buffTable = Marshal.AllocHGlobal(buffSize); |
| 96 | + |
| 97 | + try { |
| 98 | + uint statusCode = GetExtendedTcpTable(buffTable, ref buffSize, true, afInet, TcpTableClass.TcpTableOwnerPidAll); |
| 99 | + if (statusCode != 0) return; |
| 100 | + |
| 101 | + var tab = (MibTcptableOwnerPid) Marshal.PtrToStructure(buffTable, typeof(MibTcptableOwnerPid)); |
| 102 | + var rowPtr = (IntPtr)((long)buffTable + Marshal.SizeOf(tab.dwNumEntries)); |
| 103 | + table = new MibTcprowOwnerPid[tab.dwNumEntries]; |
| 104 | + |
| 105 | + for (var i = 0; i < tab.dwNumEntries; i++) { |
| 106 | + var tcpRow = (MibTcprowOwnerPid)Marshal.PtrToStructure(rowPtr, typeof(MibTcprowOwnerPid)); |
| 107 | + table[i] = tcpRow; |
| 108 | + rowPtr = (IntPtr)((long)rowPtr + Marshal.SizeOf(tcpRow)); |
| 109 | + } |
| 110 | + |
| 111 | + } finally { |
| 112 | + Marshal.FreeHGlobal(buffTable); |
| 113 | + } |
| 114 | + |
| 115 | + // Kill Path Connection |
| 116 | + var PathConnection = table.FirstOrDefault(t => t.owningPid == ProcessId); |
| 117 | + PathConnection.state = 12; |
| 118 | + var ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(PathConnection)); |
| 119 | + Marshal.StructureToPtr(PathConnection, ptr, false); |
| 120 | + SetTcpEntry(ptr); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Initialize elements |
| 126 | + /// </summary> |
| 127 | + public MainWindow() { |
| 128 | + InitializeComponent(); |
| 129 | + |
| 130 | + // Set window title |
| 131 | + Title = APP_WINDOW_TITLE; |
| 132 | + |
| 133 | + // Warn user on no admin rights |
| 134 | + if (!Win128.checkElevation()) Log("TCP disconnect will not function without elevated access", 1); |
| 135 | + |
| 136 | + // Print credentials |
| 137 | + Log(APP_WINDOW_TITLE + " by Siegrest", 0); |
| 138 | + |
| 139 | + // Hook |
| 140 | + eventHandler = new EventHandler(Event_keyboard); |
| 141 | + KeyboardHook.KeyBoardAction += eventHandler; |
| 142 | + KeyboardHook.Start(); |
| 143 | + |
| 144 | + // Run task to find application handle |
| 145 | + Log("Waiting for PoE process", 0); |
| 146 | + System.Threading.Tasks.Task.Run(() => FindGameTask()); |
| 147 | + } |
| 148 | + |
| 149 | + /// <summary> |
| 150 | + /// Get application's handler and PID |
| 151 | + /// </summary> |
| 152 | + private void FindGameTask() { |
| 153 | + // Run every 100ms and attempt to find game client |
| 154 | + while (true) { |
| 155 | + System.Threading.Thread.Sleep(1000); |
| 156 | + |
| 157 | + // Get process handler from name |
| 158 | + foreach (Process pList in Process.GetProcesses()) { |
| 159 | + if (pList.MainWindowTitle == GAME_WINDOW_TITLE) client_hWnd = pList.MainWindowHandle; |
| 160 | + } |
| 161 | + if (client_hWnd == null) continue; |
| 162 | + |
| 163 | + // Get window PID from handler |
| 164 | + Win128.GetWindowThreadProcessId(client_hWnd, out processId); |
| 165 | + if (processId <= 0) continue; |
| 166 | + |
| 167 | + break; |
| 168 | + } |
| 169 | + |
| 170 | + // Invoke dispatcher, allowing UI element updates |
| 171 | + Dispatcher.Invoke(new Action(() => { |
| 172 | + Button_SetKey.IsEnabled = true; |
| 173 | + CheckBox_Minimized.IsEnabled = true; |
| 174 | + Log("PoE process found", 0); |
| 175 | + })); |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary> |
| 179 | + /// Keyboard event handler. Fires when "registred hotkey" is pressed |
| 180 | + /// </summary> |
| 181 | + /// <param name="sender"></param> |
| 182 | + /// <param name="e"></param> |
| 183 | + private void Event_keyboard(object sender, EventArgs e) { |
| 184 | + if (KeyboardHook.flag_saveKey) { |
| 185 | + KeyboardHook.flag_saveKey = false; |
| 186 | + Button_SetKey.IsEnabled = true; |
| 187 | + Log("Assigned TCP disconnect to key: " + KeyboardHook.KEY, 0); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + // Don't send disconnect if game is minimized and checkbox is not ticked |
| 192 | + if (!(bool)CheckBox_Minimized.IsChecked) { |
| 193 | + if (Win128.GetTopmostWindowTitle() != GAME_WINDOW_TITLE) return; |
| 194 | + } |
| 195 | + |
| 196 | + // Send disconnect signal |
| 197 | + long time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; |
| 198 | + Log("Closing TCP connections...", 0); |
| 199 | + KillTCP.KillTCPConnectionForProcess(processId); |
| 200 | + time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - time; |
| 201 | + Log("Closed connections (took " + time + " ms)", 0); |
| 202 | + } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Event handler that closes hooks on program exit |
| 206 | + /// </summary> |
| 207 | + /// <param name="sender"></param> |
| 208 | + /// <param name="e"></param> |
| 209 | + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { |
| 210 | + KeyboardHook.KeyBoardAction -= eventHandler; |
| 211 | + KeyboardHook.Stop(); |
| 212 | + } |
| 213 | + |
| 214 | + /// <summary> |
| 215 | + /// Event handler that flips a flag |
| 216 | + /// </summary> |
| 217 | + /// <param name="sender"></param> |
| 218 | + /// <param name="e"></param> |
| 219 | + private void Button_SetKey_Click(object sender, RoutedEventArgs e) { |
| 220 | + Log("Press any key...", 0); |
| 221 | + Button_SetKey.IsEnabled = false; |
| 222 | + KeyboardHook.flag_saveKey = true; |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Timestamp and prefix local console messages |
| 227 | + /// </summary> |
| 228 | + /// <param name="str"></param> |
| 229 | + /// <param name="status"></param> |
| 230 | + private void Log(string str, int status) { |
| 231 | + string prefix; |
| 232 | + |
| 233 | + switch (status) { |
| 234 | + default: |
| 235 | + case 0: |
| 236 | + prefix = "[INFO] "; |
| 237 | + break; |
| 238 | + case 1: |
| 239 | + prefix = "[WARN] "; |
| 240 | + break; |
| 241 | + case 2: |
| 242 | + prefix = "[ERROR] "; |
| 243 | + break; |
| 244 | + case 3: |
| 245 | + prefix = "[CRITICAL] "; |
| 246 | + break; |
| 247 | + } |
| 248 | + |
| 249 | + string time = string.Format("{0:HH:mm:ss}", DateTime.Now); |
| 250 | + TextBox_Console.AppendText("[" + time + "]" + prefix + str + "\n"); |
| 251 | + TextBox_Console.ScrollToEnd(); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments