request.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package adslib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "io/ioutil"
  8. "miads/adslib/encrypt"
  9. "miads/adslib/utils"
  10. "net/url"
  11. "strings"
  12. "time"
  13. )
  14. type Request struct {
  15. Imei string
  16. Idfa string
  17. Model string
  18. Brand string
  19. ScreenSize string `json:"screen_size"`
  20. NetworkType int `json:"network_type,string"`
  21. Androidid string `json:"android_id"`
  22. Platform int `json:"platform,string"`
  23. OsVersion string `json:"os_version"`
  24. UaClient string `json:"ua"`
  25. ReqSource string `json:"req_source"`
  26. Mac string `json:"mac"`
  27. ReqSourceIp string `json:"ip"`
  28. DeviceId string `json:"device_id"`
  29. NewAdsFlag int64
  30. content string
  31. RequestTime int64 `json:"-"`
  32. QueryString string `json:"-"`
  33. Body string `json:"-"`
  34. }
  35. func (self *Request) Parse(c *gin.Context) {
  36. self.RequestTime = time.Now().Unix()
  37. self.QueryString = c.Request.URL.RawQuery
  38. body, _ := c.GetRawData()
  39. self.Body = string(body)
  40. // 取了body要塞回去
  41. c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
  42. self.Imei = utils.GetArgument(c, "imei", "")
  43. self.Idfa = utils.GetArgument(c, "idfa", "")
  44. self.Model = utils.GetArgument(c, "model", "")
  45. self.Brand = utils.GetArgument(c, "brand", "")
  46. self.ScreenSize = utils.GetArgument(c, "screen_size", "")
  47. self.NetworkType = int(utils.GetIntArgument(c, "network_type", 3))
  48. self.Androidid = utils.GetArgument(c, "android_id", "")
  49. self.Platform = int(utils.GetIntArgument(c, "platform", 1))
  50. self.OsVersion = utils.GetArgument(c, "os_version", "")
  51. self.UaClient = utils.GetArgument(c, "ua", "")
  52. self.ReqSource = utils.GetArgument(c, "req_source", "")
  53. self.Mac = utils.GetArgument(c, "mac", "")
  54. self.ReqSourceIp = utils.GetArgument(c, "ip", "")
  55. self.DeviceId = utils.GetArgument(c, "device_id", "")
  56. self.NewAdsFlag = utils.GetIntArgument(c, "new_ads_flag", 0)
  57. self.content = utils.GetArgument(c, "ads_content", "")
  58. fmt.Printf("ads flag: %d, content: %s\n", self.NewAdsFlag, self.content)
  59. // 进行解密
  60. if self.NewAdsFlag == 1 && self.content != "" {
  61. contentDecode, err := encrypt.Decrypt(self.content, []byte(GetConf().SecretKey))
  62. if err != nil {
  63. fmt.Printf("error: %s", err)
  64. c.String(404, "decode failed")
  65. return
  66. }
  67. contentDecode = utils.Rstrip(contentDecode)
  68. err = json.Unmarshal([]byte(contentDecode), self)
  69. if err != nil {
  70. fmt.Println(err)
  71. c.String(404, "decode json body failed: %s", err)
  72. return
  73. }
  74. fmt.Println(contentDecode)
  75. }
  76. self.UaClient, _ = url.QueryUnescape(self.UaClient)
  77. self.Mac = strings.ReplaceAll(self.Mac, ":", "")
  78. }
  79. func (self *Request) IsMiDevice() bool {
  80. if strings.Index(self.Brand, "mi") != -1 ||
  81. strings.Index(self.Brand, "MI") != -1 ||
  82. strings.Index(self.Brand, "Mi") != -1 {
  83. return true
  84. }
  85. return false
  86. }