helper.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package tlscert
  2. import (
  3. "path/filepath"
  4. "strings"
  5. )
  6. // This remove the certificates in the list where either the
  7. // public key or the private key is missing
  8. func getCertPairs(certFiles []string) []string {
  9. pemMap := make(map[string]bool)
  10. keyMap := make(map[string]bool)
  11. for _, filename := range certFiles {
  12. if filepath.Ext(filename) == ".pem" {
  13. pemMap[strings.TrimSuffix(filename, ".pem")] = true
  14. } else if filepath.Ext(filename) == ".key" {
  15. keyMap[strings.TrimSuffix(filename, ".key")] = true
  16. }
  17. }
  18. var result []string
  19. for domain := range pemMap {
  20. if keyMap[domain] {
  21. result = append(result, domain)
  22. }
  23. }
  24. return result
  25. }
  26. // Get the cloest subdomain certificate from a list of domains
  27. func matchClosestDomainCertificate(subdomain string, domains []string) string {
  28. var matchingDomain string = ""
  29. maxLength := 0
  30. for _, domain := range domains {
  31. if strings.HasSuffix(subdomain, "."+domain) && len(domain) > maxLength {
  32. matchingDomain = domain
  33. maxLength = len(domain)
  34. }
  35. }
  36. return matchingDomain
  37. }