click.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package http
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/report-click/conf"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. )
  13. const (
  14. _platWeb = "0"
  15. _platH5 = "1"
  16. _platOuter = "2"
  17. _platIos = "3"
  18. _platAndroid = "4"
  19. _platAndroidTV = "5"
  20. )
  21. var _expireCookie = time.Date(2022, time.November, 10, 23, 0, 0, 0, time.UTC)
  22. // webClick write the archive data.
  23. func webClick(c *bm.Context) {
  24. err := flashPlay(c, _platWeb, conf.Conf.Click.WebSecret)
  25. c.JSON(nil, err)
  26. }
  27. // outerClick write the archive data.
  28. func outerClick(c *bm.Context) {
  29. err := flashPlay(c, _platOuter, conf.Conf.Click.OutSecret)
  30. c.JSON(nil, err)
  31. }
  32. // iosClick write the archive data.
  33. func iosClick(c *bm.Context) {
  34. err := mobilePlay(c, conf.Conf.Click.AesKey, conf.Conf.Click.AesIv, conf.Conf.Click.AesSalt, _platIos)
  35. c.JSON(nil, err)
  36. }
  37. // androidClick write the archive data.
  38. func androidClick(c *bm.Context) {
  39. err := mobilePlay(c, conf.Conf.Click.AesKey, conf.Conf.Click.AesIv, conf.Conf.Click.AesSalt, _platAndroid)
  40. c.JSON(nil, err)
  41. }
  42. // android2Click write the archive data.
  43. func android2Click(c *bm.Context) {
  44. err := mobilePlay(c, conf.Conf.Click.AesKey2, conf.Conf.Click.AesIv2, conf.Conf.Click.AesSalt2, _platAndroid)
  45. c.JSON(nil, err)
  46. }
  47. // androidTV == android2Click write the archive data.
  48. func androidTV(c *bm.Context) {
  49. err := mobilePlay(c, conf.Conf.Click.AesKey2, conf.Conf.Click.AesIv2, conf.Conf.Click.AesSalt2, _platAndroidTV)
  50. c.JSON(nil, err)
  51. }
  52. // outerClickH5 h5 outer click same to flash plat .
  53. func outerClickH5(c *bm.Context) {
  54. c.JSON(nil, h5Play(c, _platOuter))
  55. }
  56. // h5Click write the archive data.
  57. func h5Click(c *bm.Context) {
  58. c.JSON(nil, h5Play(c, _platH5))
  59. }
  60. // webH5Click write the archive data.
  61. func webH5Click(c *bm.Context) {
  62. c.JSON(nil, h5Play(c, _platWeb))
  63. }
  64. // flashPlay.
  65. func flashPlay(c *bm.Context, plat, secret string) (err error) {
  66. var (
  67. buvid string
  68. mid int64
  69. ck *http.Cookie
  70. params = c.Request.Form
  71. unix = time.Now()
  72. )
  73. if midInter, ok := c.Get("mid"); ok {
  74. mid = midInter.(int64)
  75. }
  76. if ck, err = c.Request.Cookie("buvid3"); err == nil {
  77. buvid = ck.Value
  78. }
  79. userAgent := c.Request.Header.Get("User-Agent")
  80. refer := c.Request.Header.Get("Referer")
  81. sign := params.Get("sign")
  82. if err = clickSvr.FlashSigned(params, secret, unix); err != nil {
  83. log.Error("clickSvr.FlashSigned() error(%v)", err)
  84. if err == ecode.ClickQuerySignErr {
  85. log.Warn("click sign error(%s,%s,%s,%s)", sign, refer, userAgent, c.Request.Header.Get("Origin"))
  86. }
  87. return
  88. }
  89. midStr := params.Get("mid")
  90. if mid != 0 && midStr != strconv.FormatInt(mid, 10) {
  91. log.Warn("flashPlay stat mid(%d) not equal stat mid(%s)", mid, midStr)
  92. return
  93. }
  94. midStr = strconv.FormatInt(mid, 10)
  95. aid := params.Get("aid")
  96. var cookieSid string
  97. if ck, err := c.Request.Cookie("sid"); err == nil {
  98. cookieSid = ck.Value
  99. }
  100. typeID := params.Get("type")
  101. subType := params.Get("sub_type")
  102. sid := params.Get("sid")
  103. epid := params.Get("epid")
  104. // service.
  105. ip := metadata.String(c, metadata.RemoteIP)
  106. clickSvr.Play(c, plat, aid, params.Get("cid"), params.Get("part"),
  107. midStr, params.Get("lv"), params.Get("ftime"), params.Get("stime"),
  108. params.Get("did"), ip, userAgent, buvid, cookieSid, refer, typeID, subType, sid, epid, "", "", "", "", "", "")
  109. return
  110. }
  111. // mobilePlay.
  112. func mobilePlay(c *bm.Context, aesKey, aesIv, aesSalt, plat string) (err error) {
  113. // check params.
  114. req := c.Request
  115. unix := time.Now()
  116. bs, err := ioutil.ReadAll(req.Body)
  117. if err != nil {
  118. log.Error("ioutil.ReadAll error(%v)", err)
  119. err = ecode.ServerErr
  120. return
  121. }
  122. req.Body.Close()
  123. bs, err = clickSvr.Decrypt(bs, aesKey, aesIv)
  124. if err != nil {
  125. log.Error("clickSvr.Decrypt(%s) error(%d)", bs, err)
  126. return
  127. }
  128. p, err := clickSvr.Verify(bs, aesSalt, unix)
  129. if err != nil {
  130. log.Error("clickSvr.Verify(%s) error(%d)", bs, err)
  131. return
  132. }
  133. req.Form = p // for log
  134. typeID := p.Get("type")
  135. subType := p.Get("sub_type")
  136. sid := p.Get("sid")
  137. epid := p.Get("epid")
  138. playMode := p.Get("play_mode")
  139. platform := p.Get("platform")
  140. device := p.Get("device")
  141. mobiAapp := p.Get("mobi_app")
  142. autoPlay := p.Get("auto_play")
  143. ap, _ := strconv.ParseInt(autoPlay, 10, 64)
  144. // service.
  145. aidStr := p.Get("aid")
  146. var (
  147. accessKey string
  148. midStr = p.Get("mid")
  149. noAccess bool
  150. )
  151. paasMid, _ := strconv.ParseInt(midStr, 10, 64)
  152. accessKey = p.Get("access_key")
  153. if paasMid > 0 && accessKey == "" {
  154. noAccess = true
  155. }
  156. if accessKey != "" {
  157. c.Request.Form.Set("access_key", accessKey)
  158. authSvc.User(c)
  159. mid, ok := c.Get("mid")
  160. if !ok {
  161. log.Warn("idfSvc.Access() access_key", accessKey)
  162. if paasMid > 0 {
  163. noAccess = true
  164. }
  165. }
  166. if mid != nil {
  167. midStr = strconv.FormatInt(mid.(int64), 10)
  168. }
  169. }
  170. refer := c.Request.Header.Get("Referer")
  171. userAgent := c.Request.Header.Get("User-Agent")
  172. if noAccess {
  173. userAgent = userAgent + " (no_accesskey)"
  174. }
  175. if ap == 1 || ap == 2 { // abandon the logic that transforms the plat to 6/7/8/9, keep the plat and modify the UA
  176. userAgent += " (inline_play_begin)"
  177. }
  178. buvid := req.Header.Get("buvid")
  179. var cookieSid string
  180. if ck, err := c.Request.Cookie("sid"); err == nil {
  181. cookieSid = ck.Value
  182. }
  183. ip := metadata.String(c, metadata.RemoteIP)
  184. clickSvr.Play(c, plat, aidStr, p.Get("cid"), p.Get("part"), midStr, p.Get("lv"),
  185. p.Get("ftime"), p.Get("stime"), p.Get("did"), ip, userAgent, buvid,
  186. cookieSid, refer, typeID, subType, sid, epid, playMode, platform, device, mobiAapp, autoPlay, "")
  187. return
  188. }
  189. func h5Play(c *bm.Context, plat string) (err error) {
  190. var (
  191. buvid string
  192. mid int64
  193. ck *http.Cookie
  194. params = c.Request.Form
  195. unix = time.Now()
  196. )
  197. if midInter, ok := c.Get("mid"); ok {
  198. mid = midInter.(int64)
  199. }
  200. if ck, err = c.Request.Cookie("buvid3"); err == nil {
  201. buvid = ck.Value
  202. }
  203. // check params.
  204. st := params.Get("stime")
  205. stime, err := strconv.ParseInt(st, 10, 64)
  206. if err != nil {
  207. err = ecode.ClickQueryFormatErr
  208. return
  209. }
  210. if unix.Unix()-stime > 60 {
  211. err = ecode.ClickServerTimeout
  212. return
  213. }
  214. typeID := params.Get("type")
  215. subType := params.Get("sub_type")
  216. sid := params.Get("sid")
  217. epid := params.Get("epid")
  218. var ft string
  219. // check cookie did
  220. var (
  221. did string
  222. ip = metadata.String(c, metadata.RemoteIP)
  223. )
  224. cookie, err := c.Request.Cookie("rpdid")
  225. if err != nil || cookie.Value == "" {
  226. did = clickSvr.GenDid(ip, unix)
  227. http.SetCookie(c.Writer, &http.Cookie{Name: "rpdid", Value: did, Path: "/", Domain: ".bilibili.com", Expires: _expireCookie})
  228. err = nil
  229. } else {
  230. did = cookie.Value
  231. }
  232. _, ft = clickSvr.CheckDid(did)
  233. if ft == "" {
  234. log.Error("ft null ft:%s,did:%s", ft, did)
  235. return
  236. }
  237. midStr := params.Get("mid")
  238. if mid != 0 && midStr != strconv.FormatInt(mid, 10) {
  239. log.Warn("h5 stat mid(%d) not equal stat mid(%s)", mid, midStr)
  240. return
  241. }
  242. midStr = strconv.FormatInt(mid, 10)
  243. aid := params.Get("aid")
  244. userAgent := c.Request.Header.Get("User-Agent")
  245. var cookieSid string
  246. if ck, err := c.Request.Cookie("sid"); err == nil {
  247. cookieSid = ck.Value
  248. }
  249. refer := c.Request.Header.Get("Referer")
  250. // service.
  251. clickSvr.Play(c, plat, aid, params.Get("cid"), params.Get("part"),
  252. midStr, params.Get("lv"), ft, params.Get("stime"), did,
  253. ip, userAgent, buvid, cookieSid, refer, typeID, subType, sid, epid, "", "", "", "", "", "")
  254. return
  255. }