mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-11-18 23:03:55 +08:00
fix: Fix user settings loading failure after reinstallation (#9391)
This commit is contained in:
parent
56ae5fbf70
commit
ccec57e32d
4 changed files with 57 additions and 49 deletions
|
|
@ -32,43 +32,50 @@ func Init() {
|
|||
handleUserInfo(global.CONF.Base.ChangeUserInfo, settingRepo)
|
||||
|
||||
generateKey()
|
||||
initDockerConf()
|
||||
}
|
||||
|
||||
func handleUserInfo(tags string, settingRepo repo.ISettingRepo) {
|
||||
if len(tags) == 0 {
|
||||
return
|
||||
}
|
||||
settingMap := make(map[string]string)
|
||||
if tags == "use_existing" {
|
||||
settingMap["Port"] = global.CONF.Conn.Port
|
||||
settingMap["UserName"] = global.CONF.Base.Username
|
||||
settingMap["Password"] = global.CONF.Base.Password
|
||||
settingMap["SecurityEntrance"] = global.CONF.Conn.Entrance
|
||||
settingMap["SystemVersion"] = global.CONF.Base.Version
|
||||
settingMap["Language"] = global.CONF.Base.Language
|
||||
}
|
||||
if tags == "all" {
|
||||
if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil {
|
||||
global.LOG.Fatalf("init username before start failed, err: %v", err)
|
||||
}
|
||||
pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10))
|
||||
if err := settingRepo.Update("Password", pass); err != nil {
|
||||
global.LOG.Fatalf("init password before start failed, err: %v", err)
|
||||
}
|
||||
if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil {
|
||||
global.LOG.Fatalf("init entrance before start failed, err: %v", err)
|
||||
}
|
||||
return
|
||||
settingMap["UserName"] = common.RandStrAndNum(10)
|
||||
settingMap["Password"] = common.RandStrAndNum(10)
|
||||
settingMap["SecurityEntrance"] = common.RandStrAndNum(10)
|
||||
}
|
||||
if strings.Contains(global.CONF.Base.ChangeUserInfo, "username") {
|
||||
if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil {
|
||||
global.LOG.Fatalf("init username before start failed, err: %v", err)
|
||||
}
|
||||
settingMap["UserName"] = common.RandStrAndNum(10)
|
||||
}
|
||||
if strings.Contains(global.CONF.Base.ChangeUserInfo, "password") {
|
||||
pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10))
|
||||
if err := settingRepo.Update("Password", pass); err != nil {
|
||||
global.LOG.Fatalf("init password before start failed, err: %v", err)
|
||||
}
|
||||
settingMap["Password"] = common.RandStrAndNum(10)
|
||||
}
|
||||
if strings.Contains(global.CONF.Base.ChangeUserInfo, "entrance") {
|
||||
if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil {
|
||||
global.LOG.Fatalf("init entrance before start failed, err: %v", err)
|
||||
settingMap["SecurityEntrance"] = common.RandStrAndNum(10)
|
||||
}
|
||||
for key, val := range settingMap {
|
||||
if len(val) == 0 {
|
||||
continue
|
||||
}
|
||||
if key == "Password" {
|
||||
val, _ = encrypt.StringEncrypt(val)
|
||||
}
|
||||
if err := settingRepo.Update(key, val); err != nil {
|
||||
global.LOG.Fatalf("update %s before start failed, err: %v", key, err)
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = cmd.RunDefaultWithStdoutBashCf("%s sed -i '/CHANGE_USER_INFO=%v/d' /usr/local/bin/1pctl", cmd.SudoHandleCmd(), global.CONF.Base.ChangeUserInfo)
|
||||
_, _ = cmd.RunDefaultWithStdoutBashCf("%s sed -i '/ORIGINAL_PASSWORD=%v/d' /usr/local/bin/1pctl", cmd.SudoHandleCmd(), "******")
|
||||
}
|
||||
|
||||
func generateKey() {
|
||||
|
|
@ -76,3 +83,14 @@ func generateKey() {
|
|||
global.LOG.Errorf("generate rsa key error : %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func initDockerConf() {
|
||||
stdout, err := cmd.RunDefaultWithStdoutBashC("which docker")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dockerPath := stdout
|
||||
if strings.Contains(dockerPath, "snap") {
|
||||
constant.DaemonJsonPath = "/var/snap/docker/current/config/daemon.json"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/1Panel-dev/1Panel/core/constant"
|
||||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
"github.com/1Panel-dev/1Panel/core/init/migration/helper"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/cmd"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/common"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
|
|
@ -52,6 +53,7 @@ var InitSetting = &gormigrate.Migration{
|
|||
if err := tx.Create(&model.Setting{Key: "Password", Value: pass}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = cmd.RunDefaultWithStdoutBashCf("%s sed -i '/ORIGINAL_PASSWORD=%v/d' /usr/local/bin/1pctl", cmd.SudoHandleCmd(), "******")
|
||||
if err := tx.Create(&model.Setting{Key: "Theme", Value: "light"}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"github.com/1Panel-dev/1Panel/core/cmd/server/conf"
|
||||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/cmd"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/common"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
|
@ -39,13 +40,13 @@ func Init() {
|
|||
panic(fmt.Errorf("Fatal error config file: %s \n", err))
|
||||
}
|
||||
} else {
|
||||
baseDir = loadParams("BASE_DIR")
|
||||
port = loadParams("ORIGINAL_PORT")
|
||||
version = loadParams("ORIGINAL_VERSION")
|
||||
username = loadParams("ORIGINAL_USERNAME")
|
||||
password = loadParams("ORIGINAL_PASSWORD")
|
||||
entrance = loadParams("ORIGINAL_ENTRANCE")
|
||||
language = loadParams("LANGUAGE")
|
||||
baseDir = common.LoadParams("BASE_DIR")
|
||||
port = common.LoadParams("ORIGINAL_PORT")
|
||||
version = common.LoadParams("ORIGINAL_VERSION")
|
||||
username = common.LoadParams("ORIGINAL_USERNAME")
|
||||
password = common.LoadParams("ORIGINAL_PASSWORD")
|
||||
entrance = common.LoadParams("ORIGINAL_ENTRANCE")
|
||||
language = common.LoadParams("LANGUAGE")
|
||||
|
||||
reader := bytes.NewReader(conf.AppYaml)
|
||||
if err := v.ReadConfig(reader); err != nil {
|
||||
|
|
@ -100,18 +101,6 @@ func Init() {
|
|||
global.Viper = v
|
||||
}
|
||||
|
||||
func loadParams(param string) string {
|
||||
stdout, err := cmd.RunDefaultWithStdoutBashCf("grep '^%s=' /usr/local/bin/1pctl | cut -d'=' -f2", param)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
info := strings.ReplaceAll(stdout, "\n", "")
|
||||
if len(info) == 0 || info == `""` {
|
||||
panic(fmt.Sprintf("error `%s` find in /usr/local/bin/1pctl", param))
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
func loadChangeInfo() string {
|
||||
stdout, err := cmd.RunDefaultWithStdoutBashC("grep '^CHANGE_USER_INFO=' /usr/local/bin/1pctl | cut -d'=' -f2")
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -4,14 +4,12 @@ import (
|
|||
"fmt"
|
||||
mathRand "math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/core/constant"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
|
|
@ -225,13 +223,14 @@ func HandleIPList(content string) ([]string, error) {
|
|||
return res, nil
|
||||
}
|
||||
|
||||
func deleteCookie(w http.ResponseWriter) {
|
||||
cookie := &http.Cookie{
|
||||
Name: constant.SessionName,
|
||||
Value: "",
|
||||
Path: "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
MaxAge: -1,
|
||||
func LoadParams(param string) string {
|
||||
stdout, err := cmd.RunDefaultWithStdoutBashCf("grep '^%s=' /usr/local/bin/1pctl | cut -d'=' -f2", param)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
http.SetCookie(w, cookie)
|
||||
info := strings.ReplaceAll(stdout, "\n", "")
|
||||
if len(info) == 0 || info == `""` {
|
||||
panic(fmt.Sprintf("error `%s` find in /usr/local/bin/1pctl", param))
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue