dm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/json"
  6. "fmt"
  7. "math"
  8. "strconv"
  9. "time"
  10. dmv1pb "go-common/app/interface/live/open-interface/api/http/v1"
  11. "go-common/app/interface/live/open-interface/internal/dao"
  12. broadcasrtService "go-common/app/service/live/broadcast-proxy/api/v1"
  13. titansSdk "go-common/app/service/live/resource/sdk"
  14. "go-common/library/ecode"
  15. "go-common/library/log"
  16. "go-common/library/net/metadata"
  17. )
  18. func checksign(ts string, sign string, group string) bool {
  19. sts := time.Now().Unix()
  20. cts, _ := strconv.ParseInt(ts, 10, 64)
  21. dv := sts - cts
  22. if math.Abs(float64(dv)) > 5 {
  23. log.Info("[dm] ts err: %+d", dv)
  24. return false
  25. }
  26. userSecrets, terr := titansSdk.Get("dmUser")
  27. if terr != nil {
  28. log.Error("[dm] get titan conf err: %+v", terr)
  29. return false
  30. }
  31. juserSecrets := make(map[string]string)
  32. if jerr := json.Unmarshal([]byte(userSecrets), &juserSecrets); jerr != nil {
  33. log.Error("[dm] JSON decode titan conf err: %+v", terr)
  34. return false
  35. }
  36. secret, ok := juserSecrets[group]
  37. if !ok {
  38. log.Info("[dm] unknow group: %+v", group)
  39. return false
  40. }
  41. newSign := fmt.Sprintf("%x", md5.Sum([]byte(group+secret+ts)))
  42. if newSign != sign {
  43. log.Info("[dm] check sign err sign: %+s service sign: %s", sign, newSign)
  44. return false
  45. }
  46. return true
  47. }
  48. //Sendmsg 发送弹幕消息
  49. func (s *Service) Sendmsg(ctx context.Context, req *dmv1pb.SendMsgReq) (resp *dmv1pb.SendMsgResp, err error) {
  50. if ok := checksign(req.GetTs(), req.GetSign(), req.GetGroup()); !ok {
  51. return nil, ecode.Error(-403, "sign or ts error")
  52. }
  53. var dmString = "{\"cmd\":\"DANMU_MSG\",\"info\":[[0,1,25,16777215,%d,0,0,\"\",0,0,0],\"%s\",[0,\"\",0,0,0,0,0,\"\"],[],[0,0,0,\"\"],[\"\",\"\"],0,0]}"
  54. breq := &broadcasrtService.RoomMessageRequest{
  55. RoomId: int32(req.GetRoomID()),
  56. Message: fmt.Sprintf(dmString, time.Now().Unix(), req.GetMsg()),
  57. }
  58. _, berr := dao.BcastClient.DanmakuClient.RoomMessage(ctx, breq)
  59. if berr != nil {
  60. log.Error("[dm] SendBroadCastGrpc err: %+v", berr)
  61. return nil, ecode.Error(-400, "send msg err")
  62. }
  63. return
  64. }
  65. //GetConf 获取弹幕配置
  66. func (s *Service) GetConf(ctx context.Context, req *dmv1pb.GetConfReq) (resp *dmv1pb.GetConfResp, err error) {
  67. if ok := checksign(req.GetTs(), req.GetSign(), req.GetGroup()); !ok {
  68. return nil, ecode.Error(-403, "sign or ts error")
  69. }
  70. resp = &dmv1pb.GetConfResp{
  71. WSPort: []int64{2244},
  72. WSSPort: []int64{443},
  73. TCPPort: []int64{2243, 80},
  74. DomianList: []string{},
  75. }
  76. breq := &broadcasrtService.DispatchRequest{
  77. UserIp: metadata.String(ctx, metadata.RemoteIP),
  78. }
  79. bresp, berr := dao.BcastClient.Dispatch(ctx, breq)
  80. if berr != nil {
  81. resp.IPList = []string{"broadcastlv.chat.bilibili.com"}
  82. resp.DomianList = []string{"broadcastlv.chat.bilibili.com"}
  83. log.Error("[dm] get IPList by BcastClient Dispatch err:%+v", berr)
  84. } else {
  85. resp.IPList = bresp.Ip
  86. resp.DomianList = bresp.Host
  87. }
  88. return resp, nil
  89. }