站点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.

150 lines
2.9 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package siterpc
  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. Exist bool
  22. }
  23. /**
  24. * 由配置key取对应value
  25. * 请求及回均加密验签
  26. * 2021/01/20
  27. * GZ
  28. */
  29. func GetConfigItem(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  30. ret, err := getconfig(site_id, dbname, key, url...)
  31. if err != nil {
  32. return ret, err
  33. }
  34. if ret.Exist == false {
  35. return ret, errors.New("no data")
  36. }
  37. return ret, err
  38. }
  39. func GetConfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  40. return getconfig(site_id, dbname, key, url...)
  41. }
  42. func getconfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  43. var site_rpc_url string = "127.0.0.1:7971"
  44. if len(url) > 0 && url[0] != "" {
  45. site_rpc_url = url[0]
  46. }
  47. conn, _, err := DialSiteService("tcp", site_rpc_url)
  48. if err != nil {
  49. return nil, err
  50. }
  51. defer conn.Close()
  52. arg := SiteConfigItemReqArg{site_id, dbname, key}
  53. data_json, err := json.Marshal(arg)
  54. if err != nil {
  55. return nil, err
  56. }
  57. now_int64 := time.Now().Unix()
  58. encryData := crypter.DesEn(string(data_json), "confdata")
  59. now := strconv.FormatInt(now_int64, 10)
  60. sign := Sign(encryData, now)
  61. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  62. res := &Response{}
  63. err = conn.GetConfig(req, res)
  64. if err != nil {
  65. return nil, err
  66. }
  67. res_data := res.GetData()
  68. if res_data != "" {
  69. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  70. if err != nil {
  71. return nil, err
  72. }
  73. now_int64 = time.Now().Unix()
  74. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  75. //时间误差前后10秒,返回
  76. return nil, errors.New("返回时间错误")
  77. }
  78. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  79. if !check_sign {
  80. return nil, errors.New("返回数据签名错误")
  81. }
  82. //解密
  83. res_data_de := crypter.DesDe(res_data, "confdata")
  84. var res_arr SiteConfigItemRes
  85. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &res_arr, nil
  90. }
  91. return nil, nil
  92. }
  93. /**
  94. * 签名
  95. */
  96. func Sign(data string, salt string) string {
  97. var build strings.Builder
  98. build.WriteString(data)
  99. build.WriteString(salt)
  100. build.WriteString("site55sign33")
  101. data_str := build.String()
  102. h := md5.New()
  103. h.Write([]byte(data_str)) // 需要加密的字符串
  104. return hex.EncodeToString(h.Sum(nil)) // 输出加密结果
  105. }
  106. /**
  107. * 验证签名
  108. */
  109. func CheckSign(sign_str, data, salt string) bool {
  110. sign := Sign(data, salt)
  111. if strings.Compare(sign_str, sign) > -1 {
  112. return true
  113. }
  114. return false
  115. }