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"
|
2016-11-28 00:41:54 +07:00
|
|
|
"fmt"
|
2016-11-16 20:31:40 +07:00
|
|
|
"github.com/BurntSushi/toml"
|
2016-11-26 20:42:35 +07:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-11-28 02:21:38 +07:00
|
|
|
"github.com/miekg/dns"
|
2016-11-13 19:50:44 +07:00
|
|
|
"github.com/satori/go.uuid"
|
|
|
|
"math/big"
|
2016-11-28 02:21:38 +07:00
|
|
|
"os"
|
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-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
|
2016-11-28 02:21:38 +07:00
|
|
|
re, _ := regexp.Compile("[^A-Za-z\\-\\_0-9]+")
|
2016-11-13 19:50:44 +07:00
|
|
|
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
|
|
|
}
|
2016-11-28 00:41:54 +07:00
|
|
|
fmt.Printf("%s\n", dom)
|
2016-11-13 19:50:44 +07:00
|
|
|
return dom
|
|
|
|
}
|
|
|
|
|
2016-11-23 22:11:31 +07:00
|
|
|
func newACMETxt() (ACMETxt, error) {
|
2016-11-17 00:15:36 +07:00
|
|
|
var a = ACMETxt{}
|
2016-11-28 02:21:38 +07:00
|
|
|
password := generatePassword(40)
|
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
|
|
|
}
|
2016-11-23 22:11:31 +07:00
|
|
|
|
2016-11-28 02:21:38 +07:00
|
|
|
func setupLogging(format string, level string) {
|
2016-11-26 20:42:35 +07:00
|
|
|
if DNSConf.Logconfig.Format == "json" {
|
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
2016-11-23 22:11:31 +07:00
|
|
|
}
|
|
|
|
switch DNSConf.Logconfig.Level {
|
|
|
|
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 startDNS(listen string) *dns.Server {
|
|
|
|
// DNS server part
|
|
|
|
dns.HandleFunc(".", handleRequest)
|
|
|
|
server := &dns.Server{Addr: listen, Net: "udp"}
|
|
|
|
go func() {
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("%v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return server
|
|
|
|
}
|