dM.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package v1
  2. import (
  3. v1pb "go-common/app/interface/live/app-room/api/http/v1"
  4. "go-common/app/interface/live/app-room/conf"
  5. dmrpc "go-common/app/service/live/live-dm/api/grpc/v1"
  6. risk "go-common/app/service/live/live_riskcontrol/api/grpc/v1"
  7. xcaptcha "go-common/app/service/live/xcaptcha/api/grpc/v1"
  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. "strings"
  13. )
  14. // DMService struct
  15. type DMService struct {
  16. conf *conf.Config
  17. }
  18. var (
  19. //弹幕client
  20. dmClient dmrpc.DMClient
  21. //风控client
  22. riskClient risk.IsForbiddenClient
  23. //验证码client
  24. xcaptchaClient xcaptcha.XCaptchaClient
  25. )
  26. //NewDMService init
  27. func NewDMService(c *conf.Config) (s *DMService) {
  28. s = &DMService{
  29. conf: c,
  30. }
  31. var err error
  32. if dmClient, err = dmrpc.NewClient(c.DM); err != nil {
  33. panic(err)
  34. }
  35. if riskClient, err = risk.NewClient(c.Risk); err != nil {
  36. panic(err)
  37. }
  38. if xcaptchaClient, err = xcaptcha.NewClient(c.VerifyConf); err != nil {
  39. panic(err)
  40. }
  41. return s
  42. }
  43. func sendMsg(ctx *bm.Context, req *v1pb.SendDMReq, uid int64) (resp *v1pb.SendMsgResp, err error) {
  44. resp = &v1pb.SendMsgResp{}
  45. ip := metadata.String(ctx, metadata.RemoteIP)
  46. var ck = make([]string, 0, 10)
  47. for _, v := range ctx.Request.Cookies() {
  48. if v.Name != "SESSDATA" {
  49. ck = append(ck, v.Name+"="+v.Value)
  50. }
  51. }
  52. var dmReq = &dmrpc.SendMsgReq{
  53. Uid: uid,
  54. Roomid: req.Roomid,
  55. Msg: req.Msg,
  56. Rnd: req.Rnd,
  57. Ip: ip,
  58. Fontsize: req.Fontsize,
  59. Mode: req.Mode,
  60. Platform: req.Platform,
  61. Msgtype: 0,
  62. Bubble: req.Bubble,
  63. Lancer: &dmrpc.Lancer{
  64. Build: req.Build,
  65. Buvid: ctx.Request.Header.Get("Buvid"),
  66. UserAgent: ctx.Request.Header.Get("User-Agent"),
  67. Refer: ctx.Request.Header.Get("Referer"),
  68. Cookie: strings.Join(ck, ";"),
  69. },
  70. }
  71. gresp, gerr := dmClient.SendMsg(ctx, dmReq)
  72. if gerr != nil {
  73. log.Error("DM GRPC ERR: %v", gerr)
  74. err = ecode.Error(1003218, "系统正在维护中,请稍后尝试")
  75. return nil, err
  76. }
  77. if gresp.IsLimit {
  78. err = ecode.Error(ecode.Code(gresp.Code), gresp.LimitMsg)
  79. return nil, err
  80. }
  81. return resp, nil
  82. }
  83. //验证码风控
  84. func verifyRisk(ctx *bm.Context, uid int64, req *v1pb.SendDMReq) (resp *v1pb.SendMsgResp, err error) {
  85. //验证码
  86. if req.Anti != "" {
  87. result := checkVerify(ctx, req.Anti, uid, req.Roomid)
  88. if !result {
  89. return nil, ecode.Error(1990001, "验证码验证失败")
  90. }
  91. return sendMsg(ctx, req, uid)
  92. }
  93. //风控校验
  94. ifb, ferr := isriskcontrol(ctx, uid, req)
  95. if ifb {
  96. return nil, ferr
  97. }
  98. return sendMsg(ctx, req, uid)
  99. }
  100. // SendMsg implementation
  101. // `method:"POST"`
  102. func (s *DMService) SendMsg(ctx *bm.Context, req *v1pb.SendDMReq) (resp *v1pb.SendMsgResp, err error) {
  103. //获取UID
  104. uid, ok := ctx.Get("mid")
  105. if !ok {
  106. err = ecode.Error(1003218, "未登录")
  107. return nil, err
  108. }
  109. uid64, ok := uid.(int64)
  110. if !ok {
  111. log.Error("DM: mid error")
  112. err = ecode.Error(1003218, "未登录")
  113. return nil, err
  114. }
  115. device, ok := ctx.Get("device")
  116. if !ok {
  117. log.Error("DM: Get device error")
  118. return sendMsg(ctx, req, uid64)
  119. }
  120. devices, ok := device.(*bm.Device)
  121. if !ok {
  122. log.Error("DM: device error")
  123. return sendMsg(ctx, req, uid64)
  124. }
  125. //验证码版本控制
  126. if (devices.RawMobiApp == "android" && devices.Build >= 5360000) ||
  127. (devices.RawMobiApp == "iphone" && devices.Build >= 8290) {
  128. return verifyRisk(ctx, uid64, req)
  129. }
  130. //发送弹幕
  131. return sendMsg(ctx, req, uid64)
  132. }
  133. // GetHistory implementation
  134. // `method:"POST"`
  135. func (s *DMService) GetHistory(ctx *bm.Context, req *v1pb.HistoryReq) (resp *v1pb.HistoryResp, err error) {
  136. resp = &v1pb.HistoryResp{}
  137. var hreq = &dmrpc.HistoryReq{
  138. Roomid: req.Roomid,
  139. }
  140. gresp, err := dmClient.GetHistory(ctx, hreq)
  141. if err != nil {
  142. log.Error("DM GRPC ERR: %v", err)
  143. return
  144. }
  145. resp.Admin = gresp.Admin
  146. resp.Room = gresp.Room
  147. return
  148. }