2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-13 19:50:44 +07:00
|
|
|
"crypto/rand"
|
2016-11-16 20:31:40 +07:00
|
|
|
"errors"
|
|
|
|
"github.com/BurntSushi/toml"
|
2016-11-13 19:50:44 +07:00
|
|
|
"github.com/satori/go.uuid"
|
|
|
|
"math/big"
|
2016-11-11 21:48:00 +07:00
|
|
|
"regexp"
|
2016-11-13 19:50:44 +07:00
|
|
|
"strings"
|
2016-11-11 21:48:00 +07:00
|
|
|
)
|
|
|
|
|
2016-11-17 00:15:36 +07:00
|
|
|
func readConfig(fname string) (DNSConfig, error) {
|
|
|
|
var conf DNSConfig
|
2016-11-16 20:31:40 +07:00
|
|
|
if _, err := toml.DecodeFile(fname, &conf); err != nil {
|
2016-11-17 00:15:36 +07:00
|
|
|
return DNSConfig{}, errors.New("Malformed configuration file")
|
2016-11-16 20:31:40 +07:00
|
|
|
}
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2016-11-13 19:50:44 +07:00
|
|
|
func SanitizeString(s string) string {
|
|
|
|
// URL safe base64 alphabet without padding as defined in ACME
|
|
|
|
re, err := regexp.Compile("[^A-Za-z\\-\\_0-9]+")
|
2016-11-11 21:48:00 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%v", err)
|
|
|
|
return ""
|
|
|
|
}
|
2016-11-13 19:50:44 +07:00
|
|
|
return re.ReplaceAllString(s, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GeneratePassword(length int) (string, error) {
|
|
|
|
ret := make([]byte, length)
|
|
|
|
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
|
|
|
|
alphalen := big.NewInt(int64(len(alphabet)))
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
c, err := rand.Int(rand.Reader, alphalen)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
r := int(c.Int64())
|
|
|
|
ret[i] = alphabet[r]
|
|
|
|
}
|
|
|
|
return string(ret), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SanitizeDomainQuestion(d string) string {
|
|
|
|
var dom string
|
2016-11-17 00:15:36 +07:00
|
|
|
suffix := DNSConf.General.Domain + "."
|
|
|
|
if strings.HasSuffix(d, suffix) {
|
|
|
|
dom = d[0 : len(d)-len(suffix)]
|
2016-11-13 19:50:44 +07:00
|
|
|
} else {
|
|
|
|
dom = d
|
|
|
|
}
|
|
|
|
return dom
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewACMETxt() (ACMETxt, error) {
|
2016-11-17 00:15:36 +07:00
|
|
|
var a = ACMETxt{}
|
2016-11-13 19:50:44 +07:00
|
|
|
password, err := GeneratePassword(40)
|
|
|
|
if err != nil {
|
|
|
|
return a, err
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
2016-11-13 19:50:44 +07:00
|
|
|
a.Username = uuid.NewV4()
|
|
|
|
a.Password = password
|
|
|
|
a.Subdomain = uuid.NewV4().String()
|
|
|
|
return a, nil
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|