anticheat.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package anticheat
  2. import (
  3. "context"
  4. "net/http"
  5. "strconv"
  6. "time"
  7. "go-common/library/log"
  8. "go-common/library/log/infoc"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. )
  12. // AntiCheat send anti-cheating info to berserker.
  13. type AntiCheat struct {
  14. infoc *infoc.Infoc
  15. }
  16. // New new AntiCheat logger.
  17. func New(c *infoc.Config) (a *AntiCheat) {
  18. return &AntiCheat{infoc: infoc.New(c)}
  19. }
  20. // antiCheat 尽可能多的提供信息.
  21. type antiCheat struct {
  22. Buvid string
  23. Build string
  24. Client string // for example ClientWeb
  25. IP string
  26. UID string
  27. Aid string
  28. Mid string
  29. Sid string
  30. Refer string
  31. URL string
  32. From string
  33. ItemID string
  34. ItemType string // for example ItemTypeAv
  35. Action string // for example ActionClick
  36. ActionID string
  37. UA string
  38. TS string
  39. Extra string
  40. }
  41. // anti-cheat const.
  42. const (
  43. ClientWeb = "web"
  44. ClientIphone = "iphone"
  45. ClientIpad = "ipad"
  46. ClientAndroid = "android"
  47. // AntiCheat ItemType
  48. ItemTypeAv = "av"
  49. ItemTypeBangumi = "bangumi"
  50. ItemTypeLive = "live"
  51. ItemTypeTopic = "topic"
  52. ItemTypeRank = "rank"
  53. ItemTypeActivity = "activity"
  54. ItemTypeTag = "tag"
  55. ItemTypeAD = "ad"
  56. ItemTypeLV = "lv"
  57. // AntiCheat Action
  58. ActionClick = "click"
  59. ActionPlay = "play"
  60. ActionFav = "fav"
  61. ActionCoin = "coin"
  62. ActionDM = "dm"
  63. ActionToView = "toview"
  64. ActionShare = "share"
  65. ActionSpace = "space"
  66. Actionfollow = "follow"
  67. ActionHeartbeat = "heartbeat"
  68. ActionAnswer = "answer"
  69. )
  70. func (a *antiCheat) toSlice() (as []interface{}) {
  71. as = make([]interface{}, 0, 18)
  72. as = append(as, a.Buvid, a.Build, a.Client, a.IP, a.UID, a.Aid, a.Mid)
  73. as = append(as, a.Sid, a.Refer, a.URL, a.From, a.ItemID, a.ItemType)
  74. as = append(as, a.Action, a.ActionID, a.UA, a.TS, a.Extra)
  75. return
  76. }
  77. // InfoAntiCheat2 for new http framework(bm).
  78. func (a *AntiCheat) InfoAntiCheat2(ctx *bm.Context, uid, aid, mid, itemID, itemType, action, actionID string) error {
  79. return a.infoAntiCheat(ctx, ctx.Request, metadata.String(ctx, metadata.RemoteIP), uid, aid, mid, itemID, itemType, action, actionID)
  80. }
  81. // infoAntiCheat common logic.
  82. func (a *AntiCheat) infoAntiCheat(ctx context.Context, req *http.Request, IP, uid, aid, mid, itemID, itemType, action, actionID string) error {
  83. params := req.Form
  84. ac := &antiCheat{
  85. UID: uid,
  86. Aid: aid,
  87. Mid: mid,
  88. ItemID: itemID,
  89. ItemType: itemType,
  90. Action: action,
  91. ActionID: actionID,
  92. IP: IP,
  93. URL: req.URL.Path,
  94. Refer: req.Header.Get("Referer"),
  95. UA: req.Header.Get("User-Agent"),
  96. TS: strconv.FormatInt(time.Now().Unix(), 10),
  97. }
  98. ac.From = params.Get("from")
  99. if csid, err := req.Cookie("sid"); err == nil {
  100. ac.Sid = csid.Value
  101. }
  102. var cli string
  103. switch {
  104. case len(params.Get("access_key")) == 0:
  105. cli = ClientWeb
  106. if ck, err := req.Cookie("buvid3"); err == nil {
  107. ac.Buvid = ck.Value
  108. }
  109. case params.Get("platform") == "ios":
  110. cli = ClientIphone
  111. if params.Get("device") == "pad" {
  112. cli = ClientIpad
  113. }
  114. case params.Get("platform") == "android":
  115. cli = ClientAndroid
  116. default:
  117. log.Warn("unkown plat(%s)", params.Get("platform"))
  118. }
  119. ac.Client = cli
  120. if cli != ClientWeb {
  121. ac.Buvid = req.Header.Get("buvid")
  122. ac.Build = params.Get("build")
  123. }
  124. return a.infoc.Infov(ctx, ac.toSlice()...)
  125. }
  126. // ServiceAntiCheat common anti-cheat.
  127. func (a *AntiCheat) ServiceAntiCheat(p map[string]string) error {
  128. return a.infoc.Info(convertBase(p)...)
  129. }
  130. // ServiceAntiCheatBus for answer anti-cheat.
  131. func (a *AntiCheat) ServiceAntiCheatBus(p map[string]string, bus []interface{}) error {
  132. ac := append(convertBase(p), bus...)
  133. return a.infoc.Info(ac...)
  134. }
  135. // ServiceAntiCheatv support mirror request
  136. func (a *AntiCheat) ServiceAntiCheatv(ctx context.Context, p map[string]string) error {
  137. return a.infoc.Infov(ctx, convertBase(p)...)
  138. }
  139. // ServiceAntiCheatBusv support mirror request
  140. func (a *AntiCheat) ServiceAntiCheatBusv(ctx context.Context, p map[string]string, bus []interface{}) error {
  141. ac := append(convertBase(p), bus...)
  142. return a.infoc.Infov(ctx, ac...)
  143. }
  144. func convertBase(p map[string]string) (res []interface{}) {
  145. ac := &antiCheat{
  146. ItemType: p["itemType"],
  147. Action: p["action"],
  148. IP: p["ip"],
  149. Mid: p["mid"],
  150. UID: p["fid"],
  151. Aid: p["aid"],
  152. Sid: p["sid"],
  153. UA: p["ua"],
  154. Buvid: p["buvid"],
  155. Refer: p["refer"],
  156. URL: p["url"],
  157. TS: strconv.FormatInt(time.Now().Unix(), 10),
  158. }
  159. res = ac.toSlice()
  160. return
  161. }