ddns/frontend/frontend.go

160 lines
3.3 KiB
Go
Raw Normal View History

package frontend
import (
2017-07-02 22:58:07 +02:00
"fmt"
"github.com/gin-gonic/gin"
"github.com/pboehm/ddns/shared"
"html/template"
"log"
"net"
"net/http"
2014-07-16 01:10:35 +02:00
"regexp"
)
2017-12-03 23:07:13 +01:00
type Frontend struct {
config *shared.Config
hosts shared.HostBackend
}
2017-12-03 23:07:13 +01:00
func NewFrontend(config *shared.Config, hosts shared.HostBackend) *Frontend {
return &Frontend{
config: config,
hosts: hosts,
}
}
2017-12-03 23:07:13 +01:00
func (f *Frontend) Run() error {
r := gin.New()
r.Use(gin.Recovery())
if f.config.Verbose {
r.Use(gin.Logger())
}
r.SetHTMLTemplate(buildTemplate())
r.GET("/", func(g *gin.Context) {
2017-12-03 23:07:13 +01:00
g.HTML(200, "index.html", gin.H{"domain": f.config.Domain})
})
r.GET("/available/:hostname", func(c *gin.Context) {
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
if valid {
2017-12-03 23:07:13 +01:00
_, err := f.hosts.GetHost(hostname)
valid = err != nil
}
c.JSON(200, gin.H{
"available": valid,
})
})
r.GET("/new/:hostname", func(c *gin.Context) {
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
2014-07-16 01:10:35 +02:00
if !valid {
c.JSON(404, gin.H{"error": "This hostname is not valid"})
return
}
var err error
if _, err := f.hosts.GetHost(hostname); err == nil {
c.JSON(403, gin.H{"error": "This hostname has already been registered."})
return
}
host := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"}
host.GenerateAndSetToken()
2017-12-03 23:07:13 +01:00
if err = f.hosts.SetHost(host); err != nil {
c.JSON(400, gin.H{"error": "Could not register host."})
return
}
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) {
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
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
}
2017-12-03 23:07:13 +01:00
host, err := f.hosts.GetHost(hostname)
if err != nil {
2014-07-15 23:41:54 +02:00
c.JSON(404, gin.H{
"error": "This hostname has not been registered or is expired.",
})
return
}
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",
})
return
}
ip, err := extractRemoteAddr(c.Request)
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
}
host.Ip = ip
2017-12-03 23:07:13 +01:00
if err = f.hosts.SetHost(host); err != nil {
c.JSON(400, gin.H{
"error": "Could not update registered IP address",
})
}
2014-07-15 23:41:54 +02:00
c.JSON(200, gin.H{
"current_ip": ip,
"status": "Successfuly updated",
})
})
return r.Run(f.config.ListenFrontend)
}
// 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 extractRemoteAddr(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 {
html, err := template.New("index.html").Parse(indexTemplate)
if err != nil {
log.Fatal(err)
}
2014-07-15 23:41:54 +02:00
return html
}
2014-07-16 01:10:35 +02:00
func isValidHostname(host string) (string, bool) {
valid, _ := regexp.Match("^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?)$", []byte(host))
2014-07-16 01:10:35 +02:00
return host, valid
}