2023-07-19 16:25:41 +08:00
|
|
|
package client
|
|
|
|
|
2023-08-21 11:32:11 +08:00
|
|
|
import (
|
2023-11-29 10:46:07 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2023-08-21 11:32:11 +08:00
|
|
|
|
2023-11-29 10:46:07 +08:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2023-08-21 11:32:11 +08:00
|
|
|
)
|
|
|
|
|
2023-07-19 16:25:41 +08:00
|
|
|
type DBInfo struct {
|
2023-07-21 18:28:45 +08:00
|
|
|
From string `json:"from"`
|
2023-08-31 17:22:12 +08:00
|
|
|
Database string `json:"database"`
|
2023-07-19 16:25:41 +08:00
|
|
|
Address string `json:"address"`
|
|
|
|
Port uint `json:"port"`
|
2023-07-20 17:51:57 +08:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 16:25:41 +08:00
|
|
|
Password string `json:"password"`
|
|
|
|
|
2023-11-29 10:46:07 +08:00
|
|
|
SSL bool `json:"ssl"`
|
|
|
|
RootCert string `json:"rootCert"`
|
|
|
|
ClientKey string `json:"clientKey"`
|
|
|
|
ClientCert string `json:"clientCert"`
|
|
|
|
SkipVerify bool `json:"skipVerify"`
|
|
|
|
|
2023-07-19 16:25:41 +08:00
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
Version string `json:"version"`
|
2023-07-20 17:51:57 +08:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 16:25:41 +08: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 17:51:57 +08:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 16:25:41 +08: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 17:51:57 +08:00
|
|
|
Username string `json:"userName"`
|
2023-07-19 16:25:41 +08: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 17:51:57 +08:00
|
|
|
Username string `json:"userName"`
|
2023-07-21 18:28:45 +08:00
|
|
|
Password string `json:"password"`
|
2023-07-19 16:25:41 +08:00
|
|
|
OldPermission string `json:"oldPermission"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
|
|
|
|
Timeout uint `json:"timeout"` // second
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:08:13 +08:00
|
|
|
type BackupInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
TargetDir string `json:"targetDir"`
|
2023-07-27 14:32:23 +08:00
|
|
|
FileName string `json:"fileName"`
|
2023-07-25 17:08:13 +08: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 16:07:27 +08:00
|
|
|
type SyncDBInfo struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
From string `json:"from"`
|
2023-09-16 13:16:15 +08:00
|
|
|
MysqlName string `json:"mysqlName"`
|
2023-07-27 16:07:27 +08:00
|
|
|
Format string `json:"format"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
Permission string `json:"permission"`
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:25:41 +08: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 11:32:11 +08:00
|
|
|
|
2023-11-29 10:46:07 +08: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()
|
2023-12-08 17:26:07 +08:00
|
|
|
if len(rootCert) != 0 {
|
|
|
|
if ok := pool.AppendCertsFromPEM([]byte(rootCert)); !ok {
|
|
|
|
return "", errors.New("unable to append root cert to pool")
|
|
|
|
}
|
2023-11-29 10:46:07 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|