ddns/ddns.go

89 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
2017-07-02 22:58:07 +02:00
"flag"
"github.com/pboehm/ddns/backend"
"github.com/pboehm/ddns/config"
"github.com/pboehm/ddns/hosts"
"github.com/pboehm/ddns/web"
"log"
"os"
2014-07-12 23:37:01 +02:00
"strings"
)
const (
CmdBackend string = "backend"
CmdWeb string = "web"
)
var serviceConfig *config.Config = &config.Config{}
func init() {
flag.StringVar(&serviceConfig.Domain, "domain", "",
"The subdomain which should be handled by DDNS")
2014-07-15 23:41:54 +02:00
flag.StringVar(&serviceConfig.Listen, "listen", ":8080",
2014-07-15 23:41:54 +02:00
"Which socket should the web service use to bind itself")
2014-07-16 14:14:16 +02:00
flag.StringVar(&serviceConfig.RedisHost, "redis", ":6379",
2014-07-16 14:14:16 +02:00
"The Redis socket that should be used")
flag.StringVar(&serviceConfig.SOAFqdn, "soa_fqdn", "",
"The FQDN of the DNS server which is returned as a SOA record")
flag.IntVar(&serviceConfig.HostExpirationDays, "expiration-days", 10,
"The number of days after a host is released when it is not updated")
flag.BoolVar(&serviceConfig.Verbose, "verbose", false,
"Be more verbose")
}
func usage() {
log.Fatal("Usage: ./ddns [backend|web]")
}
func validateCommandArgs(cmd string) {
if serviceConfig.Domain == "" {
2014-07-15 21:48:15 +02:00
log.Fatal("You have to supply the domain via --domain=DOMAIN")
} else if !strings.HasPrefix(serviceConfig.Domain, ".") {
2014-07-15 21:48:15 +02:00
// get the domain in the right format
serviceConfig.Domain = "." + serviceConfig.Domain
2014-07-15 21:48:15 +02:00
}
if cmd == CmdBackend {
if serviceConfig.SOAFqdn == "" {
log.Fatal("You have to supply the server FQDN via --soa_fqdn=FQDN")
}
}
2014-07-15 21:48:15 +02:00
}
func prepareForExecution() string {
2014-07-15 21:48:15 +02:00
flag.Parse()
2014-07-15 21:48:15 +02:00
if len(flag.Args()) != 1 {
usage()
}
2014-07-15 21:48:15 +02:00
cmd := flag.Args()[0]
validateCommandArgs(cmd)
return cmd
}
func main() {
cmd := prepareForExecution()
redis := hosts.NewRedisBackend(serviceConfig)
defer redis.Close()
switch cmd {
case CmdBackend:
backend.NewPowerDnsBackend(serviceConfig, redis, os.Stdin, os.Stdout).Run()
case CmdWeb:
web.NewWebService(serviceConfig, redis).Run()
default:
usage()
}
}