diff --git a/cmd/server/cmd/reset.go b/cmd/server/cmd/reset.go new file mode 100644 index 000000000..566677b2e --- /dev/null +++ b/cmd/server/cmd/reset.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +func init() { + RootCmd.AddCommand(resetMFACmd) + RootCmd.AddCommand(resetSSLCmd) + RootCmd.AddCommand(resetEntranceCmd) +} + +var resetMFACmd = &cobra.Command{ + Use: "reset-mfa", + Short: "关闭 1Panel 两步验证", + RunE: func(cmd *cobra.Command, args []string) error { + db, err := loadDBConn() + if err != nil { + return err + } + + return setSettingByKey(db, "MFAStatus", "disable") + }, +} + +var resetSSLCmd = &cobra.Command{ + Use: "reset-ssl", + Short: "取消 1Panel https 方式登录", + RunE: func(cmd *cobra.Command, args []string) error { + db, err := loadDBConn() + if err != nil { + return err + } + + return setSettingByKey(db, "SSL", "disable") + }, +} +var resetEntranceCmd = &cobra.Command{ + Use: "reset-entrance", + Short: "取消 1Panel 安全入口", + RunE: func(cmd *cobra.Command, args []string) error { + db, err := loadDBConn() + if err != nil { + return err + } + + return setSettingByKey(db, "SecurityEntranceStatus", "disable") + }, +} diff --git a/cmd/server/cmd/root.go b/cmd/server/cmd/root.go index c4c9cbcf3..4657eb920 100644 --- a/cmd/server/cmd/root.go +++ b/cmd/server/cmd/root.go @@ -1,10 +1,14 @@ package cmd import ( + "fmt" + "strings" "time" "github.com/1Panel-dev/1Panel/backend/server" + cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/spf13/cobra" + "gorm.io/driver/sqlite" "gorm.io/gorm" ) @@ -28,8 +32,32 @@ type setting struct { About string `json:"about" gorm:"type:longText"` } +func loadDBConn() (*gorm.DB, error) { + stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") + if err != nil { + return nil, fmt.Errorf("handle load `BASE_DIR` failed, err: %v", err) + } + baseDir := strings.ReplaceAll(stdout, "\n", "") + if len(baseDir) == 0 { + return nil, fmt.Errorf("error `BASE_DIR` find in /usr/bin/1pctl \n") + } + if strings.HasSuffix(baseDir, "/") { + baseDir = baseDir[:strings.LastIndex(baseDir, "/")] + } + + db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{}) + if err != nil { + return nil, fmt.Errorf("init my db conn failed, err: %v \n", err) + } + return db, nil +} + func getSettingByKey(db *gorm.DB, key string) string { var setting setting _ = db.Where("key = ?", key).First(&setting).Error return setting.Value } + +func setSettingByKey(db *gorm.DB, key, value string) error { + return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error +} diff --git a/cmd/server/cmd/user-info.go b/cmd/server/cmd/user-info.go index c88bd3c72..2882c64a7 100644 --- a/cmd/server/cmd/user-info.go +++ b/cmd/server/cmd/user-info.go @@ -2,14 +2,10 @@ package cmd import ( "fmt" - "strings" "github.com/1Panel-dev/1Panel/backend/global" - cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/encrypt" "github.com/spf13/cobra" - "gorm.io/driver/sqlite" - "gorm.io/gorm" ) func init() { @@ -20,21 +16,9 @@ var userinfoCmd = &cobra.Command{ Use: "user-info", Short: "获取用户信息", RunE: func(cmd *cobra.Command, args []string) error { - stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") + db, err := loadDBConn() if err != nil { - panic(err) - } - baseDir := strings.ReplaceAll(stdout, "\n", "") - if len(baseDir) == 0 { - fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n") - } - if strings.HasSuffix(baseDir, "/") { - baseDir = baseDir[:strings.LastIndex(baseDir, "/")] - } - - db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{}) - if err != nil { - fmt.Printf("init my db conn failed, err: %v \n", err) + return fmt.Errorf("init my db conn failed, err: %v \n", err) } user := getSettingByKey(db, "UserName") password := getSettingByKey(db, "Password") @@ -51,15 +35,10 @@ var userinfoCmd = &cobra.Command{ p = password } - protocol := "http" - if ssl == "enable" { - protocol = "https" - } - fmt.Printf("username: %s\n", user) fmt.Printf("password: %s\n", p) fmt.Printf("port: %s\n", port) - fmt.Printf("protocol: %s\n", protocol) + fmt.Printf("ssl: %s\n", ssl) fmt.Printf("entrance: %s\n", entrance) return nil }, diff --git a/cmd/server/cmd/version.go b/cmd/server/cmd/version.go index 07d7ea0f4..170e94931 100644 --- a/cmd/server/cmd/version.go +++ b/cmd/server/cmd/version.go @@ -2,15 +2,12 @@ package cmd import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/configs" "github.com/1Panel-dev/1Panel/cmd/server/conf" "gopkg.in/yaml.v3" - "strings" - cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/spf13/cobra" - "gorm.io/driver/sqlite" - "gorm.io/gorm" ) func init() { @@ -21,21 +18,9 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "获取系统版本信息", RunE: func(cmd *cobra.Command, args []string) error { - stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") - if err != nil { - panic(err) - } - baseDir := strings.ReplaceAll(stdout, "\n", "") - if len(baseDir) == 0 { - fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n") - } - if strings.HasSuffix(baseDir, "/") { - baseDir = baseDir[:strings.LastIndex(baseDir, "/")] - } - - db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{}) + db, err := loadDBConn() if err != nil { - fmt.Printf("init my db conn failed, err: %v \n", err) + return err } version := getSettingByKey(db, "SystemVersion") appStoreVersion := getSettingByKey(db, "AppStoreVersion") @@ -44,9 +29,10 @@ var versionCmd = &cobra.Command{ fmt.Printf("appstore version: %s\n", appStoreVersion) config := configs.ServerConfig{} if err := yaml.Unmarshal(conf.AppYaml, &config); err != nil { - panic(err) + return fmt.Errorf("unmarshal conf.App.Yaml failed, errL %v", err) + } else { + fmt.Printf("mode: %s\n", config.System.Mode) } - fmt.Printf("mode: %s\n", config.System.Mode) return nil }, }