mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-24 00:24:06 +03:00
Adjustment in New Client form to have Allocation IP from suggestion API
This commit is contained in:
parent
7aec01deed
commit
15703b9185
4 changed files with 213 additions and 21 deletions
|
@ -3,6 +3,7 @@ package handler
|
|||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
@ -26,11 +27,6 @@ func WireGuardClients() echo.HandlerFunc {
|
|||
log.Error("Cannot initialize the database: ", err)
|
||||
}
|
||||
|
||||
records, err := db.ReadAll("clients")
|
||||
if err != nil {
|
||||
log.Error("Cannot fetch clients from database: ", err)
|
||||
}
|
||||
|
||||
// read server information
|
||||
serverInterface := model.ServerInterface{}
|
||||
if err := db.Read("server", "interfaces", &serverInterface); err != nil {
|
||||
|
@ -53,6 +49,11 @@ func WireGuardClients() echo.HandlerFunc {
|
|||
server.KeyPair = &serverKeyPair
|
||||
|
||||
// read client information and build a client list
|
||||
records, err := db.ReadAll("clients")
|
||||
if err != nil {
|
||||
log.Error("Cannot fetch clients from database: ", err)
|
||||
}
|
||||
|
||||
clientDataList := []model.ClientData{}
|
||||
for _, f := range records {
|
||||
client := model.Client{}
|
||||
|
@ -76,7 +77,7 @@ func WireGuardClients() echo.HandlerFunc {
|
|||
}
|
||||
|
||||
return c.Render(http.StatusOK, "clients.html", map[string]interface{}{
|
||||
"baseData": model.BaseData{""},
|
||||
"baseData": model.BaseData{Active: ""},
|
||||
"clientDataList": clientDataList,
|
||||
})
|
||||
}
|
||||
|
@ -88,6 +89,13 @@ func NewClient() echo.HandlerFunc {
|
|||
client := new(model.Client)
|
||||
c.Bind(client)
|
||||
|
||||
// validate the input Allocation IPs
|
||||
// TODO: validate if they are really available
|
||||
if util.ValidateCIDRList(client.AllocatedIPs) == false {
|
||||
log.Warnf("Invalid allocation ip input from user: %v", client.AllocatedIPs)
|
||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, "IP allocation is not valid"})
|
||||
}
|
||||
|
||||
// validate the input AllowedIPs
|
||||
if util.ValidateAllowedIPs(client.AllowedIPs) == false {
|
||||
log.Warnf("Invalid Allowed IPs input from user: %v", client.AllowedIPs)
|
||||
|
@ -167,7 +175,7 @@ func WireGuardServer() echo.HandlerFunc {
|
|||
}
|
||||
|
||||
return c.Render(http.StatusOK, "server.html", map[string]interface{}{
|
||||
"baseData": model.BaseData{"wg-server"},
|
||||
"baseData": model.BaseData{Active: "wg-server"},
|
||||
"serverInterface": serverInterface,
|
||||
"serverKeyPair": serverKeyPair,
|
||||
})
|
||||
|
@ -247,7 +255,7 @@ func GlobalSettings() echo.HandlerFunc {
|
|||
}
|
||||
|
||||
return c.Render(http.StatusOK, "global_settings.html", map[string]interface{}{
|
||||
"baseData": model.BaseData{"global-settings"},
|
||||
"baseData": model.BaseData{Active: "global-settings"},
|
||||
"globalSettings": globalSettings,
|
||||
})
|
||||
}
|
||||
|
@ -302,3 +310,41 @@ func MachineIPAddresses() echo.HandlerFunc {
|
|||
return c.JSON(http.StatusOK, interfaceList)
|
||||
}
|
||||
}
|
||||
|
||||
// SuggestIPAllocation handler to get the list of ip address for client
|
||||
func SuggestIPAllocation() echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
// initialize database directory
|
||||
dir := "./db"
|
||||
db, err := scribble.New(dir, nil)
|
||||
if err != nil {
|
||||
log.Error("Cannot initialize the database: ", err)
|
||||
}
|
||||
|
||||
// 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 the list of suggestedIPs
|
||||
// we take the first available ip address from
|
||||
// each server's network addresses.
|
||||
suggestedIPs := make([]string, 0)
|
||||
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"})
|
||||
}
|
||||
for _, cidr := range serverInterface.Addresses {
|
||||
ip, err := util.GetAvailableIP(cidr, allocatedIPs)
|
||||
if err != nil {
|
||||
log.Error("Failed to get available ip from a CIDR: ", err)
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, fmt.Sprintf("Cannot suggest ip allocation: failed to get available ip from network %s", cidr)})
|
||||
}
|
||||
suggestedIPs = append(suggestedIPs, fmt.Sprintf("%s/32", ip))
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, suggestedIPs)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue