From f2eb3a180c0abda85076f323fc81b8d11b00c5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 7 Nov 2024 00:25:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AF=B9=E9=83=A8=E5=88=86=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E8=BF=9B=E8=A1=8C=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/global.go | 1 + internal/biz/database.go | 38 +++++++++++++++++++++++++++++++- internal/biz/firewall.go | 4 ---- internal/biz/ssh.go | 41 +++++++++++++++++++++++++++++++++++ internal/bootstrap/conf.go | 4 ++++ internal/bootstrap/session.go | 2 +- 6 files changed, 84 insertions(+), 6 deletions(-) delete mode 100644 internal/biz/firewall.go diff --git a/internal/app/global.go b/internal/app/global.go index 338be02d..08b9cdd6 100644 --- a/internal/app/global.go +++ b/internal/app/global.go @@ -37,6 +37,7 @@ const ( // 面板全局变量 var ( + Key string Root string Version string Locale string diff --git a/internal/biz/database.go b/internal/biz/database.go index 44ebada1..16741c27 100644 --- a/internal/biz/database.go +++ b/internal/biz/database.go @@ -1,6 +1,13 @@ package biz -import "time" +import ( + "time" + + "github.com/go-rat/utils/crypt" + "gorm.io/gorm" + + "github.com/TheTNB/panel/internal/app" +) type Database struct { ID uint `gorm:"primaryKey" json:"id"` @@ -14,3 +21,32 @@ type Database struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } + +func (r *Database) BeforeSave(tx *gorm.DB) error { + crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key)) + if err != nil { + return err + } + + r.Password, err = crypter.Encrypt([]byte(r.Password)) + if err != nil { + return err + } + + return nil + +} + +func (r *Database) AfterFind(tx *gorm.DB) error { + crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key)) + if err != nil { + return err + } + + password, err := crypter.Decrypt(r.Password) + if err == nil { + r.Password = string(password) + } + + return nil +} diff --git a/internal/biz/firewall.go b/internal/biz/firewall.go deleted file mode 100644 index 73ea74f8..00000000 --- a/internal/biz/firewall.go +++ /dev/null @@ -1,4 +0,0 @@ -package biz - -type FirewallRepo interface { -} diff --git a/internal/biz/ssh.go b/internal/biz/ssh.go index af44683d..93a9ebec 100644 --- a/internal/biz/ssh.go +++ b/internal/biz/ssh.go @@ -3,6 +3,10 @@ package biz import ( "time" + "github.com/go-rat/utils/crypt" + "gorm.io/gorm" + + "github.com/TheTNB/panel/internal/app" "github.com/TheTNB/panel/internal/http/request" "github.com/TheTNB/panel/pkg/ssh" ) @@ -18,6 +22,43 @@ type SSH struct { UpdatedAt time.Time `json:"updated_at"` } +func (r *SSH) BeforeSave(tx *gorm.DB) error { + crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key)) + if err != nil { + return err + } + + r.Config.Key, err = crypter.Encrypt([]byte(r.Config.Key)) + if err != nil { + return err + } + r.Config.Password, err = crypter.Encrypt([]byte(r.Config.Password)) + if err != nil { + return err + } + + return nil + +} + +func (r *SSH) AfterFind(tx *gorm.DB) error { + crypter, err := crypt.NewXChacha20Poly1305([]byte(app.Key)) + if err != nil { + return err + } + + key, err := crypter.Decrypt(r.Config.Key) + if err == nil { + r.Config.Key = string(key) + } + password, err := crypter.Decrypt(r.Config.Password) + if err == nil { + r.Config.Password = string(password) + } + + return nil +} + type SSHRepo interface { List(page, limit uint) ([]*SSH, int64, error) Get(id uint) (*SSH, error) diff --git a/internal/bootstrap/conf.go b/internal/bootstrap/conf.go index c55f2179..7c96ef0e 100644 --- a/internal/bootstrap/conf.go +++ b/internal/bootstrap/conf.go @@ -25,6 +25,10 @@ func initConf() { } func initGlobal() { + app.Key = app.Conf.MustString("app.key") + if len(app.Key) != 32 { + log.Fatalf("app key must be 32 characters") + } app.Root = app.Conf.MustString("app.root") app.Version = "2.3.17" app.Locale = app.Conf.MustString("app.locale") diff --git a/internal/bootstrap/session.go b/internal/bootstrap/session.go index 9d5933dc..5a867da0 100644 --- a/internal/bootstrap/session.go +++ b/internal/bootstrap/session.go @@ -12,7 +12,7 @@ import ( func initSession() { // initialize session manager manager, err := sessions.NewManager(&sessions.ManagerOptions{ - Key: app.Conf.String("app.key"), + Key: app.Key, Lifetime: 120, GcInterval: 30, DisableDefaultDriver: true,