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

113 lines
2.2 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
3 years ago
  1. package siterpc
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strconv"
  6. "time"
  7. "git.tetele.net/tgo/crypter"
  8. "github.com/golang/protobuf/proto"
  9. )
  10. type SiteConfigItemReqArg struct {
  11. SiteId string `json:"site_id"`
  12. Dbname string `json:"dbname"`
  13. Key string `json:"key"`
  14. }
  15. type SiteConfigItemRes struct {
  16. Type string
  17. Value string
  18. Exist bool
  19. }
  20. /**
  21. * 由配置key取对应value
  22. * 请求及回均加密验签
  23. * 2021/01/20
  24. * GZ
  25. */
  26. func GetConfigItem(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  27. ret, err := getconfig(site_id, dbname, key, url...)
  28. if err != nil {
  29. return ret, err
  30. }
  31. if ret.Exist == false {
  32. return ret, errors.New("no data")
  33. }
  34. return ret, err
  35. }
  36. func GetConfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  37. return getconfig(site_id, dbname, key, url...)
  38. }
  39. func getconfig(site_id, dbname, key string, url ...string) (*SiteConfigItemRes, error) {
  40. conn, err := rpc_server_conn(url...)
  41. if err != nil {
  42. return nil, err
  43. }
  44. defer conn.Close()
  45. arg := SiteConfigItemReqArg{site_id, dbname, key}
  46. data_json, err := json.Marshal(arg)
  47. if err != nil {
  48. return nil, err
  49. }
  50. now_int64 := time.Now().Unix()
  51. encryData := crypter.DesEn(string(data_json), "confdata")
  52. now := strconv.FormatInt(now_int64, 10)
  53. sign := Sign(encryData, now)
  54. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  55. res := &Response{}
  56. err = conn.GetConfig(req, res)
  57. if err != nil {
  58. return nil, err
  59. }
  60. res_data := res.GetData()
  61. if res_data != "" {
  62. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  63. if err != nil {
  64. return nil, err
  65. }
  66. now_int64 = time.Now().Unix()
  67. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  68. //时间误差前后10秒,返回
  69. return nil, errors.New("返回时间错误")
  70. }
  71. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  72. if !check_sign {
  73. return nil, errors.New("返回数据签名错误")
  74. }
  75. //解密
  76. res_data_de := crypter.DesDe(res_data, "confdata")
  77. var res_arr SiteConfigItemRes
  78. err = json.Unmarshal([]byte(res_data_de), &res_arr)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &res_arr, nil
  83. }
  84. return nil, nil
  85. }