rdp_server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 server
  15. import (
  16. "net/http"
  17. "time"
  18. "github.com/gorilla/websocket"
  19. "yunion.io/x/log"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/onecloud/pkg/webconsole/guac"
  22. "yunion.io/x/onecloud/pkg/webconsole/options"
  23. "yunion.io/x/onecloud/pkg/webconsole/session"
  24. )
  25. type RDPServer struct {
  26. Session *session.SSession
  27. Host string
  28. Port int
  29. Username string
  30. Password string
  31. ConnectionId string
  32. Width int
  33. Height int
  34. Dpi int
  35. }
  36. func NewRDPServer(s *session.SSession) (*RDPServer, error) {
  37. info := s.ISessionData.(*session.RemoteRDPConsoleInfo)
  38. server := &RDPServer{
  39. Session: s,
  40. Host: info.Host,
  41. Port: info.Port,
  42. Username: info.Username,
  43. Password: info.Password,
  44. ConnectionId: info.ConnectionId,
  45. Width: info.Width,
  46. Height: info.Height,
  47. Dpi: info.Dpi,
  48. }
  49. return server, nil
  50. }
  51. func (s *RDPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  52. var up = websocket.Upgrader{
  53. ReadBufferSize: 1024,
  54. WriteBufferSize: 1024,
  55. CheckOrigin: func(r *http.Request) bool {
  56. return true
  57. },
  58. }
  59. ws, err := up.Upgrade(w, r, http.Header{"Sec-Websocket-Protocol": []string{
  60. r.Header.Get("Sec-Websocket-Protocol"),
  61. }})
  62. if err != nil {
  63. log.Errorf("upgrade error: %v", err)
  64. return
  65. }
  66. defer ws.Close()
  67. tunnel, err := guac.NewGuacamoleTunnel(
  68. s.Host,
  69. s.Port,
  70. s.Username,
  71. s.Password,
  72. s.ConnectionId,
  73. s.Width,
  74. s.Height,
  75. s.Dpi,
  76. s.Session.GetClientSession().GetUserId(),
  77. )
  78. if err != nil {
  79. log.Errorf("NewGuacamoleTunnel error: %v", err)
  80. return
  81. }
  82. err = tunnel.Start()
  83. if err != nil {
  84. log.Errorf("Start error: %v", err)
  85. return
  86. }
  87. done := make(chan bool, 4)
  88. timer := time.NewTimer(time.Microsecond * 100)
  89. setDone := func() {
  90. done <- true
  91. }
  92. go func() {
  93. defer setDone()
  94. for {
  95. ins, err := tunnel.ReadOne()
  96. if err != nil {
  97. return
  98. }
  99. if options.Options.RdpSessionTimeoutMinutes > 0 && timer != nil {
  100. timer.Reset(time.Duration(options.Options.RdpSessionTimeoutMinutes) * time.Minute)
  101. }
  102. err = ws.WriteMessage(websocket.TextMessage, []byte(ins.String()))
  103. if err != nil {
  104. log.Errorf("Failed writing to guacd %s: %v", ins.String(), err)
  105. return
  106. }
  107. }
  108. }()
  109. go func() {
  110. defer setDone()
  111. defer tunnel.Stop()
  112. for {
  113. _, p, err := ws.ReadMessage()
  114. if err != nil {
  115. if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
  116. return
  117. }
  118. log.Errorf("read message error %v", err)
  119. return
  120. }
  121. if options.Options.RdpSessionTimeoutMinutes > 0 && timer != nil {
  122. timer.Reset(time.Duration(options.Options.RdpSessionTimeoutMinutes) * time.Minute)
  123. }
  124. _, err = tunnel.Write(p)
  125. if err != nil {
  126. log.Errorf("Failed writing to guacd: %v", err)
  127. return
  128. }
  129. }
  130. }()
  131. stop := make(chan bool)
  132. go func() {
  133. if options.Options.RdpSessionTimeoutMinutes > 0 {
  134. timer.Reset(time.Duration(options.Options.RdpSessionTimeoutMinutes) * time.Minute)
  135. }
  136. defer timer.Stop()
  137. defer setDone()
  138. for {
  139. select {
  140. case <-stop:
  141. return
  142. case <-timer.C:
  143. if options.Options.RdpSessionTimeoutMinutes > 0 {
  144. return
  145. }
  146. timer.Reset(time.Microsecond * 100)
  147. }
  148. }
  149. }()
  150. go func() {
  151. defer setDone()
  152. err = tunnel.Wait()
  153. if err != nil && errors.Cause(err) != guac.TunnerClose {
  154. log.Errorf("wait error: %v", err)
  155. }
  156. }()
  157. <-done
  158. stop <- true
  159. log.Infof("rdp %s@%s:%d complete", s.Username, s.Host, s.Port)
  160. }