/// <summary> /// modify all server data by group name and old server name /// </summary> /// <param name="groupName">group name</param> /// <param name="oldServerName">old server name</param> /// <param name="serverElement">new server data</param> public void modifyServer(string groupName, string oldServerName, ServerElement serverElement) { XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Settings.Default.cfgpath); XmlNode xmlGroup = xmldoc.SelectSingleNode("//*[@GroupName='" + groupName + "']"); if (xmlGroup != null) { foreach (XmlElement xmlElement in xmlGroup) { switch (xmlElement.Name) { case "Server": string foundedServerName = xmlElement.GetAttribute("Name"); if (!foundedServerName.Equals(oldServerName)) { continue; } xmlElement.Attributes["Name"].Value = serverElement.Name; bool existHost = false; bool existPort = false; bool existUsername = false; bool existPassword = false; bool existChecks = false; foreach (XmlElement subElements in xmlElement.ChildNodes) { switch (subElements.Name) { case "Host": subElements.InnerText = CryptHelper.Encrypt(serverElement.Host); existHost = true; break; case "Port": subElements.InnerText = CryptHelper.Encrypt(serverElement.Port); existPort = true; break; case "Username": subElements.InnerText = CryptHelper.Encrypt(serverElement.Username); existUsername = true; break; case "Password": subElements.InnerText = CryptHelper.Encrypt(serverElement.Password); existPassword = true; break; case "Type": subElements.InnerText = CryptHelper.Encrypt(((int)serverElement.Type).ToString()); break; case "Checks": subElements.InnerText = serverElement.AutoChecks.ToString(); existChecks = true; break; } } if (!existHost) { XmlElement newElement = xmldoc.CreateElement("Host"); newElement.InnerText = CryptHelper.Encrypt(serverElement.Host); xmlElement.AppendChild(newElement); } if (!existPort) { XmlElement newElement = xmldoc.CreateElement("Port"); newElement.InnerText = CryptHelper.Encrypt(serverElement.Port); xmlElement.AppendChild(newElement); } if (!existUsername) { XmlElement newElement = xmldoc.CreateElement("Username"); newElement.InnerText = CryptHelper.Encrypt(serverElement.Username); xmlElement.AppendChild(newElement); } if (!existPassword) { XmlElement newElement = xmldoc.CreateElement("Password"); newElement.InnerText = CryptHelper.Encrypt(serverElement.Password); xmlElement.AppendChild(newElement); } if (!existChecks) { XmlElement newElement = xmldoc.CreateElement("Checks"); newElement.InnerText = serverElement.AutoChecks.ToString(); xmlElement.AppendChild(newElement); } break; } try { xmldoc.Save(Settings.Default.cfgpath); } catch (UnauthorizedAccessException) { OtherHelper.Error( "Could not write to configuration file :'(\rModifications will not be saved\rPlease check your user permissions."); } } } }
/// <summary> /// Get server data by group and server name /// </summary> /// <param name="groupName">group name</param> /// <param name="serverName">server name</param> /// <returns></returns> public static ServerElement getServerByName(string groupName, string serverName) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); XmlNode xmlGroup = xmlDocument.SelectSingleNode("//*[@GroupName='" + groupName + "']"); if (xmlGroup != null) { foreach (XmlElement xmlElement in xmlGroup) { switch (xmlElement.Name) { case "Server": string foundedServerName = xmlElement.GetAttribute("Name"); if (!foundedServerName.Equals(serverName)) { continue; } string serverHost = ""; string serverPort = ""; string serverUsername = ""; string serverPassword = ""; string serverType = ""; bool serverChecks = false; Guid id = Guid.Empty; foreach (XmlElement serverElements in xmlElement.ChildNodes) { switch (serverElements.Name) { case "Host": serverHost = CryptHelper.Decrypt(serverElements.InnerText); break; case "Port": serverPort = CryptHelper.Decrypt(serverElements.InnerText); break; case "Username": serverUsername = CryptHelper.Decrypt(serverElements.InnerText); break; case "Password": serverPassword = CryptHelper.Decrypt(serverElements.InnerText); break; case "Type": serverType = CryptHelper.Decrypt(serverElements.InnerText); break; case "Checks": serverChecks = Boolean.Parse(serverElements.InnerText); break; case "Id": Guid.TryParse(serverElements.InnerText, out id); break; } } ServerElement currentServer = new ServerElement(foundedServerName, serverHost, serverPort, serverUsername, serverPassword, serverType, serverChecks); currentServer.Id = id; return(currentServer); } } } //TODO: Fix return null return(null); }
/// <summary> /// Add new server to group in xml config /// </summary> /// <param name="groupName">group name</param> /// <param name="serverName">server name</param> /// <param name="serverHost">server host</param> /// <param name="serverPort">server port</param> /// <param name="serverUsername">server username</param> /// <param name="serverPassword">server password</param> /// <param name="serverType">server type</param> public void addServer(string groupName, string serverName, string serverHost, string serverPort, string serverUsername, string serverPassword, string serverType, bool autoChecks) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); Console.WriteLine(groupName); XmlNode xmlGroup = xmlDocument.SelectSingleNode("//*[@GroupName='" + groupName + "']"); XmlElement newServer = xmlDocument.CreateElement("Server"); XmlAttribute name = xmlDocument.CreateAttribute("Name"); name.Value = serverName; newServer.SetAttributeNode(name); if (serverHost != "") { XmlElement host = xmlDocument.CreateElement("Host"); host.InnerText = CryptHelper.Encrypt(serverHost); newServer.AppendChild(host); } if (serverPort != "") { XmlElement host = xmlDocument.CreateElement("Port"); host.InnerText = CryptHelper.Encrypt(serverPort); newServer.AppendChild(host); } if (serverUsername != "") { XmlElement host = xmlDocument.CreateElement("Username"); host.InnerText = CryptHelper.Encrypt(serverUsername); newServer.AppendChild(host); } if (serverPassword != "") { XmlElement host = xmlDocument.CreateElement("Password"); host.InnerText = CryptHelper.Encrypt(serverPassword); newServer.AppendChild(host); } if (serverType != "") { XmlElement host = xmlDocument.CreateElement("Type"); host.InnerText = CryptHelper.Encrypt(serverType); newServer.AppendChild(host); } XmlElement checks = xmlDocument.CreateElement("Checks"); checks.InnerText = autoChecks.ToString(); newServer.AppendChild(checks); XmlElement id = xmlDocument.CreateElement("Id"); id.InnerText = Guid.NewGuid().ToString(); newServer.AppendChild(id); xmlGroup?.AppendChild(newServer); try { xmlDocument.Save(Settings.Default.cfgpath); } catch (UnauthorizedAccessException) { OtherHelper.Error("Could not write to configuration file :'(\rModifications will not be saved\rPlease check your user permissions."); } }
/// <summary> /// get all data from xml config /// </summary> /// <returns>arraylist with groupelement</returns> public ArrayList getAllData() { if (!File.Exists(Settings.Default.cfgpath)) { return(new ArrayList()); } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); ArrayList groups = new ArrayList(); XmlNodeList groupNodes = xmlDocument.SelectNodes("//*[@GroupName]"); if (groupNodes != null) { if (groupNodes.Count > 0) { foreach (XmlElement groupNode in groupNodes) { string groupName = groupNode.GetAttribute("GroupName"); string groupDefaulHostname = ""; string groupDefaultPort = ""; string groupDefaultUsername = ""; string groupDefaultPassword = ""; ArrayList servers = new ArrayList(); foreach (XmlElement childNode in groupNode.ChildNodes) { switch (childNode.Name) { case "DefaultHost": groupDefaulHostname = CryptHelper.Decrypt(childNode.InnerText); break; case "DefaultPort": groupDefaultPort = CryptHelper.Decrypt(childNode.InnerText); break; case "DefaultUsername": groupDefaultUsername = CryptHelper.Decrypt(childNode.InnerText); break; case "DefaultPassword": groupDefaultPassword = CryptHelper.Decrypt(childNode.InnerText); break; case "Server": string serverName = childNode.GetAttribute("Name").Trim(); string serverHost = ""; string serverPort = ""; string serverUsername = ""; string serverPassword = ""; string serverType = ""; bool serverChecks = false; Guid id = Guid.Empty; foreach (XmlElement serverElement in childNode.ChildNodes) { switch (serverElement.Name) { case "Host": serverHost = CryptHelper.Decrypt(serverElement.InnerText); break; case "Port": serverPort = CryptHelper.Decrypt(serverElement.InnerText); break; case "Username": serverUsername = CryptHelper.Decrypt(serverElement.InnerText); break; case "Password": serverPassword = CryptHelper.Decrypt(serverElement.InnerText); break; case "Type": serverType = CryptHelper.Decrypt(serverElement.InnerText); break; case "Checks": serverChecks = Boolean.Parse(serverElement.InnerText); break; case "Id": Guid.TryParse(serverElement.InnerText, out id); break; } } var server = new ServerElement(serverName, serverHost, serverPort, serverUsername, serverPassword, serverType, serverChecks); server.Id = id; servers.Add(server); break; } } groups.Add(new GroupElement(groupName, groupDefaulHostname, groupDefaultPort, groupDefaultUsername, groupDefaultPassword, servers)); } } else { return(new ArrayList()); } } return(groups); }
/// <summary> /// Method for launch user VNC client like a TinyVNC, UltraVNC or ReadlVNC /// </summary> /// <param name="serverElement">Current selected server from Tree View</param> private static void LaunchVnc(ServerElement serverElement) { string[] vncExtractPath = ExtractFilePath(Settings.Default.vncpath); string vncPath = Environment.ExpandEnvironmentVariables(vncExtractPath[0]); string vncArgs = vncExtractPath[1]; if (File.Exists(vncPath)) { string host = serverElement.Host; string port = serverElement.Port != "" ? serverElement.Port : "5900"; _vncOutPath = ""; if (Settings.Default.vncfilespath != "" && OtherHelper.ReplaceA(ps, pr, Settings.Default.vncfilespath) != "\\") { _vncOutPath = OtherHelper.ReplaceA(ps, pr, Settings.Default.vncfilespath + "\\"); if (!Directory.Exists(_vncOutPath)) { Directory.CreateDirectory(_vncOutPath); } } TextWriter vncFile = new StreamWriter(_vncOutPath + OtherHelper.ReplaceU(f, serverElement.Name) + ".vnc"); vncFile.WriteLine("[Connection]"); vncFile.WriteLine(host != "" ? "host=" + host : ""); vncFile.WriteLine(port != "" ? "port=" + port : ""); vncFile.WriteLine(serverElement.Username != "" ? "username="******""); vncFile.WriteLine(serverElement.Password != "" ? "password="******""); vncFile.WriteLine("[Options]"); vncFile.WriteLine(Settings.Default.vncfullscreen ? "fullscreen=1" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "viewonly=1" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "sendptrevents=0" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "sendkeyevents=0" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "sendcuttext=0" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "acceptcuttext=0" : ""); vncFile.WriteLine(Settings.Default.vncviewonly ? "sharefiles=0" : ""); vncFile.WriteLine(serverElement.Password != "" && serverElement.Password.Length > 8 ? "protocol3.3=1" : ""); // f****n vnc 4.0 auth vncFile.Close(); Process myProc = new Process { StartInfo = { FileName = Settings.Default.vncpath, Arguments = "-config \"" + _vncOutPath + OtherHelper.ReplaceU(f, serverElement.Name) + ".vnc\"" + (vncArgs != "" ? " " + vncArgs : "") } }; myProc.Start(); } else { if (MessageBox.Show(Resources.connectionHelper_LaunchVnc_M1 + vncPath + Resources.connectionHelper_LaunchVnc_M2, Resources.connectionHelper_LaunchVnc_Error, MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) { formMain.optionsForm.bVNCPath_Click(); } } }
/// <summary> /// Method for launch default RDP client (mstcs.exe) /// </summary> /// <param name="serverElement">Server data for launching</param> private static void LaunchRdp(ServerElement serverElement, XmlHelper xmlHelper) { string[] rdpExtractFilePath = ExtractFilePath(Settings.Default.rdpath); string rdpPath = Environment.ExpandEnvironmentVariables(rdpExtractFilePath[0]); string rdpLaunchArgs = rdpExtractFilePath[1]; if (File.Exists(rdpPath)) { var size = xmlHelper.configGet(serverElement.Id + "_rdsize"); if (string.IsNullOrEmpty(size)) { size = Settings.Default.rdsize; } string[] sizes = size.Split('x'); _rdpOutPath = ""; if (Settings.Default.rdfilespath != "" && OtherHelper.ReplaceA(ps, pr, Settings.Default.rdfilespath) != "\\") { _rdpOutPath = OtherHelper.ReplaceA(ps, pr, Settings.Default.rdfilespath + "\\"); //TODO: add try for exception if (!Directory.Exists(_rdpOutPath)) { Directory.CreateDirectory(_rdpOutPath); } } var rddrives = Settings.Default.rddrives; bool.TryParse(xmlHelper.configGet(serverElement.Id + "_rddrives"), out rddrives); var rdadmin = Settings.Default.rdadmin; bool.TryParse(xmlHelper.configGet(serverElement.Id + "_rdadmin"), out rdadmin); var rdspan = Settings.Default.rdspan; bool.TryParse(xmlHelper.configGet(serverElement.Id + "_rdspan"), out rdspan); TextWriter rdpFileWriter = new StreamWriter(path: _rdpOutPath + OtherHelper.ReplaceU(f, serverElement.Name) + ".rdp"); //TODO: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated rdpFileWriter.WriteLine(size == "Full screen" ? "screen mode id:i:2" : "screen mode id:i:1"); rdpFileWriter.WriteLine(sizes.Length == 2 ? "desktopwidth:i:" + sizes[0] : ""); rdpFileWriter.WriteLine(sizes.Length == 2 ? "desktopheight:i:" + sizes[1] : ""); rdpFileWriter.WriteLine(serverElement.HostWithPort != "" ? "full address:s:" + serverElement.HostWithPort : ""); rdpFileWriter.WriteLine(serverElement.Username != "" ? "username:s:" + serverElement.Username : ""); rdpFileWriter.WriteLine(serverElement.Username != "" && serverElement.Password != "" ? "password 51:b:" + CryptHelper.encryptpw(serverElement.Password) : ""); rdpFileWriter.WriteLine(rddrives ? "redirectdrives:i:1" : ""); rdpFileWriter.WriteLine(rdadmin ? "administrative session:i:1" : ""); rdpFileWriter.WriteLine(rdspan ? "use multimon:i:1" : ""); rdpFileWriter.Close(); Process myProc = new Process { StartInfo = { FileName = rdpPath, Arguments = "\"" + _rdpOutPath + OtherHelper.ReplaceU(f,serverElement.Name) + ".rdp\"" + (rdpLaunchArgs != null ? " " + rdpLaunchArgs : ""), } }; myProc.Start(); } else { if (MessageBox.Show(Resources.connectionHelper_LaunchVnc_M1 + rdpPath + Resources.connectionHelper_LaunchVnc_M2, Resources.connectionHelper_LaunchVnc_Error, MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) { formMain.optionsForm.bRDPath_Click(); } } }