remotecommand.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 remotecommand
  15. import (
  16. "fmt"
  17. "io"
  18. "net/http"
  19. "net/url"
  20. "yunion.io/x/log"
  21. "yunion.io/x/onecloud/pkg/util/httpstream"
  22. "yunion.io/x/onecloud/pkg/util/pod/remotecommand/spdy"
  23. )
  24. // StreamOptions holds information pertaining to the current streaming session:
  25. // input/output streams, if the client is requesting a TTY, and a terminal size queue to
  26. // support terminal resizing.
  27. type StreamOptions struct {
  28. Stdin io.Reader
  29. Stdout io.Writer
  30. Stderr io.Writer
  31. Tty bool
  32. TerminalSizeQueue TerminalSizeQueue
  33. Header http.Header
  34. }
  35. // Executor is an interface for transporting shell-style streams.
  36. type Executor interface {
  37. // Stream initiates the transport of the standard shell streams. It will transport any
  38. // non-nil stream to a remote system, and return an error if a problem occurs.
  39. Stream(options StreamOptions) error
  40. }
  41. type streamCreator interface {
  42. CreateStream(headers http.Header) (httpstream.Stream, error)
  43. }
  44. type streamProtocolHandler interface {
  45. stream(conn streamCreator) error
  46. }
  47. // streamExecutor handles transporting standard shell streams over a httpstream connection.
  48. type streamExecutor struct {
  49. upgrader spdy.Upgrader
  50. transport http.RoundTripper
  51. method string
  52. url *url.URL
  53. protocols []string
  54. }
  55. // NewSPDYExecutor connects to the provided server and upgrades the connection to
  56. // multiplexed bidirectional streams.
  57. func NewSPDYExecutor(method string, url *url.URL) (Executor, error) {
  58. wrapper, upgradeRoundTripper, err := spdy.RoundTripperFor()
  59. if err != nil {
  60. return nil, err
  61. }
  62. return NewSPDYExecutorForTransports(wrapper, upgradeRoundTripper, method, url)
  63. }
  64. // NewSPDYExecutorForTransports connects to the provided server using the given transport,
  65. // upgrades the response using the given upgrader to multiplexed bidirectional streams.
  66. func NewSPDYExecutorForTransports(tranport http.RoundTripper, upgrader spdy.Upgrader, method string, url *url.URL) (Executor, error) {
  67. return NewSPDYExecutorForProtocols(
  68. tranport, upgrader, method, url,
  69. StreamProtocolV4Name,
  70. StreamProtocolV3Name,
  71. StreamProtocolV2Name,
  72. StreamProtocolV1Name)
  73. }
  74. // NewSPDYExecutorForProtocols connects to the provided server and upgrades the connection to
  75. // multiplexed bidirectional streams using only the provided protocols. Exposed for testing, most
  76. // callers should use NewSPDYExecutor or NewSPDYExecutorForTransports.
  77. func NewSPDYExecutorForProtocols(transport http.RoundTripper, upgrader spdy.Upgrader, method string, url *url.URL, protocols ...string) (Executor, error) {
  78. return &streamExecutor{
  79. upgrader: upgrader,
  80. transport: transport,
  81. method: method,
  82. url: url,
  83. protocols: protocols,
  84. }, nil
  85. }
  86. // Stream opens a protocol streamer to the server and streams until a client closes
  87. // the connection or the server disconnects.
  88. func (e *streamExecutor) Stream(options StreamOptions) error {
  89. req, err := http.NewRequest(e.method, e.url.String(), nil)
  90. if err != nil {
  91. return fmt.Errorf("error creating request: %v", err)
  92. }
  93. if options.Header != nil {
  94. req.Header = options.Header
  95. }
  96. conn, protocol, err := spdy.Negotiate(
  97. e.upgrader,
  98. &http.Client{Transport: e.transport},
  99. req,
  100. e.protocols...,
  101. )
  102. if err != nil {
  103. return err
  104. }
  105. defer conn.Close()
  106. var streamer streamProtocolHandler
  107. switch protocol {
  108. case StreamProtocolV4Name:
  109. streamer = newStreamProtocolV4(options)
  110. case StreamProtocolV3Name:
  111. streamer = newStreamProtocolV3(options)
  112. case StreamProtocolV2Name:
  113. streamer = newStreamProtocolV2(options)
  114. case "":
  115. log.Infof("The server did not negotiate a streaming protocol version. Falling back to %s", StreamProtocolV1Name)
  116. fallthrough
  117. case StreamProtocolV1Name:
  118. streamer = newStreamProtocolV1(options)
  119. }
  120. return streamer.stream(conn)
  121. }