tcp.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package rcli
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "net"
  11. )
  12. // Note: the globals are here to avoid import cycle
  13. // FIXME: Handle debug levels mode?
  14. var DEBUG_FLAG bool = false
  15. var CLIENT_SOCKET io.Writer = nil
  16. type DockerTCPConn struct {
  17. conn *net.TCPConn
  18. options *DockerConnOptions
  19. optionsBuf *[]byte
  20. handshaked bool
  21. client bool
  22. }
  23. func NewDockerTCPConn(conn *net.TCPConn, client bool) *DockerTCPConn {
  24. return &DockerTCPConn{
  25. conn: conn,
  26. options: &DockerConnOptions{},
  27. client: client,
  28. }
  29. }
  30. func (c *DockerTCPConn) SetOptionRawTerminal() {
  31. c.options.RawTerminal = true
  32. }
  33. func (c *DockerTCPConn) GetOptions() *DockerConnOptions {
  34. if c.client && !c.handshaked {
  35. // Attempt to parse options encoded as a JSON dict and store
  36. // the reminder of what we read from the socket in a buffer.
  37. //
  38. // bufio (and its ReadBytes method) would have been nice here,
  39. // but if json.Unmarshal() fails (which will happen if we speak
  40. // to a version of docker that doesn't send any option), then
  41. // we can't put the data back in it for the next Read().
  42. c.handshaked = true
  43. buf := make([]byte, 4096)
  44. if n, _ := c.conn.Read(buf); n > 0 {
  45. buf = buf[:n]
  46. if nl := bytes.IndexByte(buf, '\n'); nl != -1 {
  47. if err := json.Unmarshal(buf[:nl], c.options); err == nil {
  48. buf = buf[nl+1:]
  49. }
  50. }
  51. c.optionsBuf = &buf
  52. }
  53. }
  54. return c.options
  55. }
  56. func (c *DockerTCPConn) Read(b []byte) (int, error) {
  57. if c.optionsBuf != nil {
  58. // Consume what we buffered in GetOptions() first:
  59. optionsBuf := *c.optionsBuf
  60. optionsBuflen := len(optionsBuf)
  61. copied := copy(b, optionsBuf)
  62. if copied < optionsBuflen {
  63. optionsBuf = optionsBuf[copied:]
  64. c.optionsBuf = &optionsBuf
  65. return copied, nil
  66. }
  67. c.optionsBuf = nil
  68. return copied, nil
  69. }
  70. return c.conn.Read(b)
  71. }
  72. func (c *DockerTCPConn) Write(b []byte) (int, error) {
  73. optionsLen := 0
  74. if !c.client && !c.handshaked {
  75. c.handshaked = true
  76. options, _ := json.Marshal(c.options)
  77. options = append(options, '\n')
  78. if optionsLen, err := c.conn.Write(options); err != nil {
  79. return optionsLen, err
  80. }
  81. }
  82. n, err := c.conn.Write(b)
  83. return n + optionsLen, err
  84. }
  85. func (c *DockerTCPConn) Flush() error {
  86. _, err := c.Write([]byte{})
  87. return err
  88. }
  89. func (c *DockerTCPConn) Close() error { return c.conn.Close() }
  90. func (c *DockerTCPConn) CloseWrite() error { return c.conn.CloseWrite() }
  91. func (c *DockerTCPConn) CloseRead() error { return c.conn.CloseRead() }
  92. // Connect to a remote endpoint using protocol `proto` and address `addr`,
  93. // issue a single call, and return the result.
  94. // `proto` may be "tcp", "unix", etc. See the `net` package for available protocols.
  95. func Call(proto, addr string, args ...string) (DockerConn, error) {
  96. cmd, err := json.Marshal(args)
  97. if err != nil {
  98. return nil, err
  99. }
  100. conn, err := dialDocker(proto, addr)
  101. if err != nil {
  102. return nil, err
  103. }
  104. if _, err := fmt.Fprintln(conn, string(cmd)); err != nil {
  105. return nil, err
  106. }
  107. return conn, nil
  108. }
  109. // Listen on `addr`, using protocol `proto`, for incoming rcli calls,
  110. // and pass them to `service`.
  111. func ListenAndServe(proto, addr string, service Service) error {
  112. listener, err := net.Listen(proto, addr)
  113. if err != nil {
  114. return err
  115. }
  116. log.Printf("Listening for RCLI/%s on %s\n", proto, addr)
  117. defer listener.Close()
  118. for {
  119. if conn, err := listener.Accept(); err != nil {
  120. return err
  121. } else {
  122. conn, err := newDockerServerConn(conn)
  123. if err != nil {
  124. return err
  125. }
  126. go func(conn DockerConn) {
  127. defer conn.Close()
  128. if DEBUG_FLAG {
  129. CLIENT_SOCKET = conn
  130. }
  131. if err := Serve(conn, service); err != nil {
  132. log.Println("Error:", err.Error())
  133. fmt.Fprintln(conn, "Error:", err.Error())
  134. }
  135. }(conn)
  136. }
  137. }
  138. return nil
  139. }
  140. // Parse an rcli call on a new connection, and pass it to `service` if it
  141. // is valid.
  142. func Serve(conn DockerConn, service Service) error {
  143. r := bufio.NewReader(conn)
  144. var args []string
  145. if line, err := r.ReadString('\n'); err != nil {
  146. return err
  147. } else if err := json.Unmarshal([]byte(line), &args); err != nil {
  148. return err
  149. } else {
  150. return call(service, ioutil.NopCloser(r), conn, args...)
  151. }
  152. return nil
  153. }