http.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package signature_request
  2. import (
  3. "encoding/json"
  4. "git.bvbej.com/bvbej/base-golang/pkg/aes"
  5. "git.bvbej.com/bvbej/base-golang/pkg/hmac"
  6. "git.bvbej.com/bvbej/base-golang/pkg/httpclient"
  7. "git.bvbej.com/bvbej/base-golang/pkg/time_parse"
  8. "github.com/tidwall/gjson"
  9. netUrl "net/url"
  10. "strconv"
  11. "time"
  12. )
  13. type DingTalkTextAlert struct {
  14. MsgType string `json:"msgtype"`
  15. Text struct {
  16. Content string `json:"content"`
  17. } `json:"text"`
  18. }
  19. func HttpPostByAes(url, productID, key, vi string, params netUrl.Values) (body []byte, err error) {
  20. var signatureDatetime = time_parse.CSTLayoutString()
  21. unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
  22. params.Set("t", strconv.FormatInt(unix, 10))
  23. var sign string
  24. escape := netUrl.QueryEscape(params.Encode())
  25. sign, err = aes.New(key, vi).Encrypt(escape)
  26. if err != nil {
  27. return body, err
  28. }
  29. body, err = httpclient.PostForm(
  30. url,
  31. params,
  32. httpclient.WithTTL(time.Second*5),
  33. httpclient.WithHeader("Signature", sign),
  34. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  35. httpclient.WithHeader("Product-ID", productID),
  36. httpclient.WithOnFailedRetry(3, time.Second*1,
  37. func(body []byte) (shouldRetry bool) {
  38. return len(body) == 0
  39. },
  40. ),
  41. )
  42. return body, err
  43. }
  44. func HttpPostByAesWithTimeout(url, productID, key, vi string, params netUrl.Values, timeout time.Duration) (body []byte, err error) {
  45. var signatureDatetime = time_parse.CSTLayoutString()
  46. unix, _ := time_parse.CSTLayoutStringToUnix(signatureDatetime)
  47. params.Set("t", strconv.FormatInt(unix, 10))
  48. var sign string
  49. escape := netUrl.QueryEscape(params.Encode())
  50. sign, err = aes.New(key, vi).Encrypt(escape)
  51. if err != nil {
  52. return body, err
  53. }
  54. body, err = httpclient.PostForm(
  55. url,
  56. params,
  57. httpclient.WithTTL(timeout),
  58. httpclient.WithHeader("Signature", sign),
  59. httpclient.WithHeader("Signature-Datetime", signatureDatetime),
  60. httpclient.WithHeader("Product-ID", productID),
  61. httpclient.WithOnFailedRetry(3, time.Second*1,
  62. func(body []byte) (shouldRetry bool) {
  63. return len(body) == 0
  64. },
  65. ),
  66. )
  67. return body, err
  68. }
  69. func DingTalkAlert(accessToken, secret string, body json.RawMessage) bool {
  70. params := netUrl.Values{}
  71. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  72. signData := timestamp + "\n" + secret
  73. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  74. params.Set("access_token", accessToken)
  75. params.Set("sign", sign)
  76. params.Set("timestamp", timestamp)
  77. response, err := httpclient.PostJSON(
  78. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  79. body,
  80. httpclient.WithTTL(time.Second*5),
  81. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  82. httpclient.WithOnFailedRetry(3, time.Second*1,
  83. func(body []byte) (shouldRetry bool) {
  84. return len(body) == 0
  85. },
  86. ),
  87. )
  88. if err != nil {
  89. return false
  90. }
  91. return gjson.Get(string(response), "errcode").Int() == 0
  92. }
  93. func DingTalkAlertText(accessToken, secret string, text string) bool {
  94. params := netUrl.Values{}
  95. timestamp := strconv.FormatInt(time_parse.GetMilliTimestamp(), 10)
  96. signData := timestamp + "\n" + secret
  97. sign := netUrl.QueryEscape(hmac.New(secret).Sha256ToBase64String(signData))
  98. params.Set("access_token", accessToken)
  99. params.Set("sign", sign)
  100. params.Set("timestamp", timestamp)
  101. var content DingTalkTextAlert
  102. content.MsgType = "text"
  103. content.Text.Content = text
  104. body, _ := json.Marshal(content)
  105. response, err := httpclient.PostJSON(
  106. "https://oapi.dingtalk.com/robot/send?"+params.Encode(),
  107. body,
  108. httpclient.WithTTL(time.Second*5),
  109. httpclient.WithHeader("Content-Type", "application/json;charset=utf-8"),
  110. httpclient.WithOnFailedRetry(3, time.Second*1,
  111. func(body []byte) (shouldRetry bool) {
  112. return len(body) == 0
  113. },
  114. ),
  115. )
  116. if err != nil {
  117. return false
  118. }
  119. return gjson.Get(string(response), "errcode").Int() == 0
  120. }