remote_console_rdp.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 session
  15. import (
  16. "context"
  17. "os/exec"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/gotypes"
  21. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  22. api "yunion.io/x/onecloud/pkg/apis/webconsole"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. "yunion.io/x/onecloud/pkg/webconsole/recorder"
  27. )
  28. type RemoteRDPConsoleInfo struct {
  29. Host string
  30. Port int
  31. Username string
  32. Password string
  33. ConnectionId string
  34. guestDetails *compute_api.ServerDetails
  35. Width int
  36. Height int
  37. Dpi int
  38. s *mcclient.ClientSession
  39. }
  40. func NewRemoteRDPConsoleInfoByCloud(ctx context.Context, s *mcclient.ClientSession, serverId string, query jsonutils.JSONObject) (*RemoteRDPConsoleInfo, error) {
  41. info := &RemoteRDPConsoleInfo{s: s}
  42. if !gotypes.IsNil(query) {
  43. err := query.Unmarshal(&info)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "Unmarshal")
  46. }
  47. }
  48. if info.Port <= 0 {
  49. info.Port = 3389
  50. }
  51. var err error
  52. info.Host, info.Port, info.guestDetails, err = resolveServerIPPortById(ctx, s, serverId, info.Host, info.Port)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "resolveServerIPPortById")
  55. }
  56. if len(info.Host) == 0 {
  57. return nil, httperrors.NewMissingParameterError("host")
  58. }
  59. if (len(info.Password) == 0 || len(info.Username) == 0) && len(info.ConnectionId) == 0 {
  60. ret, err := modules.Servers.PerformAction(s, serverId, "login-info", jsonutils.NewDict())
  61. if err != nil {
  62. return nil, err
  63. }
  64. info.Password, _ = ret.GetString("password")
  65. info.Username, _ = ret.GetString("username")
  66. }
  67. return info, nil
  68. }
  69. func (info *RemoteRDPConsoleInfo) GetProtocol() string {
  70. return api.RDP
  71. }
  72. func (info *RemoteRDPConsoleInfo) GetCommand() *exec.Cmd {
  73. return nil
  74. }
  75. func (info *RemoteRDPConsoleInfo) GetSafeCommandString() string {
  76. return ""
  77. }
  78. func (info *RemoteRDPConsoleInfo) Cleanup() error {
  79. return nil
  80. }
  81. func (info *RemoteRDPConsoleInfo) Connect() error {
  82. return nil
  83. }
  84. func (info *RemoteRDPConsoleInfo) Scan(byte, func(string)) {
  85. return
  86. }
  87. func (info *RemoteRDPConsoleInfo) IsNeedLogin() (bool, error) {
  88. if len(info.Username) == 0 || len(info.Password) == 0 {
  89. return true, nil
  90. }
  91. return false, nil
  92. }
  93. func (info *RemoteRDPConsoleInfo) GetClientSession() *mcclient.ClientSession {
  94. return info.s
  95. }
  96. func (info *RemoteRDPConsoleInfo) GetConnectParams() (string, error) {
  97. return "", nil
  98. }
  99. func (info *RemoteRDPConsoleInfo) GetPassword() string {
  100. return info.Password
  101. }
  102. func (info *RemoteRDPConsoleInfo) GetId() string {
  103. return ""
  104. }
  105. func (info *RemoteRDPConsoleInfo) GetRecordObject() *recorder.Object {
  106. return nil
  107. }
  108. func (info *RemoteRDPConsoleInfo) GetDisplayInfo(ctx context.Context) (*SDisplayInfo, error) {
  109. userInfo, err := fetchUserInfo(ctx, info.GetClientSession())
  110. if err != nil {
  111. return nil, errors.Wrap(err, "fetchUserInfo")
  112. }
  113. dispInfo := SDisplayInfo{}
  114. dispInfo.WaterMark = fetchWaterMark(userInfo)
  115. dispInfo.fetchGuestInfo(info.guestDetails)
  116. return &dispInfo, nil
  117. }