acme-dns/util.go

120 lines
2.7 KiB
Go
Raw Permalink Normal View History

2016-11-11 21:48:00 +07:00
package main
import (
2016-11-13 19:50:44 +07:00
"crypto/rand"
"errors"
"fmt"
2016-11-13 19:50:44 +07:00
"math/big"
"os"
2016-11-11 21:48:00 +07:00
"regexp"
2016-11-13 19:50:44 +07:00
"strings"
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
2016-11-11 21:48:00 +07:00
)
func jsonError(message string) []byte {
return []byte(fmt.Sprintf("{\"error\": \"%s\"}", message))
}
func fileIsAccessible(fname string) bool {
_, err := os.Stat(fname)
if err != nil {
return false
}
f, err := os.Open(fname)
if err != nil {
return false
}
f.Close()
return true
}
func readConfig(fname string) (DNSConfig, error) {
2016-11-17 00:15:36 +07:00
var conf DNSConfig
_, err := toml.DecodeFile(fname, &conf)
if err != nil {
// Return with config file parsing errors from toml package
return conf, err
}
return prepareConfig(conf)
}
// prepareConfig checks that mandatory values exist, and can be used to set default values in the future
func prepareConfig(conf DNSConfig) (DNSConfig, error) {
if conf.Database.Engine == "" {
return conf, errors.New("missing database configuration option \"engine\"")
}
if conf.Database.Connection == "" {
return conf, errors.New("missing database configuration option \"connection\"")
}
// Default values for options added to config to keep backwards compatibility with old config
if conf.API.ACMECacheDir == "" {
conf.API.ACMECacheDir = "api-certs"
}
return conf, nil
2016-11-16 20:31:40 +07:00
}
2016-11-23 22:11:31 +07:00
func sanitizeString(s string) string {
2016-11-13 19:50:44 +07:00
// URL safe base64 alphabet without padding as defined in ACME
re, _ := regexp.Compile(`[^A-Za-z\-\_0-9]+`)
2016-11-13 19:50:44 +07:00
return re.ReplaceAllString(s, "")
}
func sanitizeIPv6addr(s string) string {
// Remove brackets from IPv6 addresses, net.ParseCIDR needs this
re, _ := regexp.Compile(`[\[\]]+`)
return re.ReplaceAllString(s, "")
}
2016-11-28 02:21:38 +07:00
func generatePassword(length int) string {
2016-11-13 19:50:44 +07:00
ret := make([]byte, length)
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
alphalen := big.NewInt(int64(len(alphabet)))
for i := 0; i < length; i++ {
2016-11-28 02:21:38 +07:00
c, _ := rand.Int(rand.Reader, alphalen)
2016-11-13 19:50:44 +07:00
r := int(c.Int64())
ret[i] = alphabet[r]
}
2016-11-28 02:21:38 +07:00
return string(ret)
2016-11-13 19:50:44 +07:00
}
2016-11-23 22:11:31 +07:00
func sanitizeDomainQuestion(d string) string {
2016-11-28 00:41:54 +07:00
dom := strings.ToLower(d)
firstDot := strings.Index(d, ".")
if firstDot > 0 {
dom = dom[0:firstDot]
2016-11-13 19:50:44 +07:00
}
return dom
}
2016-11-28 02:21:38 +07:00
func setupLogging(format string, level string) {
2016-11-28 04:21:46 +07:00
if format == "json" {
2016-11-26 20:42:35 +07:00
log.SetFormatter(&log.JSONFormatter{})
2016-11-23 22:11:31 +07:00
}
2016-11-28 03:09:13 +07:00
switch level {
2016-11-23 22:11:31 +07:00
default:
2016-11-26 20:42:35 +07:00
log.SetLevel(log.WarnLevel)
case "debug":
log.SetLevel(log.DebugLevel)
2016-11-23 22:11:31 +07:00
case "info":
2016-11-26 20:42:35 +07:00
log.SetLevel(log.InfoLevel)
case "error":
log.SetLevel(log.ErrorLevel)
2016-11-23 22:11:31 +07:00
}
2016-11-26 20:42:35 +07:00
// TODO: file logging
2016-11-23 22:11:31 +07:00
}
2016-11-28 02:21:38 +07:00
func getIPListFromHeader(header string) []string {
iplist := []string{}
for _, v := range strings.Split(header, ",") {
if len(v) > 0 {
// Ignore empty values
iplist = append(iplist, strings.TrimSpace(v))
}
}
return iplist
}