|
| 1 | +using Flow.Launcher.Infrastructure; |
| 2 | +using Flow.Launcher.Infrastructure.Logger; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using System.Management; |
| 8 | + |
| 9 | +namespace Flow.Launcher.Plugin.ProcessKiller |
| 10 | +{ |
| 11 | + internal class PortDetail |
| 12 | + { |
| 13 | + public int Port { get; set; } |
| 14 | + public int ProcessID { get; set; } |
| 15 | + public string ProcessName { get; set; } |
| 16 | + |
| 17 | + public Process Process { get; set; } |
| 18 | + public string Path { get; set; } |
| 19 | + public override string ToString() |
| 20 | + { |
| 21 | + return string.Format(@" Process Name: {0} ,Process ID: {1} , |
| 22 | + Port: {2} ,\nPath : {3}", ProcessName, |
| 23 | + ProcessID, Port, Path); |
| 24 | + } |
| 25 | + |
| 26 | + } |
| 27 | + /// <summary> |
| 28 | + /// Usage: |
| 29 | + /// int port = 8081 |
| 30 | + /// TcpHelperUtil tcpHelper = new TcpHelperUtil(); |
| 31 | + /// var details = tcpHelper.GetPortDetails(port); |
| 32 | + /// if (details.Item1) |
| 33 | + /// { |
| 34 | + /// Console.WriteLine("Port {0} in Use",port); |
| 35 | + /// Console.WriteLine(details.Item2.ToString()); |
| 36 | + /// }else |
| 37 | + /// { |
| 38 | + /// Console.WriteLine("Port {0} is free ",port); |
| 39 | + /// } |
| 40 | + /// |
| 41 | + /// </summary> |
| 42 | + internal class PortHelper |
| 43 | + { |
| 44 | + private const short MINIMUM_TOKEN_IN_A_LINE = 5; |
| 45 | + private const string COMMAND_EXE = "cmd"; |
| 46 | + |
| 47 | + public PortHelper() |
| 48 | + { |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public static Tuple<bool, PortDetail> GetPortDetails(int port) |
| 53 | + { |
| 54 | + PortDetail PortDetail = new PortDetail(); |
| 55 | + Tuple<bool, PortDetail> result = Tuple.Create(false, PortDetail); |
| 56 | + |
| 57 | + // execute netstat command for the given port |
| 58 | + string commandArgument = string.Format("/c netstat -an -o -p tcp|findstr \":{0}.*LISTENING\"", port); |
| 59 | + |
| 60 | + string commandOut = ExecuteCommandAndCaptureOutput(COMMAND_EXE, commandArgument); |
| 61 | + if (string.IsNullOrEmpty(commandOut)) |
| 62 | + { |
| 63 | + // port is not in use |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + var stringTokens = commandOut.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries); |
| 68 | + if (stringTokens.Length < MINIMUM_TOKEN_IN_A_LINE) |
| 69 | + { |
| 70 | + return result; |
| 71 | + } |
| 72 | + |
| 73 | + // split host:port |
| 74 | + var hostPortTokens = stringTokens[1].Split(new char[] { ':' }); |
| 75 | + if (hostPortTokens.Length < 2) |
| 76 | + { |
| 77 | + return result; |
| 78 | + } |
| 79 | + |
| 80 | + int portFromHostPortToken = 0; |
| 81 | + if (!int.TryParse(hostPortTokens[1], out portFromHostPortToken)) |
| 82 | + { |
| 83 | + return result; |
| 84 | + } |
| 85 | + if (portFromHostPortToken != port) |
| 86 | + { |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + PortDetail.Port = port; |
| 91 | + PortDetail.ProcessID = int.Parse(stringTokens[4].Trim()); |
| 92 | + Tuple<string, string> processNameAndPath = null; |
| 93 | + try |
| 94 | + { |
| 95 | + processNameAndPath = GetProcessNameAndCommandLineArgs(PortDetail.ProcessID); |
| 96 | + PortDetail.ProcessName = processNameAndPath.Item1; |
| 97 | + PortDetail.Path = processNameAndPath.Item2; |
| 98 | + PortDetail.Process = Process.GetProcessById(PortDetail.ProcessID); |
| 99 | + result = Tuple.Create(true, PortDetail); |
| 100 | + } |
| 101 | + catch (Exception exp) |
| 102 | + { |
| 103 | + Console.WriteLine(exp.ToString()); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + return result; |
| 108 | + |
| 109 | + } |
| 110 | + /// <summary> |
| 111 | + /// Using WMI API to get process name and path instead of |
| 112 | + /// Process.GetProcessById, because if calling process ins |
| 113 | + /// 32 bit and given process id is 64 bit, caller will not be able to |
| 114 | + /// get the process name |
| 115 | + /// </summary> |
| 116 | + /// <param name="processID"></param> |
| 117 | + /// <returns></returns> |
| 118 | + private static Tuple<string, string> GetProcessNameAndCommandLineArgs(int processID) |
| 119 | + { |
| 120 | + Tuple<string, string> result = Tuple.Create(string.Empty, string.Empty); |
| 121 | + string query = string.Format("Select Name,ExecutablePath from Win32_Process WHERE ProcessId='{0}'", processID); |
| 122 | + try |
| 123 | + { |
| 124 | + ObjectQuery wql = new ObjectQuery(query); |
| 125 | + ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql); |
| 126 | + ManagementObjectCollection results = searcher.Get(); |
| 127 | + |
| 128 | + // interested in first result. |
| 129 | + foreach (ManagementObject item in results) |
| 130 | + { |
| 131 | + result = Tuple.Create<string, string>(Convert.ToString(item["Name"]), |
| 132 | + Convert.ToString(item["ExecutablePath"])); |
| 133 | + break; |
| 134 | + |
| 135 | + } |
| 136 | + } |
| 137 | + catch (Exception) |
| 138 | + { |
| 139 | + |
| 140 | + throw; |
| 141 | + } |
| 142 | + |
| 143 | + return result; |
| 144 | + |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Execute the given command and captures the output |
| 149 | + /// </summary> |
| 150 | + /// <param name="commandName"></param> |
| 151 | + /// <param name="arguments"></param> |
| 152 | + /// <returns></returns> |
| 153 | + private static string ExecuteCommandAndCaptureOutput(string commandName, string arguments) |
| 154 | + { |
| 155 | + string commandOut = string.Empty; |
| 156 | + Process process = new Process(); |
| 157 | + process.StartInfo.FileName = commandName; |
| 158 | + process.StartInfo.Arguments = arguments; |
| 159 | + process.StartInfo.UseShellExecute = false; |
| 160 | + process.StartInfo.CreateNoWindow = true; |
| 161 | + process.StartInfo.RedirectStandardOutput = true; |
| 162 | + process.StartInfo.RedirectStandardError = true; |
| 163 | + process.Start(); |
| 164 | + |
| 165 | + commandOut = process.StandardOutput.ReadToEnd(); |
| 166 | + string errors = process.StandardError.ReadToEnd(); |
| 167 | + try |
| 168 | + { |
| 169 | + process.WaitForExit(TimeSpan.FromSeconds(2).Milliseconds); |
| 170 | + } |
| 171 | + catch (Exception exp) |
| 172 | + { |
| 173 | + Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to ExecuteCommandAndCaptureOutput {commandName + arguments}", exp); |
| 174 | + Console.WriteLine(exp.ToString()); |
| 175 | + } |
| 176 | + return commandOut; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments