2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-13 19:50:44 +07:00
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
2016-11-11 21:48:00 +07:00
|
|
|
"regexp"
|
2016-11-13 19:50:44 +07:00
|
|
|
"strings"
|
2016-12-02 22:04:16 +07:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2017-08-02 23:25:27 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-02 22:04:16 +07:00
|
|
|
"github.com/miekg/dns"
|
2016-11-11 21:48:00 +07:00
|
|
|
)
|
|
|
|
|
2016-11-28 04:21:46 +07:00
|
|
|
func readConfig(fname string) DNSConfig {
|
2016-11-17 00:15:36 +07:00
|
|
|
var conf DNSConfig
|
2016-11-28 04:21:46 +07:00
|
|
|
// Practically never errors
|
|
|
|
_, _ = toml.DecodeFile(fname, &conf)
|
|
|
|
return conf
|
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
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
2016-11-29 03:46:24 +07:00
|
|
|
func startDNS(listen string, proto string) *dns.Server {
|
2016-11-28 02:21:38 +07:00
|
|
|
// DNS server part
|
|
|
|
dns.HandleFunc(".", handleRequest)
|
2016-11-29 03:46:24 +07:00
|
|
|
server := &dns.Server{Addr: listen, Net: proto}
|
|
|
|
go server.ListenAndServe()
|
2016-11-28 02:21:38 +07:00
|
|
|
return server
|
|
|
|
}
|
2016-12-02 22:04:16 +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
|
|
|
|
}
|