From ad8bf09034ef8244459c1eca5e323bbae9aee51f Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 7 Sep 2023 15:16:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A7=A3=E5=86=B3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=90=8C=E6=AD=A5=E6=97=B6=E5=AD=98=E5=9C=A8=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E5=AD=97=E7=AC=A6=E5=AF=BC=E8=87=B4=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#2214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/common/common.go | 15 ++++++++++ backend/utils/mysql/client/info.go | 43 ++++++++++++++++++++++++---- backend/utils/mysql/client/remote.go | 4 ++- go.mod | 1 + go.sum | 2 ++ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index 54f554456..c648b053e 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -14,6 +14,7 @@ import ( "time" "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/mozillazg/go-pinyin" ) func CompareVersion(version1 string, version2 string) bool { @@ -180,3 +181,17 @@ func LoadTimeZoneByCmd() string { } return fields[2] } + +func ConvertToPinyin(text string) string { + args := pinyin.NewArgs() + args.Fallback = func(r rune, a pinyin.Args) []string { + return []string{string(r)} + } + p := pinyin.Pinyin(text, args) + var strArr []string + for i := 0; i < len(p); i++ { + strArr = append(strArr, strings.Join(p[i], "")) + } + + return strings.Join(strArr, "") +} diff --git a/backend/utils/mysql/client/info.go b/backend/utils/mysql/client/info.go index e6d7a5f3d..003fe147a 100644 --- a/backend/utils/mysql/client/info.go +++ b/backend/utils/mysql/client/info.go @@ -1,6 +1,7 @@ package client import ( + mathRand "math/rand" "strings" "github.com/1Panel-dev/1Panel/backend/utils/common" @@ -94,14 +95,44 @@ var formatMap = map[string]string{ } func loadNameByDB(name, version string) string { + nameItem := common.ConvertToPinyin(name) if strings.HasPrefix(version, "5.6") { - if len(name) <= 16 { - return name + if len(nameItem) <= 16 { + return nameItem } - return strings.TrimSuffix(name[:10], "_") + "_" + common.RandStr(5) + return strings.TrimSuffix(nameItem[:10], "_") + "_" + common.RandStr(5) } - if len(name) <= 32 { - return name + if len(nameItem) <= 32 { + return nameItem } - return strings.TrimSuffix(name[:25], "_") + "_" + common.RandStr(5) + return strings.TrimSuffix(nameItem[:25], "_") + "_" + common.RandStr(5) +} + +func randomPassword(user string) string { + passwdItem := user + if len(user) > 6 { + passwdItem = user[:6] + } + num := []rune("1234567890") + uppercase := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ") + lowercase := []rune("abcdefghijklmnopqrstuvwxyz") + special := []rune(".%@!~_-") + + b := make([]rune, 10) + for i := 0; i < 2; i++ { + b[i] = lowercase[mathRand.Intn(len(lowercase))] + } + for i := 2; i < 4; i++ { + b[i] = uppercase[mathRand.Intn(len(uppercase))] + } + b[4] = special[mathRand.Intn(len(special))] + for i := 5; i < 9; i++ { + b[i] = num[mathRand.Intn(len(num))] + } + + for i := len(b) - 1; i > 0; i-- { + j := mathRand.Intn(i + 1) + b[i], b[j] = b[j], b[i] + } + return passwdItem + "-" + (string(b)) } diff --git a/backend/utils/mysql/client/remote.go b/backend/utils/mysql/client/remote.go index 5b8169053..79ee5dbc1 100644 --- a/backend/utils/mysql/client/remote.go +++ b/backend/utils/mysql/client/remote.go @@ -317,17 +317,19 @@ func (r *Remote) SyncDB(version string) ([]SyncDBInfo, error) { } if len(dataItem.Username) == 0 { dataItem.Username = loadNameByDB(dbName, version) + dataItem.Password = common.RandStr(16) if err := r.CreateUser(CreateInfo{ Name: dbName, Format: charsetName, Version: version, Username: dataItem.Username, - Password: common.RandStr(16), + Password: dataItem.Password, Permission: "%", Timeout: 300, }, false); err != nil { return datas, fmt.Errorf("sync db from remote server failed, err: create user failed %v", err) } + dataItem.Permission = "%" } else { if isLocal { diff --git a/go.mod b/go.mod index 4c1a86c1d..962c639f0 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/mholt/archiver/v4 v4.0.0-alpha.8 github.com/minio/minio-go/v7 v7.0.36 github.com/mojocn/base64Captcha v1.3.5 + github.com/mozillazg/go-pinyin v0.20.0 github.com/nicksnyder/go-i18n/v2 v2.1.2 github.com/opencontainers/image-spec v1.1.0-rc2 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 4b29d4502..f803bd997 100644 --- a/go.sum +++ b/go.sum @@ -606,6 +606,8 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSrwhe88TQQ= github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60= +github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ= +github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=