public static void FinaliseApplyPolicy(List <PortMappingPolicy> Add, List <PortMappingPolicy> Remove) { Add = DeDup(Add); Remove = DeDup(Remove); foreach (PortMappingPolicy rm in Remove) { bool DontRemove = false; //check if this policy is in "add" (to prevent connection breaking) foreach (PortMappingPolicy ad in Add) { if (rm.BindTo0000 == ad.BindTo0000 && rm.ClientPort == ad.ClientPort && rm.EditHOSTS == ad.EditHOSTS && rm.HOSTSEntry == ad.HOSTSEntry && rm.ID == ad.ID && rm.NoBindIfSDCServerIsDetected == ad.NoBindIfSDCServerIsDetected) { DontRemove = true; break; } } if (DontRemove == true) { continue; } PortMappings_ConnectionNode RMC = null; foreach (PortMappings_ConnectionNode C in Connections) { if (C.RunningPort == rm.ClientPort && C.PolicyID == rm.ID) { RMC = C; break; } } if (RMC != null) { Connections.Remove(RMC); RMC.StopConnection(); } } for (int i = 0; i < Add.Count; i++) { bool BadPort = false; PortMappingPolicy ad = Add[i]; for (int j = 0; j < Add.Count; j++) { if (i == j) { continue; } if (ad.ClientPort == Add[j].ClientPort) { FoxEventLog.WriteEventLog("Portmapping: Port " + ad.ClientPort + " conflicts with Policy ID " + ad.ID + " and " + Add[j].ID, EventLogEntryType.Error); BadPort = true; break; } } if (BadPort == true) { continue; } foreach (PortMappings_ConnectionNode cc in Connections) { if (ad.ID == cc.PolicyID) { if (cc.PolicyData.BindTo0000 == ad.BindTo0000 && cc.PolicyData.ClientPort == ad.ClientPort && cc.PolicyData.EditHOSTS == ad.EditHOSTS && cc.PolicyData.HOSTSEntry == ad.HOSTSEntry && cc.PolicyData.NoBindIfSDCServerIsDetected == ad.NoBindIfSDCServerIsDetected) { //no changes - continue BadPort = true; break; } else { //remove connection Connections.Remove(cc); cc.StopConnection(); break; } } } if (BadPort == true) { continue; } if (NetworkUtilities.PortAvailable(ad.ClientPort) == false) { FoxEventLog.WriteEventLog("Portmapping: Port " + ad.ClientPort + " from Policy ID " + ad.ID + " is unavailable.", EventLogEntryType.Error); continue; } if (ad.NoBindIfSDCServerIsDetected == true) { bool FoundSDCS = false; using (RegistryKey reg = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\FoxSDCS")) { if (reg != null) { FoundSDCS = true; } } if (FoundSDCS == true) { continue; } } if (ad.EditHOSTS == true) { if (string.IsNullOrWhiteSpace(ad.HOSTSEntry) == false) { foreach (string HOSTS in ad.HOSTSEntry.Split('|')) { if (string.IsNullOrWhiteSpace(HOSTS) == true) { continue; } HostsEdit.AppendIntoHOSTSFile(HOSTS.Trim(), "127.0.0.1"); } } } PortMappings_ConnectionNode C = new PortMappings_ConnectionNode(ad); C.StartConnection(); Connections.Add(C); } }
private void openRemoteDesktopConnectionToolStripMenuItem_Click(object sender, EventArgs e) { if (LstOnly == true) { return; } if (lstComputers.SelectedItems.Count == 0) { return; } foreach (ListViewItem l in lstComputers.SelectedItems) { ComputerData cd = (ComputerData)l.Tag; if (cd.Approved == true) { int Port = 9999; while (NetworkUtilities.PortAvailable(Port) == false) { Port++; } string Exec = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "FoxSDC_RemoteConnect.exe"); string SessionID = Program.net.CloneSession(); if (string.IsNullOrWhiteSpace(SessionID) == true) { MessageBox.Show(this, "Cannot get a new SessionID from the Server", Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } try { Process p = new Process(); p.StartInfo.FileName = Exec; p.StartInfo.Arguments = "-direct \"" + Program.net.ConnectedURL + "\" \"" + cd.MachineID + "\" \"" + SessionID + "\" " + Port.ToString() + " \"" + "127.0.0.1" + "\" " + "3389"; p.StartInfo.UseShellExecute = false; p.Start(); } catch (Exception ee) { MessageBox.Show(this, "Cannot start the process " + Exec + " - " + ee.Message, Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Debug.WriteLine(ee.ToString()); return; } try { string MSTSCSettings = Resources.MSTSC.Replace("{ADDR}", "127.0.0.1:" + Port.ToString()); string TempFile; TempFile = Environment.ExpandEnvironmentVariables("%TEMP%"); if (TempFile.EndsWith("\\") == false) { TempFile += "\\"; } TempFile += "FoxSDC-MSTSC-" + Guid.NewGuid().ToString() + ".rdp"; File.WriteAllText(TempFile, MSTSCSettings, Encoding.ASCII); Process p = new Process(); p.StartInfo.FileName = Environment.ExpandEnvironmentVariables("%systemroot%\\system32\\mstsc.exe"); p.StartInfo.Arguments = TempFile; p.StartInfo.UseShellExecute = false; p.Start(); } catch (Exception ee) { MessageBox.Show(this, "Cannot start the process " + Exec + " - " + ee.Message, Program.Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); Debug.WriteLine(ee.ToString()); return; } } } }