refactored web component

This commit is contained in:
Philipp Böhm 2014-07-15 23:41:54 +02:00
parent 73ea58378a
commit 3015b873cb
3 changed files with 61 additions and 44 deletions

View File

@ -12,7 +12,7 @@ import (
// the response data it possible // the response data it possible
func RunBackend(conn *connection.RedisConnection) { func RunBackend(conn *connection.RedisConnection) {
// handshake with PowerDNS // 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)
@ -28,34 +28,34 @@ func RunBackend(conn *connection.RedisConnection) {
func HandleRequest(line string, conn *connection.RedisConnection) { func HandleRequest(line string, conn *connection.RedisConnection) {
defer fmt.Printf("END\n") defer fmt.Printf("END\n")
parts := strings.Split(line, "\t") parts := strings.Split(line, "\t")
if len(parts) != 6 { if len(parts) != 6 {
return return
} }
query_name := parts[1] query_name := parts[1]
query_class := parts[2] query_class := parts[2]
// query_type := parts[3] // TODO Handle SOA Requests // query_type := parts[3] // TODO Handle SOA Requests
query_id := parts[4] 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
hostname := "" hostname := ""
if strings.HasSuffix(query_name, DdnsDomain) { if strings.HasSuffix(query_name, DdnsDomain) {
hostname = query_name[:len(query_name)-len(DdnsDomain)] hostname = query_name[:len(query_name)-len(DdnsDomain)]
} }
if hostname == "" || ! conn.HostExist(hostname) { if hostname == "" || !conn.HostExist(hostname) {
return return
} }
host := conn.GetHost(hostname) host := conn.GetHost(hostname)
record := "A" record := "A"
if !host.IsIPv4() { if !host.IsIPv4() {
record = "AAAA" record = "AAAA"
} }
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)
} }

View File

@ -14,12 +14,16 @@ func HandleErr(err error) {
} }
var ( var (
DdnsDomain string DdnsDomain string
DdnsWebListenSocket string
) )
func init() { func init() {
flag.StringVar(&DdnsDomain, "domain", "", flag.StringVar(&DdnsDomain, "domain", "",
"The subdomain which should be handled by DDNS") "The subdomain which should be handled by DDNS")
flag.StringVar(&DdnsWebListenSocket, "listen", ":8080",
"Which socket should the web service use to bind itself")
} }
func ValidateCommandArgs() { func ValidateCommandArgs() {
@ -44,7 +48,7 @@ func PrepareForExecution() string {
} }
func main() { func main() {
cmd := PrepareForExecution() cmd := PrepareForExecution()
conn := connection.OpenConnection() conn := connection.OpenConnection()
defer conn.Close() defer conn.Close()

47
web.go
View File

@ -10,16 +10,8 @@ import (
) )
func RunWebService(conn *connection.RedisConnection) { func RunWebService(conn *connection.RedisConnection) {
// Add index template from bindata
index_content, err := Asset("templates/index.html")
HandleErr(err)
html, err := template.New("index.html").Parse(string(index_content))
HandleErr(err)
r := gin.Default() r := gin.Default()
r.HTMLTemplates = html r.HTMLTemplates = BuildTemplate()
r.GET("/", func(g *gin.Context) { r.GET("/", func(g *gin.Context) {
g.HTML(200, "index.html", gin.H{"domain": DdnsDomain}) g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
@ -37,7 +29,9 @@ func RunWebService(conn *connection.RedisConnection) {
hostname := c.Params.ByName("hostname") hostname := c.Params.ByName("hostname")
if conn.HostExist(hostname) { if conn.HostExist(hostname) {
c.String(403, "This hostname has already been registered.") c.JSON(403, gin.H{
"error": "This hostname has already been registered.",
})
return return
} }
@ -58,31 +52,39 @@ func RunWebService(conn *connection.RedisConnection) {
token := c.Params.ByName("token") token := c.Params.ByName("token")
if !conn.HostExist(hostname) { if !conn.HostExist(hostname) {
c.String(404, c.JSON(404, gin.H{
"This hostname has not been registered or is expired.") "error": "This hostname has not been registered or is expired.",
})
return return
} }
host := conn.GetHost(hostname) host := conn.GetHost(hostname)
if host.Token != token { if host.Token != token {
c.String(403, c.JSON(403, gin.H{
"You have supplied the wrong token to manipulate this host") "error": "You have supplied the wrong token to manipulate this host",
})
return return
} }
ip, err := GetRemoteAddr(c.Req) ip, err := GetRemoteAddr(c.Req)
if err != nil { if err != nil {
c.String(500, "Your sender IP address is not in the right format") c.JSON(400, gin.H{
"error": "Your sender IP address is not in the right format",
})
return
} }
host.Ip = ip host.Ip = ip
conn.SaveHost(host) conn.SaveHost(host)
c.String(200, fmt.Sprintf("Your current IP is %s", ip)) c.JSON(200, gin.H{
"current_ip": ip,
"status": "Successfuly updated",
})
}) })
r.Run(":8080") r.Run(DdnsWebListenSocket)
} }
// Get the Remote Address of the client. At First we try to get the // Get the Remote Address of the client. At First we try to get the
@ -98,3 +100,14 @@ func GetRemoteAddr(req *http.Request) (string, error) {
return ip, err return ip, err
} }
} }
// Get index template from bindata
func BuildTemplate() *template.Template {
index_content, err := Asset("templates/index.html")
HandleErr(err)
html, err := template.New("index.html").Parse(string(index_content))
HandleErr(err)
return html
}