Commit a3fb8a44 by 朱继来

后台用户登录IP监控

parent b386bf1e
package user_login_mail
package main
import (
"flag"
"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/cms/UserInfo"
"go-queue-server/dal/cms/UserLogin"
"go-queue-server/dal/db"
"go-queue-server/util"
"go-queue-server/util/ding"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/axgle/mahonia" //编码转换
)
func initDb(dns string) (err error) {
err = db.Init(dns)
func initDb(action string, dns string) (err error) {
err = db.CreateDB(action, dns)
if err != nil {
return
}
......@@ -34,13 +42,63 @@ func main() {
util.Init(ConfigDir)
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"cms/user_login_mail"
logConfig["log_path"] = LogDir+"cms/user_login_monitor"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
//初始化db
initDb(util.Configs.Liexin_databases.Dns)
initDb("InitCmsDB", util.Configs.Liexincms_databases.Dns)
zero_time := time.Now().Format("2006-01-02")
row, err := db.CmsDB.Queryx("SELECT * FROM user_login WHERE status = 0 and loginTime >= ?", zero_time)
defer row.Close()
if err != nil {
logger.Info("获取后台登录用户日志失败: ", err.Error())
return
}
for row.Next() {
var user_login UserLogin.UserLogin
row.StructScan(&user_login)
// 检查IP
resp, err := http.Get("http://ip.ws.126.net/ipquery?ip=" + user_login.LoginIp)
defer resp.Body.Close()
// 钉钉通知 默认参数
var mobile []string = make([]string, 0)
var isAtAll bool = false
if err != nil {
ding.Send(util.Configs.Ding_msg.Webhook,"监控告警", "后台用户登录监控,查询IP接口请求失败,原因:" + err.Error(), mobile, isAtAll)
return
}
body, _ := ioutil.ReadAll(resp.Body)
res := mahonia.NewDecoder("gbk").ConvertString(string(body))
split_1 := strings.Split(res, ";")
split_2 := strings.Split(split_1[0], ",")
province := strings.Replace(strings.Split(split_2[0], "=")[1], "\"", "", -1)
city := strings.Replace(strings.Split(split_2[1], "=")[1], "\"", "", -1)
if province != "广东省" || city != "深圳市" {
var user_info UserInfo.UserInfo
db.CmsDB.Get(&user_info, "select * FROM user_info WHERE userId = ?", user_login.UserId)
ding.Send(util.Configs.Ding_msg.Webhook,"监控告警", "后台用户登录IP监控,用户:"+ user_info.Email +",登录地址为:" + province + city, mobile, isAtAll)
// 登录状态更新为异常
db.CmsDB.Exec("UPDATE user_login set status = ? WHERE userId = ? and skey = ?", -1, user_login.UserId, user_login.Skey)
return
}
// 更新状态为正常
db.CmsDB.Exec("UPDATE user_login set status = ? WHERE userId = ? and skey = ?", 1, user_login.UserId, user_login.Skey)
}
fmt.Println("监控后台登录IP完成")
fmt.Println(111)
}
\ No newline at end of file
......@@ -13,4 +13,5 @@ type UserLogin struct {
Guid string `db:"guid"`
OpenId string `db:"openId"`
AccType int `db:"accType"`
Status int `db:"status"`
}
......@@ -4,6 +4,8 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/garyburd/redigo v1.6.0
github.com/go-sql-driver/mysql v1.5.0
github.com/gohouse/gorose/v2 v2.1.10
......@@ -14,7 +16,6 @@ require (
github.com/imroc/req v0.3.0
github.com/jmoiron/sqlx v1.2.0
github.com/kr/pretty v0.1.0 // indirect
github.com/prometheus/common v0.7.0
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71
github.com/stretchr/testify v1.4.0 // indirect
github.com/syyongx/php2go v0.9.4
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment