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

95 lines
1.6 KiB

  1. package siterpc
  2. import (
  3. "encoding/json"
  4. "log"
  5. "strconv"
  6. "time"
  7. "git.tetele.net/tgo/conf"
  8. "git.tetele.net/tgo/crypter"
  9. "github.com/golang/protobuf/proto"
  10. )
  11. type SiteInfoReq struct {
  12. Master string
  13. Host string
  14. }
  15. type SiteInfoRes struct {
  16. SiteId string `json:"site_id"`
  17. Dbname string `json:"database"`
  18. Domainname string `json:"domainname"`
  19. }
  20. /**
  21. * 由host取对应站点关键信息
  22. * 请求及回均加密验签
  23. * 2021/06/24
  24. * GZ
  25. */
  26. func GetSiteInfoByHost(host string, params ...string) (*SiteInfoRes, error) {
  27. var res_arr SiteInfoRes
  28. if conf.IS_PRIVATE {
  29. res_arr.Dbname = conf.DBNAME
  30. res_arr.SiteId = conf.SITE_ID
  31. res_arr.Domainname = conf.DOMAIN
  32. return &res_arr, nil
  33. }
  34. var url string = ""
  35. var master string = ""
  36. if len(params) > 1 {
  37. master = params[1]
  38. }
  39. if len(params) > 0 {
  40. url = params[0]
  41. }
  42. conn, err := rpc_server_conn(url)
  43. if err != nil {
  44. return nil, err
  45. }
  46. defer conn.Close()
  47. arg := SiteInfoReq{master, host}
  48. data_json, err := json.Marshal(arg)
  49. if err != nil {
  50. return nil, err
  51. }
  52. now_int64 := time.Now().Unix()
  53. encryData := crypter.DesEn(string(data_json), DES_KEY)
  54. now := strconv.FormatInt(now_int64, 10)
  55. sign := Sign(encryData, now)
  56. req := &Request{proto.String(encryData), proto.String(now), proto.String(sign), nil}
  57. res := &Response{}
  58. err = conn.GetSiteInfoByHost(req, res)
  59. if err != nil {
  60. return nil, err
  61. }
  62. res_data, err := GetOrgData(res)
  63. log.Println(res_data, err)
  64. if err != nil {
  65. return nil, err
  66. }
  67. err = json.Unmarshal([]byte(res_data), &res_arr)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return &res_arr, nil
  72. }