From c6bf995d8126845d2170153c614a4797baacaf06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20B=C3=B6hm?= Date: Sat, 12 Jul 2014 23:37:01 +0200 Subject: [PATCH] First working backend implementation --- connection/connection.go | 20 ++++++++++---------- ddns.go | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/connection/connection.go b/connection/connection.go index 0fe0af4..8f3bb11 100644 --- a/connection/connection.go +++ b/connection/connection.go @@ -5,8 +5,8 @@ import ( "fmt" "github.com/garyburd/redigo/redis" "log" - "time" "strings" + "time" ) const HostExpirationSeconds int = 10 * 24 * 60 * 60 // 10 Days @@ -33,12 +33,12 @@ type RedisConnection struct { func (self *RedisConnection) GetHost(name string) *Host { host := Host{Hostname: name} - if self.HostExist(name) { - data, err := redis.Values(self.Do("HGETALL", host.Hostname)) - HandleErr(err) + if self.HostExist(name) { + data, err := redis.Values(self.Do("HGETALL", host.Hostname)) + HandleErr(err) - HandleErr(redis.ScanStruct(data, &host)) - } + HandleErr(redis.ScanStruct(data, &host)) + } return &host } @@ -74,9 +74,9 @@ func (self *Host) GenerateAndSetToken() { // Returns true when this host has a IPv4 Address and false if IPv6 func (self *Host) IsIPv4() bool { - if strings.Contains(self.Ip, ".") { - return true - } + if strings.Contains(self.Ip, ".") { + return true + } - return False + return false } diff --git a/ddns.go b/ddns.go index dedbe5c..e256ae2 100644 --- a/ddns.go +++ b/ddns.go @@ -1,12 +1,14 @@ package main import ( + "bufio" "fmt" "github.com/gin-gonic/gin" "github.com/pboehm/ddns/connection" "log" "net" "os" + "strings" ) func HandleErr(err error) { @@ -16,7 +18,43 @@ func HandleErr(err error) { } func RunBackend() { - /* code */ + conn := connection.OpenConnection() + defer conn.Close() + + fmt.Printf("OK\tDDNS Go Backend\n") + + bio := bufio.NewReader(os.Stdin) + + for { + line, _, err := bio.ReadLine() + HandleErr(err) + + parts := strings.Split(string(line), "\t") + if len(parts) == 6 { + query_name := parts[1] + fqdn_parts := strings.Split(query_name, ".") + + query_class := parts[2] + query_id := parts[4] + + if len(fqdn_parts) > 0 { + if conn.HostExist(fqdn_parts[0]) { + host := conn.GetHost(fqdn_parts[0]) + + record := "A" + if !host.IsIPv4() { + record = "AAAA" + } + + fmt.Printf("DATA\t%s\t%s\t%s\t10\t%s\t%s\n", + query_name, query_class, record, query_id, host.Ip) + } + } + } + + fmt.Printf("END\n") + } + } func RunWebService() {