mirror of
https://github.com/nezhahq/nezha.git
synced 2025-03-11 08:18:13 -04:00
generate agent_secret for old users (#1021)
Some checks failed
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Has been cancelled
Run Tests / tests (ubuntu) (push) Has been cancelled
Run Tests / tests (windows) (push) Has been cancelled
Some checks failed
CodeQL / Analyze (go) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
Contributors / contributors (push) Waiting to run
Sync / sync-to-jihulab (push) Waiting to run
Run Tests / tests (macos) (push) Has been cancelled
Run Tests / tests (ubuntu) (push) Has been cancelled
Run Tests / tests (windows) (push) Has been cancelled
This commit is contained in:
parent
79884c781a
commit
9ee5595da7
@ -40,23 +40,23 @@ var (
|
|||||||
frontendDist embed.FS
|
frontendDist embed.FS
|
||||||
)
|
)
|
||||||
|
|
||||||
func initSystem() {
|
func initSystem() error {
|
||||||
// 初始化管理员账户
|
// 初始化管理员账户
|
||||||
var usersCount int64
|
var usersCount int64
|
||||||
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
|
if err := singleton.DB.Model(&model.User{}).Count(&usersCount).Error; err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
if usersCount == 0 {
|
if usersCount == 0 {
|
||||||
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
hash, err := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
admin := model.User{
|
admin := model.User{
|
||||||
Username: "admin",
|
Username: "admin",
|
||||||
Password: string(hash),
|
Password: string(hash),
|
||||||
}
|
}
|
||||||
if err := singleton.DB.Create(&admin).Error; err != nil {
|
if err := singleton.DB.Create(&admin).Error; err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,13 +65,14 @@ func initSystem() {
|
|||||||
|
|
||||||
// 每天的3:30 对 监控记录 和 流量记录 进行清理
|
// 每天的3:30 对 监控记录 和 流量记录 进行清理
|
||||||
if _, err := singleton.CronShared.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
|
if _, err := singleton.CronShared.AddFunc("0 30 3 * * *", singleton.CleanServiceHistory); err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 每小时对流量记录进行打点
|
// 每小时对流量记录进行打点
|
||||||
if _, err := singleton.CronShared.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
|
if _, err := singleton.CronShared.AddFunc("0 0 * * * *", singleton.RecordTransferHourlyUsage); err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// @title Nezha Monitoring API
|
// @title Nezha Monitoring API
|
||||||
@ -111,7 +112,9 @@ func main() {
|
|||||||
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
|
singleton.InitConfigFromPath(dashboardCliParam.ConfigFile)
|
||||||
singleton.InitTimezoneAndCache()
|
singleton.InitTimezoneAndCache()
|
||||||
singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation)
|
singleton.InitDBFromPath(dashboardCliParam.DatabaseLocation)
|
||||||
initSystem()
|
if err := initSystem(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", singleton.Conf.ListenHost, singleton.Conf.ListenPort))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -13,6 +13,8 @@ const (
|
|||||||
RoleMember
|
RoleMember
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const DefaultAgentSecretLength = 32
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Common
|
Common
|
||||||
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
|
Username string `json:"username,omitempty" gorm:"uniqueIndex"`
|
||||||
@ -32,7 +34,7 @@ func (u *User) BeforeSave(tx *gorm.DB) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := utils.GenerateRandomString(32)
|
key, err := utils.GenerateRandomString(DefaultAgentSecretLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"cmp"
|
"cmp"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"iter"
|
"iter"
|
||||||
"maps"
|
"maps"
|
||||||
"math/big"
|
"math/big"
|
||||||
@ -82,6 +83,14 @@ func GenerateRandomString(n int) (string, error) {
|
|||||||
return string(ret), nil
|
return string(ret), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MustGenerateRandomString(n int) string {
|
||||||
|
str, err := GenerateRandomString(n)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("MustGenerateRandomString: %v", err))
|
||||||
|
}
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
func Uint64SubInt64(a uint64, b int64) uint64 {
|
func Uint64SubInt64(a uint64, b int64) uint64 {
|
||||||
if b < 0 {
|
if b < 0 {
|
||||||
return a + uint64(-b)
|
return a + uint64(-b)
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package singleton
|
package singleton
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/nezhahq/nezha/model"
|
"github.com/nezhahq/nezha/model"
|
||||||
|
"github.com/nezhahq/nezha/pkg/utils"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,6 +31,13 @@ func initUser() {
|
|||||||
AgentSecretToUserId[Conf.AgentSecretKey] = 0
|
AgentSecretToUserId[Conf.AgentSecretKey] = 0
|
||||||
|
|
||||||
for _, u := range users {
|
for _, u := range users {
|
||||||
|
if u.AgentSecret == "" {
|
||||||
|
u.AgentSecret = utils.MustGenerateRandomString(model.DefaultAgentSecretLength)
|
||||||
|
if err := DB.Save(&u).Error; err != nil {
|
||||||
|
panic(fmt.Errorf("update of user %d failed: %v", u.ID, err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UserInfoMap[u.ID] = model.UserInfo{
|
UserInfoMap[u.ID] = model.UserInfo{
|
||||||
Role: u.Role,
|
Role: u.Role,
|
||||||
AgentSecret: u.AgentSecret,
|
AgentSecret: u.AgentSecret,
|
||||||
|
Loading…
Reference in New Issue
Block a user