device.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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", "fresh_all_keys", 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. d := DeviceConf{}
  85. err = json.Unmarshal(rspBytes, &d)
  86. if err != nil {
  87. return nil, redisKey, err
  88. }
  89. return &d, redisKey, nil
  90. }
  91. // 设置每天的city_code的值
  92. func SetDailyFakeDeviceConfByCityCode(cityCode int, d DeviceConf, dateInt int) error {
  93. conn := ads_redis.RedisConn.Get()
  94. defer conn.Close()
  95. redisKey := fmt.Sprintf("amdu_%d_%d", cityCode, dateInt)
  96. deviceConfBytes, err := json.Marshal(d)
  97. if err != nil {
  98. return err
  99. }
  100. _, _ = conn.Do("SADD", redisKey, deviceConfBytes)
  101. _, _ = conn.Do("expire", redisKey, 129600)
  102. return nil
  103. }
  104. // 获取每天的设备imei
  105. func GetDailyFakeDeviceConfByCityCode(cityCode int) (*DeviceConf, int, error) {
  106. conn := ads_redis.RedisConn.Get()
  107. defer conn.Close()
  108. dateInt, _ := strconv.Atoi(time.Now().Format("20060102"))
  109. redisKey := fmt.Sprintf("amdu_%d_%d", cityCode, dateInt)
  110. var validDeviceConf *DeviceConf
  111. // 防止无限跑
  112. maxLoopCnt := 6
  113. for i := 0; i < maxLoopCnt; i++ {
  114. rsp, err := conn.Do("srandmember", redisKey)
  115. if err != nil {
  116. return nil, 0, err
  117. }
  118. if rsp == nil {
  119. return nil, 0, nil
  120. }
  121. rspBytes, _ := redis.Bytes(rsp, err)
  122. d := DeviceConf{}
  123. err = json.Unmarshal(rspBytes, &d)
  124. if err != nil {
  125. continue
  126. }
  127. isBlackItem, err := CheckIsBlackImei(d.Imei)
  128. if err != nil {
  129. continue
  130. }
  131. if !isBlackItem {
  132. validDeviceConf = &d
  133. break
  134. }
  135. AddCityCodeBlackImei(cityCode, d.Imei)
  136. }
  137. if validDeviceConf != nil {
  138. // 用过的device_id放入明天的key中
  139. tomorrowDateInt, _ := strconv.Atoi(time.Now().AddDate(0, 0, 1).Format("20060102"))
  140. err := SetDailyFakeDeviceConfByCityCode(cityCode, *validDeviceConf, tomorrowDateInt)
  141. if err != nil {
  142. log.Errorf("set daily fake device conf by city code failed: %s", err)
  143. }
  144. }
  145. // 获取剩余的量
  146. leftCnt, err := redis.Int(conn.Do("SCARD", redisKey))
  147. if err != nil {
  148. return nil, 0, err
  149. }
  150. return validDeviceConf, leftCnt, nil
  151. }
  152. // 检查是否是黑名单
  153. func CheckIsBlackImei(imei string) (bool, error) {
  154. conn := ads_redis.RedisConn.Get()
  155. defer conn.Close()
  156. redisKey := "ads_black_device_" + imei[0:2]
  157. isBlackImei, err := redis.Bool(conn.Do("SISMEMBER", redisKey, imei))
  158. if err != nil {
  159. return false, err
  160. }
  161. return isBlackImei, nil
  162. }
  163. // 存储city_code对应下的设备黑名单
  164. func AddCityCodeBlackImei(cityCode int, imei string) {
  165. conn := ads_redis.RedisConn.Get()
  166. defer conn.Close()
  167. redisKey := fmt.Sprintf("blic_%d", cityCode)
  168. _, _ = conn.Do("sadd", redisKey, imei)
  169. }