mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-07 00:46:58 +03:00
Changed GetAllocatedIPs to use IStore
Previously the GetAllocatedIPs function used it's own database connection. It now uses a pointer with the store.IStore interface to access the DB. Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>
This commit is contained in:
parent
25588b5000
commit
228d434405
2 changed files with 11 additions and 19 deletions
|
@ -148,7 +148,7 @@ func NewClient(db store.IStore) echo.HandlerFunc {
|
|||
}
|
||||
|
||||
// validate the input Allocation IPs
|
||||
allocatedIPs, err := util.GetAllocatedIPs("")
|
||||
allocatedIPs, err := util.GetAllocatedIPs(db, "")
|
||||
check, err := util.ValidateIPAllocation(server.Interface.Addresses, allocatedIPs, client.AllocatedIPs)
|
||||
if !check {
|
||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, fmt.Sprintf("%s", err)})
|
||||
|
@ -268,7 +268,7 @@ func UpdateClient(db store.IStore) echo.HandlerFunc {
|
|||
}
|
||||
client := *clientData.Client
|
||||
// validate the input Allocation IPs
|
||||
allocatedIPs, err := util.GetAllocatedIPs(client.ID)
|
||||
allocatedIPs, err := util.GetAllocatedIPs(db, client.ID)
|
||||
check, err := util.ValidateIPAllocation(server.Interface.Addresses, allocatedIPs, _client.AllocatedIPs)
|
||||
if !check {
|
||||
return c.JSON(http.StatusBadRequest, jsonHTTPResponse{false, fmt.Sprintf("%s", err)})
|
||||
|
@ -624,7 +624,7 @@ func SuggestIPAllocation(db store.IStore) 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(db, "")
|
||||
if err != nil {
|
||||
log.Error("Cannot suggest ip allocation. Failed to get list of allocated ip addresses: ", err)
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{
|
||||
|
|
22
util/util.go
22
util/util.go
|
@ -1,7 +1,6 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
|
@ -15,7 +14,7 @@ import (
|
|||
externalip "github.com/glendc/go-external-ip"
|
||||
"github.com/labstack/gommon/log"
|
||||
"github.com/ngoduykhanh/wireguard-ui/model"
|
||||
"github.com/sdomino/scribble"
|
||||
"github.com/ngoduykhanh/wireguard-ui/store"
|
||||
)
|
||||
|
||||
// BuildClientConfig to create wireguard client config string
|
||||
|
@ -214,20 +213,15 @@ func GetIPFromCIDR(cidr string) (string, error) {
|
|||
}
|
||||
|
||||
// GetAllocatedIPs to get all ip addresses allocated to clients and server
|
||||
func GetAllocatedIPs(ignoreClientID string) ([]string, error) {
|
||||
func GetAllocatedIPs(db store.IStore, ignoreClientID string) ([]string, error) {
|
||||
allocatedIPs := make([]string, 0)
|
||||
|
||||
// initialize database directory
|
||||
dir := "./db"
|
||||
db, err := scribble.New(dir, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// read server information
|
||||
serverInterface := model.ServerInterface{}
|
||||
if err := db.Read("server", "interfaces", &serverInterface); err != nil {
|
||||
if server, err := db.GetServer(); err != nil {
|
||||
return nil, err
|
||||
} else{
|
||||
serverInterface = *server.Interface
|
||||
}
|
||||
|
||||
// append server's addresses to the result
|
||||
|
@ -240,7 +234,7 @@ func GetAllocatedIPs(ignoreClientID string) ([]string, error) {
|
|||
}
|
||||
|
||||
// read client information
|
||||
records, err := db.ReadAll("clients")
|
||||
records, err := db.GetClients(false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -248,9 +242,7 @@ func GetAllocatedIPs(ignoreClientID string) ([]string, error) {
|
|||
// append client's addresses to the result
|
||||
for _, f := range records {
|
||||
client := model.Client{}
|
||||
if err := json.Unmarshal([]byte(f), &client); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client = *f.Client
|
||||
|
||||
if client.ID != ignoreClientID {
|
||||
for _, cidr := range client.AllocatedIPs {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue