feat: 解决数据库同步时存在特殊字符导致失败的问题 (#2214)

This commit is contained in:
ssongliu 2023-09-07 15:16:11 +08:00 committed by GitHub
parent e75c9a0304
commit ad8bf09034
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 7 deletions

View File

@ -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, "")
}

View File

@ -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))
}

View File

@ -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 {

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=