ssh_session.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. "net"
  18. "os/exec"
  19. "strconv"
  20. "time"
  21. "golang.org/x/crypto/ssh"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. "yunion.io/x/jsonutils"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/util/stringutils"
  26. compute_api "yunion.io/x/onecloud/pkg/apis/compute"
  27. api "yunion.io/x/onecloud/pkg/apis/webconsole"
  28. "yunion.io/x/onecloud/pkg/mcclient"
  29. "yunion.io/x/onecloud/pkg/webconsole/helper"
  30. o "yunion.io/x/onecloud/pkg/webconsole/options"
  31. "yunion.io/x/onecloud/pkg/webconsole/recorder"
  32. )
  33. type SSshSession struct {
  34. us *mcclient.ClientSession
  35. id string
  36. // vm name
  37. name string
  38. Host string
  39. Port int
  40. PrivateKey string
  41. Username string
  42. // 保持原有 Username ,不实用 cloudroot 的同时使用 PrivateKey
  43. KeepUsername bool
  44. Password string
  45. guestDetails *compute_api.ServerDetails
  46. hostDetails *compute_api.HostDetails
  47. }
  48. func NewSshSession(ctx context.Context, us *mcclient.ClientSession, conn SSshConnectionInfo) *SSshSession {
  49. ret := &SSshSession{
  50. us: us,
  51. id: stringutils.UUID4(),
  52. Port: conn.Port,
  53. Host: conn.IP,
  54. name: conn.Name,
  55. Username: conn.Username,
  56. KeepUsername: conn.KeepUsername,
  57. Password: conn.Password,
  58. guestDetails: conn.GuestDetails,
  59. hostDetails: conn.HostDetails,
  60. }
  61. if conn.Port <= 0 {
  62. ret.Port = 22
  63. }
  64. return ret
  65. }
  66. func (s *SSshSession) GetId() string {
  67. return s.id
  68. }
  69. func (s *SSshSession) Cleanup() error {
  70. return cloudprovider.ErrNotImplemented
  71. }
  72. func (s *SSshSession) GetClientSession() *mcclient.ClientSession {
  73. return s.us
  74. }
  75. func (s *SSshSession) GetProtocol() string {
  76. return api.WS
  77. }
  78. func (s *SSshSession) GetRecordObject() *recorder.Object {
  79. if len(s.name) == 0 {
  80. s.name = s.Username
  81. }
  82. return recorder.NewObject(s.id, s.name, "server", s.Username, jsonutils.Marshal(map[string]interface{}{"ip": s.Host, "port": s.Port}))
  83. }
  84. func (s *SSshSession) GetCommand() *exec.Cmd {
  85. return nil
  86. }
  87. func (s *SSshSession) GetSafeCommandString() string {
  88. return ""
  89. }
  90. func (s *SSshSession) IsNeedLogin() (bool, error) {
  91. if len(s.Username) > 0 && len(s.Password) > 0 {
  92. config := &ssh.ClientConfig{
  93. Timeout: time.Second,
  94. User: s.Username,
  95. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  96. Auth: []ssh.AuthMethod{
  97. ssh.Password(s.Password),
  98. },
  99. }
  100. addr := net.JoinHostPort(s.Host, strconv.Itoa(s.Port))
  101. client, err := ssh.Dial("tcp", addr, config)
  102. if err != nil {
  103. return true, err
  104. }
  105. defer client.Close()
  106. return false, nil
  107. }
  108. if !o.Options.EnableAutoLogin {
  109. return true, nil
  110. }
  111. if !s.KeepUsername {
  112. s.Username = "cloudroot"
  113. } else {
  114. if s.Username == "" {
  115. return true, errors.Error("username is empty")
  116. }
  117. }
  118. privateKey, err := helper.GetValidPrivateKey(s.Host, s.Port, s.Username, s.us.GetProjectId())
  119. if err != nil {
  120. return true, errors.Wrap(err, "try to use cloud admin private_key for ssh login")
  121. }
  122. s.PrivateKey = privateKey
  123. return false, nil
  124. }
  125. func (s *SSshSession) Scan(d byte, send func(msg string)) {
  126. }
  127. func (s *SSshSession) GetDisplayInfo(ctx context.Context) (*SDisplayInfo, error) {
  128. userInfo, err := fetchUserInfo(ctx, s.GetClientSession())
  129. if err != nil {
  130. return nil, errors.Wrap(err, "fetchUserInfo")
  131. }
  132. dispInfo := SDisplayInfo{}
  133. dispInfo.WaterMark = fetchWaterMark(userInfo)
  134. if s.guestDetails != nil {
  135. dispInfo.fetchGuestInfo(s.guestDetails)
  136. } else if s.hostDetails != nil {
  137. dispInfo.fetchHostInfo(s.hostDetails)
  138. } else {
  139. dispInfo.Ips = s.Host
  140. if len(s.name) > 0 {
  141. dispInfo.InstanceName = s.name
  142. }
  143. }
  144. return &dispInfo, nil
  145. }