diff --git a/custom/js/helper.js b/custom/js/helper.js index 9591026..42bc17a 100644 --- a/custom/js/helper.js +++ b/custom/js/helper.js @@ -9,13 +9,13 @@ function renderClientList(data) { // render client allocated ip addresses let allocatedIpsHtml = ""; $.each(obj.Client.allocated_ips, function(index, obj) { - allocatedIpsHtml += `${obj}`; + allocatedIpsHtml += `${obj} `; }) // render client allowed ip addresses let allowedIpsHtml = ""; $.each(obj.Client.allowed_ips, function(index, obj) { - allowedIpsHtml += `${obj}`; + allowedIpsHtml += `${obj} `; }) // render client html content diff --git a/handler/routes.go b/handler/routes.go index 39b3ceb..5164fd6 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -145,7 +145,7 @@ func NewClient() echo.HandlerFunc { } // validate the input Allocation IPs - allocatedIPs, err := util.GetAllocatedIPs() + allocatedIPs, err := util.GetAllocatedIPs("") check, err := util.ValidateIPAllocation(serverInterface.Addresses, allocatedIPs, client.AllocatedIPs) if !check { return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, fmt.Sprintf("%s", err)}) @@ -188,6 +188,63 @@ func NewClient() echo.HandlerFunc { } } +// UpdateClient handler to update client information +func UpdateClient() echo.HandlerFunc { + return func(c echo.Context) error { + // access validation + validSession(c) + + _client := new(model.Client) + c.Bind(_client) + + db, err := util.DBConn() + if err != nil { + log.Error("Cannot initialize database: ", err) + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"}) + } + + // validate client existence + client := model.Client{} + if err := db.Read("clients", _client.ID, &client); err != nil { + return c.JSON(http.StatusNotFound, jsonHTTPResponse{false, "Client not found"}) + } + + // read server information + serverInterface := model.ServerInterface{} + if err := db.Read("server", "interfaces", &serverInterface); err != nil { + log.Error("Cannot fetch server interface config from database: ", err) + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, fmt.Sprintf("Cannot fetch server config: %s", err)}) + } + + // validate the input Allocation IPs + allocatedIPs, err := util.GetAllocatedIPs(client.ID) + check, err := util.ValidateIPAllocation(serverInterface.Addresses, allocatedIPs, _client.AllocatedIPs) + if !check { + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, fmt.Sprintf("%s", err)}) + } + + // validate the input AllowedIPs + if util.ValidateAllowedIPs(_client.AllowedIPs) == false { + log.Warnf("Invalid Allowed IPs input from user: %v", _client.AllowedIPs) + return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "Allowed IPs must be in CIDR format"}) + } + + // map new data + client.Name = _client.Name + client.Email = _client.Email + client.Enabled = _client.Enabled + client.AllocatedIPs = _client.AllocatedIPs + client.AllowedIPs = _client.AllowedIPs + client.UpdatedAt = time.Now().UTC() + + // write to the database + db.Write("clients", client.ID, &client) + log.Infof("Updated client information successfully => %v", client) + + return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated client successfully"}) + } +} + // SetClientStatus handler to enable / disable a client func SetClientStatus() echo.HandlerFunc { return func(c echo.Context) error { @@ -212,7 +269,7 @@ func SetClientStatus() echo.HandlerFunc { client := model.Client{} if err := db.Read("clients", clientID, &client); err != nil { - log.Error("Cannot fetch server interface config from database: ", err) + log.Error("Cannot get client from database: ", err) } client.Enabled = status @@ -451,7 +508,7 @@ func SuggestIPAllocation() echo.HandlerFunc { // we take the first available ip address from // each server's network addresses. suggestedIPs := make([]string, 0) - allocatedIPs, err := util.GetAllocatedIPs() + allocatedIPs, err := util.GetAllocatedIPs("") if err != nil { log.Error("Cannot suggest ip allocation. Failed to get list of allocated ip addresses: ", err) return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot suggest ip allocation: failed to get list of allocated ip addresses"}) diff --git a/main.go b/main.go index 95ce032..b289c99 100644 --- a/main.go +++ b/main.go @@ -49,6 +49,7 @@ func main() { app.POST("/login", handler.Login()) app.GET("/logout", handler.Logout()) app.POST("/new-client", handler.NewClient()) + app.POST("/update-client", handler.UpdateClient()) app.POST("/client/set-status", handler.SetClientStatus()) app.POST("/remove-client", handler.RemoveClient()) app.GET("/download", handler.DownloadClient()) diff --git a/templates/base.html b/templates/base.html index 681bb45..0e04f31 100644 --- a/templates/base.html +++ b/templates/base.html @@ -377,7 +377,7 @@ }, client_email: { required: "Please enter an email address", - email: "Please enter a vaild email address" + email: "Please enter a valid email address" }, }, errorElement: 'span', diff --git a/templates/clients.html b/templates/clients.html index 33fd92f..689d5d2 100644 --- a/templates/clients.html +++ b/templates/clients.html @@ -41,6 +41,7 @@ Wireguard Clients