2023-07-19 08:25:41 +00:00
|
|
|
package client
|
|
|
|
|
2023-08-21 03:32:11 +00:00
|
|
|
import (
|
2023-11-29 02:46:07 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2023-08-21 03:32:11 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2023-11-29 02:46:07 +00:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2023-08-21 03:32:11 +00:00
|
|
|
)
|
|
|
|
|
2023-07-19 08:25:41 +00:00
|
|
|
type DBInfo struct {
|
2023-07-21 10:28:45 +00:00
|
|
|
From string `json:"from"`
|
2023-08-31 09:22:12 +00:00
|
|
|
Database string `json:"database"`
|
2023-07-19 08:25:41 +00:00
|
|
|
Address string `json:"address"`
|
|
|
|
Port uint `json:"port"`
|
2023-07-20 09:51:57 +00:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 08:25:41 +00:00
|
|
|
Password string `json:"password"`
|
|
|
|
|
2023-11-29 02:46:07 +00:00
|
|
|
SSL bool `json:"ssl"`
|
|
|
|
RootCert string `json:"rootCert"`
|
|
|
|
ClientKey string `json:"clientKey"`
|
|
|
|
ClientCert string `json:"clientCert"`
|
|
|
|
SkipVerify bool `json:"skipVerify"`
|
|
|
|
|
2023-07-19 08:25:41 +00:00
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Version string `json:"version"`
|
2023-07-20 09:51:57 +00:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 08:25:41 +00:00
|
|
|
Password string `json:"password"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
2023-07-20 09:51:57 +00:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 08:25:41 +00:00
|
|
|
Permission string `json:"permission"`
|
|
|
|
|
|
|
|
ForceDelete bool `json:"forceDelete"`
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type PasswordChangeInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
2023-07-20 09:51:57 +00:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 08:25:41 +00:00
|
|
|
Password string `json:"password"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type AccessChangeInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Version string `json:"version"`
|
2023-07-20 09:51:57 +00:00
|
|
|
Username string `json:"userName"`
|
2023-07-21 10:28:45 +00:00
|
|
|
Password string `json:"password"`
|
2023-07-19 08:25:41 +00:00
|
|
|
OldPermission string `json:"oldPermission"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
2023-07-25 09:08:13 +00:00
|
|
|
type BackupInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
TargetDir string `json:"targetDir"`
|
2023-07-27 06:32:23 +00:00
|
|
|
FileName string `json:"fileName"`
|
2023-07-25 09:08:13 +00:00
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type RecoverInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
SourceFile string `json:"sourceFile"`
|
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
2023-07-27 08:07:27 +00:00
|
|
|
type SyncDBInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
From string `json:"from"`
|
2023-09-16 05:16:15 +00:00
|
|
|
MysqlName string `json:"mysqlName"`
|
2023-07-27 08:07:27 +00:00
|
|
|
Format string `json:"format"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
}
|
|
|
|
|
2023-07-19 08:25:41 +00:00
|
|
|
var formatMap = map[string]string{
|
|
|
|
"utf8": "utf8_general_ci",
|
|
|
|
"utf8mb4": "utf8mb4_general_ci",
|
|
|
|
"gbk": "gbk_chinese_ci",
|
|
|
|
"big5": "big5_chinese_ci",
|
|
|
|
}
|
2023-08-21 03:32:11 +00:00
|
|
|
|
|
|
|
func loadNameByDB(name, version string) string {
|
2023-09-07 07:16:11 +00:00
|
|
|
nameItem := common.ConvertToPinyin(name)
|
2023-08-21 03:32:11 +00:00
|
|
|
if strings.HasPrefix(version, "5.6") {
|
2023-09-07 07:16:11 +00:00
|
|
|
if len(nameItem) <= 16 {
|
|
|
|
return nameItem
|
2023-08-21 03:32:11 +00:00
|
|
|
}
|
2023-09-07 07:16:11 +00:00
|
|
|
return strings.TrimSuffix(nameItem[:10], "_") + "_" + common.RandStr(5)
|
2023-08-21 03:32:11 +00:00
|
|
|
}
|
2023-09-07 07:16:11 +00:00
|
|
|
if len(nameItem) <= 32 {
|
|
|
|
return nameItem
|
2023-08-21 03:32:11 +00:00
|
|
|
}
|
2023-09-07 07:16:11 +00:00
|
|
|
return strings.TrimSuffix(nameItem[:25], "_") + "_" + common.RandStr(5)
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomPassword(user string) string {
|
|
|
|
passwdItem := user
|
|
|
|
if len(user) > 6 {
|
|
|
|
passwdItem = user[:6]
|
|
|
|
}
|
2023-09-15 10:48:14 +00:00
|
|
|
return passwdItem + "@" + common.RandStrAndNum(8)
|
2023-08-21 03:32:11 +00:00
|
|
|
}
|
2023-11-29 02:46:07 +00:00
|
|
|
|
|
|
|
func VerifyPeerCertFunc(pool *x509.CertPool) func([][]byte, [][]*x509.Certificate) error {
|
|
|
|
return func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
|
|
|
|
if len(rawCerts) == 0 {
|
|
|
|
return errors.New("no certificates available to verify")
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(rawCerts[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := x509.VerifyOptions{Roots: pool}
|
|
|
|
if _, err = cert.Verify(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnWithSSL(ssl, skipVerify bool, clientKey, clientCert, rootCert string) (string, error) {
|
|
|
|
if !ssl {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
if ok := pool.AppendCertsFromPEM([]byte(rootCert)); !ok {
|
|
|
|
return "", errors.New("unable to append root cert to pool")
|
|
|
|
}
|
|
|
|
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := mysql.RegisterTLSConfig("cloudsql", &tls.Config{
|
|
|
|
RootCAs: pool,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
InsecureSkipVerify: skipVerify,
|
|
|
|
VerifyPeerCertificate: VerifyPeerCertFunc(pool),
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "&tls=cloudsql", nil
|
|
|
|
}
|