idrac9.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. "net/http"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/httputils"
  21. )
  22. const (
  23. IDRAC9_LOGIN_URL = "/sysmgmt/2015/bmc/session"
  24. IDRAC9_VCONSOLE_JAVA = "/sysmgmt/2015/server/vconsole?type=Java"
  25. )
  26. func (r *SBMCConsole) GetIdrac9ConsoleJNLP(ctx context.Context) (string, error) {
  27. cookies := make(map[string]string)
  28. cookies["-http-session-"] = ""
  29. // first do html login
  30. postHdr := http.Header{}
  31. postHdr.Set("password", r.password)
  32. postHdr.Set("user", r.username)
  33. postHdr.Set("Content-Length", "0")
  34. setCookieHeader(postHdr, cookies)
  35. hdr, _, err := r.RawRequest(ctx, httputils.POST, IDRAC9_LOGIN_URL, postHdr, nil)
  36. if err != nil {
  37. return "", errors.Wrap(err, "r.FormPost Login")
  38. }
  39. for _, cookieHdr := range hdr["Set-Cookie"] {
  40. parts := strings.Split(cookieHdr, ";")
  41. if len(parts) > 0 {
  42. pparts := strings.Split(parts[0], "=")
  43. if len(pparts) > 1 {
  44. cookies[pparts[0]] = pparts[1]
  45. }
  46. }
  47. }
  48. // XSRF-TOKEN: a636651fcc8841a3c146cea89730389c
  49. xsrfToken := hdr.Get("Xsrf-Token")
  50. getHdr := http.Header{}
  51. setCookieHeader(getHdr, cookies)
  52. getHdr.Set("Xsrf-Token", xsrfToken)
  53. getHdr.Set("Sec-Fetch-Dest", "empty")
  54. getHdr.Set("Sec-Fetch-Mode", "cors")
  55. getHdr.Set("Sec-Fetch-Site", "same-origin")
  56. _, rspBody, err := r.RawRequest(ctx, httputils.GET, IDRAC9_VCONSOLE_JAVA, getHdr, nil)
  57. if err != nil {
  58. return "", errors.Wrap(err, "r.RawGet")
  59. }
  60. return string(rspBody), nil
  61. }