mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-24 00:24:06 +03:00
Validate the client IP allocation
This commit is contained in:
parent
15703b9185
commit
e52ffaf686
2 changed files with 58 additions and 10 deletions
|
@ -89,11 +89,25 @@ func NewClient() echo.HandlerFunc {
|
|||
client := new(model.Client)
|
||||
c.Bind(client)
|
||||
|
||||
// initialize db
|
||||
dir := "./db"
|
||||
db, err := scribble.New(dir, nil)
|
||||
if err != nil {
|
||||
log.Error("Cannot initialize the database: ", err)
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"})
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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"})
|
||||
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)})
|
||||
}
|
||||
|
||||
// validate the input AllowedIPs
|
||||
|
@ -118,12 +132,6 @@ func NewClient() echo.HandlerFunc {
|
|||
client.UpdatedAt = client.CreatedAt
|
||||
|
||||
// write client to the database
|
||||
dir := "./db"
|
||||
db, err := scribble.New(dir, nil)
|
||||
if err != nil {
|
||||
log.Error("Cannot initialize the database: ", err)
|
||||
return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot access database"})
|
||||
}
|
||||
db.Write("clients", client.ID, client)
|
||||
log.Infof("Created wireguard client: %v", client)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue