Esempio n. 1
0
        public List <DomainOption> GetDomainOptionsFromSite(string siteId)
        {
            var defaultNoDomainHost = "";
            var domainOptions       = new List <DomainOption>();

            var matchingSites   = _iisManager.GetSiteBindingList(Certify.Properties.Settings.Default.IgnoreStoppedSites, siteId);
            var siteBindingList = matchingSites.Where(s => s.SiteId == siteId);

            bool includeEmptyHostnameBindings = false;

            foreach (var siteDetails in siteBindingList)
            {
                //if domain not currently in the list of options, add it
                if (!domainOptions.Any(item => item.Domain == siteDetails.Host))
                {
                    DomainOption opt = new DomainOption
                    {
                        Domain          = siteDetails.Host,
                        IsPrimaryDomain = false,
                        IsSelected      = true,
                        Title           = ""
                    };

                    if (String.IsNullOrWhiteSpace(opt.Domain))
                    {
                        //binding has no hostname/domain set - user will need to specify
                        opt.Title         = defaultNoDomainHost;
                        opt.Domain        = defaultNoDomainHost;
                        opt.IsManualEntry = true;
                    }
                    else
                    {
                        opt.Title = siteDetails.Protocol + "://" + opt.Domain;
                    }

                    if (siteDetails.IP != null && siteDetails.IP != "0.0.0.0")
                    {
                        opt.Title += " : " + siteDetails.IP;
                    }

                    if (!opt.IsManualEntry || (opt.IsManualEntry && includeEmptyHostnameBindings))
                    {
                        domainOptions.Add(opt);
                    }
                }
            }

            //TODO: if one or more binding is to a specific IP, how to manage in UI?

            if (domainOptions.Any(d => !String.IsNullOrEmpty(d.Domain)))
            {
                // mark first domain as primary, if we have no other settings
                if (!domainOptions.Any(d => d.IsPrimaryDomain == true))
                {
                    var electableDomains = domainOptions.Where(d => !String.IsNullOrEmpty(d.Domain) && d.Domain != defaultNoDomainHost);
                    if (electableDomains.Any())
                    {
                        // promote first domain in list to primary by default
                        electableDomains.First().IsPrimaryDomain = true;
                    }
                }
            }

            return(domainOptions);
        }
Esempio n. 2
0
        public List <ManagedSite> ImportManagedSitesFromVault(bool mergeSitesAsSan = false)
        {
            var sites = new List <ManagedSite>();

            //get dns identifiers from vault
            var vaultManager = new VaultManager(Properties.Settings.Default.VaultPath, ACMESharp.Vault.Providers.LocalDiskVault.VAULT);
            var iisManager   = new IISManager();

            var identifiers = vaultManager.GetIdentifiers();
            var iisSites    = iisManager.GetSiteBindingList(ignoreStoppedSites: Certify.Properties.Settings.Default.IgnoreStoppedSites);

            foreach (var identifier in identifiers)
            {
                //identify IIS site related to this identifier (if any)
                var iisSite = iisSites.FirstOrDefault(d => d.Host == identifier.Dns);
                var site    = new ManagedSite
                {
                    Id                 = Guid.NewGuid().ToString(),
                    GroupId            = iisSite?.SiteId,
                    Name               = identifier.Dns + (iisSite != null ? " : " + iisSite.SiteName : ""),
                    IncludeInAutoRenew = true,
                    Comments           = "Imported from vault",
                    ItemType           = ManagedItemType.SSL_LetsEncrypt_LocalIIS,
                    TargetHost         = "localhost",
                    RequestConfig      = new CertRequestConfig
                    {
                        BindingIPAddress                 = iisSite?.IP,
                        BindingPort                      = iisSite?.Port.ToString(),
                        ChallengeType                    = "http-01",
                        EnableFailureNotifications       = true,
                        PerformAutoConfig                = true,
                        PerformAutomatedCertBinding      = true,
                        PerformChallengeFileCopy         = true,
                        PerformExtensionlessConfigChecks = true,
                        PrimaryDomain                    = identifier.Dns,
                        SubjectAlternativeNames          = new string[] { identifier.Dns },
                        WebsiteRootPath                  = iisSite?.PhysicalPath
                    },
                    DomainOptions = new List <DomainOption>()
                    {
                        new DomainOption {
                            Domain = identifier.Dns, IsPrimaryDomain = true, IsSelected = true
                        }
                    }
                };

                sites.Add(site);
            }

            if (mergeSitesAsSan)
            {
                foreach (var s in sites)
                {
                    //merge sites with same group (iis site etc) and different primary domain
                    if (sites.Any(m => m.GroupId != null && m.GroupId == s.GroupId && m.RequestConfig.PrimaryDomain != s.RequestConfig.PrimaryDomain))
                    {
                        //existing site to merge into
                        //add san for dns
                        var mergedSite = sites.FirstOrDefault(m =>
                                                              m.GroupId != null && m.GroupId == s.GroupId &&
                                                              m.RequestConfig.PrimaryDomain != s.RequestConfig.PrimaryDomain &&
                                                              m.RequestConfig.PrimaryDomain != null
                                                              );
                        if (mergedSite != null)
                        {
                            mergedSite.DomainOptions.Add(new DomainOption {
                                Domain = s.RequestConfig.PrimaryDomain, IsPrimaryDomain = false, IsSelected = true
                            });

                            //use shortest version of domain name as site name
                            if (mergedSite.RequestConfig.PrimaryDomain.Contains(s.RequestConfig.PrimaryDomain))
                            {
                                mergedSite.Name = mergedSite.Name.Replace(mergedSite.RequestConfig.PrimaryDomain, s.RequestConfig.PrimaryDomain);
                            }

                            //flag spare site config to be discar
                            s.RequestConfig.PrimaryDomain = null;
                        }
                    }
                }

                //discard sites which have been merged into other sites
                sites.RemoveAll(s => s.RequestConfig.PrimaryDomain == null);
            }
            return(sites);
        }