ilo.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "bytes"
  17. "context"
  18. "fmt"
  19. "net/http"
  20. "strings"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/pkg/util/httputils"
  24. )
  25. func (r *SBMCConsole) GetIloConsoleJNLP(ctx context.Context) (string, error) {
  26. loginData := jsonutils.NewDict()
  27. loginData.Add(jsonutils.NewString("login"), "method")
  28. loginData.Add(jsonutils.NewString(r.username), "user_login")
  29. loginData.Add(jsonutils.NewString(r.password), "password")
  30. postHdr := http.Header{}
  31. postHdr.Set("Content-Type", "application/json")
  32. _, loginRespBytes, err := r.RawRequest(ctx, httputils.POST, "/json/login_session", postHdr, []byte(loginData.String()))
  33. if err != nil {
  34. return "", errors.Wrap(err, "r.FormPost Login")
  35. }
  36. loginRespJson, err := jsonutils.Parse(loginRespBytes)
  37. if err != nil {
  38. return "", errors.Wrap(err, "jsonutils.Parse loginRespBytes")
  39. }
  40. sessionKey, err := loginRespJson.GetString("session_key")
  41. if err != nil {
  42. return "", errors.Wrap(err, "Get session_key")
  43. }
  44. endpoint := fmt.Sprintf("https://%s/", r.host)
  45. cookies := make(map[string]string)
  46. cookies["sessionKey"] = sessionKey
  47. cookies["sessionLang"] = "en"
  48. cookies["sessionUrl"] = endpoint
  49. getHdr := http.Header{}
  50. setCookieHeader(getHdr, cookies)
  51. _, tempBytes, err := r.RawRequest(ctx, httputils.GET, "/html/jnlp_template.html", getHdr, nil)
  52. if err != nil {
  53. return "", errors.Wrap(err, "request template")
  54. }
  55. startToken := []byte("<![CDATA[\n")
  56. endToken := []byte("]]>")
  57. pos := bytes.Index(tempBytes, startToken)
  58. if pos < 0 {
  59. return "", errors.Wrapf(err, "invalid template content %s: no start token", tempBytes)
  60. }
  61. tempBytes = tempBytes[pos+len(startToken):]
  62. pos = bytes.Index(tempBytes, endToken)
  63. if pos < 0 {
  64. return "", errors.Wrapf(err, "invalid template content %s: no end token", tempBytes)
  65. }
  66. template := string(tempBytes[:pos])
  67. // replace variables
  68. template = strings.ReplaceAll(template, "<%= this.baseUrl %>", endpoint)
  69. template = strings.ReplaceAll(template, "<%= this.sessionKey %>", sessionKey)
  70. template = strings.ReplaceAll(template, "<%= this.langId %>", "en")
  71. return template, nil
  72. }