Skip to content

Commit 145aaea

Browse files
committed
Get free profile id to generate ip
1 parent 9722f35 commit 145aaea

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

cmd/subspace/config.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,14 @@ func (c *Config) UpdateProfile(id string, fn func(*Profile) error) error {
210210
return c.save()
211211
}
212212

213-
func (c *Config) AddProfile(userID, name, platform string, ipspeer string, allowedips string) (Profile, error) {
213+
func (c *Config) AddProfile(
214+
number int, userID, name, platform string, ipspeer string, allowedips string,
215+
) (Profile, error) {
214216
c.Lock()
215217
defer c.Unlock()
216218

217219
id := RandomString(16)
218220

219-
number := 2 // MUST start at 2
220-
for _, p := range c.Profiles {
221-
if p.Number >= number {
222-
number = p.Number + 1
223-
}
224-
}
225221
profile := Profile{
226222
ID: id,
227223
UserID: userID,

cmd/subspace/handlers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"os"
1010
"regexp"
11+
"sort"
1112
"strings"
1213

1314
"github.com/crewjam/saml/samlsp"
@@ -412,7 +413,8 @@ func profileAddHandler(w *Web) {
412413

413414
//if len(config.ListProfiles()) >= maxProfiles {
414415
// Check whether there is a room to assign new IP address or not.
415-
if _, _, err := wgConfig.generateIPAddr(uint32(len(config.ListProfiles()))); err != nil {
416+
firstFreeID := FindFirstFreeID(config.Profiles)
417+
if _, _, err := wgConfig.generateIPAddr(firstFreeID); err != nil {
416418
w.Redirect("/?error=addprofile")
417419
return
418420
}
@@ -422,7 +424,7 @@ func profileAddHandler(w *Web) {
422424
return
423425
}
424426
}
425-
profile, err := config.AddProfile(userID, name, platform, ipspeer, allowedips)
427+
profile, err := config.AddProfile(int(firstFreeID), userID, name, platform, ipspeer, allowedips)
426428
if err != nil {
427429
logger.Warn(err)
428430
w.Redirect("/?error=addprofile")

cmd/subspace/helpers.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import "sort"
4+
5+
func FindFirstFreeID(profiles []*Profile) uint32 {
6+
profileIDs := getProfileIDs(profiles)
7+
sort.Slice(profileIDs, func(i, j int) bool {
8+
return profileIDs[i] < profileIDs[j]
9+
})
10+
11+
const minID = 2
12+
if len(profileIDs) == 0 {
13+
return minID
14+
}
15+
maxID := profileIDs[len(profileIDs)-1]
16+
freeID := uint32(maxID + 1)
17+
for i := minID; i < maxID; i++ {
18+
if i != profileIDs[i-minID] {
19+
freeID = uint32(i)
20+
break
21+
}
22+
}
23+
24+
return freeID
25+
}
26+
27+
func getProfileIDs(profiles []*Profile) []int {
28+
var profileIDs = make([]int, len(profiles))
29+
for i, profile := range profiles {
30+
profileIDs[i] = profile.Number
31+
}
32+
33+
return profileIDs
34+
}

0 commit comments

Comments
 (0)