2016-11-28 18:20:19 +07:00
|
|
|
//+build !test
|
|
|
|
|
2016-11-11 21:48:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-11-15 04:54:29 +07:00
|
|
|
"crypto/tls"
|
|
|
|
stdlog "log"
|
|
|
|
"net/http"
|
2016-11-11 21:48:00 +07:00
|
|
|
"os"
|
2017-01-30 17:19:22 +07:00
|
|
|
|
2017-11-15 04:54:29 +07:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"github.com/rs/cors"
|
2017-08-02 23:25:27 +07:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-11-15 04:54:29 +07:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
2016-11-11 21:48:00 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-11-13 19:50:44 +07:00
|
|
|
// Read global config
|
2017-11-14 05:42:30 +07:00
|
|
|
if fileExists("/etc/acme-dns/config.cfg") {
|
|
|
|
Config = readConfig("/etc/acme-dns/config.cfg")
|
2017-11-15 04:54:29 +07:00
|
|
|
log.WithFields(log.Fields{"file": "/etc/acme-dns/config.cfg"}).Info("Using config file")
|
|
|
|
|
2017-11-14 05:42:30 +07:00
|
|
|
} else {
|
2017-11-15 04:54:29 +07:00
|
|
|
log.WithFields(log.Fields{"file": "./config.cfg"}).Info("Using config file")
|
2017-11-14 05:42:30 +07:00
|
|
|
Config = readConfig("config.cfg")
|
|
|
|
}
|
2016-11-23 22:11:31 +07:00
|
|
|
|
2017-11-14 05:42:30 +07:00
|
|
|
setupLogging(Config.Logconfig.Format, Config.Logconfig.Level)
|
2016-11-13 19:50:44 +07:00
|
|
|
|
|
|
|
// Read the default records in
|
2017-11-14 05:42:30 +07:00
|
|
|
RR.Parse(Config.General)
|
2016-11-11 21:48:00 +07:00
|
|
|
|
|
|
|
// Open database
|
2016-11-28 04:21:46 +07:00
|
|
|
newDB := new(acmedb)
|
2017-11-14 05:42:30 +07:00
|
|
|
err := newDB.Init(Config.Database.Engine, Config.Database.Connection)
|
2016-11-11 21:48:00 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not open database [%v]", err)
|
|
|
|
os.Exit(1)
|
2018-01-22 14:53:07 +07:00
|
|
|
} else {
|
|
|
|
log.Info("Connected to database")
|
2016-11-11 21:48:00 +07:00
|
|
|
}
|
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
|
2017-11-14 05:42:30 +07:00
|
|
|
startDNS(Config.General.Listen, Config.General.Proto)
|
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() {
|
2017-11-15 04:54:29 +07:00
|
|
|
// Setup http logger
|
|
|
|
logger := log.New()
|
|
|
|
logwriter := logger.Writer()
|
|
|
|
defer logwriter.Close()
|
|
|
|
api := httprouter.New()
|
|
|
|
c := cors.New(cors.Options{
|
2017-11-14 05:42:30 +07:00
|
|
|
AllowedOrigins: Config.API.CorsOrigins,
|
2016-11-28 18:09:10 +07:00
|
|
|
AllowedMethods: []string{"GET", "POST"},
|
|
|
|
OptionsPassthrough: false,
|
2017-11-14 05:42:30 +07:00
|
|
|
Debug: Config.General.Debug,
|
2017-11-15 04:54:29 +07:00
|
|
|
})
|
2017-11-16 02:35:35 +07:00
|
|
|
if Config.General.Debug {
|
|
|
|
// Logwriter for saner log output
|
|
|
|
c.Log = stdlog.New(logwriter, "", 0)
|
|
|
|
}
|
2017-11-15 04:54:29 +07:00
|
|
|
api.POST("/register", webRegisterPost)
|
|
|
|
api.POST("/update", Auth(webUpdatePost))
|
|
|
|
|
|
|
|
host := Config.API.IP + ":" + Config.API.Port
|
|
|
|
|
|
|
|
cfg := &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2017-11-13 04:40:15 +07:00
|
|
|
|
2017-11-14 05:42:30 +07:00
|
|
|
switch Config.API.TLS {
|
2016-11-28 18:09:10 +07:00
|
|
|
case "letsencrypt":
|
2017-11-15 04:54:29 +07:00
|
|
|
m := autocert.Manager{
|
|
|
|
Cache: autocert.DirCache("api-certs"),
|
|
|
|
Prompt: autocert.AcceptTOS,
|
|
|
|
HostPolicy: autocert.HostWhitelist(Config.API.Domain),
|
|
|
|
}
|
2018-02-01 15:53:34 +07:00
|
|
|
autocerthost := Config.API.IP + ":" + Config.API.AutocertPort
|
|
|
|
log.WithFields(log.Fields{"autocerthost": autocerthost, "domain": Config.API.Domain}).Debug("Opening HTTP port for autocert")
|
|
|
|
go http.ListenAndServe(autocerthost, m.HTTPHandler(nil))
|
2017-11-15 04:54:29 +07:00
|
|
|
cfg.GetCertificate = m.GetCertificate
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: host,
|
|
|
|
Handler: c.Handler(api),
|
|
|
|
TLSConfig: cfg,
|
|
|
|
ErrorLog: stdlog.New(logwriter, "", 0),
|
|
|
|
}
|
2018-02-01 15:53:34 +07:00
|
|
|
log.WithFields(log.Fields{"host": host, "domain": Config.API.Domain}).Info("Listening HTTPS, using certificate from autocert")
|
2017-11-15 04:54:29 +07:00
|
|
|
log.Fatal(srv.ListenAndServeTLS("", ""))
|
2016-11-28 18:09:10 +07:00
|
|
|
case "cert":
|
2017-11-15 04:54:29 +07:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: host,
|
|
|
|
Handler: c.Handler(api),
|
|
|
|
TLSConfig: cfg,
|
|
|
|
ErrorLog: stdlog.New(logwriter, "", 0),
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{"host": host}).Info("Listening HTTPS")
|
|
|
|
log.Fatal(srv.ListenAndServeTLS(Config.API.TLSCertFullchain, Config.API.TLSCertPrivkey))
|
2016-11-28 18:09:10 +07:00
|
|
|
default:
|
2017-11-15 04:54:29 +07:00
|
|
|
log.WithFields(log.Fields{"host": host}).Info("Listening HTTP")
|
|
|
|
log.Fatal(http.ListenAndServe(host, c.Handler(api)))
|
2016-11-28 18:09:10 +07:00
|
|
|
}
|
|
|
|
}
|