Esempio n. 1
0
        public override void HandleDoubleClick(MainForm mainForm)
        {
            if (ServerManager != null)
            {
                return;
            }

            if (_status == NodeStatus.Loading)
            {
                return;
            }

            MainForm = mainForm;
            mainForm.DisconnectButton.Enabled = false;
            _status = NodeStatus.Loading;
            mainForm.ShowInfo($"Connecting to {HostName}...");
            try
            {
                if (Mode == WorkingMode.IisExpress)
                {
                    ServerManager = new IisExpressServerManager(HostName);
                }
                else if (Mode == WorkingMode.Iis)
                {
                    ServerManager = new IisServerManager(false, HostName);
                }
                else
                {
                    ServerManager = new JexusServerManager(HostName, Credentials);
                }

                ServerManager.Name = DisplayName;

                if (Mode == WorkingMode.Jexus)
                {
                    SetHandler();
                    var server  = (JexusServerManager)ServerManager;
                    var version = AsyncHelper.RunSync(() => server.GetVersionAsync());
                    if (version == null)
                    {
                        MessageBox.Show("Authentication failed.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ServerManager = null;
                        _status       = NodeStatus.Default;
                        mainForm.DisconnectButton.Enabled = true;
                        return;
                    }

                    if (version < JexusServerManager.MinimumServerVersion)
                    {
                        var toContinue =
                            MessageBox.Show(
                                $"The server version is {version}, while minimum compatible version is {JexusServerManager.MinimumServerVersion}. Making changes might corrupt server configuration. Do you want to continue?",
                                Text,
                                MessageBoxButtons.YesNoCancel,
                                MessageBoxIcon.Question);
                        if (toContinue != DialogResult.Yes)
                        {
                            ServerManager = null;
                            _status       = NodeStatus.Default;
                            mainForm.DisconnectButton.Enabled = true;
                            return;
                        }
                    }

                    var conflict = AsyncHelper.RunSync(() => server.HelloAsync());
                    if (Environment.MachineName != conflict)
                    {
                        MessageBox.Show(
                            $"The server is also connected to {conflict}. Making changes on multiple clients might corrupt server configuration.",
                            Text,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Warning);
                    }
                }

                ServerManager.IsLocalhost = IsLocalhost;
                LoadServer(mainForm.ApplicationPoolsMenu, mainForm.SitesMenu, mainForm.SiteMenu);
                Tag = ServerManager;
                mainForm.EnableServerMenuItems(true);
                _status = NodeStatus.Loaded;
                mainForm.DisconnectButton.Enabled = true;
            }
            catch (Exception ex)
            {
                File.WriteAllText(DialogHelper.DebugLog, ex.ToString());
                var last = ex;
                while (last is AggregateException)
                {
                    last = last.InnerException;
                }

                var message = new StringBuilder();
                message.AppendLine("Could not connect to the specified computer.")
                .AppendLine()
                .AppendFormat("Details: {0}", last?.Message);
                MessageBox.Show(message.ToString(), mainForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                ServerManager = null;
                _status       = NodeStatus.Default;
                mainForm.DisconnectButton.Enabled = true;
            }
            finally
            {
                mainForm.HideInfo();
            }
        }
Esempio n. 2
0
        private async Task <bool> OpenConnection(SynchronizationContext context)
        {
            string accepted = null;
            var    handler  = ServicePointManager.ServerCertificateValidationCallback;

            ServicePointManager.ServerCertificateValidationCallback =
                (sender1, certificate, chain, sslPolicyErrors)
                =>
            {
                var hash = certificate.GetCertHashString();
                if (accepted == hash)
                {
                    return(true);
                }

                if (sslPolicyErrors == SslPolicyErrors.None)
                {
                    accepted = hash;
                    return(true);
                }

                var dialog = new CertificateErrorsDialog(certificate);
                var result = dialog.ShowDialog();
                if (result != DialogResult.OK)
                {
                    return(false);
                }

                accepted = hash;
                return(true);
            };

            var service = (IManagementUIService)GetService(typeof(IManagementUIService));

            try
            {
                var data   = (ConnectionWizardData)WizardData;
                var server = new JexusServerManager(data.HostName, data.UserName + "|" + data.Password);
                data.Server = server;
                var version = await server.GetVersionAsync();

                if (version == null)
                {
                    service.ShowMessage("Authentication failed.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                if (version < JexusServerManager.MinimumServerVersion)
                {
                    var toContinue =
                        service.ShowMessage(
                            string.Format(
                                "The server version is {0}, while minimum compatible version is {1}. Making changes might corrupt server configuration. Do you want to continue?",
                                version,
                                JexusServerManager.MinimumServerVersion),
                            Text,
                            MessageBoxButtons.YesNoCancel,
                            MessageBoxIcon.Question);
                    if (toContinue != DialogResult.Yes)
                    {
                        return(false);
                    }
                }

                var conflict = await server.HelloAsync();

                if (Environment.MachineName != conflict)
                {
                    service.ShowMessage(string.Format("The server is also connected to {0}. Making changes on multiple clients might corrupt server configuration.", conflict), Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                data.CertificateHash = accepted;
                return(true);
            }
            catch (Exception ex)
            {
                File.WriteAllText(DialogHelper.DebugLog, ex.ToString());
                var last = ex;
                while (last is AggregateException)
                {
                    last = last.InnerException;
                }

                var message = new StringBuilder();
                message.AppendLine("Could not connect to the specified computer.")
                .AppendLine()
                .AppendFormat("Details: {0}", last.Message);
                service.ShowMessage(message.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = handler;
            }
        }