Exemple #1
0
        public void ShowRemoteHost(uint serverId)
        {
            Debug.Assert(serverId > 0);
            Debug.Assert(GlobalData.Instance.ServerList.Any(x => x.Id == serverId));
            var server = GlobalData.Instance.ServerList.First(x => x.Id == serverId);

            // update last conn time
            server.LastConnTime = DateTime.Now;
            Server.AddOrUpdate(server);

            // is connected now! activate it then return.
            if (server.OnlyOneInstance && _protocolHosts.ContainsKey(serverId.ToString()))
            {
                if (_protocolHosts[serverId.ToString()].ParentWindow is TabWindow t)
                {
                    var s = t.Vm?.Items?.First(x => x.Content?.ProtocolServer?.Id == serverId);
                    if (s != null)
                    {
                        t.Vm.SelectedItem = s;
                    }
                    t.Activate();
                }
                return;
            }

            // create new remote session
            TabWindow tab = null;

            try
            {
                if (server.IsConnWithFullScreen())
                {
                    // for those people using 2+ monitors in different scale factors, we will try "mstsc.exe" instead of "PRemoteM".
                    if (Screen.AllScreens.Length > 1 &&
                        server is ProtocolServerRDP rdp &&
                        rdp.RdpFullScreenFlag == ERdpFullScreenFlag.EnableFullAllScreens)
                    {
                        int factor = (int)(new ScreenInfoEx(Screen.PrimaryScreen).ScaleFactor * 100);
                        // check if screens are in different scale factors
                        bool differentScaleFactorFlag = Screen.AllScreens.Select(screen => (int)(new ScreenInfoEx(screen).ScaleFactor * 100)).Any(factor2 => factor != factor2);
                        if (differentScaleFactorFlag || true)
                        {
                            var tmp         = Path.GetTempPath();
                            var rdpFileName = $"{rdp.DispName}_{rdp.Port}_{rdp.UserName}";
                            var invalid     = new string(Path.GetInvalidFileNameChars()) +
                                              new string(Path.GetInvalidPathChars());
                            rdpFileName = invalid.Aggregate(rdpFileName, (current, c) => current.Replace(c.ToString(), ""));
                            var rdpFile = Path.Combine(tmp, rdpFileName + ".rdp");
                            try
                            {
                                File.WriteAllText(rdpFile, rdp.ToRdpConfig().ToString());
                                var p = new Process
                                {
                                    StartInfo =
                                    {
                                        FileName               = "cmd.exe",
                                        UseShellExecute        = false,
                                        RedirectStandardInput  = true,
                                        RedirectStandardOutput = true,
                                        RedirectStandardError  = true,
                                        CreateNoWindow         = true
                                    }
                                };
                                p.Start();
                                p.StandardInput.WriteLine("mstsc -admin \"" + rdpFile + "\"");
                                p.StandardInput.WriteLine("exit");
                            }
                            finally
                            {
                                // delete tmp rdp file, ETA 10s
                                var t = new Task(() =>
                                {
                                    Thread.Sleep(1000 * 10);
                                    if (File.Exists(rdpFile))
                                    {
                                        File.Delete(rdpFile);
                                    }
                                });
                                t.Start();
                            }
                            return;
                        }
                    }


                    var host = ProtocolHostFactory.Get(server);
                    host.OnClosed            += OnProtocolClose;
                    host.OnFullScreen2Window += OnFullScreen2Window;
                    AddProtocolHost(host);
                    MoveProtocolHostToFullScreen(host.ConnectionId);
                    host.Conn();
                    SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with full");
                }
                else
                {
                    switch (SystemConfig.Instance.General.TabMode)
                    {
                    case EnumTabMode.NewItemGoesToGroup:
                        // work in tab by group mode
                        if (_tabWindows.Any(x => x.Value.Vm.Tag == server.GroupName))
                        {
                            tab = _tabWindows.First(x => x.Value.Vm.Tag == server.GroupName).Value;
                        }
                        break;

                    case EnumTabMode.NewItemGoesToProtocol:
                        // work in tab by protocol mode
                        if (_tabWindows.Any(x => x.Value.Vm.Tag == server.ProtocolDisplayName))
                        {
                            tab = _tabWindows.First(x => x.Value.Vm.Tag == server.ProtocolDisplayName).Value;
                        }
                        break;

                    default:
                        // work in tab by latest tab mode
                        if (!string.IsNullOrEmpty(_lastTabToken) && _tabWindows.ContainsKey(_lastTabToken))
                        {
                            tab = _tabWindows[_lastTabToken];
                        }
                        break;
                    }

                    if (tab == null)
                    {
                        var token = DateTime.Now.Ticks.ToString();
                        AddTab(new TabWindow(token));
                        tab = _tabWindows[token];
                        tab.Show();
                        _lastTabToken = token;

                        if (SystemConfig.Instance.General.TabMode == EnumTabMode.NewItemGoesToGroup)
                        {
                            tab.Vm.Tag = server.GroupName;
                        }
                        else if (SystemConfig.Instance.General.TabMode == EnumTabMode.NewItemGoesToProtocol)
                        {
                            tab.Vm.Tag = server.ProtocolDisplayName;
                        }
                    }
                    tab.Activate();
                    var size = tab.GetTabContentSize();
                    var host = ProtocolHostFactory.Get(server, size.Width, size.Height);
                    host.OnClosed            += OnProtocolClose;
                    host.OnFullScreen2Window += OnFullScreen2Window;
                    host.ParentWindow         = tab;
                    tab.Vm.Items.Add(new TabItemViewModel()
                    {
                        Content = host,
                        Header  = server.DispName,
                    });
                    tab.Vm.SelectedItem = tab.Vm.Items.Last();
                    host.Conn();
                    _protocolHosts.Add(host.ConnectionId, host);
                    SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with Tab({tab.GetHashCode()})");
                    SimpleLogHelper.Debug($@"ProtocolHosts.Count = {_protocolHosts.Count}, FullWin.Count = {_host2FullScreenWindows.Count}, _tabWindows.Count = {_tabWindows.Count}");
                }
            }
            catch (Exception e)
            {
                CloseEmpytTab();
                SimpleLogHelper.Error(e);
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemple #2
0
        public void ShowRemoteHost(uint id)
        {
            Debug.Assert(id > 0);
            Debug.Assert(GlobalData.Instance.ServerList.Any(x => x.Id == id));
            var server = GlobalData.Instance.ServerList.First(x => x.Id == id);

            // update last conn time
            server.LastConnTime = DateTime.Now;
            Server.AddOrUpdate(server);

            // start conn
            if (server.OnlyOneInstance && _protocolHosts.ContainsKey(id.ToString()))
            {
                _protocolHosts[id.ToString()].ParentWindow?.Activate();
                return;
            }

            TabWindow tab = null;

            try
            {
                if (server.IsConnWithFullScreen())
                {
                    // for those people using 2+ monitor which are in different scale factors, we will try "mstsc.exe" instead of "PRemoteM".
                    if (Screen.AllScreens.Length > 1 &&
                        server is ProtocolServerRDP rdp &&
                        rdp.RdpFullScreenFlag == ERdpFullScreenFlag.EnableFullAllScreens)
                    {
                        int factor = (int)(new ScreenInfoEx(Screen.PrimaryScreen).ScaleFactor * 100);
                        // check if screens are in different scale factors
                        bool differentScaleFactorFlag = Screen.AllScreens.Select(screen => (int)(new ScreenInfoEx(screen).ScaleFactor * 100)).Any(factor2 => factor != factor2);
                        if (differentScaleFactorFlag)
                        {
                            var tmp     = Path.GetTempPath();
                            var dp      = rdp.DispName;
                            var invalid = new string(Path.GetInvalidFileNameChars()) +
                                          new string(Path.GetInvalidPathChars());
                            dp = invalid.Aggregate(dp, (current, c) => current.Replace(c.ToString(), ""));
                            var rdpFile = Path.Combine(tmp, dp + ".rdp");
                            try
                            {
                                File.WriteAllText(rdpFile, rdp.ToRdpConfig().ToString());
                                var p = new Process
                                {
                                    StartInfo =
                                    {
                                        FileName               = "cmd.exe",
                                        UseShellExecute        = false,
                                        RedirectStandardInput  = true,
                                        RedirectStandardOutput = true,
                                        RedirectStandardError  = true,
                                        CreateNoWindow         = true
                                    }
                                };
                                p.Start();
                                p.StandardInput.WriteLine("mstsc -admin " + rdpFile);
                                p.StandardInput.WriteLine("exit");
                            }
                            catch (Exception)
                            {
                                throw;
                            }
                            finally
                            {
                                var t = new Task(() =>
                                {
                                    Thread.Sleep(1000 * 10);
                                    if (File.Exists(rdpFile))
                                    {
                                        File.Delete(rdpFile);
                                    }
                                });
                                t.Start();
                            }
                            return;
                        }
                    }


                    var host = ProtocolHostFactory.Get(server);
                    host.OnClosed            += OnProtocolClose;
                    host.OnFullScreen2Window += OnFullScreen2Window;
                    AddProtocolHost(host);
                    MoveProtocolHostToFullScreen(host.ConnectionId);
                    host.Conn();
                    SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with full");
                }
                else
                {
                    if (!string.IsNullOrEmpty(_lastTabToken) && _tabWindows.ContainsKey(_lastTabToken))
                    {
                        tab = _tabWindows[_lastTabToken];
                    }
                    else
                    {
                        var token = DateTime.Now.Ticks.ToString();
                        AddTab(new TabWindow(token));
                        tab = _tabWindows[token];
                        tab.Show();
                        _lastTabToken = token;
                    }
                    tab.Activate();
                    var size = tab.GetTabContentSize();
                    var host = ProtocolHostFactory.Get(server, size.Width, size.Height);
                    host.OnClosed            += OnProtocolClose;
                    host.OnFullScreen2Window += OnFullScreen2Window;
                    host.ParentWindow         = tab;
                    tab.Vm.Items.Add(new TabItemViewModel()
                    {
                        Content = host,
                        Header  = server.DispName,
                    });
                    tab.Vm.SelectedItem = tab.Vm.Items.Last();
                    host.Conn();
                    _protocolHosts.Add(host.ConnectionId, host);
                    SimpleLogHelper.Debug($@"Start Conn: {server.DispName}({server.GetHashCode()}) by host({host.GetHashCode()}) with Tab({tab.GetHashCode()})");
                    SimpleLogHelper.Debug($@"ProtocolHosts.Count = {_protocolHosts.Count}, FullWin.Count = {_host2FullScreenWindows.Count}, _tabWindows.Count = {_tabWindows.Count}");
                }
            }
            catch (Exception e)
            {
                if (tab?.Vm != null && (tab.Vm?.Items?.Count ?? 0) == 0)
                {
                    CloseTabWindow(tab.Vm.Token);
                }
                SimpleLogHelper.Error(e);
                MessageBox.Show(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }