2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-11-26 20:42:35 +07:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-11-28 18:09:10 +07:00
|
|
|
"github.com/iris-contrib/middleware/cors"
|
|
|
|
"github.com/kataras/iris"
|
2016-11-11 21:48:00 +07:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-11-13 19:50:44 +07:00
|
|
|
// Read global config
|
2016-11-28 04:21:46 +07:00
|
|
|
configTmp := readConfig("config.cfg")
|
2016-11-17 00:15:36 +07:00
|
|
|
DNSConf = configTmp
|
2016-11-23 22:11:31 +07:00
|
|
|
|
2016-11-28 02:21:38 +07:00
|
|
|
setupLogging(DNSConf.Logconfig.Format, DNSConf.Logconfig.Level)
|
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-28 04:21:46 +07:00
|
|
|
newDB := new(acmedb)
|
|
|
|
err := newDB.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)
|
|
|
|
}
|
2016-11-28 04:21:46 +07:00
|
|
|
DB = newDB
|
|
|
|
defer DB.Close()
|
2016-11-11 21:48:00 +07:00
|
|
|
|
2016-11-28 02:21:38 +07:00
|
|
|
// DNS server
|
|
|
|
startDNS(DNSConf.General.Listen)
|
2016-11-11 21:48:00 +07:00
|
|
|
|
2016-11-28 03:09:13 +07:00
|
|
|
// HTTP API
|
|
|
|
startHTTPAPI()
|
|
|
|
|
2016-11-11 21:48:00 +07:00
|
|
|
log.Debugf("Shutting down...")
|
|
|
|
}
|
2016-11-28 18:09:10 +07:00
|
|
|
|
|
|
|
func startHTTPAPI() {
|
|
|
|
api := iris.New()
|
|
|
|
api.Config.DisableBanner = true
|
|
|
|
crs := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: DNSConf.API.CorsOrigins,
|
|
|
|
AllowedMethods: []string{"GET", "POST"},
|
|
|
|
OptionsPassthrough: false,
|
|
|
|
Debug: DNSConf.General.Debug,
|
|
|
|
})
|
|
|
|
api.Use(crs)
|
|
|
|
var ForceAuth = authMiddleware{}
|
|
|
|
api.Get("/register", webRegisterGet)
|
|
|
|
api.Post("/register", webRegisterPost)
|
|
|
|
api.Post("/update", ForceAuth.Serve, webUpdatePost)
|
|
|
|
switch DNSConf.API.TLS {
|
|
|
|
case "letsencrypt":
|
|
|
|
listener, err := iris.LETSENCRYPTPROD(DNSConf.API.Domain)
|
|
|
|
err = api.Serve(listener)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error in HTTP server [%v]", err)
|
|
|
|
}
|
|
|
|
case "cert":
|
|
|
|
host := DNSConf.API.Domain + ":" + DNSConf.API.Port
|
|
|
|
api.ListenTLS(host, DNSConf.API.TLSCertFullchain, DNSConf.API.TLSCertPrivkey)
|
|
|
|
default:
|
|
|
|
host := DNSConf.API.Domain + ":" + DNSConf.API.Port
|
|
|
|
api.Listen(host)
|
|
|
|
}
|
|
|
|
}
|