Browse Source

feat: 1pctl 支持 mfa、ssl 和安全入口关闭 (#818)

pull/821/head
ssongliu 2 years ago committed by GitHub
parent
commit
0ebd04f012
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      cmd/server/cmd/reset.go
  2. 28
      cmd/server/cmd/root.go
  3. 27
      cmd/server/cmd/user-info.go
  4. 26
      cmd/server/cmd/version.go

49
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")
},
}

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

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

26
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)
}
return nil
},
}

Loading…
Cancel
Save