123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- package bo
- import (
- "encoding/json"
- "errors"
- "fmt"
- "io/ioutil"
- "log"
- "os"
- "scd_check_tools/email"
- "scd_check_tools/global"
- "scd_check_tools/logger"
- "scd_check_tools/models/enum"
- "scd_check_tools/mqtt"
- "scd_check_tools/tools"
- "strconv"
- "strings"
- "sync"
- "github.com/astaxie/beego/orm"
- _ "github.com/astaxie/beego/orm"
- )
- type Global_sys_param struct {
- Param_name string `orm:"pk"`
- Param_value string `orm:"size(2000)"`
- Param_memo string `orm:"size(50)"`
- }
- func init() {
- orm.RegisterModel(new(Global_sys_param))
- }
- //初始化加载系统参数。建议在系统启动时调用
- func LoadSysParam() {
- //用户密码规则及强度:simple|strong
- GetSysParamValue("user_pwd_rule", "simple")
- //初始化用户密码过期时间(天)。默认为60天,为0时永不过期
- GetSysParamValue("user_pwd_expire_day", "60")
- //初始化用户密码修改模式。默认只能管理员修改修改。可配置成自己可修改
- GetSysParamValue("user_pwd_edit_mod", "admin") //admin|self
- //初始化登录失败锁定次数
- GetSysParamValue("login_fail_maxcount", "5") //默认为5次
- //初始化登录失败锁定时长,单位:秒
- GetSysParamValue("login_fail_locktime", "180") //默认180秒
- r, _ := GetSysParamValue("session_timeout", "600")
- rInt, _ := strconv.Atoi(r)
- if rInt > 60 {
- global.SessionTimeout = rInt
- } else {
- //永不过期
- global.SessionTimeout = 0
- }
- loginFailMaxCount, _ := GetSysParamValue("login_fail_maxcount", "5")
- global.LoginFailMaxCount, _ = strconv.Atoi(loginFailMaxCount)
- global.UserLoginClientLimt, _ = GetSysParamValue("user_login_client_limt", "none")
- global.AllowAccessIps, _ = GetSysParamValue("allow_access_ips", "*")
- //初始化人像比对相似度
- //r, _ = GetSysParamValue("FaceSameValue", "60")
- //global.FaceSameValue = r
- //初始化Mqtt 地址参数。前端页面连接时的信息
- //获取配置的mqtt地址
- mqttinfo, _ := GetSysParamValue("Mqtt_JS_URL", "")
- if mqttinfo == "" {
- cnffile := "conf/mqtt.cnf"
- fileHanlder, err := os.Open(cnffile)
- if err == nil {
- txt, _ := ioutil.ReadAll(fileHanlder)
- fileHanlder.Close()
- txtStr := string(txt)
- if txtStr != "" {
- cfgdata := mqtt.Config{}
- err = json.Unmarshal(txt, &cfgdata)
- if err == nil {
- mqttinfo = cfgdata.Host + ":8083" //mqtt的js协议默认端口是8083
- if cfgdata.Host == "127.0.0.1" || cfgdata.Host == "" {
- //获取本机地址
- mqttinfo, _ := tools.LocalIPv4()
- mqttinfo = mqttinfo + ":8083"
- }
- sp := Global_sys_param{Param_name: "Mqtt_JS_URL", Param_value: mqttinfo, Param_memo: "mqtt的js库连接地址"}
- SaveSysParam(sp)
- }
- }
- }
- }
- }
- func SaveSysParam(obj Global_sys_param, userinfo ...map[string]interface{}) (result int64, err error) {
- if obj.Param_name == "log_fliterrule_staff_list" && obj.Param_value != "" {
- //需要验证用户名称有效性
- usernames := strings.ReplaceAll(obj.Param_value, ",", ",")
- usernameList := strings.Split(usernames, ",")
- for _, uname := range usernameList {
- has, _ := HasUserName(uname)
- if !has {
- return 0, errors.New("用户名称" + uname + "不存在!")
- }
- }
- }
- if obj.Param_name == "allow_access_ips" {
- if obj.Param_value != "" && obj.Param_value != "*" {
- // 判断ip有效性
- if !tools.VerifyIPFormat(obj.Param_value) {
- return 0, errors.New("无效的IP或IP段格式!")
- }
- }
- }
- if obj.Param_name == "log_alarm_email" && obj.Param_value != "" {
- //邮箱格式校验
- if !tools.VerifyEmailFormat(obj.Param_value) {
- return 0, errors.New("邮箱格式不正确!")
- }
- if !email.EmailConfig.Enable {
- return 0, errors.New("系统还未配置邮件发送!")
- }
- }
- if obj.Param_name == "log_size_max" {
- //日志容量阈值配置
- v, er := strconv.Atoi(obj.Param_value)
- if er != nil {
- return 0, errors.New("容量阈值只能为数值!")
- }
- if v <= 0 {
- return 0, errors.New("容量阈值必须大于0!")
- }
- }
- if obj.Param_name == "log_size_alarm1" {
- //日志预警阈值配置
- v, er := strconv.Atoi(obj.Param_value)
- if er != nil {
- return 0, errors.New("预警阈值只能为数值!")
- }
- if v <= 0 {
- return 0, errors.New("预警阈值必须大于0!")
- }
- v2, _ := GetSysParamValue("log_size_alarm2", "0")
- if v2 != "0" {
- v2v, _ := strconv.Atoi(v2)
- if v > v2v {
- return 0, errors.New("预警阈值不能大于告警阈值!")
- }
- }
- }
- if obj.Param_name == "log_size_alarm2" {
- //日志告警阈值配置
- v, er := strconv.Atoi(obj.Param_value)
- if er != nil {
- return 0, errors.New("告警阈值只能为数值!")
- }
- if v <= 0 {
- return 0, errors.New("告警阈值必须大于0!")
- }
- v2, _ := GetSysParamValue("log_size_alarm1", "0")
- if v2 != "0" {
- v2v, _ := strconv.Atoi(v2)
- if v < v2v {
- return 0, errors.New("告警阈值不能小于预警阈值!")
- }
- }
- }
- dblog := new(SystemLog)
- if len(userinfo) == 0 {
- dblog.SetUserInfo(map[string]interface{}{"name": "", "ip": "127.0.0.1"})
- dblog.Eventtype = enum.OptEventType_System
- } else {
- dblog.SetUserInfo(userinfo[0])
- dblog.Eventtype = enum.OptEventType_Bus
- }
- dblog.Audittype = enum.AuditType_admin_system_paramater
- dblog.Logtype = enum.LogType_Update
- dblog.Eventlevel = enum.OptEventLevel_Hight
- dblog.Description = fmt.Sprintf("保存系统参数,数据:%+v", obj)
- o := orm.NewOrm()
- readObj := Global_sys_param{Param_name: obj.Param_name}
- has := o.Read(&readObj)
- var id int64
- if has == nil {
- id, err = o.Update(&obj)
- } else if has == orm.ErrNoRows {
- dblog.Logtype = enum.LogType_Insert
- id, err = o.Insert(&obj)
- } else {
- return 0, err
- }
- if err != nil {
- logger.Logger.Error(err)
- dblog.Fail2()
- return 0, nil
- }
- dblog.Success2()
- global.GoCahce.Set(obj.Param_name, obj.Param_value, -1)
- switch obj.Param_name {
- case "session_timeout":
- if tools.IsEmpty(obj.Param_value) == "" {
- global.SessionTimeout = 0
- } else {
- global.SessionTimeout, _ = strconv.Atoi(obj.Param_value)
- }
- case "login_fail_maxcount":
- global.LoginFailMaxCount, _ = strconv.Atoi(obj.Param_value)
- case "login_fail_locktime":
- global.LoginFailLockTime, _ = strconv.Atoi(obj.Param_value)
- if global.LoginFailLockTime <= 0 {
- global.LoginFailLockTime = 180
- }
- case "user_pwd_expire_day":
- if obj.Param_value == "0" {
- //永不过期,设置所有用户的密码过期时间
- o.Raw("update t_data_user set pwd_expire=?", "1970-01-01 00:00:00").Exec()
- } else {
- o.Raw("update t_data_user set pwd_expire=date_add(now(),interval ? DAY)", obj.Param_value).Exec()
- }
- case "user_login_client_limt":
- if obj.Param_value != "none" && obj.Param_value != "o2o" {
- log.Println("无效的user_login_client_limt参数值:" + obj.Param_value)
- } else {
- global.UserLoginClientLimt = obj.Param_value
- }
- case "allow_access_ips":
- if obj.Param_value == "" {
- obj.Param_value = "*"
- }
- global.AllowAccessIps = obj.Param_value
- //重新初始化已经访问ip队列
- global.AccessedIps = sync.Map{}
- }
- return id, err
- }
- func GetSysParamValue(param_name string, defaultvalue string) (result string, err error) {
- if v, has := global.GoCahce.Get(param_name); has {
- return tools.IsEmpty(v), nil
- }
- o := orm.NewOrm()
- v := Global_sys_param{Param_name: param_name}
- if err = o.Read(&v); err == nil {
- global.GoCahce.Set(param_name, v.Param_value, -1)
- return v.Param_value, nil
- } else {
- v.Param_value = defaultvalue
- SaveSysParam(v)
- return defaultvalue, nil
- }
- }
- func GetSysParamList(obj Global_sys_param, userinfo map[string]interface{}) (maps []orm.Params, err error) {
- dblog := new(SystemLog)
- dblog.SetUserInfo(userinfo)
- dblog.Audittype = enum.AuditType_admin_system_paramater
- dblog.Logtype = enum.LogType_Query
- dblog.Eventlevel = enum.OptEventLevel_Low
- o := orm.NewOrm()
- var num int64
- var dberr error
- if obj.Param_name != "" {
- sql := "select * FROM global_sys_param WHERE param_name=?"
- dblog.Description = fmt.Sprintf("SQL:%s,参数:%+v", sql, obj.Param_name)
- num, dberr = o.Raw(sql, obj.Param_name).Values(&maps)
- } else {
- sql := "select * FROM global_sys_param"
- dblog.Description = sql
- num, dberr = o.Raw(sql).Values(&maps)
- }
- if dberr == nil && num > 0 {
- dblog.Success2()
- return maps, nil
- } else {
- dblog.Fail2()
- logger.Logger.Error(dberr, dblog.Description)
- }
- return nil, dberr
- }
|