From dbb85cb7591a96eb7fe1adc053b496e054fa4dd1 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Wed, 22 Apr 2020 17:11:28 +0700 Subject: [PATCH] Adjustment to have enable/disable client on UI --- handler/routes.go | 33 ++++++++++++ main.go | 1 + templates/base.html | 4 ++ templates/clients.html | 98 ++++++++++++++++++++++++++++++++-- templates/global_settings.html | 3 ++ templates/server.html | 3 ++ 6 files changed, 138 insertions(+), 4 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index b103c08..dd8cde4 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -139,6 +139,39 @@ func NewClient() echo.HandlerFunc { } } +// SetClientStatus handler to enable / disable a client +func SetClientStatus() echo.HandlerFunc { + return func(c echo.Context) error { + data := make(map[string]interface{}) + err := json.NewDecoder(c.Request().Body).Decode(&data) + + if err != nil { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Bad post data"}) + } + + clientID := data["id"].(string) + status := data["status"].(bool) + + // initialize database directory + dir := "./db" + db, err := scribble.New(dir, nil) + if err != nil { + log.Error("Cannot initialize the database: ", err) + } + + client := model.Client{} + if err := db.Read("clients", clientID, &client); err != nil { + log.Error("Cannot fetch server interface config from database: ", err) + } + + client.Enabled = status + db.Write("clients", clientID, &client) + log.Infof("Change client %s to status %b", client.ID, status) + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "ok"}) + } +} + // RemoveClient handler func RemoveClient() echo.HandlerFunc { return func(c echo.Context) error { diff --git a/main.go b/main.go index df6f7b1..bcd4056 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ func main() { app.GET("/", handler.WireGuardClients()) app.POST("/new-client", handler.NewClient()) + app.POST("/client/set-status", handler.SetClientStatus()) app.POST("/remove-client", handler.RemoveClient()) app.GET("/wg-server", handler.WireGuardServer()) app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) diff --git a/templates/base.html b/templates/base.html index 5b1e923..28d1705 100644 --- a/templates/base.html +++ b/templates/base.html @@ -25,6 +25,10 @@ + + + {{template "top_css" .}} + diff --git a/templates/clients.html b/templates/clients.html index 3faf054..2d8d25d 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -2,6 +2,16 @@ Wireguard Clients {{end}} +{{define "top_css"}} + +{{end}} + {{define "username"}} Admin {{end}} @@ -18,14 +28,21 @@ Wireguard Clients {{range .clientDataList}}
+
+ +
- - + +

{{ .Client.Name }} @@ -54,6 +71,28 @@ Wireguard Clients
+ + +