Commit 10cfbc65 authored by ale's avatar ale

Drop redirectord binary

parent fa38d911
Pipeline #2699 failed with stages
in 1 minute and 38 seconds
package main
import (
"errors"
"flag"
"fmt"
"log"
"strings"
"net"
_ "net/http/pprof"
"git.autistici.org/ale/autoradio"
"git.autistici.org/ale/autoradio/fe"
"git.autistici.org/ale/autoradio/instrumentation"
"git.autistici.org/ale/autoradio/util"
)
var (
domain = flag.String("domain", "", "DNS domain to serve")
publicIPs = util.IPList("ip", "Public IP for this machine (may be specified more than once). If unset, the program will try to resolve the local hostname, or it will fall back to inspecting network devices.")
dnsPort = flag.Int("dns-port", 53, "DNS port")
httpPort = flag.Int("http-port", 80, "HTTP port")
staticDir = flag.String("static-dir", "/usr/share/autoradio/htdocs/static", "Static content directory")
templateDir = flag.String("template-dir", "/usr/share/autoradio/htdocs/templates", "HTML templates directory")
lbPolicy = flag.String("lb-policy", "listeners_available,listeners_score,weighted", "Load balancing rules specification (see godoc documentation for details)")
nameservers = flag.String("nameservers", "", "Comma-separated list of name servers (not IPs) for the zone specified in --domain")
redirectMap = flag.String("redirect-map", "", "File containing a list of source path / target redirects, space-separated, one per line")
// Default DNS TTL (seconds).
dnsTtl = 5
)
func getFQDN(ips []net.IP) (string, error) {
for _, ip := range ips {
if names, err := net.LookupAddr(ip.String()); err == nil && len(names) > 0 {
// This is a pretty weak criteria for qualification.
name := strings.TrimSuffix(names[0], ".")
if strings.Contains(name, ".") {
return names[0], nil
}
}
}
return "", errors.New("reverse resolution failed")
}
func main() {
log.SetFlags(0)
flag.Parse()
if *domain == "" {
log.Fatal("Must specify --domain")
}
if err := util.DetectPublicNetworkParams(publicIPs, nil, nil); err != nil {
log.Fatal(err)
}
instrumentation.NewCounter("redirectord.restarts").Incr()
client := autoradio.NewClient(autoradio.NewEtcdClient(false))
// If no nameservers are specified, use the fqdn of the local
// host. It is not going to provide a lot of reliability for
// clients that cache the authoritative NS records for long,
// but at least it will work.
var ns []string
if *nameservers != "" {
ns = strings.Split(*nameservers, ",")
} else {
fqdn, err := getFQDN(*publicIPs)
if err != nil {
log.Fatal("Could not determine fully-qualified name of local host, and --nameservers is not specified")
}
log.Printf("autodetected fqdn %s", fqdn)
ns = []string{fqdn}
}
dnsRed := fe.NewDNSRedirector(client, *domain, *publicIPs, dnsTtl, ns)
dnsRed.Start(fmt.Sprintf(":%d", *dnsPort))
httpRed, err := fe.NewHTTPRedirector(client, *domain, *lbPolicy, *staticDir, *templateDir)
if err != nil {
log.Fatal(err)
}
if *redirectMap != "" {
if err := httpRed.LoadStaticRedirects(*redirectMap); err != nil {
// An error loading the redirect map should not be fatal.
log.Printf("Warning: could not load static redirect map: %v", err)
}
}
httpRed.Run(fmt.Sprintf(":%d", *httpPort))
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment