From 2840f5b3755055df41fd80ebf6e97d204332ce33 Mon Sep 17 00:00:00 2001 From: deepzz0 Date: Mon, 31 Oct 2016 01:28:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B0=B7=E6=AD=8C=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/app.yml | 5 +++++ front.go | 20 ++++++++++++++------ helper.go | 6 +++--- setting/setting.go | 11 ++++++++--- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/conf/app.yml b/conf/app.yml index c0f9b7e..29f4335 100644 --- a/conf/app.yml +++ b/conf/app.yml @@ -34,6 +34,11 @@ disqus: # 热搜词配置 hotwords: - docker +# 谷歌统计 +google: + tid: UA-77251712-1 + v: "1" + t: pageview # 运行模式 mode: # you can fix certfile, keyfile, domain diff --git a/front.go b/front.go index d9d176c..d8ec137 100644 --- a/front.go +++ b/front.go @@ -29,10 +29,9 @@ func Filter() gin.HandlerFunc { // 用户识别 func UserCookie(c *gin.Context) { - cookie, err := c.Request.Cookie("u") - if err != nil || cookie.Value == "" { - b := []byte(c.ClientIP() + time.Now().String()) - c.SetCookie("u", fmt.Sprintf("%x", SHA1(b)), 86400*999, "/", "", true, true) + cookie, err := c.Cookie("u") + if err != nil || cookie == "" { + c.SetCookie("u", RandUUIDv4(), 86400*730, "/", "", true, true) } } @@ -209,8 +208,17 @@ func HandleSitemap(c *gin.Context) { func HandleBeacon(c *gin.Context) { ua := c.Request.UserAgent() // TODO 过滤黑名单 + vals := c.Request.URL.Query() + vals.Set("v", setting.Conf.Google.V) + vals.Set("tid", setting.Conf.Google.Tid) + vals.Set("t", setting.Conf.Google.T) + cookie, _ := c.Cookie("u") + vals.Set("cid", cookie) + + vals.Set("dl", c.Request.Host+c.Request.RequestURI) + vals.Set("uip", c.ClientIP()) go func() { - req, err := http.NewRequest("POST", "https://www.google-analytics.com/collect", strings.NewReader(c.Request.URL.RawQuery)) + req, err := http.NewRequest("POST", "https://www.google-analytics.com/collect", strings.NewReader(vals.Encode())) if err != nil { logd.Error(err) return @@ -231,7 +239,7 @@ func HandleBeacon(c *gin.Context) { logd.Error(string(data)) } }() - c.String(http.StatusAccepted, "accepted") + c.String(http.StatusNoContent, "accepted") } // 服务端获取评论详细 diff --git a/helper.go b/helper.go index 6164513..1f2cf1e 100644 --- a/helper.go +++ b/helper.go @@ -1,7 +1,6 @@ package main import ( - "crypto/sha1" "crypto/sha256" "fmt" "io" @@ -11,6 +10,7 @@ import ( "time" "github.com/eiblog/utils/logd" + "github.com/eiblog/utils/uuid" ) const ( @@ -32,8 +32,8 @@ func VerifyPasswd(origin, name, input string) bool { return origin == EncryptPasswd(name, input) } -func SHA1(data []byte) [sha1.Size]byte { - return sha1.Sum(data) +func RandUUIDv4() string { + return uuid.NewV4().String() } func ReadDir(dir string, filter func(name string) bool) (files []string) { diff --git a/setting/setting.go b/setting/setting.go index 73e75db..f607db8 100644 --- a/setting/setting.go +++ b/setting/setting.go @@ -43,9 +43,14 @@ type Config struct { Interval int } HotWords []string // 热搜词 - Mode RunMode // 运行模式 - Twitter string // twitter地址 - Blogger struct { // 初始化数据 + Google struct { // 谷歌统计 + Tid string + V string + T string + } + Mode RunMode // 运行模式 + Twitter string // twitter地址 + Blogger struct { // 初始化数据 BlogName string SubTitle string BeiAn string