From 75239c0ad69ea0ea43ba6fc461a87217bc1f7926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Wed, 29 Mar 2023 22:15:30 +0800 Subject: [PATCH] - update log - update request --- CHANGELOG.md | 5 ++ library.go | 2 +- utils/golog/gin.custom.config.go | 22 +++++ utils/golog/gin.custom.go | 75 +++++++++++++++++ utils/golog/gin.custom.model.go | 137 +++++++++++++++++++++++++++++++ utils/gorequest/gin.go | 43 ++++++++++ 6 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 utils/golog/gin.custom.config.go create mode 100644 utils/golog/gin.custom.go create mode 100644 utils/golog/gin.custom.model.go create mode 100644 utils/gorequest/gin.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e17a415b..249eddeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v1.0.113 + +- update [golog](utils%2Fgolog) +- update [gorequest](utils%2Fgorequest) + ## v1.0.111-v1.0.112 - update [wechatopen](service%2Fwechatopen) diff --git a/library.go b/library.go index ae644646..c2725de5 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.112" + return "1.0.113" } diff --git a/utils/golog/gin.custom.config.go b/utils/golog/gin.custom.config.go new file mode 100644 index 00000000..6ced1428 --- /dev/null +++ b/utils/golog/gin.custom.config.go @@ -0,0 +1,22 @@ +package golog + +import ( + "context" + "github.com/dtapps/go-library" + "github.com/dtapps/go-library/utils/goip" + "runtime" +) + +func (c *GinCustomClient) setConfig(ctx context.Context) { + + info := getSystem() + + c.config.systemHostname = info.SystemHostname + c.config.systemOs = info.SystemOs + c.config.systemKernel = info.SystemKernel + + c.config.systemInsideIp = goip.GetInsideIp(ctx) + + c.config.sdkVersion = go_library.Version() + c.config.goVersion = runtime.Version() +} diff --git a/utils/golog/gin.custom.go b/utils/golog/gin.custom.go new file mode 100644 index 00000000..51a44660 --- /dev/null +++ b/utils/golog/gin.custom.go @@ -0,0 +1,75 @@ +package golog + +import ( + "context" + "errors" + "github.com/dtapps/go-library/utils/dorm" + "github.com/dtapps/go-library/utils/goip" +) + +type GinCustomClient struct { + gormClient *dorm.GormClient // 数据库驱动 + ipService *goip.Client // ip服务 + config struct { + systemHostname string // 主机名 + systemOs string // 系统类型 + systemKernel string // 系统内核 + systemInsideIp string // 内网ip + systemOutsideIp string // 外网ip + goVersion string // go版本 + sdkVersion string // sdk版本 + } + gormConfig struct { + stats bool // 状态 + tableName string // 表名 + } +} + +type ConfigGinCustomClient struct { + IpService *goip.Client // ip服务 + GormClientFun dorm.GormClientTableFun // 日志配置 +} + +func NewGinCustomClient(config *ConfigGinCustomClient) (*GinCustomClient, error) { + + var ctx = context.Background() + + c := &GinCustomClient{} + + c.config.systemOutsideIp = goip.IsIp(c.config.systemOutsideIp) + if c.config.systemOutsideIp == "" { + return nil, currentIpNoConfig + } + + c.ipService = config.IpService + + // 配置信息 + c.setConfig(ctx) + + gormClient, gormTableName := config.GormClientFun() + + if gormClient == nil || gormClient.GetDb() == nil { + return nil, dbClientFunNoConfig + } + + // 配置关系数据库 + if gormClient != nil || gormClient.GetDb() != nil { + + c.gormClient = gormClient + + if gormTableName == "" { + return nil, errors.New("没有设置表名") + } else { + c.gormConfig.tableName = gormTableName + } + + err := c.autoMigrate(ctx) + if err != nil { + return nil, err + } + + c.gormConfig.stats = true + } + + return c, nil +} diff --git a/utils/golog/gin.custom.model.go b/utils/golog/gin.custom.model.go new file mode 100644 index 00000000..6861d03c --- /dev/null +++ b/utils/golog/gin.custom.model.go @@ -0,0 +1,137 @@ +package golog + +import ( + "context" + "github.com/dtapps/go-library/utils/dorm" + "github.com/dtapps/go-library/utils/goip" + "github.com/dtapps/go-library/utils/gorequest" + "github.com/dtapps/go-library/utils/gotime" + "github.com/dtapps/go-library/utils/gotrace_id" + "github.com/dtapps/go-library/utils/gourl" + "github.com/gin-gonic/gin" + "gorm.io/gorm" + "time" +) + +// 模型 +type ginPostgresqlLogCustom struct { + LogID int64 `gorm:"primaryKey;comment:【日志】编号" json:"log_id,omitempty"` //【日志】编号 + LogTime time.Time `gorm:"comment:【日志】时间" json:"log_time,omitempty"` //【记日志录】时间 + TraceId string `gorm:"index;comment:【系统】跟踪编号" json:"trace_id,omitempty"` //【系统】跟踪编号 + RequestUri string `gorm:"comment:【请求】请求链接 域名+路径+参数" json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 + RequestUrl string `gorm:"comment:【请求】请求链接 域名+路径" json:"request_url,omitempty"` //【请求】请求链接 域名+路径 + RequestApi string `gorm:"index;comment:【请求】请求接口 路径" json:"request_api,omitempty"` //【请求】请求接口 路径 + RequestMethod string `gorm:"index;comment:【请求】请求方式" json:"request_method,omitempty"` //【请求】请求方式 + RequestProto string `gorm:"comment:【请求】请求协议" json:"request_proto,omitempty"` //【请求】请求协议 + RequestUa string `gorm:"comment:【请求】请求UA" json:"request_ua,omitempty"` //【请求】请求UA + RequestReferer string `gorm:"comment:【请求】请求referer" json:"request_referer,omitempty"` //【请求】请求referer + RequestUrlQuery string `gorm:"comment:【请求】请求URL参数" json:"request_url_query,omitempty"` //【请求】请求URL参数 + RequestHeader string `gorm:"comment:【请求】请求头" json:"request_header,omitempty"` //【请求】请求头 + RequestIp string `gorm:"default:0.0.0.0;index;comment:【请求】请求客户端Ip" json:"request_ip,omitempty"` //【请求】请求客户端Ip + RequestIpCountry string `gorm:"index;comment:【请求】请求客户端城市" json:"request_ip_country,omitempty"` //【请求】请求客户端城市 + RequestIpProvince string `gorm:"index;comment:【请求】请求客户端省份" json:"request_ip_province,omitempty"` //【请求】请求客户端省份 + RequestIpCity string `gorm:"index;comment:【请求】请求客户端城市" json:"request_ip_city,omitempty"` //【请求】请求客户端城市 + RequestIpIsp string `gorm:"index;comment:【请求】请求客户端运营商" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 + RequestIpLongitude float64 `gorm:"index;comment:【请求】请求客户端经度" json:"request_ip_longitude,omitempty"` //【请求】请求客户端经度 + RequestIpLatitude float64 `gorm:"index;comment:【请求】请求客户端纬度" json:"request_ip_latitude,omitempty"` //【请求】请求客户端纬度 + SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名 + SystemInsideIp string `gorm:"default:0.0.0.0;comment:【系统】内网ip" json:"system_inside_ip,omitempty"` //【系统】内网ip + SystemOs string `gorm:"index;comment:【系统】系统类型" json:"system_os,omitempty"` //【系统】系统类型 + SystemArch string `gorm:"index;comment:【系统】系统架构" json:"system_arch,omitempty"` //【系统】系统架构 + GoVersion string `gorm:"comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本 + SdkVersion string `gorm:"comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本 + CustomId string `gorm:"index;comment:【日志】自定义编号" json:"custom_id,omitempty"` //【日志】自定义编号 + CustomType string `gorm:"index;comment:【日志】自定义类型" json:"custom_type,omitempty"` //【日志】自定义类型 + CustomContent string `gorm:"comment:【日志】自定义内容" json:"custom_content,omitempty"` //【日志】自定义内容 +} + +// 创建模型 +func (c *GinCustomClient) autoMigrate(ctx context.Context) error { + return c.gormClient.GetDb().Table(c.gormConfig.tableName).AutoMigrate(&ginPostgresqlLogCustom{}) +} + +type GinCustomClientGinRecordOperation struct { + gormClient *dorm.GormClient // 数据库驱动 + ipService *goip.Client // ip服务 + tableName string // 表名 + data *ginPostgresqlLogCustom // 数据 +} + +// GinRecord 记录日志 +func (c *GinCustomClient) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation { + operation := &GinCustomClientGinRecordOperation{ + gormClient: c.gormClient, + ipService: c.ipService, + tableName: c.gormConfig.tableName, + } + operation.data.LogTime = gotime.Current().Time //【日志】时间 + operation.data.TraceId = gotrace_id.GetGinTraceId(ginCtx) // 【系统】跟踪编号 + if ginCtx.Request.TLS == nil { + operation.data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 + } else { + operation.data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 + } + operation.data.RequestUrl = ginCtx.Request.RequestURI //【请求】请求链接 域名+路径 + operation.data.RequestApi = gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI) //【请求】请求接口 路径 + operation.data.RequestMethod = ginCtx.Request.Method //【请求】请求方式 + operation.data.RequestProto = ginCtx.Request.Proto //【请求】请求协议 + operation.data.RequestUa = ginCtx.Request.UserAgent() //【请求】请求UA + operation.data.RequestReferer = ginCtx.Request.Referer() //【请求】请求referer + operation.data.RequestUrlQuery = dorm.JsonEncodeNoError(ginCtx.Request.URL.Query()) //【请求】请求URL参数 + operation.data.RequestHeader = dorm.JsonEncodeNoError(ginCtx.Request.Header) //【请求】请求头 + operation.data.RequestIp = gorequest.ClientIp(ginCtx.Request) //【请求】请求客户端Ip + operation.data.SystemHostName = c.config.systemHostname //【系统】主机名 + operation.data.SystemInsideIp = c.config.systemInsideIp //【系统】内网ip + operation.data.SystemOs = c.config.systemOs //【系统】系统类型 + operation.data.SystemArch = c.config.systemKernel //【系统】系统架构 + operation.data.GoVersion = c.config.goVersion //【程序】Go版本 + operation.data.SdkVersion = c.config.sdkVersion //【程序】Sdk版本 + return operation +} + +func (o *GinCustomClientGinRecordOperation) CustomInfo(customId, customType, customContent string) *GinCustomClientGinRecordOperation { + o.data.CustomId = customId //【日志】自定义编号 + o.data.CustomType = customType //【日志】自定义类型 + o.data.CustomContent = customContent //【日志】自定义内容 + return o +} + +func (o *GinCustomClientGinRecordOperation) CreateData() error { + err := o.gormClient.GetDb().Table(o.tableName).Create(&o.data).Error + if o.data.LogID != 0 { + go func() { + ginCustomClientRecordUpdateIpInfo(o.ipService, o.gormClient.GetDb(), o.tableName, o.data.LogID, o.data.RequestIp) + }() + } + return err +} + +func (o *GinCustomClientGinRecordOperation) CreateDataNoError() { + o.gormClient.GetDb().Table(o.tableName).Create(&o.data) + if o.data.LogID != 0 { + go func() { + ginCustomClientRecordUpdateIpInfo(o.ipService, o.gormClient.GetDb(), o.tableName, o.data.LogID, o.data.RequestIp) + }() + } +} + +func ginCustomClientRecordUpdateIpInfo(ipService *goip.Client, pdTx *gorm.DB, tableName string, logId int64, requestIp string) { + ipInfo := ipService.Analyse(requestIp) + pdTx.Table(tableName).Where("log_id = ?", logId). + Select( + "request_ip_country", + "request_ip_province", + "request_ip_city", + "request_ip_isp", + "request_ip_longitude", + "request_ip_longitude", + ). + Updates(ginPostgresqlLogCustom{ + RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市 + RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份 + RequestIpCity: ipInfo.City, //【请求】请求客户端城市 + RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商 + RequestIpLongitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度 + RequestIpLatitude: ipInfo.LocationLongitude, //【请求】请求客户端经度 + }) +} diff --git a/utils/gorequest/gin.go b/utils/gorequest/gin.go new file mode 100644 index 00000000..01fda161 --- /dev/null +++ b/utils/gorequest/gin.go @@ -0,0 +1,43 @@ +package gorequest + +import ( + "errors" + "github.com/gin-gonic/gin" + "strings" +) + +func IsWechatMiniProgramRequest(ginCtx *gin.Context, appid string) error { + referer := ginCtx.Request.Referer() + userAgent := ginCtx.Request.UserAgent() + if referer == "" { + return errors.New("网络请求没有达到要求") + } + return isWechatMiniProgramRequestReferer(userAgent, referer, appid) +} + +// https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html#%E4%BD%BF%E7%94%A8%E9%99%90%E5%88%B6 +func isWechatMiniProgramRequestReferer(userAgent, referer string, appid string) error { + // 判断结尾 + suffix := strings.HasSuffix(referer, "/page-frame.html") + if suffix { + // 判断开头 + prefix := strings.HasPrefix(referer, "https://servicewechat.com/") + if prefix { + // 判断加上appid + prefixAppid := strings.HasPrefix(referer, "https://servicewechat.com/"+appid) + if prefixAppid { + return isWechatMiniProgramRequestUserAgent(userAgent) + } + return isWechatMiniProgramRequestUserAgent(userAgent) + } + return errors.New("格式固定不对2") + } + return errors.New("格式固定不对1") +} + +func isWechatMiniProgramRequestUserAgent(userAgent string) error { + if strings.Contains(userAgent, "MicroMessenger") { + return nil + } + return errors.New("伪装数据") +}