weboskceInfo.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/hmac"
  5. "crypto/md5"
  6. "crypto/sha256"
  7. "encoding/hex"
  8. "encoding/json"
  9. "fmt"
  10. "io/ioutil"
  11. "math/rand"
  12. "net/http"
  13. "sort"
  14. "strings"
  15. "time"
  16. "github.com/pkg/errors"
  17. )
  18. type BaseResp struct {
  19. Code int64 `json:"code"`
  20. Message string `json:"message"`
  21. RequestId string `json:"request_id"`
  22. Data WebsocketInfoResp `json:"data"`
  23. }
  24. type WebsocketInfoResp struct {
  25. Ip []string `json:"ip"`
  26. AuthBody string `json:"auth_body"`
  27. Host []string `json:"host"`
  28. TcpPort []int64 `json:"tcp_port"`
  29. WsPort []int64 `json:"ws_port"`
  30. WssPort []int64 `json:"wss_port"`
  31. }
  32. func hmacSHA256(key string, data string) string {
  33. mac := hmac.New(sha256.New, []byte(key))
  34. mac.Write([]byte(data))
  35. return hex.EncodeToString(mac.Sum(nil))
  36. }
  37. func toSortedString(headerMap map[string]string) string {
  38. mapKeys := make([]string, 0, len(headerMap))
  39. for k := range headerMap {
  40. mapKeys = append(mapKeys, k)
  41. }
  42. sort.Strings(mapKeys)
  43. headerStr := ""
  44. for _, mapKey := range mapKeys {
  45. headerStr += fmt.Sprintf("%v:%v\n", mapKey, headerMap[mapKey])
  46. }
  47. return strings.TrimRight(headerStr, "\n")
  48. }
  49. func Sign(params map[string]interface{}, key, secret string) map[string]string {
  50. paramStr, _ := json.Marshal(params)
  51. md5Str := fmt.Sprintf("%x", md5.Sum(paramStr))
  52. now := time.Now().Unix()
  53. headerMap := map[string]string{
  54. "x-bili-accesskeyid": key,
  55. "x-bili-content-md5": md5Str,
  56. "x-bili-signature-method": "HMAC-SHA256",
  57. "x-bili-signature-version": "1.0",
  58. "x-bili-signature-nonce": fmt.Sprintf("%v", rand.Int63n(100000)+now),
  59. "x-bili-timestamp": fmt.Sprintf("%v", now),
  60. }
  61. headerStr := toSortedString(headerMap)
  62. headerMap["Authorization"] = hmacSHA256(secret, headerStr)
  63. headerMap["Content-Type"] = "application/json"
  64. headerMap["Accept"] = "application/json"
  65. return headerMap
  66. }
  67. func SendPost(url, key, secret string, params map[string]interface{}) (*http.Response, error) {
  68. paramsBytes, _ := json.Marshal(params)
  69. header := Sign(params, key, secret)
  70. req, err := http.NewRequest("POST", url, bytes.NewBuffer(paramsBytes))
  71. if err != nil {
  72. return nil, err
  73. }
  74. for k, v := range header {
  75. req.Header.Set(k, v)
  76. }
  77. client := &http.Client{}
  78. return client.Do(req)
  79. }
  80. func GetWebsocketInfo(akId, akSecret string) (host string, port int64, authBody string, err error) {
  81. resp, err := SendPost(fmt.Sprintf("%s/v1/common/websocketInfo", TestHttpHost), akId, akSecret, map[string]interface{}{})
  82. body, _ := ioutil.ReadAll(resp.Body)
  83. infoData := &BaseResp{}
  84. if err = json.Unmarshal(body, infoData); err != nil {
  85. err = errors.Wrapf(err, "[websocketInfo | GetWebsocketInfo] json.Unmarshal err resp:%+v", resp)
  86. return
  87. }
  88. // fmt.Printf("%#v\n", infoData.Data)
  89. // 这里简单写下,实际可以根据需求取
  90. if len(infoData.Data.WsPort) != 0 && len(infoData.Data.Host) != 0 {
  91. host, port, authBody = infoData.Data.Host[0], infoData.Data.WsPort[0], infoData.Data.AuthBody
  92. }
  93. return
  94. }