tty_server.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. "github.com/creack/pty"
  17. socketio "github.com/googollee/go-socket.io"
  18. "yunion.io/x/log"
  19. "yunion.io/x/onecloud/pkg/webconsole/session"
  20. )
  21. const (
  22. ON_CONNECTION = "connection"
  23. ON_DISCONNECTION = "disconnection"
  24. ON_ERROR = "error"
  25. DISCONNECT_EVENT = "disconnect"
  26. OUTPUT_EVENT = "output"
  27. INPUT_EVENT = "input"
  28. RESIZE_EVENT = "resize"
  29. COMMAND_QUERY = "command"
  30. ARGS_QUERY = "args"
  31. )
  32. type TTYServer struct {
  33. *socketio.Server
  34. }
  35. func NewTTYServer(s *session.SSession) (*TTYServer, error) {
  36. socketioServer, err := socketio.NewServer(nil)
  37. if err != nil {
  38. return nil, err
  39. }
  40. server := &TTYServer{
  41. Server: socketioServer,
  42. }
  43. server.initEventHandler(s)
  44. return server, nil
  45. }
  46. func (server *TTYServer) initEventHandler(s *session.SSession) {
  47. server.On(ON_CONNECTION, func(so socketio.Socket) error {
  48. log.Infof("[%q] On connection", so.Id())
  49. p, err := session.NewPty(s)
  50. if err != nil {
  51. log.Errorf("Create Pty error: %v", err)
  52. return err
  53. }
  54. initSocketHandler(so, p)
  55. return nil
  56. })
  57. }
  58. func initSocketHandler(so socketio.Socket, p *session.Pty) {
  59. // handle command output
  60. go func() {
  61. for !p.Exit {
  62. if p.IsInShellMode() {
  63. data, err := p.Read()
  64. if err != nil {
  65. log.Errorf("[%s] read data error: %v", so.Id(), err)
  66. cleanUp(so, p)
  67. } else {
  68. // log.Errorf("--p.Pty.output data: %q", data)
  69. so.Emit(OUTPUT_EVENT, string(data))
  70. go p.Session.GetRecorder().Write("", string(data))
  71. }
  72. continue
  73. }
  74. }
  75. }()
  76. // handle user input write
  77. so.On(INPUT_EVENT, func(data string) {
  78. if !p.IsInShellMode() {
  79. for _, d := range []byte(data) {
  80. p.Session.Scan(d, func(msg string) {
  81. if len(msg) > 0 {
  82. so.Emit(OUTPUT_EVENT, msg)
  83. }
  84. })
  85. }
  86. cmd := p.Session.GetCommand()
  87. if cmd != nil {
  88. pty, err := pty.Start(cmd)
  89. if err != nil {
  90. log.Errorf("failed to start cmd: %v, error: %v", cmd, err)
  91. so.Emit(OUTPUT_EVENT, err.Error()+"\r\n")
  92. return
  93. }
  94. p.Pty, p.Cmd = pty, cmd
  95. if p.OriginSize != nil {
  96. p.Resize(p.OriginSize)
  97. }
  98. }
  99. } else {
  100. p.Pty.Write([]byte(data))
  101. go p.Session.GetRecorder().Write(data, "")
  102. }
  103. })
  104. // handle resize
  105. so.On(RESIZE_EVENT, func(colRow []uint16) {
  106. if len(colRow) != 2 {
  107. log.Errorf("Invalid window size: %v", colRow)
  108. cleanUp(so, p)
  109. return
  110. }
  111. //size, err := pty.GetsizeFull(p.Pty)
  112. //if err != nil {
  113. //log.Errorf("Get pty window size error: %v", err)
  114. //return
  115. //}
  116. newSize := pty.Winsize{
  117. Cols: colRow[0],
  118. Rows: colRow[1],
  119. }
  120. p.Resize(&newSize)
  121. })
  122. // handle disconnection
  123. so.On(ON_DISCONNECTION, func(msg string) {
  124. log.Infof("[%s] closed: %s", so.Id(), msg)
  125. cleanUp(so, p)
  126. })
  127. // handle error
  128. so.On(ON_ERROR, func(err error) {
  129. log.Errorf("[%s] on error: %v", so.Id(), err)
  130. cleanUp(so, p)
  131. })
  132. }
  133. func cleanUp(so socketio.Socket, p *session.Pty) {
  134. defer func() {
  135. if err := recover(); err != nil {
  136. log.Errorf("recover error: %v", err)
  137. }
  138. }()
  139. so.Disconnect()
  140. p.Stop()
  141. p.Exit = true
  142. }