device.go 4.7 KB

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