w3K 9c38e33f07
Fix 404 errors on updates
Before change you get these errors constantly on any host ip update:

{"time":"2022-05-09T19:03:38.40895553Z","id":"","remote_ip":"69.1.2.3","host":"ddns.domain.com","method":"GET","uri":"/nic/update?system=dyndns&hostname=test1.ddns.domain.com&myip=&wildcard=OFF&mx=NO&backmx=NO&offline=NO","user_agent":"Hikvision-dvrdvs-1.0.0","status":404,"error":"code=404, message=Not Found","latency":895808,"latency_human":"895.808µs","bytes_in":0,"bytes_out":24}
{"time":"2022-05-09T19:03:40.496361052Z","id":"","remote_ip":"69.3.2.1","host":"ddns.domain.com","method":"GET","uri":"/nic/update?system=dyndns&hostname=test2.ddns.domain.com&myip=&wildcard=OFF&mx=NO&backmx=NO&offline=NO","user_agent":"Hikvision-dvrdvs-1.0.0","status":404,"error":"code=404, message=Not Found","latency":796442,"latency_human":"796.442µs","bytes_in":0,"bytes_out":24}
{"time":"2022-05-09T19:03:45.375560893Z","id":"","remote_ip":"69.4.4.4","host":"ddns.domain.com","method":"GET","uri":"/nic/update?system=dyndns&hostname=test3.ddns.domain.com&myip=&wildcard=OFF&mx=NO&backmx=NO&offline=NO","user_agent":"Hikvision-dvrdvs-1.0.0","status":404,"error":"code=404, message=Not Found","latency":774547,"latency_human":"774.547µs","bytes_in":0,"bytes_out":24}

After change... hosts are updated... no issues.
2022-05-28 19:14:06 -04:00

120 lines
3.0 KiB
Go

package main
import (
"github.com/benjaminbear/docker-ddns-server/dyndns/handler"
"github.com/foolin/goview"
"github.com/foolin/goview/supports/echoview-v4"
"github.com/go-playground/validator/v10"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"html/template"
"net/http"
"time"
)
func main() {
// Set new instance
e := echo.New()
e.Logger.SetLevel(log.ERROR)
e.Use(middleware.Logger())
// Set Renderer
e.Renderer = echoview.New(goview.Config{
Root: "views",
Master: "layouts/master",
Extension: ".html",
Funcs: template.FuncMap{
"year": func() string {
return time.Now().Format("2006")
},
},
DisableCache: true,
})
// Set Validator
e.Validator = &handler.CustomValidator{Validator: validator.New()}
// Set Statics
e.Static("/static", "static")
// Initialize handler
h := &handler.Handler{}
// Database connection
if err := h.InitDB(); err != nil {
e.Logger.Fatal(err)
}
defer h.DB.Close()
authAdmin, err := h.ParseEnvs()
if err != nil {
e.Logger.Fatal(err)
}
// UI Routes
groupPublic := e.Group("/")
groupPublic.GET("*", func(c echo.Context) error {
//redirect to admin
return c.Redirect(301, "./admin/")
})
groupAdmin := e.Group("/admin")
if authAdmin {
groupAdmin.Use(middleware.BasicAuth(h.AuthenticateAdmin))
}
groupAdmin.GET("/", h.ListHosts)
groupAdmin.GET("/hosts/add", h.AddHost)
groupAdmin.GET("/hosts/edit/:id", h.EditHost)
groupAdmin.GET("/hosts", h.ListHosts)
groupAdmin.GET("/cnames/add", h.AddCName)
groupAdmin.GET("/cnames", h.ListCNames)
groupAdmin.GET("/logs", h.ShowLogs)
groupAdmin.GET("/logs/host/:id", h.ShowHostLogs)
// Rest Routes
groupAdmin.POST("/hosts/add", h.CreateHost)
groupAdmin.POST("/hosts/edit/:id", h.UpdateHost)
groupAdmin.GET("/hosts/delete/:id", h.DeleteHost)
//redirect to logout
groupAdmin.GET("/logout", func(c echo.Context) error {
// either custom url
if len(h.LogoutUrl) > 0 {
return c.Redirect(302, h.LogoutUrl)
}
// or standard url
return c.Redirect(302, "../")
})
groupAdmin.POST("/cnames/add", h.CreateCName)
groupAdmin.GET("/cnames/delete/:id", h.DeleteCName)
// dyndns compatible api
// (avoid breaking changes and create groups for each update endpoint)
updateRoute := e.Group("/update")
updateRoute.Use(middleware.BasicAuth(h.AuthenticateUpdate))
updateRoute.GET("", h.UpdateIP)
nicRoute := e.Group("/nic")
nicRoute.Use(middleware.BasicAuth(h.AuthenticateUpdate))
nicRoute.GET("/update", h.UpdateIP)
v2Route := e.Group("/v2")
v2Route.Use(middleware.BasicAuth(h.AuthenticateUpdate))
v2Route.GET("/update", h.UpdateIP)
v3Route := e.Group("/v3")
v3Route.Use(middleware.BasicAuth(h.AuthenticateUpdate))
v3Route.GET("/update", h.UpdateIP)
// health-check
e.GET("/ping", func(c echo.Context) error {
u := &handler.Error{
Message: "OK",
}
return c.JSON(http.StatusOK, u)
})
// Start server
e.Logger.Fatal(e.Start(":8080"))
}