rpc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
2.5 KiB

3 years ago
3 years ago
3 years ago
  1. package site
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "git.tetele.net/tgo/crypter"
  11. "github.com/golang/protobuf/proto"
  12. )
  13. type SiteConfigItemReqArg struct {
  14. SiteId string `json:"site_id"`
  15. Dbname string `json:"dbname"`
  16. Key string `json:"key"`
  17. }
  18. type SiteConfigItemRes struct {
  19. Type string
  20. Value string
  21. }
  22. /**
  23. * 由配置key取对应value
  24. * 请求及回均加密验签
  25. * 2021/01/20
  26. * GZ
  27. */
  28. func GetConfigItem(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  29. var site_rpc_url string = "127.0.0.1:7971"
  30. if len(url) > 0 && url[0] != "" {
  31. site_rpc_url = url[0]
  32. }
  33. conn, _, err := DialSiteService("tcp", site_rpc_url)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer conn.Close()
  38. arg := SiteConfigItemReqArg{site_id, dbname, key}
  39. data_json, err := json.Marshal(arg)
  40. if err != nil {
  41. return nil, err
  42. }
  43. now_int64 := time.Now().Unix()
  44. encryData := crypter.DesEn(string(data_json), "confdata")
  45. now := strconv.FormatInt(now_int64, 10)
  46. sign := Sign(encryData, now)
  47. req := &ConfigRequest{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  48. res := &ConfigResponse{}
  49. err = conn.GetConfig(req, res)
  50. if err != nil {
  51. return nil, err
  52. }
  53. res_data := res.GetData()
  54. if res_data != "" {
  55. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  56. if err != nil {
  57. return nil, err
  58. }
  59. now_int64 = time.Now().Unix()
  60. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  61. //时间误差前后10秒,返回
  62. return nil, errors.New("返回时间错误")
  63. }
  64. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  65. if !check_sign {
  66. return nil, errors.New("返回数据签名错误")
  67. }
  68. //解密
  69. res_data_de := crypter.DesDe(res_data, "confdata")
  70. var res_arr SiteConfigItemRes
  71. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &res_arr, nil
  76. }
  77. return nil, nil
  78. }
  79. /**
  80. * 签名
  81. */
  82. func Sign(data string, salt string) string {
  83. var build strings.Builder
  84. build.WriteString(data)
  85. build.WriteString(salt)
  86. build.WriteString("site55sign33")
  87. data_str := build.String()
  88. h := md5.New()
  89. h.Write([]byte(data_str)) // 需要加密的字符串
  90. return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
  91. }
  92. /**
  93. * 验证签名
  94. */
  95. func CheckSign(sign_str, data, salt string) bool {
  96. sign := Sign(data, salt)
  97. if strings.Compare(sign_str, sign) > -1 {
  98. return true
  99. }
  100. return false
  101. }