From 91f06914d4e23c8c1aa55b1c77d08bd487707e3c Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sat, 25 Apr 2020 16:58:14 +0700 Subject: [PATCH] Add download button --- handler/routes.go | 30 +++++++++++++++++++++++++++++- main.go | 1 + templates/clients.html | 5 +++-- util/db.go | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 90c03a8..7b0d689 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "time" "github.com/gorilla/sessions" @@ -178,7 +179,34 @@ func SetClientStatus() echo.HandlerFunc { db.Write("clients", clientID, &client) log.Infof("Changed client %s enabled status to %v", client.ID, status) - return c.JSON(http.StatusOK, jsonHTTPResponse{true, "ok"}) + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Changed client status successfully"}) + } +} + +// DownloadClient handler +func DownloadClient() echo.HandlerFunc { + return func(c echo.Context) error { + clientID := c.QueryParam("clientid") + if clientID == "" { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Missing clientid parameter"}) + } + + client, err := util.GetClientByID(clientID) + if err != nil { + log.Errorf("Cannot generate client id %s config file for downloading: %v", clientID, err) + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"}) + } + // build config + server, _ := util.GetServer() + globalSettings, _ := util.GetGlobalSettings() + config := util.BuildClientConfig(client, server, globalSettings) + + // create io reader from string + reader := strings.NewReader(config) + + // set response header for downloading + c.Response().Header().Set(echo.HeaderContentDisposition, "attachment; filename=wg0.conf") + return c.Stream(http.StatusOK, "text/plain", reader) } } diff --git a/main.go b/main.go index d82f7a2..bf8d911 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ func main() { app.POST("/new-client", handler.NewClient()) app.POST("/client/set-status", handler.SetClientStatus()) app.POST("/remove-client", handler.RemoveClient()) + app.GET("/download", handler.DownloadClient()) app.GET("/wg-server", handler.WireGuardServer()) app.POST("wg-server/interfaces", handler.WireGuardServerInterfaces()) app.POST("wg-server/keypair", handler.WireGuardServerKeyPair()) diff --git a/templates/clients.html b/templates/clients.html index 390c5bb..2dfce15 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -36,8 +36,9 @@ Wireguard Clients src="{{ .QRCode }}" />
- - + + diff --git a/util/db.go b/util/db.go index 99c16bf..8dcbdd0 100644 --- a/util/db.go +++ b/util/db.go @@ -224,3 +224,20 @@ func GetClients(hasQRCode bool) ([]model.ClientData, error) { return clients, nil } + +// GetClientByID func to query a client from the database +func GetClientByID(clientID string) (model.Client, error) { + client := model.Client{} + + db, err := DBConn() + if err != nil { + return client, err + } + + // read client information + if err := db.Read("clients", clientID, &client); err != nil { + return client, err + } + + return client, nil +}