mirror of
https://github.com/joohoi/acme-dns.git
synced 2024-12-22 21:33:51 +07:00
856cc05881
* Added supplementary errorichecking * After running util.go through gofmt * Updated main and util * Minor updates to main and util * Slight refactoring * Add tests
107 lines
2.2 KiB
Go
107 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"math/big"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/miekg/dns"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
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) {
|
|
var conf DNSConfig
|
|
_, err := toml.DecodeFile(fname, &conf)
|
|
return conf, err
|
|
}
|
|
|
|
func sanitizeString(s string) string {
|
|
// URL safe base64 alphabet without padding as defined in ACME
|
|
re, _ := regexp.Compile("[^A-Za-z\\-\\_0-9]+")
|
|
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, "")
|
|
}
|
|
|
|
func generatePassword(length int) string {
|
|
ret := make([]byte, length)
|
|
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_"
|
|
alphalen := big.NewInt(int64(len(alphabet)))
|
|
for i := 0; i < length; i++ {
|
|
c, _ := rand.Int(rand.Reader, alphalen)
|
|
r := int(c.Int64())
|
|
ret[i] = alphabet[r]
|
|
}
|
|
return string(ret)
|
|
}
|
|
|
|
func sanitizeDomainQuestion(d string) string {
|
|
dom := strings.ToLower(d)
|
|
firstDot := strings.Index(d, ".")
|
|
if firstDot > 0 {
|
|
dom = dom[0:firstDot]
|
|
}
|
|
return dom
|
|
}
|
|
|
|
func setupLogging(format string, level string) {
|
|
if format == "json" {
|
|
log.SetFormatter(&log.JSONFormatter{})
|
|
}
|
|
switch level {
|
|
default:
|
|
log.SetLevel(log.WarnLevel)
|
|
case "debug":
|
|
log.SetLevel(log.DebugLevel)
|
|
case "info":
|
|
log.SetLevel(log.InfoLevel)
|
|
case "error":
|
|
log.SetLevel(log.ErrorLevel)
|
|
}
|
|
// TODO: file logging
|
|
}
|
|
|
|
func startDNS(listen string, proto string) *dns.Server {
|
|
// DNS server part
|
|
dns.HandleFunc(".", handleRequest)
|
|
server := &dns.Server{Addr: listen, Net: proto}
|
|
go server.ListenAndServe()
|
|
return server
|
|
}
|
|
|
|
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
|
|
}
|