ddns/backend.go

65 lines
1.2 KiB
Go
Raw Normal View History

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
2014-07-15 23:26:50 +02:00
// This function implements the PowerDNS-Pipe-Backend protocol and generates
// the response data it possible
func RunBackend(conn *RedisConnection) {
2014-07-15 23:41:54 +02:00
// handshake with PowerDNS
fmt.Printf("OK\tDDNS Go Backend\n")
bio := bufio.NewReader(os.Stdin)
for {
line, _, err := bio.ReadLine()
2014-07-15 23:26:50 +02:00
HandleErr(err)
HandleRequest(string(line), conn)
}
}
2014-07-15 23:26:50 +02:00
func HandleRequest(line string, conn *RedisConnection) {
2014-07-15 23:26:50 +02:00
defer fmt.Printf("END\n")
if Verbose {
fmt.Printf("LOG\t'%s'\n", line)
}
2014-07-15 23:41:54 +02:00
parts := strings.Split(line, "\t")
if len(parts) != 6 {
return
}
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
query_name := parts[1]
query_class := parts[2]
// query_type := parts[3] // TODO Handle SOA Requests
query_id := parts[4]
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
// get the host part of the fqdn
// pi.d.example.org -> pi
hostname := ""
if strings.HasSuffix(query_name, DdnsDomain) {
hostname = query_name[:len(query_name)-len(DdnsDomain)]
}
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
if hostname == "" || !conn.HostExist(hostname) {
return
}
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
host := conn.GetHost(hostname)
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
record := "A"
if !host.IsIPv4() {
record = "AAAA"
}
2014-07-15 23:26:50 +02:00
2014-07-15 23:41:54 +02:00
fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n",
query_name, query_class, record, query_id, host.Ip)
2014-07-15 23:26:50 +02:00
}