2023-07-19 08:25:41 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2023-07-25 09:08:13 +00:00
|
|
|
"os"
|
2023-08-01 03:20:16 +00:00
|
|
|
"os/exec"
|
2023-07-19 08:25:41 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-07-25 09:08:13 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2023-08-01 03:20:16 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/mysql/helper"
|
2023-07-19 08:25:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Remote struct {
|
2023-07-25 09:08:13 +00:00
|
|
|
Client *sql.DB
|
2023-08-31 09:22:12 +00:00
|
|
|
Database string
|
2023-07-25 09:08:13 +00:00
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
Address string
|
|
|
|
Port uint
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 09:08:13 +00:00
|
|
|
func NewRemote(db Remote) *Remote {
|
|
|
|
return &db
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Create(info CreateInfo) error {
|
|
|
|
createSql := fmt.Sprintf("create database `%s` default character set %s collate %s", info.Name, info.Format, formatMap[info.Format])
|
|
|
|
if err := r.ExecSQL(createSql, info.Timeout); err != nil {
|
|
|
|
if strings.Contains(err.Error(), "ERROR 1007") {
|
|
|
|
return buserr.New(constant.ErrDatabaseIsExist)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:32:11 +00:00
|
|
|
if err := r.CreateUser(info, true); err != nil {
|
2023-08-04 10:20:04 +00:00
|
|
|
_ = r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout)
|
2023-07-19 08:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:32:11 +00:00
|
|
|
func (r *Remote) CreateUser(info CreateInfo, withDeleteDB bool) error {
|
2023-07-19 08:25:41 +00:00
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("create user %s identified by '%s';", user, info.Password), info.Timeout); err != nil {
|
2023-08-21 03:32:11 +00:00
|
|
|
if withDeleteDB {
|
|
|
|
_ = r.Delete(DeleteInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Permission: info.Permission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300})
|
|
|
|
if strings.Contains(err.Error(), "ERROR 1396") {
|
|
|
|
return buserr.New(constant.ErrUserIsExist)
|
|
|
|
}
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
grantStr := fmt.Sprintf("grant all privileges on `%s`.* to %s", info.Name, user)
|
|
|
|
if info.Name == "*" {
|
|
|
|
grantStr = fmt.Sprintf("grant all privileges on *.* to %s", user)
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(info.Version, "5.7") || strings.HasPrefix(info.Version, "5.6") {
|
|
|
|
grantStr = fmt.Sprintf("%s identified by '%s' with grant option;", grantStr, info.Password)
|
|
|
|
}
|
|
|
|
if err := r.ExecSQL(grantStr, info.Timeout); err != nil {
|
2023-08-21 03:32:11 +00:00
|
|
|
if withDeleteDB {
|
|
|
|
_ = r.Delete(DeleteInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Permission: info.Permission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300})
|
|
|
|
}
|
2023-07-19 08:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Delete(info DeleteInfo) error {
|
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
if strings.HasPrefix(info.Version, "5.6") {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop user %s", user), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop user if exists %s", user), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(info.Name) != 0 {
|
|
|
|
if err := r.ExecSQL(fmt.Sprintf("drop database if exists `%s`", info.Name), info.Timeout); err != nil && !info.ForceDelete {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !info.ForceDelete {
|
|
|
|
global.LOG.Info("execute delete database sql successful, now start to drop uploads and records")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ChangePassword(info PasswordChangeInfo) error {
|
2023-07-20 09:51:57 +00:00
|
|
|
if info.Username != "root" {
|
2023-07-19 08:25:41 +00:00
|
|
|
var userlist []string
|
|
|
|
if strings.Contains(info.Permission, ",") {
|
|
|
|
ips := strings.Split(info.Permission, ",")
|
|
|
|
for _, ip := range ips {
|
|
|
|
if len(ip) != 0 {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, ip))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-20 09:51:57 +00:00
|
|
|
userlist = append(userlist, fmt.Sprintf("'%s'@'%s'", info.Username, info.Permission))
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range userlist {
|
|
|
|
passwordChangeSql := fmt.Sprintf("set password for %s = password('%s')", user, info.Password)
|
|
|
|
if !strings.HasPrefix(info.Version, "5.7") && !strings.HasPrefix(info.Version, "5.6") {
|
2023-09-01 04:28:12 +00:00
|
|
|
passwordChangeSql = fmt.Sprintf("ALTER USER %s IDENTIFIED BY '%s';", user, info.Password)
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
if err := r.ExecSQL(passwordChangeSql, info.Timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := r.ExecSQLForHosts(info.Timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, host := range hosts {
|
|
|
|
if host == "%" || host == "localhost" {
|
|
|
|
passwordRootChangeCMD := fmt.Sprintf("set password for 'root'@'%s' = password('%s')", host, info.Password)
|
|
|
|
if !strings.HasPrefix(info.Version, "5.7") && !strings.HasPrefix(info.Version, "5.6") {
|
2023-08-29 02:50:15 +00:00
|
|
|
passwordRootChangeCMD = fmt.Sprintf("alter user 'root'@'%s' identified by '%s';", host, info.Password)
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
if err := r.ExecSQL(passwordRootChangeCMD, info.Timeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ChangeAccess(info AccessChangeInfo) error {
|
2023-07-20 09:51:57 +00:00
|
|
|
if info.Username == "root" {
|
2023-07-19 08:25:41 +00:00
|
|
|
info.OldPermission = "%"
|
|
|
|
info.Name = "*"
|
2023-08-07 03:52:11 +00:00
|
|
|
info.Password = r.Password
|
2023-07-19 08:25:41 +00:00
|
|
|
}
|
|
|
|
if info.Permission != info.OldPermission {
|
|
|
|
if err := r.Delete(DeleteInfo{
|
|
|
|
Version: info.Version,
|
2023-07-20 09:51:57 +00:00
|
|
|
Username: info.Username,
|
2023-07-19 08:25:41 +00:00
|
|
|
Permission: info.OldPermission,
|
|
|
|
ForceDelete: true,
|
|
|
|
Timeout: 300}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-20 09:51:57 +00:00
|
|
|
if info.Username == "root" {
|
2023-07-19 08:25:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-07-21 10:28:45 +00:00
|
|
|
if err := r.CreateUser(CreateInfo{
|
|
|
|
Name: info.Name,
|
|
|
|
Version: info.Version,
|
|
|
|
Username: info.Username,
|
|
|
|
Password: info.Password,
|
|
|
|
Permission: info.Permission,
|
|
|
|
Timeout: info.Timeout,
|
2023-08-21 03:32:11 +00:00
|
|
|
}, false); err != nil {
|
2023-07-20 09:51:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.ExecSQL("flush privileges", 300); err != nil {
|
2023-07-25 09:08:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
func (r *Remote) Backup(info BackupInfo) error {
|
2023-07-25 09:08:13 +00:00
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
if !fileOp.Stat(info.TargetDir) {
|
|
|
|
if err := os.MkdirAll(info.TargetDir, os.ModePerm); err != nil {
|
2023-07-27 06:32:23 +00:00
|
|
|
return fmt.Errorf("mkdir %s failed, err: %v", info.TargetDir, err)
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 06:32:23 +00:00
|
|
|
fileNameItem := info.TargetDir + "/" + strings.TrimSuffix(info.FileName, ".gz")
|
2023-07-25 09:08:13 +00:00
|
|
|
dns := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=%s&parseTime=true&loc=Asia%sShanghai", r.User, r.Password, r.Address, r.Port, info.Name, info.Format, "%2F")
|
|
|
|
|
2023-07-27 06:32:23 +00:00
|
|
|
f, _ := os.OpenFile(fileNameItem, os.O_RDWR|os.O_CREATE, 0755)
|
2023-07-25 09:08:13 +00:00
|
|
|
defer f.Close()
|
2023-08-01 03:20:16 +00:00
|
|
|
if err := helper.Dump(dns, helper.WithData(), helper.WithDropTable(), helper.WithWriter(f)); err != nil {
|
2023-07-27 06:32:23 +00:00
|
|
|
return err
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-01 03:20:16 +00:00
|
|
|
gzipCmd := exec.Command("gzip", fileNameItem)
|
|
|
|
stdout, err := gzipCmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gzip file %s failed, stdout: %v, err: %v", strings.TrimSuffix(info.FileName, ".gz"), string(stdout), err)
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
2023-07-27 06:32:23 +00:00
|
|
|
return nil
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) Recover(info RecoverInfo) error {
|
|
|
|
fileName := info.SourceFile
|
|
|
|
if strings.HasSuffix(info.SourceFile, ".sql.gz") {
|
|
|
|
fileName = strings.TrimSuffix(info.SourceFile, ".gz")
|
2023-08-01 03:20:16 +00:00
|
|
|
gzipCmd := exec.Command("gunzip", info.SourceFile)
|
|
|
|
stdout, err := gzipCmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gunzip file %s failed, stdout: %v, err: %v", info.SourceFile, string(stdout), err)
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
2023-08-03 08:19:30 +00:00
|
|
|
defer func() {
|
|
|
|
gzipCmd := exec.Command("gzip", fileName)
|
|
|
|
_, _ = gzipCmd.CombinedOutput()
|
|
|
|
}()
|
2023-07-25 09:08:13 +00:00
|
|
|
}
|
|
|
|
dns := fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=%s&parseTime=true&loc=Asia%sShanghai", r.User, r.Password, r.Address, r.Port, info.Name, info.Format, "%2F")
|
|
|
|
f, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2023-08-01 03:20:16 +00:00
|
|
|
if err := helper.Source(dns, f, helper.WithMergeInsert(1000)); err != nil {
|
2023-07-19 08:25:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 08:07:27 +00:00
|
|
|
func (r *Remote) SyncDB(version string) ([]SyncDBInfo, error) {
|
|
|
|
var datas []SyncDBInfo
|
2023-08-16 15:36:11 +00:00
|
|
|
rows, err := r.Client.Query("select schema_name, default_character_set_name from information_schema.SCHEMATA")
|
2023-07-27 08:07:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var dbName, charsetName string
|
|
|
|
if err = rows.Scan(&dbName, &charsetName); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
if dbName == "information_schema" || dbName == "mysql" || dbName == "performance_schema" || dbName == "sys" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dataItem := SyncDBInfo{
|
|
|
|
Name: dbName,
|
2023-08-31 09:22:12 +00:00
|
|
|
From: "remote",
|
|
|
|
MysqlName: r.Database,
|
2023-07-27 08:07:27 +00:00
|
|
|
Format: charsetName,
|
|
|
|
}
|
2023-08-16 15:36:11 +00:00
|
|
|
userRows, err := r.Client.Query("select user,host from mysql.db where db = ?", dbName)
|
2023-07-27 08:07:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var permissionItem []string
|
|
|
|
isLocal := true
|
|
|
|
i := 0
|
|
|
|
for userRows.Next() {
|
|
|
|
var user, host string
|
|
|
|
if err = userRows.Scan(&user, &host); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
if user == "root" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
dataItem.Username = user
|
|
|
|
}
|
|
|
|
if dataItem.Username == user && host == "%" {
|
|
|
|
isLocal = false
|
|
|
|
dataItem.Permission = "%"
|
|
|
|
} else if dataItem.Username == user && host != "localhost" {
|
|
|
|
isLocal = false
|
|
|
|
permissionItem = append(permissionItem, host)
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if len(dataItem.Username) == 0 {
|
2023-08-22 08:32:12 +00:00
|
|
|
dataItem.Username = loadNameByDB(dbName, version)
|
2023-09-07 09:42:10 +00:00
|
|
|
dataItem.Password = randomPassword(dataItem.Username)
|
2023-07-27 08:07:27 +00:00
|
|
|
if err := r.CreateUser(CreateInfo{
|
|
|
|
Name: dbName,
|
|
|
|
Format: charsetName,
|
|
|
|
Version: version,
|
2023-08-22 08:32:12 +00:00
|
|
|
Username: dataItem.Username,
|
2023-09-07 07:16:11 +00:00
|
|
|
Password: dataItem.Password,
|
2023-07-27 08:07:27 +00:00
|
|
|
Permission: "%",
|
|
|
|
Timeout: 300,
|
2023-08-21 03:32:11 +00:00
|
|
|
}, false); err != nil {
|
|
|
|
return datas, fmt.Errorf("sync db from remote server failed, err: create user failed %v", err)
|
2023-07-27 08:07:27 +00:00
|
|
|
}
|
|
|
|
dataItem.Permission = "%"
|
|
|
|
} else {
|
|
|
|
if isLocal {
|
|
|
|
dataItem.Permission = "localhost"
|
|
|
|
}
|
|
|
|
if len(dataItem.Permission) == 0 {
|
|
|
|
dataItem.Permission = strings.Join(permissionItem, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
datas = append(datas, dataItem)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return datas, err
|
|
|
|
}
|
|
|
|
return datas, nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 08:25:41 +00:00
|
|
|
func (r *Remote) Close() {
|
|
|
|
_ = r.Client.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ExecSQL(command string, timeout uint) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if _, err := r.Client.ExecContext(ctx, command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return buserr.New(constant.ErrExecTimeOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remote) ExecSQLForHosts(timeout uint) ([]string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
results, err := r.Client.QueryContext(ctx, "select host from mysql.user where user='root';")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
return nil, buserr.New(constant.ErrExecTimeOut)
|
|
|
|
}
|
|
|
|
var rows []string
|
|
|
|
for results.Next() {
|
|
|
|
var host string
|
|
|
|
if err := results.Scan(&host); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rows = append(rows, host)
|
|
|
|
}
|
|
|
|
return rows, nil
|
|
|
|
}
|