feat: 完成用户、版本信息获取

This commit is contained in:
ssongliu 2023-02-13 11:04:14 +08:00 committed by ssongliu
parent 80a0dfdfc0
commit 71349d2a63
12 changed files with 108 additions and 70 deletions

View File

@ -5,5 +5,4 @@ type ServerConfig struct {
System System `mapstructure:"system"`
LogConfig LogConfig `mapstructure:"log"`
CORS CORS `mapstructure:"cors"`
Encrypt Encrypt `mapstructure:"encrypt"`
}

View File

@ -1,5 +0,0 @@
package configs
type Encrypt struct {
Key string `mapstructure:"key" json:"key" yaml:"key"`
}

View File

@ -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"`
}

View File

@ -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")

View File

@ -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 {

View File

@ -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

View File

@ -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
},
}

View File

@ -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
},
}

View File

@ -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
}

View File

@ -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
},
}

31
cmd/server/cmd/version.go Normal file
View File

@ -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
},
}

View File

@ -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!
allow-credentials: true