Example #1
0
        // David Fouler would really like us not to do this
        // https://github.com/aspnet/KestrelHttpServer/issues/2306#issuecomment-364478486
        public EndpointDetailsViewModel HackEndpointDetails()
        {
            var model = new EndpointDetailsViewModel();

            try {
                KestrelServer kestrel = server as KestrelServer;
                if (kestrel == null)
                {
                    model.NotKestrel = true;
                    return(model);
                }

                KestrelServerOptions options = kestrel.Options;
                // reflect out the ListenOptions array
                Type                 kestrelServerOptionsType = typeof(KestrelServerOptions);
                PropertyInfo         listenOptionsProp        = kestrelServerOptionsType.GetProperty("ListenOptions", BindingFlags.Instance | BindingFlags.NonPublic);
                List <ListenOptions> listenOptions            = (List <ListenOptions>)listenOptionsProp.GetValue(options);

                foreach (ListenOptions listenOption in listenOptions)
                {
                    if (listenOption.ConnectionAdapters?.Count > 0)
                    {
                        foreach (IConnectionAdapter connectionAdapter in listenOption.ConnectionAdapters)
                        {
                            // Grab all the details for this endpoint
                            EndpointDetail endpointDetail = new EndpointDetail {
                                Address = listenOption.IPEndPoint.Address.ToString(),
                                Port    = listenOption.IPEndPoint.Port,
                                IsHttps = connectionAdapter.IsHttps
                            };
                            if (connectionAdapter is HttpsConnectionAdapter)
                            {
                                endpointDetail.Certificate = typeof(HttpsConnectionAdapter).GetField("_serverCertificate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(connectionAdapter) as X509Certificate2;
                            }

                            model.EndpointDetails.Add(endpointDetail);
                        }
                    }
                    else
                    {
                        model.EndpointDetails.Add(new EndpointDetail {
                            Address = listenOption.IPEndPoint.Address.ToString(),
                            Port    = listenOption.IPEndPoint.Port,
                            IsHttps = false
                        });
                    }
                }

                // Reflect the dev cert
                model.IsDevCertLoaded    = (bool)(kestrelServerOptionsType.GetProperty("IsDevCertLoaded", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options));
                model.DefaultCertificate = kestrelServerOptionsType.GetProperty("DefaultCertificate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options) as X509Certificate2;
            } catch (Exception ex) {
                // because this is hacky enough that it'll likely fall down easily
                model.Exception = ex.Message;
            }
            return(model);
        }
Example #2
0
        public ActionResult <IEnumerable <string> > Get()
        {
            EndpointDetailsViewModel model       = this.endpointDetailsService.HackEndpointDetails();
            List <string>            thumbprints = (
                from t in model.EndpointDetails
                where t.Certificate?.Thumbprint != null
                select t.Certificate?.Thumbprint
                ).Distinct().ToList();

            return(thumbprints);
            //return new string[] { "value1", "value2" };
        }
        // David Fowler would really like us not to do this
        // https://github.com/aspnet/KestrelHttpServer/issues/2306#issuecomment-364478486
        public EndpointDetailsViewModel HackEndpointDetails()
        {
            var model = new EndpointDetailsViewModel();

            try
            {
                KestrelServer kestrel = server as KestrelServer;
                if (kestrel == null)
                {
                    model.NotKestrel = true;
                    return(model);
                }

                KestrelServerOptions options = kestrel.Options;

                // reflection voodoo
                Type                 kestrelServerOptionsType = typeof(KestrelServerOptions);
                PropertyInfo         listenOptionsProp        = kestrelServerOptionsType.GetProperty("ListenOptions", BindingFlags.Instance | BindingFlags.NonPublic);
                PropertyInfo         isTlsProp     = typeof(ListenOptions).GetProperty("IsTls", BindingFlags.Instance | BindingFlags.NonPublic);
                List <ListenOptions> listenOptions = (List <ListenOptions>)listenOptionsProp.GetValue(options);

                foreach (ListenOptions listenOption in listenOptions)
                {
                    bool isTls = (bool)isTlsProp.GetValue(listenOption);

                    // Grab all the details for this endpoint
                    EndpointDetail endpointDetail = new EndpointDetail
                    {
                        Address = listenOption.IPEndPoint.Address.ToString(),
                        Port    = listenOption.IPEndPoint.Port,
                        IsHttps = isTls
                    };
                    model.EndpointDetails.Add(endpointDetail);

                    if (isTls)
                    {
                        // it appears all middleware is configured for all listenOptions even if they aren't https
                        endpointDetail.Certificate = GetCertificateFromOptions(listenOption);
                    }
                }

                // Reflect the dev cert
                model.IsDevCertLoaded    = (bool)(kestrelServerOptionsType.GetProperty("IsDevCertLoaded", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options));
                model.DefaultCertificate = kestrelServerOptionsType.GetProperty("DefaultCertificate", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(options) as X509Certificate2;
            }
            catch (Exception ex)
            {
                // because this is hacky enough that it'll likely fall down easily
                model.Exception = ex.Message;
            }
            return(model);
        }
        public IActionResult Endpoints()
        {
            EndpointDetailsViewModel model = endpointDetailsService.HackEndpointDetails();

            return(View(model));
        }