private void Execute(ISender client, GetConnections message) { var table = GetTable(); var connections = new TcpConnection[table.Length]; for (int i = 0; i < table.Length; i++) { string processName; try { var p = System.Diagnostics.Process.GetProcessById((int)table[i].owningPid); processName = p.ProcessName; } catch { processName = $"PID: {table[i].owningPid}"; } connections[i] = new TcpConnection { ProcessName = processName, LocalAddress = table[i].LocalAddress.ToString(), LocalPort = table[i].LocalPort, RemoteAddress = table[i].RemoteAddress.ToString(), RemotePort = table[i].RemotePort, State = (ConnectionState)table[i].state }; } client.Send(new GetConnectionsResponse { Connections = connections }); }
public static void HandleGetConnections(Client client, GetConnections packet) { MIB_TCPROW_OWNER_PID[] table = GetTable(); string[] Processes = new string[table.Length]; string[] LocalAddresses = new string[table.Length]; string[] LocalPorts = new string[table.Length]; string[] RemoteAddresses = new string[table.Length]; string[] RemotePorts = new string[table.Length]; byte[] States = new byte[table.Length]; /*int i = 0; * foreach (string proc in Processes)*/ for (int i = 0; i < table.Length; i++) { LocalAddresses[i] = string.Format("{0}", table[i].LocalAddress); LocalPorts[i] = string.Format("{0}", table[i].LocalPort); RemoteAddresses[i] = string.Format("{0}", table[i].RemoteAddress); RemotePorts[i] = string.Format("{0}", table[i].RemotePort); States[i] = Convert.ToByte(table[i].state); try { Process p = Process.GetProcessById((int)table[i].owningPid); Processes[i] = p.ProcessName; } catch { Processes[i] = string.Format("PID: {0}", table[i].owningPid); } } new Packets.ClientPackets.GetConnectionsResponse(Processes, LocalAddresses, LocalPorts, RemoteAddresses, RemotePorts, States).Execute(client); }
public static void HandleGetConnections(Client client, GetConnections packet) { var table = GetTable(); var processes = new string[table.Length]; var localAddresses = new string[table.Length]; var localPorts = new string[table.Length]; var remoteAddresses = new string[table.Length]; var remotePorts = new string[table.Length]; var states = new byte[table.Length]; for (var i = 0; i < table.Length; i++) { localAddresses[i] = table[i].LocalAddress.ToString(); localPorts[i] = table[i].LocalPort.ToString(); remoteAddresses[i] = table[i].RemoteAddress.ToString(); remotePorts[i] = table[i].RemotePort.ToString(); states[i] = Convert.ToByte(table[i].state); try { var p = Process.GetProcessById((int)table[i].owningPid); processes[i] = p.ProcessName; } catch { processes[i] = string.Format("PID: {0}", table[i].owningPid); } } new GetConnectionsResponse(processes, localAddresses, localPorts, remoteAddresses, remotePorts, states) .Execute(client); }
public static void HandleGetConnections(Client client, GetConnections packet) { var table = GetTable(); var connections = new Models.TcpConnection[table.Length]; for (int i = 0; i < table.Length; i++) { string processName; try { var p = Process.GetProcessById((int)table[i].owningPid); processName = p.ProcessName; } catch { processName = $"PID: {table[i].owningPid}"; } connections[i] = new Models.TcpConnection { ProcessName = processName, LocalAddress = table[i].LocalAddress.ToString(), LocalPort = table[i].LocalPort, RemoteAddress = table[i].RemoteAddress.ToString(), RemotePort = table[i].RemotePort, State = (ConnectionState)table[i].state }; } client.Send(new GetConnectionsResponse { Connections = connections }); }
private void ReadJSON() { // gets the file path that can be used on release string realFilePath = Path.Combine(Application.streamingAssetsPath, path); using (StreamReader sr = new StreamReader(File.OpenRead(realFilePath))) { //string json_item = sr.ReadToEnd(); // reads entire JSON file into String (there's no way this will ever break...) string json_item = sr.ReadLine(); // read single line into string while (json_item != null && json_item != "") { Dictionary <string, string> fields = new Dictionary <string, string>(); // found https://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp // probably look into this more in depth JObject line = JsonConvert.DeserializeObject <JObject>(json_item); // https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm JProperty result = line.Property("result"); // get "result" property // https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JProperty.htm JToken actual = result.First; // returns first JToken in JProperty (there should only be one in this case) // https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm // now add each token in the JToken to Dictionary IEnumerable <JToken> childs = actual.Children(); foreach (JToken child in childs) { string[] split = child.ToString().Split(new char[] { ':' }, 2); split[0] = split[0].Trim().Trim('"'); // kill leading whitespace and " char split[1] = split[1].Trim().Trim('"'); fields.Add(split[0], split[1]); } // Add this result to list GetConnections.Add(fields); json_item = sr.ReadLine(); } } }