Refactored Backend

This commit is contained in:
Philipp Böhm 2014-07-15 23:26:50 +02:00
parent 86f922a472
commit 73ea58378a
2 changed files with 41 additions and 34 deletions

View File

@ -8,19 +8,35 @@ import (
"strings" "strings"
) )
// This function implements the PowerDNS-Pipe-Backend protocol and generates
// the response data it possible
func RunBackend(conn *connection.RedisConnection) { func RunBackend(conn *connection.RedisConnection) {
// handshake with PowerDNS
fmt.Printf("OK\tDDNS Go Backend\n") fmt.Printf("OK\tDDNS Go Backend\n")
bio := bufio.NewReader(os.Stdin) bio := bufio.NewReader(os.Stdin)
for { for {
line, _, err := bio.ReadLine() line, _, err := bio.ReadLine()
HandleErr(err)
parts := strings.Split(string(line), "\t") HandleErr(err)
if len(parts) == 6 { HandleRequest(string(line), conn)
}
}
func HandleRequest(line string, conn *connection.RedisConnection) {
defer fmt.Printf("END\n")
parts := strings.Split(line, "\t")
if len(parts) != 6 {
return
}
query_name := parts[1] query_name := parts[1]
query_class := parts[2]
// query_type := parts[3] // TODO Handle SOA Requests
query_id := parts[4]
// get the host part of the fqdn // get the host part of the fqdn
// pi.d.example.org -> pi // pi.d.example.org -> pi
@ -29,13 +45,10 @@ func RunBackend(conn *connection.RedisConnection) {
hostname = query_name[:len(query_name)-len(DdnsDomain)] hostname = query_name[:len(query_name)-len(DdnsDomain)]
} }
query_class := parts[2] if hostname == "" || ! conn.HostExist(hostname) {
// query_type := parts[3] return
query_id := parts[4] }
if hostname != "" {
// check for existance and create response
if conn.HostExist(hostname) {
host := conn.GetHost(hostname) host := conn.GetHost(hostname)
record := "A" record := "A"
@ -46,9 +59,3 @@ func RunBackend(conn *connection.RedisConnection) {
fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n",
query_name, query_class, record, query_id, host.Ip) query_name, query_class, record, query_id, host.Ip)
} }
}
}
fmt.Printf("END\n")
}
}

View File

@ -44,7 +44,7 @@ func PrepareForExecution() string {
} }
func main() { func main() {
cmd = PrepareForExecution() cmd := PrepareForExecution()
conn := connection.OpenConnection() conn := connection.OpenConnection()
defer conn.Close() defer conn.Close()