This commit is contained in:
2021-07-21 15:38:38 +02:00
parent 64c307cb7b
commit 588c818c0d
9 changed files with 87 additions and 48 deletions

View File

@@ -1,12 +1,14 @@
package main
import (
"crypto/subtle"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"net"
"net/http"
auth "github.com/abbot/go-http-auth"
)
@@ -108,11 +110,11 @@ func (h *HttpServer) doaction(w http.ResponseWriter, r *http.Request, params *Ad
case "add":
host := r.URL.Query().Get("host")
_, err := h._database.CreateHost(host)
pw, _, err := h._database.CreateHost(host)
if err == nil {
params.Alerts = append(params.Alerts, Alert{
Type: "success",
Message: fmt.Sprintf("created host %s", host),
Message: fmt.Sprintf("created host %s\npassword: %s", host, pw),
})
} else {
params.Alerts = append(params.Alerts, Alert{
@@ -154,6 +156,7 @@ func (h *HttpServer) adminPage(w http.ResponseWriter, r *http.Request) {
params.Alerts = make([]Alert, 0)
params.Hosts = make(map[string]string)
params.IpAddresses = h._ipAddresses
params.LogoutUrl = h._oauth.LogoutUrl
h.doaction(w,r, params)
@@ -161,8 +164,7 @@ func (h *HttpServer) adminPage(w http.ResponseWriter, r *http.Request) {
claims, _ := h._oauth.GetClaims(w, token)
params.Email = claims.Email
params.Profile = claims.Profile
params.Claims = *claims
if settings.FilterString != nil {
params.Hosts = h._database.GetExistingHosts(*settings.FilterString)
@@ -275,13 +277,35 @@ func (h *HttpServer) updateHandler(w http.ResponseWriter, r *http.Request) {
log.Info("authorization successful.")
log.Info("will update host ", host, " ip ", myip)
w.WriteHeader(http.StatusOK)
if !h._database.ExistHost(host) {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
err := h._dnsclient.Update(host, myip)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
}
}
func (h *HttpServer) Listen() {
h._server.ListenAndServe()
}
func Secret(user, realm string) string {
if user == "john" {
// password is "hello"
return "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1"
}
return ""
}
func CreateHttpServer(config *Config) *HttpServer {
var httpserver *HttpServer
@@ -315,6 +339,10 @@ func CreateHttpServer(config *Config) *HttpServer {
Handler: r,
}
auth := auth.NewBasicAuthenticator(config.AuthRealm, func(user string) {
return httpserver._database._db.Get("hosts/"+ user)
})
r.HandleFunc("/register", httpserver.registerHandler)
r.HandleFunc("/update", httpserver.updateHandler)
r.HandleFunc("/admin", httpserver.adminPage)