acme-dns/main.go

88 lines
2.0 KiB
Go
Raw Normal View History

2016-11-11 21:48:00 +07:00
package main
import (
"fmt"
2016-11-16 19:56:49 +07:00
"github.com/iris-contrib/middleware/cors"
2016-11-11 21:48:00 +07:00
"github.com/kataras/iris"
"github.com/miekg/dns"
"github.com/op/go-logging"
"os"
)
// Logging config
var log = logging.MustGetLogger("acme-dns")
2016-11-23 23:07:38 +07:00
// DNSConf is global configuration struct
2016-11-17 00:15:36 +07:00
var DNSConf DNSConfig
2016-11-11 21:48:00 +07:00
2016-11-23 23:07:38 +07:00
// DB is used to access the database functions in acme-dns
var DB database
2016-11-11 21:48:00 +07:00
2016-11-23 23:07:38 +07:00
// RR holds the static DNS records
2016-11-11 21:48:00 +07:00
var RR Records
func main() {
2016-11-13 19:50:44 +07:00
// Read global config
2016-11-17 00:15:36 +07:00
configTmp, err := readConfig("config.cfg")
2016-11-11 21:48:00 +07:00
if err != nil {
2016-11-17 00:15:36 +07:00
fmt.Printf("Got error %v\n", DNSConf.Logconfig.File)
2016-11-11 21:48:00 +07:00
os.Exit(1)
}
2016-11-17 00:15:36 +07:00
DNSConf = configTmp
2016-11-23 22:11:31 +07:00
setupLogging()
2016-11-13 19:50:44 +07:00
// Read the default records in
2016-11-17 00:15:36 +07:00
RR.Parse(DNSConf.General.StaticRecords)
2016-11-11 21:48:00 +07:00
// Open database
2016-11-17 22:52:55 +07:00
err = DB.Init(DNSConf.Database.Engine, DNSConf.Database.Connection)
2016-11-11 21:48:00 +07:00
if err != nil {
log.Errorf("Could not open database [%v]", err)
os.Exit(1)
}
defer DB.DB.Close()
// DNS server part
dns.HandleFunc(".", handleRequest)
server := &dns.Server{Addr: ":53", Net: "udp"}
go func() {
err = server.ListenAndServe()
if err != nil {
log.Errorf("%v", err)
os.Exit(1)
}
}()
2016-11-13 19:50:44 +07:00
// API server and endpoints
2016-11-11 21:48:00 +07:00
api := iris.New()
2016-11-23 21:42:39 +07:00
api.Config.DisableBanner = true
2016-11-16 19:56:49 +07:00
crs := cors.New(cors.Options{
2016-11-17 00:15:36 +07:00
AllowedOrigins: DNSConf.API.CorsOrigins,
2016-11-16 19:56:49 +07:00
AllowedMethods: []string{"GET", "POST"},
OptionsPassthrough: false,
2016-11-17 00:15:36 +07:00
Debug: DNSConf.General.Debug,
2016-11-16 19:56:49 +07:00
})
api.Use(crs)
2016-11-23 23:07:38 +07:00
var ForceAuth = authMiddleware{}
api.Get("/register", webRegisterGet)
api.Post("/register", webRegisterPost)
api.Post("/update", ForceAuth.Serve, webUpdatePost)
2016-11-13 19:50:44 +07:00
// TODO: migrate to api.Serve(iris.LETSENCRYPTPROD("mydomain.com"))
2016-11-17 00:15:36 +07:00
switch DNSConf.API.TLS {
2016-11-13 19:50:44 +07:00
case "letsencrypt":
2016-11-17 00:15:36 +07:00
host := DNSConf.API.Domain + ":" + DNSConf.API.Port
2016-11-13 19:50:44 +07:00
api.Listen(host)
case "cert":
2016-11-17 00:15:36 +07:00
host := DNSConf.API.Domain + ":" + DNSConf.API.Port
api.ListenTLS(host, DNSConf.API.TLSCertFullchain, DNSConf.API.TLSCertPrivkey)
2016-11-13 19:50:44 +07:00
default:
2016-11-17 00:15:36 +07:00
host := DNSConf.API.Domain + ":" + DNSConf.API.Port
2016-11-13 19:50:44 +07:00
api.Listen(host)
2016-11-11 21:48:00 +07:00
}
2016-11-13 19:50:44 +07:00
if err != nil {
log.Errorf("Error in HTTP server [%v]", err)
2016-11-11 21:48:00 +07:00
}
log.Debugf("Shutting down...")
}