ddns/ddns.go

93 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
2014-07-15 21:48:15 +02:00
"flag"
"log"
2014-07-12 23:37:01 +02:00
"strings"
)
func HandleErr(err error) {
if err != nil {
log.Fatal(err)
}
}
const (
CmdBackend string = "backend"
CmdWeb string = "web"
)
var (
2014-07-15 23:41:54 +02:00
DdnsDomain string
DdnsWebListenSocket string
2014-07-16 14:14:16 +02:00
DdnsRedisHost string
DdnsSoaFqdn string
Verbose bool
)
func init() {
flag.StringVar(&DdnsDomain, "domain", "",
"The subdomain which should be handled by DDNS")
2014-07-15 23:41:54 +02:00
flag.StringVar(&DdnsWebListenSocket, "listen", ":8080",
"Which socket should the web service use to bind itself")
2014-07-16 14:14:16 +02:00
flag.StringVar(&DdnsRedisHost, "redis", ":6379",
"The Redis socket that should be used")
flag.StringVar(&DdnsSoaFqdn, "soa_fqdn", "",
"The FQDN of the DNS server which is returned as a SOA record")
flag.BoolVar(&Verbose, "verbose", false,
"Be more verbose")
}
func ValidateCommandArgs(cmd string) {
2014-07-15 21:48:15 +02:00
if DdnsDomain == "" {
log.Fatal("You have to supply the domain via --domain=DOMAIN")
} else if !strings.HasPrefix(DdnsDomain, ".") {
// get the domain in the right format
DdnsDomain = "." + DdnsDomain
}
if cmd == CmdBackend {
if DdnsSoaFqdn == "" {
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() {
2014-07-15 23:41:54 +02:00
cmd := PrepareForExecution()
conn := OpenConnection(DdnsRedisHost)
defer conn.Close()
switch cmd {
case CmdBackend:
log.Printf("Starting PDNS Backend\n")
RunBackend(conn)
case CmdWeb:
log.Printf("Starting Web Service\n")
RunWebService(conn)
default:
usage()
}
}
func usage() {
log.Fatal("Usage: ./ddns [backend|web]")
}