From 0a9a82118b3173e1afb66c1d19061e80106b7b38 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 21 Feb 2025 20:41:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=8E=B7=E5=8F=96=E7=BD=91=E5=85=83?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E4=BF=A1=E6=81=AF=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- features/state/getstate.go | 77 ++++++++++++++-- lib/dborm/dborm.go | 150 ++------------------------------ src/framework/database/db/db.go | 4 +- 3 files changed, 82 insertions(+), 149 deletions(-) diff --git a/features/state/getstate.go b/features/state/getstate.go index d07c33e2..bcb9135c 100644 --- a/features/state/getstate.go +++ b/features/state/getstate.go @@ -18,7 +18,9 @@ import ( "be.ems/lib/global" "be.ems/lib/log" "be.ems/lib/services" + cfg "be.ems/src/framework/config" "be.ems/src/framework/constants" + "be.ems/src/framework/database/db" ) type CpuUsage struct { @@ -213,7 +215,7 @@ func GetOneLicenseInfoFromNF(w http.ResponseWriter, r *http.Request) { //systemState := make(map[string]interface{}) systemState := &SysState{} - result := make(map[string]interface{}) + var result map[string]interface{} //sysInfo := &SystemInfo{} omcNeTypeLower := "omc" if config.GetYamlConfig().OMC.NeType != "" { @@ -329,7 +331,7 @@ func GetAllLicenseInfoFromNF(w http.ResponseWriter, r *http.Request) { } for _, ne := range neList { - result := make(map[string]interface{}) + var result map[string]interface{} log.Debugf("r.RemoteAddr: %s omcNeTypeLower: %s", r.RemoteAddr, omcNeTypeLower) log.Debug("ne: ", ne) //if strings.ToLower(ne.NeType) != omcNeTypeLower || !strings.Contains(r.RemoteAddr, ne.Ip) { @@ -455,7 +457,7 @@ func GetOneSysinfoFromNF(w http.ResponseWriter, r *http.Request) { //systemState := make(map[string]interface{}) systemState := &SysState{} - result := make(map[string]interface{}) + var result map[string]interface{} //sysInfo := &SystemInfo{} omcNeTypeLower := "omc" if config.GetYamlConfig().OMC.NeType != "" { @@ -600,7 +602,7 @@ func GetAllSysinfoFromNF(w http.ResponseWriter, r *http.Request) { } for _, ne := range neList { - result := make(map[string]interface{}) + var result map[string]interface{} log.Debugf("r.RemoteAddr: %s omcNeTypeLower: %s", r.RemoteAddr, omcNeTypeLower) log.Debug("ne: ", ne) //if strings.ToLower(ne.NeType) != omcNeTypeLower || !strings.Contains(r.RemoteAddr, ne.Ip) { @@ -914,7 +916,7 @@ func GetEMSState(ip string) *SysState { } hostName, _ := os.Hostname() - dbInfo, _ := dborm.XormGetMySQLVersion() + dbInfo := getDbInfo() emsState := &SysState{ HostName: hostName, OsInfo: getUnameStr(), @@ -934,3 +936,68 @@ func GetEMSState(ip string) *SysState { //getSystemInfo() return emsState } + +func getDbInfo() string { + var dbConfig map[string]any + defSource := cfg.Get("database.defaultDataSourceName").(string) + datasource := cfg.Get("database.datasource").(map[string]any) + for key, value := range datasource { + item := value.(map[string]any) + if key == defSource { + dbConfig = item + break + } + } + + if dbConfig["type"] == "mysql" { + return getMySQLVersion() + } + if dbConfig["type"] == "sqlite" { + return "3.35.5 SQLite3" + } + return "" +} + +func getMySQLVersion() string { + var info string = "" + + var ver, verComment, verCompileOS, verCompile string + + // 版本 + m, err := db.RawDB("", "SHOW VARIABLES LIKE 'version'", nil) + if err != nil || len(m) != 1 { + return info + } + if v, ok := m[0]["Value"]; ok { + ver = v.(string) + } + + // 详细版本 + m, err = db.RawDB("", "SHOW VARIABLES LIKE 'version_comment'", nil) + if err != nil || len(m) != 1 { + return info + } + if v, ok := m[0]["Value"]; ok { + verComment = v.(string) + } + + // 编译操作系统 + m, err = db.RawDB("", "SHOW VARIABLES LIKE 'version_compile_os'", nil) + if err != nil || len(m) != 1 { + return info + } + if v, ok := m[0]["Value"]; ok { + verCompileOS = v.(string) + } + + // 编译机器 + m, err = db.RawDB("", "SHOW VARIABLES LIKE 'version_compile_machine'", nil) + if err != nil || len(m) != 1 { + return info + } + if v, ok := m[0]["Value"]; ok { + verCompileOS = v.(string) + } + + return fmt.Sprintf("%s %s, for %s (%s)", ver, verComment, verCompileOS, verCompile) +} diff --git a/lib/dborm/dborm.go b/lib/dborm/dborm.go index a239afe7..1943ee0b 100644 --- a/lib/dborm/dborm.go +++ b/lib/dborm/dborm.go @@ -205,34 +205,6 @@ type NeInfo struct { UpdateTime time.Time `json:"updateTime" xorm:"<-"` } -func XormGetMySQLVersion() (string, error) { - var versionInfo string = "" - - var ver, verComment, verCompileOS, verCompile string - _, err := xEngine.SQL("SHOW VARIABLES LIKE 'version'").Get(&ver, &ver) - if err != nil { - return versionInfo, err - } - - _, err = xEngine.SQL("SHOW VARIABLES LIKE 'version_comment'").Get(&verComment, &verComment) - if err != nil { - return versionInfo, err - } - - _, err = xEngine.SQL("SHOW VARIABLES LIKE 'version_compile_os'").Get(&verCompileOS, &verCompileOS) - if err != nil { - return versionInfo, err - } - - _, err = xEngine.SQL("SHOW VARIABLES LIKE 'version_compile_machine'").Get(&verCompile, &verCompile) - if err != nil { - return versionInfo, err - } - - versionInfo = fmt.Sprintf("%s %s, for %s (%s)", ver, verComment, verCompileOS, verCompile) - return versionInfo, nil -} - func XormGetNeInfo(neType string, neId string) (*NeInfo, error) { log.Debug("XormGetNeInfo processing... ") @@ -394,19 +366,19 @@ func XormParseResult(body []byte) ([]NeInfo, error) { var neInfo []NeInfo var re interface{} if data["data"] == nil { - return nil, errors.New("The data is Not found") + return nil, errors.New("the data is not found") } for _, d := range data["data"].([]interface{}) { if d == nil { - return nil, errors.New("The data is Not found") + return nil, errors.New("the data is Not found") } for _, re = range d.(map[string]interface{}) { if re == nil { - return nil, errors.New("The data is Not found") + return nil, errors.New("the data is Not found") } for _, rc := range re.([]interface{}) { if rc == nil { - return nil, errors.New("The data is Not found") + return nil, errors.New("the data is Not found") } var s NeInfo record := rc.(map[string]interface{}) @@ -492,9 +464,7 @@ func ConstructUpdateSQL(tableName string, updateData interface{}, whereCondition func ConstructDeleteSQL(tableName string, whereCondition string) string { log.Debug("ConstructDeleteSQL processing... ") - var sql string - - sql = "DELETE from " + tableName + " WHERE " + whereCondition + var sql string = "DELETE from " + tableName + " WHERE " + whereCondition log.Debug("sql:", sql) return sql } @@ -526,7 +496,7 @@ func GetTableByWhere(whereCondition string, tableName string) (*[]interface{}, e if err != nil { log.Errorf("Failed to get table %s from database:%v", tableName, err) return nil, err - } else if has == false { + } else if !has { log.Infof("Not found table %s from database:where=%d", tableName, whereCondition) return nil, nil } @@ -543,7 +513,7 @@ func GetTableById(id int, tableName string) (*[]interface{}, error) { if err != nil { log.Errorf("Failed to get table %s from database:id=%d, %v", tableName, id, err) return nil, err - } else if has == false { + } else if !has { log.Infof("Not found table %s from database:id=%d", tableName, id) return nil, nil } @@ -576,112 +546,8 @@ func XormUpdateTableByWhere(whereCondition string, tableName string, tbInfo inte return affected, nil } -// 记录密码登录错误次数 -func pwdErrCountAdd(accountId, profileStr string, reset bool) int { - if profileStr == "" { - profileStr = "{}" - } - profile := make(map[string]any) - count := 0 - timeMlli := time.Now().UnixMilli() - - // 反序列去值 - err := json.Unmarshal([]byte(profileStr), &profile) - if err != nil { - log.Error("json Unmarshal:%s", err.Error()) - return 0 - } - - // 读取配置信息 登录策略设置 - result, err := XormGetConfig("Security", "loginSecurity") - if err != nil { - return 0 - } - data := make(map[string]any) - err = json.Unmarshal([]byte(result["value_json"].(string)), &data) - if err != nil { - return 0 - } - limitNum := data["limit_num"].(string) - passwordLimitTime := data["password_limit_time"].(string) - - // 重置 - if reset { - // xEngine.Exec("UPDATE user SET status = 'Active' WHERE account_id = ?", accountId) - profile["pwdErrCount"] = float64(0) - profile["pwdErrTime"] = 0 - } else { - if v, ok := profile["pwdErrTime"]; ok && v != nil { - // 获取当前时间 - currentTime := time.Now() - - // 获取给定的时间戳(毫秒) - timestamp := int64(v.(float64)) // 要比较的时间戳(毫秒) - // 将时间戳转换为时间类型 - tm := time.Unix(timestamp/1000, (timestamp%1000)*int64(time.Millisecond)) - - // 计算当前时间与给定时间之间的差值 - duration := currentTime.Sub(tm) - - // // 比较差值是否超过30分钟 - // if duration.Minutes() > 30 { - // xEngine.Exec("UPDATE user SET status = 'Active' WHERE account_id = ?", accountId) - // profile["pwdErrCount"] = float64(0) - // profile["pwdErrTime"] = 0 - // } - - // 比较差值是否小于限定时间s - passwordLimitTimeInt, err := strconv.Atoi(passwordLimitTime) - if err != nil { - passwordLimitTimeInt = 0 - } - if duration.Seconds() > float64(passwordLimitTimeInt) { - // xEngine.Exec("UPDATE user SET status = 'Active' WHERE account_id = ?", accountId) - profile["pwdErrCount"] = float64(0) - profile["pwdErrTime"] = 0 - } - } - - if v, ok := profile["pwdErrCount"]; ok && v != nil { - count = int(v.(float64)) + 1 - profile["pwdErrCount"] = count - profile["pwdErrTime"] = timeMlli - // 错误最大后锁定 - limitNumInt, err := strconv.Atoi(limitNum) - if err != nil { - limitNumInt = 0 - } - // if count == limitNumInt { - // _, err := xEngine.Exec("UPDATE user SET status = 'Locked' WHERE account_id = ?", accountId) - // if err != nil { - // return count - // } - // } - if count >= limitNumInt { - return count - } - } else { - count = 1 - profile["pwdErrCount"] = count - profile["pwdErrTime"] = timeMlli - } - } - - // 序列后记录 - strByte, err := json.Marshal(profile) - if err != nil { - log.Error("json Marshal:%s", err.Error()) - return count - } - _, err = xEngine.Exec("UPDATE user SET profile = ? WHERE account_id = ?", string(strByte), accountId) - if err != nil { - return count - } - return count -} - func XormIsExistUser(accid string) (bool, error) { - log.Info("XormIsExistUser processing... ") + log.Info("xormIsExistUser processing... ") exist, err := xEngine.Table("user"). Where("account_id=?", accid). diff --git a/src/framework/database/db/db.go b/src/framework/database/db/db.go index 59dec95d..e07b44f6 100644 --- a/src/framework/database/db/db.go +++ b/src/framework/database/db/db.go @@ -39,7 +39,7 @@ func loadDialect() map[string]dialectInfo { dsn := fmt.Sprint(item["database"]) dialects[key] = dialectInfo{ dialectic: sqlite.Open(dsn), - logging: item["logging"].(bool), + logging: parse.Boolean(item["logging"]), } case "mysql": dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", @@ -51,7 +51,7 @@ func loadDialect() map[string]dialectInfo { ) dialects[key] = dialectInfo{ dialectic: mysql.Open(dsn), - logging: item["logging"].(bool), + logging: parse.Boolean(item["logging"]), } default: logger.Warnf("%s: %v\n Not Load DB Config Type", key, item)