任务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.

106 lines
2.1 KiB

  1. package taskrpc
  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. func SetResData(data interface{}, res *Response) {
  11. res_data_json, err := json.Marshal(data)
  12. if err == nil {
  13. encryData := crypter.DesEn(string(res_data_json), DES_KEY)
  14. now_str := strconv.FormatInt(time.Now().Unix(), 10)
  15. res_sign := Sign(encryData, now_str)
  16. res.Data = proto.String(encryData)
  17. res.Time = proto.String(now_str)
  18. res.Sign = proto.String(res_sign)
  19. }
  20. }
  21. func SetReqData(arg interface{}) (*Request, error) {
  22. data_json, err := json.Marshal(arg)
  23. if err != nil {
  24. return nil, err
  25. }
  26. now_int64 := time.Now().Unix()
  27. encryData := crypter.DesEn(string(data_json), DES_KEY)
  28. now := strconv.FormatInt(now_int64, 10)
  29. sign := Sign(encryData, now)
  30. return &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}, nil
  31. }
  32. func GetReqData(req *Request) (string, error) {
  33. res_data := req.GetData()
  34. if res_data != "" {
  35. time_int64, err := strconv.ParseInt(req.GetTime(), 10, 64)
  36. if err != nil {
  37. return "", err
  38. }
  39. now_int64 := time.Now().Unix()
  40. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  41. //时间误差前后10秒,返回
  42. return "", errors.New("返回时间错误")
  43. }
  44. check_sign := CheckSign(req.GetSign(), res_data, req.GetTime())
  45. if !check_sign {
  46. return "", errors.New("返回数据签名错误")
  47. }
  48. //解密
  49. return crypter.DesDe(res_data, DES_KEY), nil
  50. }
  51. return "", nil
  52. }
  53. func GetResData(res *Response) (string, error) {
  54. res_data := res.GetData()
  55. if res_data != "" {
  56. time_int64, err := strconv.ParseInt(res.GetTime(), 10, 64)
  57. if err != nil {
  58. return "", err
  59. }
  60. now_int64 := time.Now().Unix()
  61. if now_int64-time_int64 > 10 || time_int64-now_int64 > 10 {
  62. //时间误差前后10秒,返回
  63. return "", errors.New("返回时间错误")
  64. }
  65. check_sign := CheckSign(res.GetSign(), res_data, res.GetTime())
  66. if !check_sign {
  67. return "", errors.New("返回数据签名错误")
  68. }
  69. //解密
  70. return crypter.DesDe(res_data, DES_KEY), nil
  71. }
  72. return "", nil
  73. }