2014-07-15 23:12:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"github.com/pboehm/ddns/connection"
|
|
|
|
"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
|
2014-07-15 23:12:27 +02:00
|
|
|
func RunBackend(conn *connection.RedisConnection) {
|
|
|
|
|
2014-07-15 23:41:54 +02:00
|
|
|
// handshake with PowerDNS
|
2014-07-15 23:12:27 +02:00
|
|
|
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:12:27 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 23:26:50 +02:00
|
|
|
|
|
|
|
func HandleRequest(line string, conn *connection.RedisConnection) {
|
|
|
|
defer fmt.Printf("END\n")
|
|
|
|
|
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
|
|
|
}
|