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

92 lines
1.5 KiB

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