lenovo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/pkg/errors"
  24. "yunion.io/x/pkg/util/httputils"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. )
  27. func (r *SBMCConsole) GetLenovoConsoleJNLP(ctx context.Context) (string, error) {
  28. loginData := strings.Join([]string{
  29. "user=" + url.QueryEscape(r.username),
  30. "password=" + url.QueryEscape(r.password),
  31. }, "&")
  32. // cookie:
  33. // _appwebSessionId_=09eb9a178d520d2c9fa1430dd355dc27; path=/; httponly; secure
  34. cookies := make(map[string]string)
  35. cookies["_appwebSessionId_"] = ""
  36. // first do html login
  37. postHdr := http.Header{}
  38. postHdr.Set("Content-Type", "application/x-www-form-urlencoded")
  39. postHdr.Set("Referer", fmt.Sprintf("https://%s/login.html", r.host))
  40. setCookieHeader(postHdr, cookies)
  41. hdr, _, err := r.RawRequest(ctx, httputils.POST, "/data/login", postHdr, []byte(loginData))
  42. if err != nil {
  43. return "", errors.Wrap(err, "r.FormPost Login")
  44. }
  45. for _, cookieHdr := range hdr["Set-Cookie"] {
  46. parts := strings.Split(cookieHdr, ";")
  47. if len(parts) > 0 {
  48. pparts := strings.Split(parts[0], "=")
  49. if len(pparts) > 1 {
  50. cookies[pparts[0]] = pparts[1]
  51. }
  52. }
  53. }
  54. getHdr := http.Header{}
  55. getHdr.Set("Referer", fmt.Sprintf("https://%s/bmctree.html", r.host))
  56. setCookieHeader(getHdr, cookies)
  57. _, launchResp, err := r.RawRequest(ctx, httputils.GET, "/vkvmLaunch.html", getHdr, nil)
  58. if err != nil {
  59. return "", errors.Wrap(err, "Get vkvmLauch.html")
  60. }
  61. var token string
  62. st3Pattern := regexp.MustCompile(`<input type="hidden" name="ST3" value="(\w+)">`)
  63. matched := st3Pattern.FindAllStringSubmatch(string(launchResp), -1)
  64. if len(matched) > 0 && len(matched[0]) > 1 {
  65. token = matched[0][1]
  66. }
  67. if len(token) == 0 {
  68. return "", errors.Wrap(httperrors.ErrBadRequest, "no valid ST3 token")
  69. }
  70. path := fmt.Sprintf("viewer.jnlp(%s@0@%d)", r.host, time.Now().UnixNano()/1000000)
  71. body := "ST3=" + url.QueryEscape(token)
  72. getHdr.Set("Referer", fmt.Sprintf("https://%s/vkvmLaunch.html", r.host))
  73. _, rspBody, err := r.RawRequest(ctx, httputils.POST, path, getHdr, []byte(body))
  74. if err != nil {
  75. return "", errors.Wrapf(err, "r.RawGet %s", path)
  76. }
  77. return string(rspBody), nil
  78. }