/// <summary> /// ??? /// </summary> /// <param name="node"></param> public void dropNode(string node) { string file = Settings.Default.cfgpath; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(file); XmlNodeList xmlNodeList = xmlDocument.SelectNodes("//*[@" + node + "]"); if (xmlDocument.DocumentElement != null) { if (xmlNodeList != null) { xmlDocument.DocumentElement.RemoveChild(xmlNodeList[0]); } } try { xmlDocument.Save(file); } catch (UnauthorizedAccessException) { OtherHelper.Error("Could not write to configuration file :'(\rModifications will not be saved\rPlease check your user permissions."); } }
/// <summary> /// ??? /// </summary> /// <param name="id"></param> /// <returns></returns> public string configGet(string id) { string file = Settings.Default.cfgpath; XmlDocument xmldoc = new XmlDocument(); try { xmldoc.Load(file); } catch { OtherHelper.Error("\"" + Settings.Default.cfgpath + "\" file is corrupt, delete it and try again."); Environment.Exit(-1); } XmlNodeList xmlnode = xmldoc.SelectNodes("//*[@ID=" + parseXpathString(id) + "]"); if (xmlnode != null) { if (xmlnode.Count > 0) { return(xmlnode[0].InnerText); } } return(""); }
public static void setServerParamByServerId(string _groupName, string _serverName, string _paramName, string _newData) { 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 currentData = ""; bool isCurrentExist = false; foreach (XmlElement serverElements in xmlElement.ChildNodes) { if (serverElements.Name == _paramName) { serverElements.InnerText = _newData; isCurrentExist = true; } } if (!isCurrentExist) { XmlElement newElement = xmlDocument.CreateElement(_paramName); newElement.InnerText = _newData; xmlElement.AppendChild(newElement); } break; } } } 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> /// Modify group information /// </summary> /// <param name="groupName">old group name for search</param> /// <param name="newGroupName">new group name</param> /// <param name="defaultHost">new default host</param> /// <param name="defaultPort">new default port</param> /// <param name="defaultUsername">new default username</param> /// <param name="defaultPassword">new default password</param> public void modifyGroup(string groupName, string newGroupName, string defaultHost, string defaultPort, string defaultUsername, string defaultPassword) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); XmlNodeList groupNodes = xmlDocument.SelectNodes("//*[@GroupName='" + groupName + "']"); if (groupNodes != null) { if (groupNodes.Count > 0) { XmlNode currentGroup = groupNodes[0]; if (currentGroup.Attributes != null) { currentGroup.Attributes["GroupName"].Value = newGroupName; } foreach (XmlElement groupNode in currentGroup.ChildNodes) { switch (groupNode.Name) { case "DefaultHost": groupNode.InnerText = CryptHelper.Encrypt(defaultHost); break; case "DefaultPort": groupNode.InnerText = CryptHelper.Encrypt(defaultPort); break; case "DefaultUsername": groupNode.InnerText = CryptHelper.Encrypt(defaultUsername); break; case "DefaultPassword": groupNode.InnerText = CryptHelper.Encrypt(defaultPassword); break; } } } } 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> /// Create new group in xml config /// </summary> /// <param name="groupName">new group name</param> /// <param name="defaultHost">default param</param> /// <param name="defaultPort">default param</param> /// <param name="defaultUsername">default param</param> /// <param name="defaultPassword">default param</param> public void createGroup(string groupName, string defaultHost, string defaultPort, string defaultUsername, string defaultPassword) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); XmlElement newGroup = xmlDocument.CreateElement("Group"); XmlAttribute name = xmlDocument.CreateAttribute("GroupName"); name.Value = groupName; newGroup.SetAttributeNode(name); if (defaultHost != "") { XmlElement host = xmlDocument.CreateElement("DefaultHost"); host.InnerText = CryptHelper.Encrypt(defaultHost); newGroup.AppendChild(host); } if (defaultPort != "") { XmlElement host = xmlDocument.CreateElement("DefaultPort"); host.InnerText = CryptHelper.Encrypt(defaultPort); newGroup.AppendChild(host); } if (defaultUsername != "") { XmlElement host = xmlDocument.CreateElement("DefaultUsername"); host.InnerText = CryptHelper.Encrypt(defaultUsername); newGroup.AppendChild(host); } if (defaultPassword != "") { XmlElement host = xmlDocument.CreateElement("DefaultPassword"); host.InnerText = CryptHelper.Encrypt(defaultPassword); newGroup.AppendChild(host); } xmlDocument.DocumentElement?.InsertAfter(newGroup, xmlDocument.DocumentElement.LastChild); 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> /// Method for launch WinSCP: SCP, FTP, SFTP Protocols /// </summary> /// <param name="protocol"> "scp://" or "ftp://" or "sftp://"</param> /// <param name="serverElement">Current selected server from Tree View</param> private static void LaunchWinScp(string protocol, ServerElement serverElement) { string[] winScpExtractPath = ExtractFilePath(Settings.Default.winscppath); string winScpPath = Environment.ExpandEnvironmentVariables(winScpExtractPath[0]); string winScpArgs = winScpExtractPath[1]; if (File.Exists(winScpPath)) { string host = serverElement.Host; string port = serverElement.Port != "" ? serverElement.Port : ""; using (Process winScpProcess = new Process()) { winScpProcess.StartInfo.FileName = Settings.Default.winscppath; winScpProcess.StartInfo.Arguments = protocol; if (serverElement.Username != "") { string[] s = { "%", " ", "+", "/", "@", "\"", ":", ";" }; serverElement.Username = OtherHelper.ReplaceU(s, serverElement.Username); serverElement.Password = OtherHelper.ReplaceU(s, serverElement.Password); winScpProcess.StartInfo.Arguments += serverElement.Username; winScpProcess.StartInfo.Arguments += serverElement.Password != "" ? ":" + serverElement.Password : ""; winScpProcess.StartInfo.Arguments += "@"; } winScpProcess.StartInfo.Arguments += (host != "" ? HttpUtility.UrlEncode(host) : "") ?? throw new InvalidOperationException(); winScpProcess.StartInfo.Arguments += port != "" ? ":" + port : ""; winScpProcess.StartInfo.Arguments += protocol == "ftp://" ? " /passive=" + (Settings.Default.winscppassive ? "on" : "off") : ""; winScpProcess.StartInfo.Arguments += Settings.Default.winscpkey && Settings.Default.winscpkeyfile != "" ? " /privatekey=\"" + Settings.Default.winscpkeyfile + "\"" : ""; winScpProcess.StartInfo.Arguments += winScpArgs != "" ? " " + winScpArgs : ""; winScpProcess.Start(); } } else { if (MessageBox.Show(Resources.connectionHelper_LaunchVnc_M1 + winScpPath + Resources.connectionHelper_LaunchVnc_M2, Resources.connectionHelper_LaunchVnc_Error, MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) { formMain.optionsForm.bWSCPPath_Click(); } } }
/// <summary> /// ??? /// </summary> /// <param name="id"></param> /// <param name="val"></param> public void configSet(string id, string val) { string file = Settings.Default.cfgpath; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(file); XmlElement newPath = xmlDocument.CreateElement("Config"); XmlAttribute name = xmlDocument.CreateAttribute("ID"); name.Value = id; newPath.SetAttributeNode(name); newPath.InnerText = val; XmlNodeList xmlnode = xmlDocument.SelectNodes("//*[@ID=" + parseXpathString(id) + "]"); if (xmlnode != null) { if (xmlDocument.DocumentElement != null) { if (xmlnode.Count > 0) { xmlDocument.DocumentElement.ReplaceChild(newPath, xmlnode[0]); } else { xmlDocument.DocumentElement.InsertBefore(newPath, xmlDocument.DocumentElement.FirstChild); } } } try { xmlDocument.Save(file); } catch (UnauthorizedAccessException) { OtherHelper.Error("Could not write to configuration file :'(\rModifications will not be saved\rPlease check your user permissions."); } }
/// <summary> /// Remove group from xml config /// </summary> /// <param name="groupName">group name</param> public void deleteGroup(string groupName) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(Settings.Default.cfgpath); XmlNodeList groupNodes = xmlDocument.SelectNodes("//*[@GroupName='" + groupName + "']"); if (groupNodes != null && groupNodes.Count > 0) { xmlDocument.DocumentElement?.RemoveChild(groupNodes[0]); } 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> /// Remove server from xml config /// </summary> /// <param name="groupName">group name</param> /// <param name="serverName">server name</param> public void deleteServerByName(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; } xmlGroup.RemoveChild(xmlElement); break; } } } 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> /// 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> /// 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> /// Method for launch PuTTy /// </summary> /// <param name="serverElement">Current selected server from Tree View<</param> private static void LaunchPuTTy(ServerElement serverElement) { string[] puttyExtractPath = ExtractFilePath(Settings.Default.puttypath); string puttyPath = Environment.ExpandEnvironmentVariables(puttyExtractPath[0]); string puttyArgs = puttyExtractPath[1]; if (File.Exists(puttyPath)) { string host = serverElement.Host; string port = serverElement.Port != "" ? serverElement.Port : "22"; using (Process puttyProcess = new Process()) { puttyProcess.StartInfo.FileName = Settings.Default.puttypath; puttyProcess.StartInfo.Arguments = "-ssh "; puttyProcess.StartInfo.Arguments += serverElement.Username != "" ? serverElement.Username + "@" : ""; puttyProcess.StartInfo.Arguments += host != "" ? host : ""; puttyProcess.StartInfo.Arguments += port != "" ? " " + port : ""; puttyProcess.StartInfo.Arguments += serverElement.Username != "" && serverElement.Password != "" ? " -pw \"" + OtherHelper.ReplaceA(passs, passr, serverElement.Password) + "\"" : ""; puttyProcess.StartInfo.Arguments += Settings.Default.puttyexecute && Settings.Default.puttycommand != "" ? " -m \"" + Settings.Default.puttycommand + "\"" : ""; puttyProcess.StartInfo.Arguments += Settings.Default.puttykey && Settings.Default.puttykeyfile != "" ? " -i \"" + Settings.Default.puttykeyfile + "\"" : ""; puttyProcess.StartInfo.Arguments += Settings.Default.puttyforward ? " -X" : ""; puttyProcess.StartInfo.Arguments += puttyArgs != "" ? " " + puttyArgs + "@" : ""; puttyProcess.Start(); } } else { if (MessageBox.Show(Resources.connectionHelper_LaunchVnc_M1 + puttyPath + Resources.connectionHelper_LaunchVnc_M2, Resources.connectionHelper_LaunchVnc_Error, MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK) { formMain.optionsForm.bPuTTYPath_Click(); } } }
/// <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(); } } }