2017-12-03 22:42:25 +01:00
|
|
|
package frontend
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
import (
|
2017-07-02 22:58:07 +02:00
|
|
|
"fmt"
|
2021-02-21 14:09:18 +01:00
|
|
|
"github.com/Depado/ginprom"
|
2019-08-26 12:00:32 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-12-03 22:42:25 +01:00
|
|
|
"github.com/pboehm/ddns/shared"
|
2021-02-21 14:09:18 +01:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2014-07-15 23:12:27 +02:00
|
|
|
"html/template"
|
2017-01-29 19:02:14 +01:00
|
|
|
"log"
|
2014-07-15 23:12:27 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2014-07-16 01:10:35 +02:00
|
|
|
"regexp"
|
2014-07-15 23:12:27 +02:00
|
|
|
)
|
|
|
|
|
2017-12-03 23:07:13 +01:00
|
|
|
type Frontend struct {
|
2021-02-21 14:09:18 +01:00
|
|
|
config *shared.Config
|
|
|
|
hosts shared.HostBackend
|
|
|
|
registry *prometheus.Registry
|
2017-01-29 19:02:14 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 14:09:18 +01:00
|
|
|
func NewFrontend(config *shared.Config, hosts shared.HostBackend, registry *prometheus.Registry) *Frontend {
|
2017-12-03 23:07:13 +01:00
|
|
|
return &Frontend{
|
2021-02-21 14:09:18 +01:00
|
|
|
config: config,
|
|
|
|
hosts: hosts,
|
|
|
|
registry: registry,
|
2017-01-29 19:02:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:07:13 +01:00
|
|
|
func (f *Frontend) Run() error {
|
2021-02-21 14:09:18 +01:00
|
|
|
prom := ginprom.New(ginprom.Namespace("ddns"),
|
|
|
|
ginprom.Subsystem("frontend"), ginprom.Registry(f.registry))
|
|
|
|
|
2018-01-28 01:49:41 +01:00
|
|
|
r := gin.New()
|
2021-02-21 14:09:18 +01:00
|
|
|
r.Use(prom.Instrument())
|
|
|
|
prom.Engine = r // we don't want to expose the metrics on the frontend, so we are not using `prom.Use(r)`
|
|
|
|
|
2018-01-28 01:49:41 +01:00
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
|
|
|
if f.config.Verbose {
|
|
|
|
r.Use(gin.Logger())
|
|
|
|
}
|
|
|
|
|
2021-02-21 15:26:23 +01:00
|
|
|
r.SetHTMLTemplate(buildTemplate(f.config.CustomTemplatePath))
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
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})
|
2014-07-15 23:12:27 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/available/:hostname", func(c *gin.Context) {
|
2017-01-29 19:02:14 +01:00
|
|
|
hostname, valid := isValidHostname(c.Params.ByName("hostname"))
|
|
|
|
|
|
|
|
if valid {
|
2017-12-03 23:07:13 +01:00
|
|
|
_, err := f.hosts.GetHost(hostname)
|
2017-11-29 00:43:00 +01:00
|
|
|
valid = err != nil
|
2017-01-29 19:02:14 +01:00
|
|
|
}
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
c.JSON(200, gin.H{
|
2017-01-29 19:02:14 +01:00
|
|
|
"available": valid,
|
2014-07-15 23:12:27 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/new/:hostname", func(c *gin.Context) {
|
2017-01-29 19:02:14 +01:00
|
|
|
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
|
|
|
|
}
|
2014-07-15 23:12:27 +02:00
|
|
|
|
2017-01-29 19:02:14 +01:00
|
|
|
var err error
|
|
|
|
|
2018-02-11 21:20:47 +01:00
|
|
|
if _, err := f.hosts.GetHost(hostname); err == nil {
|
|
|
|
c.JSON(403, gin.H{"error": "This hostname has already been registered."})
|
2014-07-15 23:12:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-03 22:42:25 +01:00
|
|
|
host := &shared.Host{Hostname: hostname, Ip: "127.0.0.1"}
|
2014-07-15 23:12:27 +02:00
|
|
|
host.GenerateAndSetToken()
|
|
|
|
|
2017-12-03 23:07:13 +01:00
|
|
|
if err = f.hosts.SetHost(host); err != nil {
|
2017-01-29 19:02:14 +01:00
|
|
|
c.JSON(400, gin.H{"error": "Could not register host."})
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 23:12:27 +02:00
|
|
|
|
|
|
|
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) {
|
2017-01-29 19:02:14 +01:00
|
|
|
hostname, valid := isValidHostname(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
|
|
|
|
}
|
|
|
|
|
2017-12-03 23:07:13 +01:00
|
|
|
host, err := f.hosts.GetHost(hostname)
|
2017-01-29 19:02:14 +01:00
|
|
|
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.",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
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",
|
|
|
|
})
|
2014-07-15 23:12:27 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-29 19:02:14 +01:00
|
|
|
ip, err := extractRemoteAddr(c.Request)
|
2014-07-15 23:12:27 +02:00
|
|
|
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
|
2017-12-03 23:07:13 +01:00
|
|
|
if err = f.hosts.SetHost(host); err != nil {
|
2017-01-29 19:02:14 +01:00
|
|
|
c.JSON(400, gin.H{
|
|
|
|
"error": "Could not update registered IP address",
|
|
|
|
})
|
|
|
|
}
|
2014-07-15 23:12:27 +02:00
|
|
|
|
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
|
|
|
})
|
|
|
|
|
2017-12-03 23:18:47 +01:00
|
|
|
return r.Run(f.config.ListenFrontend)
|
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
|
2017-01-29 19:02:14 +01:00
|
|
|
func extractRemoteAddr(req *http.Request) (string, error) {
|
2014-07-15 23:12:27 +02:00
|
|
|
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
|
2021-02-21 15:26:23 +01:00
|
|
|
func buildTemplate(customTemplatePath string) *template.Template {
|
|
|
|
var html *template.Template
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if customTemplatePath != "" {
|
|
|
|
html, err = template.ParseFiles(customTemplatePath)
|
|
|
|
} else {
|
|
|
|
html, err = template.New("index.html").Parse(indexTemplate)
|
|
|
|
}
|
|
|
|
|
2017-01-29 19:02:14 +01:00
|
|
|
if err != nil {
|
2021-02-21 15:26:23 +01:00
|
|
|
log.Fatalf("Error parsing frontend template: %v", err)
|
2017-01-29 19:02:14 +01:00
|
|
|
}
|
2014-07-15 23:41:54 +02:00
|
|
|
|
|
|
|
return html
|
|
|
|
}
|
2014-07-16 01:10:35 +02:00
|
|
|
|
2017-01-29 19:02:14 +01:00
|
|
|
func isValidHostname(host string) (string, bool) {
|
2021-02-16 17:23:28 +01:00
|
|
|
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
|
|
|
|
}
|