|
@@ -4,6 +4,7 @@ import (
|
|
util "app.yhyue.com/moapp/jybase/common"
|
|
util "app.yhyue.com/moapp/jybase/common"
|
|
"app.yhyue.com/moapp/jybase/encrypt"
|
|
"app.yhyue.com/moapp/jybase/encrypt"
|
|
"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
"app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
|
|
+ "app.yhyue.com/moapp/jybase/redis"
|
|
"app.yhyue.com/moapp/jypkg/public"
|
|
"app.yhyue.com/moapp/jypkg/public"
|
|
"crypto/rand"
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/rsa"
|
|
@@ -15,6 +16,7 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/bwmarrin/snowflake"
|
|
"github.com/bwmarrin/snowflake"
|
|
"io/ioutil"
|
|
"io/ioutil"
|
|
|
|
+ "jy/src/jfw/config"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
"strings"
|
|
"strings"
|
|
@@ -30,7 +32,8 @@ type AnonymousAuth struct {
|
|
}
|
|
}
|
|
|
|
|
|
var (
|
|
var (
|
|
- Node *snowflake.Node
|
|
|
|
|
|
+ Node *snowflake.Node
|
|
|
|
+ saveChan = make(chan map[string]interface{}, 10000)
|
|
)
|
|
)
|
|
|
|
|
|
func initPrivatePublicKey() {
|
|
func initPrivatePublicKey() {
|
|
@@ -93,9 +96,95 @@ func initPrivatePublicKey() {
|
|
PublicKey = rsaPublicKey
|
|
PublicKey = rsaPublicKey
|
|
}
|
|
}
|
|
|
|
|
|
-func (sk *AnonymousAuth) Do() {
|
|
|
|
|
|
+func ChanMonitor() {
|
|
|
|
+
|
|
|
|
+ var (
|
|
|
|
+ saveData []map[string]interface{}
|
|
|
|
+ upData []map[string]interface{}
|
|
|
|
+ saveKey = []string{"ip", "client", "os", "browse", "url", "guestUID", "mdescribe", "refer", "method", "creation_time"}
|
|
|
|
+ count int
|
|
|
|
+ )
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case data := <-saveChan:
|
|
|
|
+ count++
|
|
|
|
+ if data["type"] == 1 { //插入
|
|
|
|
+ saveData = append(saveData, data)
|
|
|
|
+ } else if data["type"] == 2 { //更新
|
|
|
|
+ upData = append(upData, data)
|
|
|
|
+ }
|
|
|
|
+ // 处理 100条时处理一次 saveChan 收到的数据
|
|
|
|
+ if count == 100 {
|
|
|
|
+ if len(saveData) > 0 {
|
|
|
|
+ var ss []interface{}
|
|
|
|
+ for _, datum := range saveData {
|
|
|
|
+ for _, s := range saveKey {
|
|
|
|
+ ss = append(ss, datum[s])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public.BaseMysql.InsertBatch("anonymous_identity", saveKey, ss)
|
|
|
|
+ saveData = []map[string]interface{}{}
|
|
|
|
+ }
|
|
|
|
+ // 处理 upChan 收到的数据
|
|
|
|
+ if len(upData) > 0 {
|
|
|
|
+ var (
|
|
|
|
+ ids []string
|
|
|
|
+ column1, column2 []string
|
|
|
|
+ )
|
|
|
|
+ for _, i := range upData {
|
|
|
|
+ //upChan <- map[string]interface{}{"fid":| fid.Value, "trustedId": trustedId, "upId": jyGuestUID.Value}
|
|
|
|
+ jyGuestUID := util.InterfaceToStr(i["upId"])
|
|
|
|
+ ids = append(ids, fmt.Sprintf(`"%s"`, jyGuestUID))
|
|
|
|
+ column1 = append(column1, fmt.Sprintf(` WHEN guestUID = '%s' THEN '%s' `, jyGuestUID, util.InterfaceToStr(i["fid"])))
|
|
|
|
+ column2 = append(column2, fmt.Sprintf(` WHEN guestUID = '%s' THEN '%s' `, jyGuestUID, util.InterfaceToStr(i["trustedId"])))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public.BaseMysql.SelectBySql(fmt.Sprintf(`UPDATE anonymous_identity
|
|
|
|
+ SET fid = CASE %s ELSE fid END,
|
|
|
|
+ trustedId = CASE %s ELSE trustedId END
|
|
|
|
+ WHERE guestUID IN (%s);`, strings.Join(column1, " "), strings.Join(column2, " "), strings.Join(ids, " ")))
|
|
|
|
+ upData = []map[string]interface{}{}
|
|
|
|
+ }
|
|
|
|
+ count = 0
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ case <-time.After(30 * time.Second):
|
|
|
|
+ if len(saveData) > 0 {
|
|
|
|
+ var ss []interface{}
|
|
|
|
+ for _, datum := range saveData {
|
|
|
|
+ for _, s := range saveKey {
|
|
|
|
+ ss = append(ss, datum[s])
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public.BaseMysql.InsertBatch("anonymous_identity", saveKey, ss)
|
|
|
|
+ saveData = []map[string]interface{}{}
|
|
|
|
+ }
|
|
|
|
+ if len(upData) > 0 {
|
|
|
|
+ var (
|
|
|
|
+ ids []string
|
|
|
|
+ column1, column2 []string
|
|
|
|
+ )
|
|
|
|
+ for _, i := range upData {
|
|
|
|
+ //upChan <- map[string]interface{}{"fid":| fid.Value, "trustedId": trustedId, "upId": jyGuestUID.Value}
|
|
|
|
+ jyGuestUID := util.InterfaceToStr(i["upId"])
|
|
|
|
+ ids = append(ids, fmt.Sprintf(`"%s"`, jyGuestUID))
|
|
|
|
+ column1 = append(column1, fmt.Sprintf(` WHEN guestUID = '%s' THEN '%s' `, jyGuestUID, util.InterfaceToStr(i["fid"])))
|
|
|
|
+ column2 = append(column2, fmt.Sprintf(` WHEN guestUID = '%s' THEN '%s' `, jyGuestUID, util.InterfaceToStr(i["trustedId"])))
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public.BaseMysql.SelectBySql(fmt.Sprintf(`UPDATE anonymous_identity
|
|
|
|
+ SET fid = CASE %s ELSE fid END,
|
|
|
|
+ trustedId = CASE %s ELSE trustedId END
|
|
|
|
+ WHERE guestUID IN (%s);`, strings.Join(column1, " "), strings.Join(column2, " "), strings.Join(ids, ",")))
|
|
|
|
+ upData = []map[string]interface{}{}
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (sk *AnonymousAuth) Do() bool {
|
|
if sk.GetSession["mgoUserId"] != nil && sk.GetSession["mgoUserId"] != "" {
|
|
if sk.GetSession["mgoUserId"] != nil && sk.GetSession["mgoUserId"] != "" {
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
jyTrustedId, _ := sk.R.Cookie("JYTrustedId")
|
|
jyTrustedId, _ := sk.R.Cookie("JYTrustedId")
|
|
if jyTrustedId == nil || jyTrustedId.Value == "" { //不存在信用标识
|
|
if jyTrustedId == nil || jyTrustedId.Value == "" { //不存在信用标识
|
|
@@ -103,8 +192,14 @@ func (sk *AnonymousAuth) Do() {
|
|
if jyGuestUID == nil || jyGuestUID.Value == "" { //不存在后端id 生成后端id 并创建匿名身份信息
|
|
if jyGuestUID == nil || jyGuestUID.Value == "" { //不存在后端id 生成后端id 并创建匿名身份信息
|
|
accept := sk.R.Header.Get("Accept")
|
|
accept := sk.R.Header.Get("Accept")
|
|
if sk.R.Method != "GET" || !strings.Contains(accept, "text/html") { //避免多请求
|
|
if sk.R.Method != "GET" || !strings.Contains(accept, "text/html") { //避免多请求
|
|
- return
|
|
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ //增加限制未携带唯一标识的请求频率
|
|
|
|
+ if !sk.noLoginLimit(sk.R, util.If(len(sk.GetSession) > 1, string(sk.Session.Id()), "").(string)) {
|
|
|
|
+ sk.W.WriteHeader(502)
|
|
|
|
+ return false
|
|
}
|
|
}
|
|
|
|
+
|
|
guestUID := Node.Generate()
|
|
guestUID := Node.Generate()
|
|
cookie := &http.Cookie{
|
|
cookie := &http.Cookie{
|
|
Name: "JYGuestUID",
|
|
Name: "JYGuestUID",
|
|
@@ -130,8 +225,10 @@ func (sk *AnonymousAuth) Do() {
|
|
"refer": sk.R.Referer(),
|
|
"refer": sk.R.Referer(),
|
|
"method": sk.R.Method,
|
|
"method": sk.R.Method,
|
|
"creation_time": time.Now().Unix(),
|
|
"creation_time": time.Now().Unix(),
|
|
|
|
+ "type": 1,
|
|
}
|
|
}
|
|
- public.BaseMysql.Insert("anonymous_identity", data)
|
|
|
|
|
|
+ saveChan <- data
|
|
|
|
+ //public.BaseMysql.Insert("anonymous_identity", data)
|
|
//stmt, err := config.ClickhouseDb.Prepare("INSERT INTO anonymous_identity (ip, client, os, browse, url, guestUID, mdescribe, refer, method, creation_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
//stmt, err := config.ClickhouseDb.Prepare("INSERT INTO anonymous_identity (ip, client, os, browse, url, guestUID, mdescribe, refer, method, creation_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
|
|
//if err != nil {
|
|
//if err != nil {
|
|
// return
|
|
// return
|
|
@@ -141,25 +238,25 @@ func (sk *AnonymousAuth) Do() {
|
|
//if err != nil {
|
|
//if err != nil {
|
|
// return
|
|
// return
|
|
//}
|
|
//}
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
|
|
|
|
fid, err := sk.R.Cookie("fid")
|
|
fid, err := sk.R.Cookie("fid")
|
|
if err != nil {
|
|
if err != nil {
|
|
//log.Println("匿名用户获取fid失败", err.Error())
|
|
//log.Println("匿名用户获取fid失败", err.Error())
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
eid, err := sk.R.Cookie("eid")
|
|
eid, err := sk.R.Cookie("eid")
|
|
if err != nil {
|
|
if err != nil {
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
if fid.Value != "" && eid.Value != "" {
|
|
if fid.Value != "" && eid.Value != "" {
|
|
eData, err1 := Decryption(eid.Value)
|
|
eData, err1 := Decryption(eid.Value)
|
|
if err1 != nil {
|
|
if err1 != nil {
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
if fid.Value != eData {
|
|
if fid.Value != eData {
|
|
- return
|
|
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
trustedId := encrypt.SE.EncodeString(fid.Value + "***" + jyGuestUID.Value)
|
|
trustedId := encrypt.SE.EncodeString(fid.Value + "***" + jyGuestUID.Value)
|
|
cookie := &http.Cookie{
|
|
cookie := &http.Cookie{
|
|
@@ -171,8 +268,9 @@ func (sk *AnonymousAuth) Do() {
|
|
Expires: time.Now().AddDate(10, 0, 0),
|
|
Expires: time.Now().AddDate(10, 0, 0),
|
|
}
|
|
}
|
|
http.SetCookie(sk.W, cookie)
|
|
http.SetCookie(sk.W, cookie)
|
|
- public.BaseMysql.Update("anonymous_identity", map[string]interface{}{"guestUID": jyGuestUID.Value},
|
|
|
|
- map[string]interface{}{"fid": fid.Value, "trustedId": trustedId})
|
|
|
|
|
|
+ saveChan <- map[string]interface{}{"fid": fid.Value, "trustedId": trustedId, "upId": jyGuestUID.Value, "type": 2}
|
|
|
|
+ //public.BaseMysql.Update("anonymous_identity", map[string]interface{}{"guestUID": jyGuestUID.Value},
|
|
|
|
+ // map[string]interface{}{"fid": fid.Value, "trustedId": trustedId})
|
|
//stmt, err := config.ClickhouseDb.Prepare("UPDATE anonymous_identity SET fid = ? and trustedId = ? WHERE guestUID = ?")
|
|
//stmt, err := config.ClickhouseDb.Prepare("UPDATE anonymous_identity SET fid = ? and trustedId = ? WHERE guestUID = ?")
|
|
//if err != nil {
|
|
//if err != nil {
|
|
// fmt.Println("Failed to prepare statement:", err)
|
|
// fmt.Println("Failed to prepare statement:", err)
|
|
@@ -192,6 +290,7 @@ func (sk *AnonymousAuth) Do() {
|
|
log.Println("无效信用id")
|
|
log.Println("无效信用id")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ return true
|
|
}
|
|
}
|
|
|
|
|
|
// Encryption 加密
|
|
// Encryption 加密
|
|
@@ -230,3 +329,38 @@ func Decryption(content string) (string, error) {
|
|
}
|
|
}
|
|
return string(decryptedText), nil
|
|
return string(decryptedText), nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func (sk *AnonymousAuth) noLoginLimit(r *http.Request, sessionId string) bool {
|
|
|
|
+ if !config.JyAnonymousLimit.Switch {
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+ var ip string
|
|
|
|
+ for _, t_ip := range strings.Split(util.GetIp(r), ",") {
|
|
|
|
+ if len(t_ip) > 0 && t_ip != "127.0.0.1" {
|
|
|
|
+ ip = t_ip
|
|
|
|
+ break
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if len(ip) > 0 && config.IpList.Match(ip) { //百度白名单
|
|
|
|
+ return true
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ limitKey := fmt.Sprintf("anonymousLimit_Id_%s", sessionId)
|
|
|
|
+ if sessionId == "" {
|
|
|
|
+ limitKey = fmt.Sprintf("anonymousLimit_Ip_%s", ip)
|
|
|
|
+ }
|
|
|
|
+ if redis.Get("limitation", fmt.Sprintf("%s_limit", limitKey)) != nil {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ val := redis.Incr("limitation", limitKey)
|
|
|
|
+ if val == 1 { //设置过期值
|
|
|
|
+ if err := redis.SetExpire("limitation", limitKey, config.JyAnonymousLimit.Second); err != nil {
|
|
|
|
+ log.Printf("noLoginLimit SetExpire key:%s err:%v ", limitKey, err.Error())
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if val > config.JyAnonymousLimit.Times {
|
|
|
|
+ redis.Put("limitation", fmt.Sprintf("%s_limit", limitKey), 1, config.JyAnonymousLimit.Limit)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ return true
|
|
|
|
+}
|