//todo cache session static public bool TestConnection(JCompDevice jo, bool remote = true) { try { Session session = Reconnect(jo, remote); bool testResult = IsRemoteSessionOpened(jo); CloseRemoteSession(jo); return(testResult); } catch (SessionLocalException sle) { string errorDetail = "WinSCP: There was an error communicating with winscp process. winscp cannot be found or executed."; errorDetail += Environment.NewLine + "Message:" + sle.Message; errorDetail += Environment.NewLine + "Target Site:" + sle.TargetSite; errorDetail += Environment.NewLine + "Inner Exception:" + sle.InnerException; errorDetail += Environment.NewLine + "Stacktrace: " + sle.StackTrace; throw sle; } catch (SessionRemoteException sre) { string errorDetail = "WinSCP: Error is reported by the remote server; Local error occurs in WinSCP console session, such as error reading local file."; errorDetail += Environment.NewLine + "Message:" + sre.Message; errorDetail += Environment.NewLine + "Target Site:" + sre.TargetSite; errorDetail += Environment.NewLine + "Inner Exception:" + sre.InnerException; errorDetail += Environment.NewLine + "Stacktrace: " + sre.StackTrace; throw sre; } catch (Exception ex) { throw ex; } }
static private Session GetRemoteSession(JCompDevice jd) { Session session = null; sessions.TryGetValue(jd.JCompDeviceId, out session); return(session); }
public void MakeContextMenu(IListProcessor list, List <ToolStripItem> menuItemList, object selectedListItem, object selectedObject, string aspectName) { menuItemList.Add(new ToolStripSeparator()); JCompDevice item = (JCompDevice)selectedObject; if (item != null) { ToolStripItem menuItem = null; if (WinSCPUtils.IsRemoteSessionOpened(item)) { menuItem = new ToolStripMenuItem(); menuItem.Text = "Закрыть удаленную сессию подключения WinSCP"; menuItem.Click += (s, em) => { try { WinSCPUtils.CloseRemoteSession(item); } catch (Exception ex) { Log.ShowError(ex); } }; menuItemList.Add(menuItem); } } }
override protected void ComplateNodeFromObject(TreeNode node, object o) { base.ComplateNodeFromObject(node, o); object x = (node.Tag is TreeObjectWrap) ? (node.Tag as TreeObjectWrap).Tag : node.Tag; if (x == null) return; if (x is JCompDeviceStorage) { JCompDeviceStorage h = (x as JCompDeviceStorage); if (h.MasterStorage != null && h.MasterStorage.JCompDevice != h.JCompDevice) { node.Text = h.JCompDevice.Name + " " + node.Text; node.ToolTipText = h.JCompDevice.Name + " " + node.ToolTipText; } } else if (x is JSoftInstance) { JSoftInstance h = (x as JSoftInstance); if (h.IsDocker) { SetImageForNode(node, NetworkAccountHelperLibRes.docker, "JSoftInstance_IsDocker"); } } else if (x is JCompDevice) { JCompDevice h = (x as JCompDevice); if (CompDeviceTypeEnum.Virtual.ToString().Equals(h.Stage)) { SetImageForNode(node, NetworkAccountHelperLibRes.virtualbox, "JCompDevice_virtualbox"); } } }
override public void InitApplication() { base.InitApplication(); if (VpnSelectorDm.MyMac != null) { NADm.MyCompDevice = Dm.Instance.FindAll <JCompDevice>().FirstOrDefault <JCompDevice>(c => BaseNetworkUtils.CompareMACs(c.MACAddress, VpnSelectorDm.MyMac)); } if (VpnSelectorDm.DnsMac != null) { //detect network for MAC stage 1 NADm.MyCurrentCompDeviceNetwork = Dm.Instance.FindAll <JCompDeviceNetwork>().FirstOrDefault <JCompDeviceNetwork>(c => BaseNetworkUtils.CompareMACs(c.RouterMacAddress, VpnSelectorDm.DnsMac)); //detect network for MAC stage 2 if (NADm.MyCurrentCompDeviceNetwork == null) { JCompDevice router = Dm.Instance.FindAll <JCompDevice>().FirstOrDefault <JCompDevice>(c => BaseNetworkUtils.CompareMACs(c.MACAddress, VpnSelectorDm.DnsMac)); if (router != null) { NADm.MyCurrentCompDeviceNetwork = router.JCompDeviceNetworkParentLevel; } } if (NADm.MyCurrentCompDeviceNetwork == null) { DialogResult res = DialogResult.OK; if (VpnConnUtils.CurrentVPNServer != null && JCompDeviceNetwork.FindByVPN(VpnConnUtils.CurrentVPNServer as JVPNServer) != null) { res = MessageBox.Show("Не удалось определить текущую сеть. Но есть сеть у текущего прокси сервера. Продолжить? Нажмите ОК, чтобы продолжить и установить сеть потом вручную. Нажмите Cancel для выхода из программы.", FrwConstants.WARNING, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } else { res = MessageBox.Show("Не удалось определить текущую сеть. Продолжить? Нажмите ОК, чтобы продолжить и установить сеть потом вручную. Нажмите Cancel для выхода из программы.", FrwConstants.WARNING, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } if (res != DialogResult.OK) { Application.Exit(); } } //else //{ //if (NADm.MyCurrentCompDeviceNetwork.IsInternal == false) //{ // NADm.MyCurrentCompDeviceNetwork.IsInternal = true; // Dm.Instance.SaveObject(NADm.MyCurrentCompDeviceNetwork); //} //} } Console.WriteLine("My comp: " + (NADm.MyCompDevice != null ? NADm.MyCompDevice.Name : "")); Console.WriteLine("My current comp network: " + (NADm.MyCurrentCompDeviceNetwork != null ? NADm.MyCurrentCompDeviceNetwork.Name : "")); Console.WriteLine("My current VPN: " + (VpnConnUtils.CurrentVPNServer != null ? VpnConnUtils.CurrentVPNServer.GetConnectionName() : "")); }
public static bool IsRemoteSessionOpened(JCompDevice jd) { Session session = GetRemoteSession(jd); if (session != null) { return(session.Opened); } else { return(false); } }
public static void CloseRemoteSession(JCompDevice jd) { Session session = GetRemoteSession(jd); if (session != null) { if (session.Opened) { //session.Abort(); session.Close(); session.Dispose(); sessions.Remove(jd.JCompDeviceId); } } }
private static Session Reconnect(JCompDevice jd, bool remote = true) { Session session = GetRemoteSession(jd); if (session == null) { session = new Session(); AddRemoteSession(jd, session); } if (session.Opened == false) { SessionOptions sessionOptions = SessionOptionsFromJCompDeviceStorage(jd, remote); session.Open(sessionOptions); session.OutputDataReceived += Session_OutputDataReceived; } return(session); }
public static void ExecuteCommand(string command, JCompDevice jd, bool remote = true) { try { Session session = Reconnect(jd, remote); //SessionOptions sessionOptions = SessionOptionsFromJCompDeviceStorage(jo, remote); //using (Session session = new Session()) //{ // Connect //session.Open(sessionOptions); //session.OutputDataReceived += Session_OutputDataReceived; CommandExecutionResult result = session.ExecuteCommand(command); result.Check(); if (result.Output != null) { Log.ProcessDebug("Result of commmand: " + result.Output); } //} } catch (SessionLocalException sle) { string errorDetail = "WinSCP: There was an error communicating with winscp process. winscp cannot be found or executed."; errorDetail += Environment.NewLine + "Message:" + sle.Message; errorDetail += Environment.NewLine + "Target Site:" + sle.TargetSite; errorDetail += Environment.NewLine + "Inner Exception:" + sle.InnerException; errorDetail += Environment.NewLine + "Stacktrace: " + sle.StackTrace; throw sle; } catch (SessionRemoteException sre) { string errorDetail = "WinSCP: Error is reported by the remote server; Local error occurs in WinSCP console session, such as error reading local file."; errorDetail += Environment.NewLine + "Message:" + sre.Message; errorDetail += Environment.NewLine + "Target Site:" + sre.TargetSite; errorDetail += Environment.NewLine + "Inner Exception:" + sre.InnerException; errorDetail += Environment.NewLine + "Stacktrace: " + sre.StackTrace; throw sre; } catch (Exception ex) { throw ex; } }
static public SessionOptions SessionOptionsFromJCompDeviceStorage(JCompDevice jd, bool remote = true) { SessionOptions o = new SessionOptions(); o.Protocol = Protocol.Sftp; //todo webinfo if (remote) { o.HostName = jd.JCompDeviceNetworkParentLevel.GetExternalAddress();//.RemoteHostName; if (!string.IsNullOrEmpty(jd.WebEntryInfo.ExtPortSSH)) { o.PortNumber = int.Parse(jd.WebEntryInfo.ExtPortSSH); } } else { o.HostName = jd.GetInternalAddress(); if (!string.IsNullOrEmpty(jd.WebEntryInfo.PortSSH)) { o.PortNumber = int.Parse(jd.WebEntryInfo.PortSSH); } } o.Password = jd.Password; o.UserName = jd.Login; if (string.IsNullOrEmpty(jd.SshHostKeyFingerprint)) { o.GiveUpSecurityAndAcceptAnySshHostKey = true; } else { o.GiveUpSecurityAndAcceptAnySshHostKey = false; o.SshHostKeyFingerprint = jd.SshHostKeyFingerprint; } //https://winscp.net/eng/docs/faq_hostkey //In exceptional situations, when security is not required, such as when connecting within a trusted private network, you can use -hostkey=* or SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey to blindly accept any host key. //If you want to allow a user to manually verify the host key, use the Session.ScanFingerprint method to retrieve the key fingerprint.Then let the user to verify it and assign the verified value to the SessionOptions.SshHostKeyFingerprint property. //https://winscp.net/eng/docs/library_example_known_hosts return(o); }
static private void AddRemoteSession(JCompDevice jd, Session session) { sessions.Add(jd.JCompDeviceId, session); }