device.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package device
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gomodule/redigo/redis"
  6. log "github.com/sirupsen/logrus"
  7. "math/rand"
  8. "miads/adslib/ads_redis"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type DeviceConf struct {
  14. Imei string `json:"imei"`
  15. OriginImei string `json:"origin_imei"`
  16. Ua string `json:"ua"`
  17. }
  18. func SetAdsTagLog(adv string, reqSource string, key string, cityCode int) {
  19. k := fmt.Sprintf("%s___%s___%s___%d", adv, reqSource, key, cityCode)
  20. setTagLog(k)
  21. }
  22. func setTagLog(key string) {
  23. conn := ads_redis.RedisConn.Get()
  24. defer conn.Close()
  25. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  26. k := fmt.Sprintf("{%s}___{%d}", key, dateInt)
  27. _, _ = conn.Do("INCR", k)
  28. _, _ = conn.Do("SADD", fmt.Sprintf("fresh_all_keys_%d", dateInt), k)
  29. }
  30. // 存储ip上一次请求时间
  31. func SetIpReqTime(ip string, reqTime int64) {
  32. conn := ads_redis.RedisConn.Get()
  33. defer conn.Close()
  34. ipKey := strings.ReplaceAll(ip, ".", "")
  35. redisKey := "adip_" + ipKey
  36. _, _ = conn.Do("SET", redisKey, reqTime)
  37. }
  38. // 获取这个ip最近一次请求的时间
  39. func GetIpReqTime(ip string) (int64, error) {
  40. conn := ads_redis.RedisConn.Get()
  41. defer conn.Close()
  42. ipKey := strings.ReplaceAll(ip, ".", "")
  43. redisKey := "adip_" + ipKey
  44. rsp, err := conn.Do("GET", redisKey)
  45. if err != nil {
  46. return 0, err
  47. }
  48. if rsp == nil {
  49. return 0, err
  50. }
  51. lastReqTime, _ := redis.Int64(rsp, err)
  52. return lastReqTime, nil
  53. }
  54. func SetMiDeviceConf(key string, d DeviceConf) error {
  55. conn := ads_redis.RedisConn.Get()
  56. defer conn.Close()
  57. deviceConfBytes, err := json.Marshal(d)
  58. if err != nil {
  59. return err
  60. }
  61. _, _ = conn.Do("set", key, deviceConfBytes, 86400)
  62. return nil
  63. }
  64. // 获取设备
  65. func GetMiDeviceConf(ip string, randNum int, specialDeviceNum int) (*DeviceConf, string, error) {
  66. conn := ads_redis.RedisConn.Get()
  67. defer conn.Close()
  68. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  69. rd := 0
  70. if specialDeviceNum == 0 {
  71. rd = rand.Intn(randNum-1) + 1
  72. } else {
  73. rd = specialDeviceNum
  74. }
  75. redisKey := fmt.Sprintf("adur_%s_%d_%d_v4", ip, dateInt, rd)
  76. rsp, err := conn.Do("GET", redisKey)
  77. if err != nil {
  78. return nil, redisKey, err
  79. }
  80. if rsp == nil {
  81. return nil, redisKey, nil
  82. }
  83. rspBytes, _ := redis.Bytes(rsp, err)
  84. rspStr := strings.ReplaceAll(string(rspBytes), "u", "")
  85. rspStr = strings.ReplaceAll(string(rspStr), "'", "\"")
  86. d := DeviceConf{}
  87. err = json.Unmarshal([]byte(rspStr), &d)
  88. if err != nil {
  89. return nil, redisKey, err
  90. }
  91. return &d, redisKey, nil
  92. }
  93. // 设置每天的city_code的值
  94. func SetDailyFakeDeviceConfByCityCode(cityCode int, d DeviceConf, dateInt int) error {
  95. conn := ads_redis.RedisConn.Get()
  96. defer conn.Close()
  97. redisKey := fmt.Sprintf("amdu_%d_%d", cityCode, dateInt)
  98. deviceConfBytes, err := json.Marshal(d)
  99. if err != nil {
  100. return err
  101. }
  102. _, _ = conn.Do("SADD", redisKey, deviceConfBytes)
  103. _, _ = conn.Do("expire", redisKey, 129600)
  104. return nil
  105. }
  106. // 获取每天的设备imei
  107. func GetDailyFakeDeviceConfByCityCode(cityCode int) (*DeviceConf, int, error) {
  108. conn := ads_redis.RedisConn.Get()
  109. defer conn.Close()
  110. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  111. redisKey := fmt.Sprintf("amdu_%d_%d", cityCode, dateInt)
  112. var validDeviceConf *DeviceConf
  113. // 防止无限跑
  114. maxLoopCnt := 6
  115. for i := 0; i < maxLoopCnt; i++ {
  116. rsp, err := conn.Do("srandmember", redisKey)
  117. if err != nil {
  118. return nil, 0, err
  119. }
  120. if rsp == nil {
  121. return nil, 0, nil
  122. }
  123. rspBytes, _ := redis.Bytes(rsp, err)
  124. d := DeviceConf{}
  125. err = json.Unmarshal(rspBytes, &d)
  126. if err != nil {
  127. continue
  128. }
  129. isBlackItem, err := CheckIsBlackImei(d.Imei)
  130. if err != nil {
  131. continue
  132. }
  133. if !isBlackItem {
  134. validDeviceConf = &d
  135. break
  136. }
  137. AddCityCodeBlackImei(cityCode, d.Imei)
  138. }
  139. if validDeviceConf != nil {
  140. // 用过的device_id放入明天的key中
  141. tomorrowDateInt, _ := strconv.Atoi(time.Now().AddDate(0, 0, 1).Format("20060102"))
  142. err := SetDailyFakeDeviceConfByCityCode(cityCode, *validDeviceConf, tomorrowDateInt)
  143. if err != nil {
  144. log.Errorf("set daily fake device conf by city code failed: %s", err)
  145. }
  146. }
  147. // 获取剩余的量
  148. leftCnt, err := redis.Int(conn.Do("SCARD", redisKey))
  149. if err != nil {
  150. return nil, 0, err
  151. }
  152. return validDeviceConf, leftCnt, nil
  153. }
  154. // 检查是否是黑名单
  155. func CheckIsBlackImei(imei string) (bool, error) {
  156. conn := ads_redis.RedisConn.Get()
  157. defer conn.Close()
  158. redisKey := "ads_black_device_" + imei[0:2]
  159. isBlackImei, err := redis.Bool(conn.Do("SISMEMBER", redisKey, imei))
  160. if err != nil {
  161. return false, err
  162. }
  163. return isBlackImei, nil
  164. }
  165. // 存储city_code对应下的设备黑名单
  166. func AddCityCodeBlackImei(cityCode int, imei string) {
  167. conn := ads_redis.RedisConn.Get()
  168. defer conn.Close()
  169. redisKey := fmt.Sprintf("blic_%d", cityCode)
  170. _, _ = conn.Do("sadd", redisKey, imei)
  171. }