zt_httpclient.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package thirdparty
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/x509"
  8. "gd_management/common.in/utils"
  9. "encoding/base64"
  10. "encoding/json"
  11. "encoding/pem"
  12. "errors"
  13. "go.uber.org/zap"
  14. "io/ioutil"
  15. "net/http"
  16. "time"
  17. )
  18. var applicationId = "TINYTIMES_SERVICE"
  19. var publicKey = []byte(`
  20. -----BEGIN PUBLIC KEY-----
  21. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCakL0HbEbh3I/WS/BnLjfohhDt+n3ITxHy5mc/
  22. GN4fbWUEBM419VJz7qwelrNSuFsKJl057h9yGzQ4wYHaJCpQZaLYFqJXJYqC9nNtD0KM7c7J08qs
  23. i24Nx+qKDtLTWH6pK5Qc+aWlF7s4LWNyCb/STJv7f6G+Q/qtVji1fNW2aQIDAQAB
  24. -----END PUBLIC KEY-----
  25. `)
  26. var privateKey = []byte(`
  27. -----BEGIN RSA PRIVATE KEY-----
  28. MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJqQvQdsRuHcj9ZL8GcuN+iGEO36
  29. fchPEfLmZz8Y3h9tZQQEzjX1UnPurB6Ws1K4WwomXTnuH3IbNDjBgdokKlBlotgWolclioL2c20P
  30. QoztzsnTyqyLbg3H6ooO0tNYfqkrlBz5paUXuzgtY3IJv9JMm/t/ob5D+q1WOLV81bZpAgMBAAEC
  31. gYEAlOVLg8QVwGy2D21WKD+I0jGcS3RcCUPKeaDk4w60o7fBcQ6kC6zJAi9wlYL9OnmoEe4FLtIq
  32. bsSwGOCok5j1PL9jHBLfdO3yojgMKbTj/3n3Wh5fipTL06bJhWsDbTo3P0EjfbNZlj8A6tQmlUSk
  33. h7UIBIEfVkivwSpwMwllnoUCQQDWjOqM6Ds4iwGPxGVakM/S5gl/M5OEcXddPo5ac7KToUIy99Yg
  34. /lzxrY6G+ZEU3zWEm6lt3Pzdkuj24UJNikXvAkEAuG0fjj7/g+mH96NKxSeKhBQeUiBPCJc8niPu
  35. ax+6aWyZqQeGunSILXxC+pm0eA8ukOEvRPiJCU/jYCrV7ZLhJwJAEAgNiqPbsPjqD/UL4b2KWebo
  36. dMHE1xOnaKdA+c9FW7Sj/qz4KhT3mvNt//Xr+lRHanY7LcNifaf5kmD4lFStrwJAIvNrO4Xs3o7v
  37. WZr0nJB1y7oUVPQShkpDDqU4lcyZg0k1hbcLbWKQPC6EcwGWFfqqlIJYdvWy4rTlHmB/0lo3mQJB
  38. AK4cdZIWBOII7KtVn20NTs5COtcFMyz9ztmjxEMD1vkPH9fJ66YcVz6GWKVo4vBHopbNUXkqpH6v
  39. DIR3C+LXw8M=
  40. -----END RSA PRIVATE KEY-----
  41. `)
  42. type ZtData struct {
  43. SendTime string `json:"sendTime"`
  44. Sign string `json:"sign"`
  45. Msg string `json:"msg"`
  46. Data map[string]string `json:"data"`
  47. }
  48. func GenPrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
  49. var err error
  50. var block *pem.Block
  51. block, _ = pem.Decode(privateKey)
  52. if block == nil {
  53. return nil, errors.New("私钥错误")
  54. }
  55. prkI, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  56. x509.ParsePKCS1PrivateKey(block.Bytes)
  57. if err != nil {
  58. return nil, errors.New("解析私钥错误")
  59. }
  60. pri := prkI.(*rsa.PrivateKey)
  61. return pri, nil
  62. }
  63. func SignPKCS1v15(src, privateKey []byte, hash crypto.Hash) ([]byte, error) {
  64. h := hash.New()
  65. h.Write(src)
  66. var hashed = h.Sum(nil)
  67. pri, err := GenPrivateKey(privateKey)
  68. if err != nil {
  69. return nil, err
  70. }
  71. ret, err := rsa.SignPKCS1v15(rand.Reader, pri, hash, hashed)
  72. if err != nil {
  73. return nil, errors.New("签名错误")
  74. }
  75. return ret, err
  76. }
  77. func signMd5(data []byte) (s string, err error) {
  78. sig, err := SignPKCS1v15(data, privateKey, crypto.MD5)
  79. if err != nil {
  80. return "", err
  81. }
  82. s = base64.StdEncoding.EncodeToString(sig)
  83. return s, nil
  84. }
  85. func ztFullUrl(host, api string) string {
  86. return host + "/" + api
  87. }
  88. func ZtHttpPost(api string, data map[string]string) (result []byte, err error) {
  89. //fullApi := ztFullUrl(host, api)
  90. defer func() {
  91. l.Info("thirdparty",
  92. zap.String("api", api),
  93. zap.String("request", utils.MarshalJsonString(data)),
  94. zap.String("response", utils.MarshalJsonString(result)))
  95. }()
  96. if data == nil {
  97. data = make(map[string]string, 0)
  98. }
  99. data["applicationID"] = applicationId
  100. // 解析待签名的数据
  101. jsonData, err := json.Marshal(data)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // Md5WithRsa 签名
  106. ret, err := signMd5(jsonData)
  107. if err != nil {
  108. return nil, err
  109. }
  110. var ztD ZtData
  111. var timeLayout = "2006-01-02 15:04:05"
  112. timeNow := time.Now().Format(timeLayout)
  113. ztD.SendTime = timeNow
  114. ztD.Sign = ret
  115. ztD.Data = data
  116. ztData, err := json.Marshal(ztD)
  117. if err != nil {
  118. return nil, err
  119. }
  120. client := &http.Client{}
  121. client.Timeout = 10 * time.Second
  122. req, err := http.NewRequest("POST", api, bytes.NewBuffer(ztData))
  123. if err != nil {
  124. return nil, err
  125. }
  126. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  127. resp, err := client.Do(req)
  128. if err != nil {
  129. return nil, err
  130. }
  131. defer resp.Body.Close()
  132. result, err = ioutil.ReadAll(resp.Body)
  133. return result, err
  134. }