helper.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. crtMap := make(map[string]bool)
  10. keyMap := make(map[string]bool)
  11. for _, filename := range certFiles {
  12. if filepath.Ext(filename) == ".crt" {
  13. crtMap[strings.TrimSuffix(filename, ".crt")] = 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 crtMap {
  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. }
  38. //Check if a requesting domain is a subdomain of a given domain
  39. func isSubdomain(subdomain, domain string) bool {
  40. subdomainParts := strings.Split(subdomain, ".")
  41. domainParts := strings.Split(domain, ".")
  42. if len(subdomainParts) < len(domainParts) {
  43. return false
  44. }
  45. for i := range domainParts {
  46. if subdomainParts[len(subdomainParts)-1-i] != domainParts[len(domainParts)-1-i] {
  47. return false
  48. }
  49. }
  50. return true
  51. }