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

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