refactored web component
This commit is contained in:
parent
73ea58378a
commit
3015b873cb
50
backend.go
50
backend.go
|
@ -12,7 +12,7 @@ import (
|
|||
// the response data it possible
|
||||
func RunBackend(conn *connection.RedisConnection) {
|
||||
|
||||
// handshake with PowerDNS
|
||||
// handshake with PowerDNS
|
||||
fmt.Printf("OK\tDDNS Go Backend\n")
|
||||
|
||||
bio := bufio.NewReader(os.Stdin)
|
||||
|
@ -28,34 +28,34 @@ func RunBackend(conn *connection.RedisConnection) {
|
|||
func HandleRequest(line string, conn *connection.RedisConnection) {
|
||||
defer fmt.Printf("END\n")
|
||||
|
||||
parts := strings.Split(line, "\t")
|
||||
if len(parts) != 6 {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(line, "\t")
|
||||
if len(parts) != 6 {
|
||||
return
|
||||
}
|
||||
|
||||
query_name := parts[1]
|
||||
query_class := parts[2]
|
||||
// query_type := parts[3] // TODO Handle SOA Requests
|
||||
query_id := parts[4]
|
||||
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
|
||||
// pi.d.example.org -> pi
|
||||
hostname := ""
|
||||
if strings.HasSuffix(query_name, DdnsDomain) {
|
||||
hostname = query_name[:len(query_name)-len(DdnsDomain)]
|
||||
}
|
||||
// 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)]
|
||||
}
|
||||
|
||||
if hostname == "" || ! conn.HostExist(hostname) {
|
||||
return
|
||||
}
|
||||
if hostname == "" || !conn.HostExist(hostname) {
|
||||
return
|
||||
}
|
||||
|
||||
host := conn.GetHost(hostname)
|
||||
host := conn.GetHost(hostname)
|
||||
|
||||
record := "A"
|
||||
if !host.IsIPv4() {
|
||||
record = "AAAA"
|
||||
}
|
||||
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("DATA\t%s\t%s\t%s\t10\t%s\t%s\n",
|
||||
query_name, query_class, record, query_id, host.Ip)
|
||||
}
|
||||
|
|
8
ddns.go
8
ddns.go
|
@ -14,12 +14,16 @@ func HandleErr(err error) {
|
|||
}
|
||||
|
||||
var (
|
||||
DdnsDomain string
|
||||
DdnsDomain string
|
||||
DdnsWebListenSocket string
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&DdnsDomain, "domain", "",
|
||||
"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() {
|
||||
|
@ -44,7 +48,7 @@ func PrepareForExecution() string {
|
|||
}
|
||||
|
||||
func main() {
|
||||
cmd := PrepareForExecution()
|
||||
cmd := PrepareForExecution()
|
||||
|
||||
conn := connection.OpenConnection()
|
||||
defer conn.Close()
|
||||
|
|
47
web.go
47
web.go
|
@ -10,16 +10,8 @@ import (
|
|||
)
|
||||
|
||||
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.HTMLTemplates = html
|
||||
r.HTMLTemplates = BuildTemplate()
|
||||
|
||||
r.GET("/", func(g *gin.Context) {
|
||||
g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
|
||||
|
@ -37,7 +29,9 @@ func RunWebService(conn *connection.RedisConnection) {
|
|||
hostname := c.Params.ByName("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
|
||||
}
|
||||
|
||||
|
@ -58,31 +52,39 @@ func RunWebService(conn *connection.RedisConnection) {
|
|||
token := c.Params.ByName("token")
|
||||
|
||||
if !conn.HostExist(hostname) {
|
||||
c.String(404,
|
||||
"This hostname has not been registered or is expired.")
|
||||
c.JSON(404, gin.H{
|
||||
"error": "This hostname has not been registered or is expired.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
host := conn.GetHost(hostname)
|
||||
|
||||
if host.Token != token {
|
||||
c.String(403,
|
||||
"You have supplied the wrong token to manipulate this host")
|
||||
c.JSON(403, gin.H{
|
||||
"error": "You have supplied the wrong token to manipulate this host",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ip, err := GetRemoteAddr(c.Req)
|
||||
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
|
||||
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
|
||||
|
@ -98,3 +100,14 @@ func GetRemoteAddr(req *http.Request) (string, error) {
|
|||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue