mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-09-12 13:10:05 +00:00
feat(db): migration, and seeder hash existing passwords
This commit is contained in:
parent
84dbcf7869
commit
049e0c4056
2 changed files with 50 additions and 4 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"x-ui/config"
|
"x-ui/config"
|
||||||
"x-ui/database/model"
|
"x-ui/database/model"
|
||||||
|
@ -34,6 +35,7 @@ func initModels() error {
|
||||||
&model.Setting{},
|
&model.Setting{},
|
||||||
&model.InboundClientIps{},
|
&model.InboundClientIps{},
|
||||||
&xray.ClientTraffic{},
|
&xray.ClientTraffic{},
|
||||||
|
&model.HistoryOfSeeders{},
|
||||||
}
|
}
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
if err := db.AutoMigrate(model); err != nil {
|
if err := db.AutoMigrate(model); err != nil {
|
||||||
|
@ -61,6 +63,40 @@ func initUser() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runSeeders(isUsersEmpty bool) error {
|
||||||
|
empty, err := isTableEmpty("history_of_seeders")
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error checking if users table is empty: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if empty && isUsersEmpty {
|
||||||
|
hashSeeder := &model.HistoryOfSeeders{
|
||||||
|
SeederName: "UserPasswordHash",
|
||||||
|
}
|
||||||
|
return db.Create(hashSeeder).Error
|
||||||
|
} else {
|
||||||
|
var seedersHistory []string
|
||||||
|
db.Model(&model.HistoryOfSeeders{}).Pluck("seeder_name", &seedersHistory)
|
||||||
|
|
||||||
|
if !slices.Contains(seedersHistory, "UserPasswordHash") && !isUsersEmpty {
|
||||||
|
var users []model.User
|
||||||
|
db.Find(&users)
|
||||||
|
|
||||||
|
for _, user := range users {
|
||||||
|
db.Model(&user).Update("password", crypto.HashSHA256(user.Password))
|
||||||
|
}
|
||||||
|
|
||||||
|
hashSeeder := &model.HistoryOfSeeders{
|
||||||
|
SeederName: "UserPasswordHash",
|
||||||
|
}
|
||||||
|
return db.Create(hashSeeder).Error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func isTableEmpty(tableName string) (bool, error) {
|
func isTableEmpty(tableName string) (bool, error) {
|
||||||
var count int64
|
var count int64
|
||||||
err := db.Table(tableName).Count(&count).Error
|
err := db.Table(tableName).Count(&count).Error
|
||||||
|
@ -93,11 +129,16 @@ func InitDB(dbPath string) error {
|
||||||
if err := initModels(); err != nil {
|
if err := initModels(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
empty, err := isTableEmpty("users")
|
||||||
|
if empty {
|
||||||
if err := initUser(); err != nil {
|
if err := initUser(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
}
|
||||||
|
return runSeeders(empty)
|
||||||
|
|
||||||
|
// return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CloseDB() error {
|
func CloseDB() error {
|
||||||
|
|
|
@ -63,6 +63,11 @@ type InboundClientIps struct {
|
||||||
Ips string `json:"ips" form:"ips"`
|
Ips string `json:"ips" form:"ips"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HistoryOfSeeders struct {
|
||||||
|
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
|
||||||
|
SeederName string `json:"seederName"`
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
|
func (i *Inbound) GenXrayInboundConfig() *xray.InboundConfig {
|
||||||
listen := i.Listen
|
listen := i.Listen
|
||||||
if listen != "" {
|
if listen != "" {
|
||||||
|
|
Loading…
Reference in a new issue