idrac6.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bmconsole
  15. import (
  16. "context"
  17. "fmt"
  18. "net/http"
  19. "net/url"
  20. "regexp"
  21. "strings"
  22. "time"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/util/httputils"
  26. "yunion.io/x/onecloud/pkg/httperrors"
  27. )
  28. func (r *SBMCConsole) GetIdrac6ConsoleJNLP(ctx context.Context, sku, model string) (string, error) {
  29. cookies := make(map[string]string)
  30. hdr, _, err := r.RawRequest(ctx, httputils.GET, "/start.html", nil, nil)
  31. if err != nil {
  32. return "", errors.Wrap(err, "r.Get start.html")
  33. }
  34. log.Debugf("start.html hdr %s", hdr)
  35. if setCookies, ok := hdr["Set-Cookie"]; ok {
  36. for _, cookieHdr := range setCookies {
  37. parts := strings.Split(cookieHdr, ";")
  38. if len(parts) > 0 {
  39. pparts := strings.Split(parts[0], "=")
  40. if len(pparts) > 1 {
  41. cookies[pparts[0]] = pparts[1]
  42. }
  43. }
  44. }
  45. } else {
  46. // find no cookie
  47. }
  48. loginData := strings.Join([]string{
  49. "user=" + url.QueryEscape(r.username),
  50. "password=" + url.QueryEscape(strings.ReplaceAll(r.password, "@", "@040")),
  51. }, "&")
  52. postHdr := http.Header{}
  53. postHdr.Set("Content-Type", "application/x-www-form-urlencoded")
  54. setCookieHeader(postHdr, cookies)
  55. _, loginResp, err := r.RawRequest(ctx, httputils.POST, "/data/login", postHdr, []byte(loginData))
  56. if err != nil {
  57. return "", errors.Wrap(err, "r.FormPost Login")
  58. }
  59. log.Debugf("LoginResp: %s", loginResp)
  60. forwardUrlPattern := regexp.MustCompile(`<forwardUrl>(.*)</forwardUrl>`)
  61. matched := forwardUrlPattern.FindAllStringSubmatch(string(loginResp), -1)
  62. indexUrlStr := ""
  63. if len(matched) > 0 && len(matched[0]) > 1 {
  64. indexUrlStr = matched[0][1]
  65. }
  66. if len(indexUrlStr) == 0 {
  67. return "", errors.Wrapf(httperrors.ErrBadRequest, "no valid forwardUrl")
  68. }
  69. tokenPattern := regexp.MustCompile(`ST1=(\w+),ST2=`)
  70. matched = tokenPattern.FindAllStringSubmatch(indexUrlStr, -1)
  71. log.Debugf("%s", matched)
  72. token := ""
  73. if len(matched) > 0 && len(matched[0]) > 1 {
  74. token = matched[0][1]
  75. }
  76. log.Debugf("token: %s", token)
  77. cookies["batteriesIcon"] = "status_normal"
  78. cookies["fansIcon"] = "status_normal"
  79. cookies["intrusionIcon"] = "status_normal"
  80. cookies["powerSuppliesIcon"] = "status_normal"
  81. cookies["removableFlashMediaIcon"] = "status_normal"
  82. cookies["temperaturesIcon"] = "status_normal"
  83. cookies["voltagesIcon"] = "status_normal"
  84. getHdr := http.Header{}
  85. setCookieHeader(getHdr, cookies)
  86. getHdr.Set("Referer", fmt.Sprintf("https://%s/sysSummaryData.html", r.host))
  87. getHdr.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
  88. getHdr.Set("Accept-Encoding", "gzip, deflate, br")
  89. getHdr.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
  90. sysStr := url.QueryEscape(fmt.Sprintf("idrac-%s, %s, User:%s", sku, model, r.username))
  91. // sysStr := url.QueryEscape(fmt.Sprintf("idrac-%s, %s, slot , &#29992;&#25143;&#65306; %s", sku, model, r.username))
  92. path := fmt.Sprintf("viewer.jnlp(%s@0@%s@%d)", r.host, sysStr, time.Now().UnixNano()/1000000)
  93. _, rspBody, err := r.RawRequest(ctx, httputils.GET, path, getHdr, nil)
  94. if err != nil {
  95. return "", errors.Wrapf(err, "r.RawGet %s", path)
  96. }
  97. return string(rspBody), nil
  98. }