Skip to content

Commit

Permalink
feat: default network uses random password
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Oct 24, 2024
1 parent 421ac7d commit 43c78a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func AdminAddUser(c *gin.Context) {
netModel := &model.Net{
UserID: user.ID,
Name: "@",
Password: request.Password,
Password: randomString(8),
DHCP: "192.168.202.0/24",
Broadcast: true,
}
Expand Down
13 changes: 12 additions & 1 deletion api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"crypto/sha256"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -156,7 +157,7 @@ func UserRegister(c *gin.Context) {
netModel := &model.Net{
UserID: user.ID,
Name: "@",
Password: request.Password,
Password: randomString(8),
DHCP: "192.168.202.0/24",
Broadcast: true,
}
Expand Down Expand Up @@ -259,3 +260,13 @@ func hashUserPassword(username, password string) string {
hash := sha256.Sum256([]byte(username + ":" + password))
return fmt.Sprintf("%x", hash[:])
}

func randomString(n int) string {
letters := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
r := rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, n)
for i := range b {
b[i] = letters[r.Intn(len(letters))]
}
return string(b)
}

0 comments on commit 43c78a5

Please sign in to comment.