2014-07-15 23:12:27 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/pboehm/ddns/connection"
|
|
|
|
"html/template"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2014-07-16 01:10:35 +02:00
|
|
|
"regexp"
|
2014-07-15 23:12:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunWebService(conn *connection.RedisConnection) {
|
|
|
|
r := gin.Default()
|
2014-07-15 23:41:54 +02:00
|
|
|
r.HTMLTemplates = BuildTemplate()
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
r.GET("/", func(g *gin.Context) {
|
|
|
|
g.HTML(200, "index.html", gin.H{"domain": DdnsDomain})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/available/:hostname", func(c *gin.Context) {
|
2014-07-16 01:10:35 +02:00
|
|
|
hostname, valid := ValidHostname(c.Params.ByName("hostname"))
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
2014-07-16 01:10:35 +02:00
|
|
|
"available": valid && !conn.HostExist(hostname),
|
2014-07-15 23:12:27 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/new/:hostname", func(c *gin.Context) {
|
2014-07-16 01:10:35 +02:00
|
|
|
hostname, valid := ValidHostname(c.Params.ByName("hostname"))
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
c.JSON(404, gin.H{"error": "This hostname is not valid"})
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
if conn.HostExist(hostname) {
|
2014-07-15 23:41:54 +02:00
|
|
|
c.JSON(403, gin.H{
|
|
|
|
"error": "This hostname has already been registered.",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host := &connection.Host{Hostname: hostname, Ip: "127.0.0.1"}
|
|
|
|
host.GenerateAndSetToken()
|
|
|
|
|
|
|
|
conn.SaveHost(host)
|
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"hostname": host.Hostname,
|
|
|
|
"token": host.Token,
|
|
|
|
"update_link": fmt.Sprintf("/update/%s/%s", host.Hostname, host.Token),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/update/:hostname/:token", func(c *gin.Context) {
|
2014-07-16 01:10:35 +02:00
|
|
|
hostname, valid := ValidHostname(c.Params.ByName("hostname"))
|
2014-07-15 23:12:27 +02:00
|
|
|
token := c.Params.ByName("token")
|
|
|
|
|
2014-07-16 01:10:35 +02:00
|
|
|
if !valid {
|
|
|
|
c.JSON(404, gin.H{"error": "This hostname is not valid"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-15 23:12:27 +02:00
|
|
|
if !conn.HostExist(hostname) {
|
2014-07-15 23:41:54 +02:00
|
|
|
c.JSON(404, gin.H{
|
|
|
|
"error": "This hostname has not been registered or is expired.",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
host := conn.GetHost(hostname)
|
|
|
|
|
|
|
|
if host.Token != token {
|
2014-07-15 23:41:54 +02:00
|
|
|
c.JSON(403, gin.H{
|
|
|
|
"error": "You have supplied the wrong token to manipulate this host",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := GetRemoteAddr(c.Req)
|
|
|
|
if err != nil {
|
2014-07-15 23:41:54 +02:00
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": "Your sender IP address is not in the right format",
|
|
|
|
})
|
|
|
|
return
|
2014-07-15 23:12:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
host.Ip = ip
|
|
|
|
conn.SaveHost(host)
|
|
|
|
|
2014-07-15 23:41:54 +02:00
|
|
|
c.JSON(200, gin.H{
|
|
|
|
"current_ip": ip,
|
|
|
|
"status": "Successfuly updated",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
})
|
|
|
|
|
2014-07-15 23:41:54 +02:00
|
|
|
r.Run(DdnsWebListenSocket)
|
2014-07-15 23:12:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Remote Address of the client. At First we try to get the
|
|
|
|
// X-Forwarded-For Header which holds the IP if we are behind a proxy,
|
|
|
|
// otherwise the RemoteAddr is used
|
|
|
|
func GetRemoteAddr(req *http.Request) (string, error) {
|
|
|
|
header_data, ok := req.Header["X-Forwarded-For"]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return header_data[0], nil
|
|
|
|
} else {
|
|
|
|
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
|
|
return ip, err
|
|
|
|
}
|
|
|
|
}
|
2014-07-15 23:41:54 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2014-07-16 01:10:35 +02:00
|
|
|
|
|
|
|
func ValidHostname(host string) (string, bool) {
|
|
|
|
valid, _ := regexp.Match("^[a-z0-9]{1,32}$", []byte(host))
|
|
|
|
|
|
|
|
return host, valid
|
|
|
|
}
|