From 71349d2a630cfd5d2d84dbabb3b13bc9f242f447 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 13 Feb 2023 11:04:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E3=80=81=E7=89=88=E6=9C=AC=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/configs/config.go | 1 - backend/configs/encrypt.go | 5 --- backend/configs/system.go | 1 + backend/init/hook/hook.go | 5 +++ backend/init/migration/migrations/init.go | 4 +++ backend/utils/encrypt/encrypt.go | 4 +-- cmd/server/cmd/backup.go | 20 ----------- cmd/server/cmd/restore.go | 19 ---------- cmd/server/cmd/root.go | 39 ++++++++++---------- cmd/server/cmd/userinfo.go | 44 +++++++++++++++++++++++ cmd/server/cmd/version.go | 31 ++++++++++++++++ cmd/server/conf/app.yaml | 5 +-- 12 files changed, 108 insertions(+), 70 deletions(-) delete mode 100644 backend/configs/encrypt.go delete mode 100644 cmd/server/cmd/backup.go delete mode 100644 cmd/server/cmd/restore.go create mode 100644 cmd/server/cmd/userinfo.go create mode 100644 cmd/server/cmd/version.go diff --git a/backend/configs/config.go b/backend/configs/config.go index 35910ef40..bb179f767 100644 --- a/backend/configs/config.go +++ b/backend/configs/config.go @@ -5,5 +5,4 @@ type ServerConfig struct { System System `mapstructure:"system"` LogConfig LogConfig `mapstructure:"log"` CORS CORS `mapstructure:"cors"` - Encrypt Encrypt `mapstructure:"encrypt"` } diff --git a/backend/configs/encrypt.go b/backend/configs/encrypt.go deleted file mode 100644 index 9af42c174..000000000 --- a/backend/configs/encrypt.go +++ /dev/null @@ -1,5 +0,0 @@ -package configs - -type Encrypt struct { - Key string `mapstructure:"key" json:"key" yaml:"key"` -} diff --git a/backend/configs/system.go b/backend/configs/system.go index d6fc84588..2570b9da1 100644 --- a/backend/configs/system.go +++ b/backend/configs/system.go @@ -11,4 +11,5 @@ type System struct { AppOss string `mapstructure:"app_oss"` AppRepoOwner string `mapstructure:"app_repo_owner"` AppRepoName string `mapstructure:"app_repo_name"` + EncryptKey string `mapstructure:"encrypt_key"` } diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index fa9d3ecb4..c5affaa61 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -12,6 +12,11 @@ func Init() { global.LOG.Errorf("load service port from setting failed, err: %v", err) } global.CONF.System.Port = portSetting.Value + enptrySetting, err := settingRepo.Get(settingRepo.WithByKey("EncryptKey")) + if err != nil { + global.LOG.Errorf("load service encrypt key from setting failed, err: %v", err) + } + global.CONF.System.EncryptKey = enptrySetting.Value if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil { _ = settingRepo.Create("SystemStatus", "Free") diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 485c72cd6..38c68de75 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -97,6 +97,9 @@ var AddTableSetting = &gormigrate.Migration{ if err := tx.Create(&model.Setting{Key: "JWTSigningKey", Value: common.RandStr(16)}).Error; err != nil { return err } + if err := tx.Create(&model.Setting{Key: "EncryptKey", Value: common.RandStr(16)}).Error; err != nil { + return err + } if err := tx.Create(&model.Setting{Key: "ExpirationTime", Value: time.Now().AddDate(0, 0, 10).Format("2006-01-02 15:04:05")}).Error; err != nil { return err @@ -137,6 +140,7 @@ var AddTableSetting = &gormigrate.Migration{ return err } if err := tx.Create(&model.Setting{Key: "SystemStatus", Value: "Free"}).Error; err != nil { + tx.Rollback() return err } if err := tx.Create(&model.Setting{Key: "AppStoreVersion", Value: "0"}).Error; err != nil { diff --git a/backend/utils/encrypt/encrypt.go b/backend/utils/encrypt/encrypt.go index d0d67ec43..cd77a1ec9 100644 --- a/backend/utils/encrypt/encrypt.go +++ b/backend/utils/encrypt/encrypt.go @@ -15,7 +15,7 @@ import ( ) func StringEncrypt(text string) (string, error) { - key := global.CONF.Encrypt.Key + key := global.CONF.System.EncryptKey pass := []byte(text) xpass, err := aesEncryptWithSalt([]byte(key), pass) if err == nil { @@ -26,7 +26,7 @@ func StringEncrypt(text string) (string, error) { } func StringDecrypt(text string) (string, error) { - key := global.CONF.Encrypt.Key + key := global.CONF.System.EncryptKey bytesPass, err := base64.StdEncoding.DecodeString(text) if err != nil { return "", err diff --git a/cmd/server/cmd/backup.go b/cmd/server/cmd/backup.go deleted file mode 100644 index 3046c49c2..000000000 --- a/cmd/server/cmd/backup.go +++ /dev/null @@ -1,20 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/spf13/cobra" -) - -func init() { - backupCmd.Flags().StringVarP(&configPath, "dir", "d", "/opt/backup", "备份目录") - RootCmd.AddCommand(backupCmd) -} - -var backupCmd = &cobra.Command{ - Use: "backup", - Short: "备份1Panel", - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Println("备份成功") - return nil - }, -} diff --git a/cmd/server/cmd/restore.go b/cmd/server/cmd/restore.go deleted file mode 100644 index e9099fdca..000000000 --- a/cmd/server/cmd/restore.go +++ /dev/null @@ -1,19 +0,0 @@ -package cmd - -import ( - "fmt" - "github.com/spf13/cobra" -) - -func init() { - RootCmd.AddCommand(restoreCmd) -} - -var restoreCmd = &cobra.Command{ - Use: "restore", - Short: "回滚1Panel", - RunE: func(cmd *cobra.Command, args []string) error { - fmt.Println("回滚成功") - return nil - }, -} diff --git a/cmd/server/cmd/root.go b/cmd/server/cmd/root.go index f8a4cae8d..c4c9cbcf3 100644 --- a/cmd/server/cmd/root.go +++ b/cmd/server/cmd/root.go @@ -1,34 +1,35 @@ package cmd import ( + "time" + "github.com/1Panel-dev/1Panel/backend/server" "github.com/spf13/cobra" + "gorm.io/gorm" ) -var ( - configPath string -) - -func init() { - RootCmd.Flags().BoolP("run", "r", false, "运行面板") - RootCmd.Flags().StringVarP(&configPath, "config", "c", "/opt/1panel/conf/app.yml", "配置文件路径") -} +func init() {} var RootCmd = &cobra.Command{ Use: "1panel", Short: "1Panel ,一款现代化的 Linux 面板", - Long: `欢迎使用 1Panel 面板 -github地址: https://github.com/1Panel-dev/1Panel -你可以使用如下命令操作1Panel -例如: 1panel -r 启动1Panel服务 -1panel backup -d /some/path 备份1Panel -你也可以使用 1panel --help 查看帮助信息 -或者使用 1panel xx --help 查看具体命令的帮助信息`, RunE: func(cmd *cobra.Command, args []string) error { - if len(args) == 0 || args[0] == "run" || args[0] == "r" { - server.Start() - return nil - } + server.Start() return nil }, } + +type setting struct { + ID uint `gorm:"primarykey;AUTO_INCREMENT" json:"id"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + Key string `json:"key" gorm:"type:varchar(256);not null;"` + Value string `json:"value" gorm:"type:varchar(256)"` + About string `json:"about" gorm:"type:longText"` +} + +func getSettingByKey(db *gorm.DB, key string) string { + var setting setting + _ = db.Where("key = ?", key).First(&setting).Error + return setting.Value +} diff --git a/cmd/server/cmd/userinfo.go b/cmd/server/cmd/userinfo.go new file mode 100644 index 000000000..d558a5994 --- /dev/null +++ b/cmd/server/cmd/userinfo.go @@ -0,0 +1,44 @@ +package cmd + +import ( + "fmt" + + "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/encrypt" + "github.com/spf13/cobra" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func init() { + RootCmd.AddCommand(userinfoCmd) +} + +var userinfoCmd = &cobra.Command{ + Use: "userinfo", + Short: "获取用户信息", + RunE: func(cmd *cobra.Command, args []string) error { + fullPath := "/opt/1panel/db/1Panel.db" + db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{}) + if err != nil { + fmt.Printf("init my db conn failed, err: %v \n", err) + } + user := getSettingByKey(db, "UserName") + password := getSettingByKey(db, "Password") + port := getSettingByKey(db, "ServerPort") + enptrySetting := getSettingByKey(db, "ServerPort") + + p := "" + if len(enptrySetting) == 16 { + global.CONF.System.EncryptKey = enptrySetting + p, _ = encrypt.StringDecrypt(password) + } else { + p = password + } + + fmt.Printf("username: %s\n", user) + fmt.Printf("password: %s\n", p) + fmt.Printf("port: %s\n", port) + return nil + }, +} diff --git a/cmd/server/cmd/version.go b/cmd/server/cmd/version.go new file mode 100644 index 000000000..8c640d3bf --- /dev/null +++ b/cmd/server/cmd/version.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +func init() { + RootCmd.AddCommand(versionCmd) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "获取系统版本信息", + RunE: func(cmd *cobra.Command, args []string) error { + fullPath := "/opt/1panel/db/1Panel.db" + db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{}) + if err != nil { + fmt.Printf("init my db conn failed, err: %v \n", err) + } + version := getSettingByKey(db, "SystemVersion") + appStoreVersion := getSettingByKey(db, "AppStoreVersion") + + fmt.Printf("1panel version: %s\n", version) + fmt.Printf("appstore version: %s\n", appStoreVersion) + return nil + }, +} diff --git a/cmd/server/conf/app.yaml b/cmd/server/conf/app.yaml index b407ee20d..5a10f3a9a 100644 --- a/cmd/server/conf/app.yaml +++ b/cmd/server/conf/app.yaml @@ -23,7 +23,4 @@ cors: allow-headers: content-type allow-methods: GET, POST expose-headers: Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type - allow-credentials: true - -encrypt: - key: 1Panel_key@2023! \ No newline at end of file + allow-credentials: true \ No newline at end of file