2018-03-05 19:54:04 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// Domain holds a domain name with SANs.
|
2018-03-05 19:54:04 +00:00
|
|
|
type Domain struct {
|
2019-07-01 09:30:05 +00:00
|
|
|
Main string `description:"Default subject name." json:"main,omitempty" toml:"main,omitempty" yaml:"main,omitempty"`
|
|
|
|
SANs []string `description:"Subject alternative names." json:"sans,omitempty" toml:"sans,omitempty" yaml:"sans,omitempty"`
|
2018-03-05 19:54:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// ToStrArray convert a domain into an array of strings.
|
2018-03-26 12:12:03 +00:00
|
|
|
func (d *Domain) ToStrArray() []string {
|
|
|
|
var domains []string
|
|
|
|
if len(d.Main) > 0 {
|
|
|
|
domains = []string{d.Main}
|
|
|
|
}
|
|
|
|
return append(domains, d.SANs...)
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// Set sets a domains from an array of strings.
|
2018-03-26 12:12:03 +00:00
|
|
|
func (d *Domain) Set(domains []string) {
|
|
|
|
if len(domains) > 0 {
|
|
|
|
d.Main = domains[0]
|
|
|
|
d.SANs = domains[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// Domains parse []Domain.
|
2018-03-05 19:54:04 +00:00
|
|
|
type Domains []Domain
|
|
|
|
|
|
|
|
// Set []Domain
|
|
|
|
func (ds *Domains) Set(str string) error {
|
|
|
|
fargs := func(c rune) bool {
|
|
|
|
return c == ',' || c == ';'
|
|
|
|
}
|
|
|
|
|
|
|
|
// get function
|
|
|
|
slice := strings.FieldsFunc(str, fargs)
|
|
|
|
if len(slice) < 1 {
|
|
|
|
return fmt.Errorf("parse error ACME.Domain. Unable to parse %s", str)
|
|
|
|
}
|
|
|
|
|
|
|
|
d := Domain{
|
|
|
|
Main: slice[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(slice) > 1 {
|
|
|
|
d.SANs = slice[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
*ds = append(*ds, d)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// Get []Domain.
|
2018-03-05 19:54:04 +00:00
|
|
|
func (ds *Domains) Get() interface{} { return []Domain(*ds) }
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// String returns []Domain in string.
|
2018-03-05 19:54:04 +00:00
|
|
|
func (ds *Domains) String() string { return fmt.Sprintf("%+v", *ds) }
|
|
|
|
|
|
|
|
// SetValue sets []Domain into the parser
|
|
|
|
func (ds *Domains) SetValue(val interface{}) {
|
|
|
|
*ds = val.([]Domain)
|
|
|
|
}
|
2018-03-27 14:18:03 +00:00
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// MatchDomain returns true if a domain match the cert domain.
|
2018-03-27 14:18:03 +00:00
|
|
|
func MatchDomain(domain string, certDomain string) bool {
|
|
|
|
if domain == certDomain {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(certDomain) > 0 && certDomain[len(certDomain)-1] == '.' {
|
|
|
|
certDomain = certDomain[:len(certDomain)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := strings.Split(domain, ".")
|
|
|
|
for i := range labels {
|
|
|
|
labels[i] = "*"
|
|
|
|
candidate := strings.Join(labels, ".")
|
|
|
|
if certDomain == candidate {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
// CanonicalDomain returns a lower case domain with trim space.
|
|
|
|
func CanonicalDomain(domain string) string {
|
|
|
|
return strings.ToLower(strings.TrimSpace(domain))
|
|
|
|
}
|