supermicro.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "strings"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/httputils"
  23. )
  24. func (r *SBMCConsole) GetSupermicroConsoleJNLP(ctx context.Context) (string, error) {
  25. loginData := strings.Join([]string{
  26. "name=" + url.QueryEscape(r.username),
  27. "pwd=" + url.QueryEscape(r.password),
  28. }, "&")
  29. // cookie:
  30. // -http-session-=::http.session::0103fd02ceac2d642361b6fdcd4a5994;
  31. // sysidledicon=ledIcon%20grayLed;
  32. // tokenvalue=478be97abdaeb4d454c0418fcca9094d
  33. cookies := make(map[string]string)
  34. // first do html login
  35. postHdr := http.Header{}
  36. postHdr.Set("Content-Type", "application/x-www-form-urlencoded")
  37. postHdr.Set("Referer", fmt.Sprintf("http://%s/", r.host))
  38. setCookieHeader(postHdr, cookies)
  39. hdr, _, err := r.RawRequest(ctx, httputils.POST, "/cgi/login.cgi", postHdr, []byte(loginData))
  40. if err != nil {
  41. return "", errors.Wrap(err, "r.FormPost Login")
  42. }
  43. for _, cookieHdr := range hdr["Set-Cookie"] {
  44. parts := strings.Split(cookieHdr, ";")
  45. if len(parts) > 0 {
  46. pparts := strings.Split(parts[0], "=")
  47. if len(pparts) > 1 {
  48. cookies[pparts[0]] = pparts[1]
  49. }
  50. }
  51. }
  52. getHdr := http.Header{}
  53. getHdr.Set("Referer", fmt.Sprintf("https://%s/cgi/url_redirect.cgi?url_name=man_ikvm", r.host))
  54. setCookieHeader(getHdr, cookies)
  55. // now := time.Now()
  56. // fwtype=255&time_stamp=Thu%20Apr%2023%202020%2002%3A25%3A13%20GMT%2B0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&_=
  57. /*loginData = strings.Join([]string{
  58. "fwtype=255",
  59. "time_stamp=" + url.QueryEscape(timeutils.RFC2882Time(now)),
  60. "_=",
  61. }, "&")
  62. _, rspBody, err := r.RawRequest(ctx, httputils.POST, "/cgi/upgrade_process.cgi", getHdr, []byte(loginData))
  63. if err != nil {
  64. return "", errors.Wrapf(err, "r.RawPost %s", loginData)
  65. }
  66. if r.isDebug {
  67. log.Debugf("upgrade_process.cgi %s", rspBody)
  68. }
  69. */
  70. _, rspBody, err := r.RawRequest(ctx, httputils.GET, "/cgi/url_redirect.cgi?url_name=ikvm&url_type=jwsk", getHdr, nil)
  71. if err != nil {
  72. return "", errors.Wrap(err, "r.RawGet")
  73. }
  74. return string(rspBody), nil
  75. }