Skip to content

Commit

Permalink
v3.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
cookieY committed Dec 21, 2022
1 parent 85c0fe2 commit 7b649a1
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 11 deletions.
Binary file modified Juno
Binary file not shown.
3 changes: 3 additions & 0 deletions src/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,7 @@ type CheckArgs struct {
Username string
Port int
Password string
CA string
Cert string
Key string
}
25 changes: 22 additions & 3 deletions src/handler/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@ func ScanDataRows(s model.CoreDataSource, database, sql, meta string, isQuery bo
return res, errors.New("连接失败,密码解析错误!")
}

db, err := model.NewDBSub(fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", s.Username, ps, s.IP, s.Port, database))
db, err := model.NewDBSub(model.DSN{
Username: s.Username,
Password: ps,
Host: s.IP,
Port: s.Port,
DBName: database,
CA: s.CAFile,
Cert: s.Cert,
Key: s.KeyFile,
})
if err != nil {
return nil, err
return res, err
}

defer func() {
_ = model.Close(db)
}()
Expand Down Expand Up @@ -71,7 +81,16 @@ func checkMeta(s, database, flag string) string {
func Highlight(s *model.CoreDataSource) []map[string]string {
ps := lib.Decrypt(s.Password)
var list []map[string]string
db, err := model.NewDBSub(fmt.Sprintf("%s:%s@(%s:%d)/?charset=utf8&parseTime=True&loc=Local", s.Username, ps, s.IP, s.Port))
db, err := model.NewDBSub(model.DSN{
Username: s.Username,
Password: ps,
Host: s.IP,
Port: s.Port,
DBName: "",
CA: s.CAFile,
Cert: s.Cert,
Key: s.KeyFile,
})
if err != nil {
logger.DefaultLogger.Error(err)
return nil
Expand Down
3 changes: 3 additions & 0 deletions src/handler/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func FetchSQLTest(c yee.Context) (err error) {
Username: s.Username,
Port: s.Port,
Password: lib.Decrypt(s.Password),
CA: s.CAFile,
Cert: s.Cert,
Key: s.KeyFile,
Kind: u.Kind,
Lang: "zh-cn",
Rule: model.GloRole,
Expand Down
12 changes: 10 additions & 2 deletions src/handler/fetch/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"Yearning-go/src/lib"
"Yearning-go/src/model"
"fmt"
"strconv"
)

const (
Expand Down Expand Up @@ -46,7 +45,16 @@ func (u *_FetchBind) FetchTableFieldsOrIndexes() error {
model.DB().Where("source_id =?", u.SourceId).First(&s)

ps := lib.Decrypt(s.Password)
db, err := model.NewDBSub(fmt.Sprintf("%s:%s@(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", s.Username, ps, s.IP, strconv.Itoa(s.Port), u.DataBase))
db, err := model.NewDBSub(model.DSN{
Username: s.Username,
Password: ps,
Host: s.IP,
Port: s.Port,
DBName: u.DataBase,
CA: s.CAFile,
Cert: s.Cert,
Key: s.KeyFile,
})
if err != nil {
return err
}
Expand Down
19 changes: 17 additions & 2 deletions src/handler/manage/db/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"Yearning-go/src/lib"
"Yearning-go/src/model"
"encoding/json"
"fmt"
"github.com/google/uuid"
drive "gorm.io/driver/mysql"
"gorm.io/gorm"
Expand All @@ -27,8 +26,21 @@ type CommonDBPost struct {
}

func ConnTest(u *model.CoreDataSource) error {
dsn, err := model.InitDSN(model.DSN{
Username: u.Username,
Password: u.Password,
Host: u.IP,
Port: u.Port,
DBName: "",
CA: u.CAFile,
Cert: u.Cert,
Key: u.KeyFile,
})
if err != nil {
return err
}
db, err := gorm.Open(drive.New(drive.Config{
DSN: fmt.Sprintf("%s:%s@(%s:%d)/?charset=utf8&parseTime=True&loc=Local", u.Username, u.Password, u.IP, u.Port),
DSN: dsn,
DefaultStringSize: 256, // string 类型字段的默认长度
SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置
}), &gorm.Config{})
Expand All @@ -55,6 +67,9 @@ func SuperEditSource(source *model.CoreDataSource) common.Resp {
"exclude_db_list": source.ExcludeDbList,
"principal": source.Principal,
"insulate_word_list": source.InsulateWordList,
"ca_file": source.CAFile,
"cert": source.Cert,
"key_file": source.KeyFile,
})
var k []model.CoreRoleGroup
model.DB().Find(&k)
Expand Down
6 changes: 6 additions & 0 deletions src/handler/order/audit/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type ExecArgs struct {
Port int
Username string
Password string
CA string
Cert string
Key string
Message model.Message
MaxAffectRows uint
}
Expand Down Expand Up @@ -74,6 +77,9 @@ func ExecuteOrder(u *Confirm, user string) common.Resp {
Port: source.Port,
Username: source.Username,
Password: lib.Decrypt(source.Password),
CA: source.CAFile,
Cert: source.Cert,
Key: source.KeyFile,
Message: model.GloMessage,
}, &isCall); err != nil {
return common.ERR_RPC
Expand Down
2 changes: 1 addition & 1 deletion src/lib/sendMail.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func SendMail(addr string, mail model.Message, tmpl string) {
}
// Send the email to Bob, Cora and Dan.
if err := d.DialAndSend(m); err != nil {
logger.DefaultLogger.Errorf("send mail:%s", err)
logger.DefaultLogger.Errorf("send mail:%s", err.Error())
return
}
}
Expand Down
63 changes: 60 additions & 3 deletions src/model/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
package model

import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/BurntSushi/toml"
"github.com/cookieY/yee/logger"
mmsql "github.com/go-sql-driver/mysql"
drive "gorm.io/driver/mysql"
"gorm.io/gorm"
"os"
Expand All @@ -25,6 +28,17 @@ import (

var sqlDB *gorm.DB

type DSN struct {
Username string
Password string
Host string
Port int
DBName string
CA string
Cert string
Key string
}

func DBNew(c string) {
_, err := toml.DecodeFile(c, &C)
if err != nil {
Expand Down Expand Up @@ -62,9 +76,13 @@ func DB() *gorm.DB {
return sqlDB
}

func NewDBSub(dsn string) (*gorm.DB, error) {
func NewDBSub(dsn DSN) (*gorm.DB, error) {
d, err := InitDSN(dsn)
if err != nil {
return nil, err
}
db, err := gorm.Open(drive.New(drive.Config{
DSN: dsn,
DSN: d,
DefaultStringSize: 256, // string 类型字段的默认长度
SkipInitializeWithVersion: false, // 根据当前 MySQL 版本自动配置
}), &gorm.Config{})
Expand All @@ -75,6 +93,45 @@ func NewDBSub(dsn string) (*gorm.DB, error) {
}

func Close(db *gorm.DB) error {
orm, _ := db.DB()
orm, err := db.DB()
if err != nil {
return err
}
return orm.Close()
}

func InitDSN(dsn DSN) (string, error) {
isTLS := false
if dsn.CA != "" && dsn.Cert != "" && dsn.Key != "" {
isTLS = true
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM([]byte(dsn.CA)); !ok {
return "", fmt.Errorf("failed to append ca certs")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.X509KeyPair([]byte(dsn.Cert), []byte(dsn.Key))
if err != nil {
return "", err
}
clientCert = append(clientCert, certs)
_ = mmsql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: certPool,
Certificates: clientCert,
InsecureSkipVerify: true,
})
}
cfg := mmsql.Config{
User: dsn.Username,
Passwd: dsn.Password,
Addr: fmt.Sprintf("%s:%d", dsn.Host, dsn.Port), //IP:PORT
Net: "tcp",
DBName: dsn.DBName,
Loc: time.Local,
AllowNativePasswords: true,
ParseTime: true,
}
if isTLS == true {
cfg.TLSConfig = "custom"
}
return cfg.FormatDSN(), nil
}
3 changes: 3 additions & 0 deletions src/model/modal.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ type CoreDataSource struct {
ExcludeDbList string `gorm:"type:varchar(200);not null" json:"exclude_db_list"`
InsulateWordList string `gorm:"type:varchar(200);not null" json:"insulate_word_list"`
Principal string `gorm:"type:varchar(150);not null" json:"principal"`
CAFile string `gorm:"type:longtext;default ''" json:"ca_file"`
Cert string `gorm:"type:longtext;default ''" json:"cert"`
KeyFile string `gorm:"type:longtext;default ''" json:"key_file"`
}

type CoreGrained struct {
Expand Down

0 comments on commit 7b649a1

Please sign in to comment.