Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,133 @@ |
| 0 |
+package client |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "crypto/tls" |
|
| 4 |
+ "fmt" |
|
| 5 |
+ "io" |
|
| 6 |
+ "net" |
|
| 7 |
+ "net/http" |
|
| 8 |
+ "net/http/httputil" |
|
| 9 |
+ "os" |
|
| 10 |
+ "runtime" |
|
| 11 |
+ "strings" |
|
| 12 |
+ |
|
| 13 |
+ "github.com/dotcloud/docker/api" |
|
| 14 |
+ "github.com/dotcloud/docker/dockerversion" |
|
| 15 |
+ "github.com/dotcloud/docker/pkg/term" |
|
| 16 |
+ "github.com/dotcloud/docker/utils" |
|
| 17 |
+) |
|
| 18 |
+ |
|
| 19 |
+func (cli *DockerCli) dial() (net.Conn, error) {
|
|
| 20 |
+ if cli.tlsConfig != nil && cli.proto != "unix" {
|
|
| 21 |
+ return tls.Dial(cli.proto, cli.addr, cli.tlsConfig) |
|
| 22 |
+ } |
|
| 23 |
+ return net.Dial(cli.proto, cli.addr) |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer) error {
|
|
| 27 |
+ defer func() {
|
|
| 28 |
+ if started != nil {
|
|
| 29 |
+ close(started) |
|
| 30 |
+ } |
|
| 31 |
+ }() |
|
| 32 |
+ |
|
| 33 |
+ req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.APIVERSION, path), nil)
|
|
| 34 |
+ if err != nil {
|
|
| 35 |
+ return err |
|
| 36 |
+ } |
|
| 37 |
+ req.Header.Set("User-Agent", "Docker-Client/"+dockerversion.VERSION)
|
|
| 38 |
+ req.Header.Set("Content-Type", "plain/text")
|
|
| 39 |
+ req.Host = cli.addr |
|
| 40 |
+ |
|
| 41 |
+ dial, err := cli.dial() |
|
| 42 |
+ if err != nil {
|
|
| 43 |
+ if strings.Contains(err.Error(), "connection refused") {
|
|
| 44 |
+ return fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker -d' running on this host?")
|
|
| 45 |
+ } |
|
| 46 |
+ return err |
|
| 47 |
+ } |
|
| 48 |
+ clientconn := httputil.NewClientConn(dial, nil) |
|
| 49 |
+ defer clientconn.Close() |
|
| 50 |
+ |
|
| 51 |
+ // Server hijacks the connection, error 'connection closed' expected |
|
| 52 |
+ clientconn.Do(req) |
|
| 53 |
+ |
|
| 54 |
+ rwc, br := clientconn.Hijack() |
|
| 55 |
+ defer rwc.Close() |
|
| 56 |
+ |
|
| 57 |
+ if started != nil {
|
|
| 58 |
+ started <- rwc |
|
| 59 |
+ } |
|
| 60 |
+ |
|
| 61 |
+ var receiveStdout chan error |
|
| 62 |
+ |
|
| 63 |
+ var oldState *term.State |
|
| 64 |
+ |
|
| 65 |
+ if in != nil && setRawTerminal && cli.isTerminal && os.Getenv("NORAW") == "" {
|
|
| 66 |
+ oldState, err = term.SetRawTerminal(cli.terminalFd) |
|
| 67 |
+ if err != nil {
|
|
| 68 |
+ return err |
|
| 69 |
+ } |
|
| 70 |
+ defer term.RestoreTerminal(cli.terminalFd, oldState) |
|
| 71 |
+ } |
|
| 72 |
+ |
|
| 73 |
+ if stdout != nil || stderr != nil {
|
|
| 74 |
+ receiveStdout = utils.Go(func() (err error) {
|
|
| 75 |
+ defer func() {
|
|
| 76 |
+ if in != nil {
|
|
| 77 |
+ if setRawTerminal && cli.isTerminal {
|
|
| 78 |
+ term.RestoreTerminal(cli.terminalFd, oldState) |
|
| 79 |
+ } |
|
| 80 |
+ // For some reason this Close call blocks on darwin.. |
|
| 81 |
+ // As the client exists right after, simply discard the close |
|
| 82 |
+ // until we find a better solution. |
|
| 83 |
+ if runtime.GOOS != "darwin" {
|
|
| 84 |
+ in.Close() |
|
| 85 |
+ } |
|
| 86 |
+ } |
|
| 87 |
+ }() |
|
| 88 |
+ |
|
| 89 |
+ // When TTY is ON, use regular copy |
|
| 90 |
+ if setRawTerminal {
|
|
| 91 |
+ _, err = io.Copy(stdout, br) |
|
| 92 |
+ } else {
|
|
| 93 |
+ _, err = utils.StdCopy(stdout, stderr, br) |
|
| 94 |
+ } |
|
| 95 |
+ utils.Debugf("[hijack] End of stdout")
|
|
| 96 |
+ return err |
|
| 97 |
+ }) |
|
| 98 |
+ } |
|
| 99 |
+ |
|
| 100 |
+ sendStdin := utils.Go(func() error {
|
|
| 101 |
+ if in != nil {
|
|
| 102 |
+ io.Copy(rwc, in) |
|
| 103 |
+ utils.Debugf("[hijack] End of stdin")
|
|
| 104 |
+ } |
|
| 105 |
+ if tcpc, ok := rwc.(*net.TCPConn); ok {
|
|
| 106 |
+ if err := tcpc.CloseWrite(); err != nil {
|
|
| 107 |
+ utils.Debugf("Couldn't send EOF: %s\n", err)
|
|
| 108 |
+ } |
|
| 109 |
+ } else if unixc, ok := rwc.(*net.UnixConn); ok {
|
|
| 110 |
+ if err := unixc.CloseWrite(); err != nil {
|
|
| 111 |
+ utils.Debugf("Couldn't send EOF: %s\n", err)
|
|
| 112 |
+ } |
|
| 113 |
+ } |
|
| 114 |
+ // Discard errors due to pipe interruption |
|
| 115 |
+ return nil |
|
| 116 |
+ }) |
|
| 117 |
+ |
|
| 118 |
+ if stdout != nil || stderr != nil {
|
|
| 119 |
+ if err := <-receiveStdout; err != nil {
|
|
| 120 |
+ utils.Debugf("Error receiveStdout: %s", err)
|
|
| 121 |
+ return err |
|
| 122 |
+ } |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ if !cli.isTerminal {
|
|
| 126 |
+ if err := <-sendStdin; err != nil {
|
|
| 127 |
+ utils.Debugf("Error sendStdin: %s", err)
|
|
| 128 |
+ return err |
|
| 129 |
+ } |
|
| 130 |
+ } |
|
| 131 |
+ return nil |
|
| 132 |
+} |
| ... | ... |
@@ -2,7 +2,6 @@ package client |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"bytes" |
| 5 |
- "crypto/tls" |
|
| 6 | 5 |
"encoding/base64" |
| 7 | 6 |
"encoding/json" |
| 8 | 7 |
"errors" |
| ... | ... |
@@ -11,12 +10,9 @@ import ( |
| 11 | 11 |
"io/ioutil" |
| 12 | 12 |
"net" |
| 13 | 13 |
"net/http" |
| 14 |
- "net/http/httputil" |
|
| 15 | 14 |
"net/url" |
| 16 | 15 |
"os" |
| 17 | 16 |
gosignal "os/signal" |
| 18 |
- "regexp" |
|
| 19 |
- goruntime "runtime" |
|
| 20 | 17 |
"strconv" |
| 21 | 18 |
"strings" |
| 22 | 19 |
"syscall" |
| ... | ... |
@@ -35,23 +31,14 @@ var ( |
| 35 | 35 |
|
| 36 | 36 |
func (cli *DockerCli) HTTPClient() *http.Client {
|
| 37 | 37 |
tr := &http.Transport{
|
| 38 |
+ TLSClientConfig: cli.tlsConfig, |
|
| 38 | 39 |
Dial: func(network, addr string) (net.Conn, error) {
|
| 39 | 40 |
return net.Dial(cli.proto, cli.addr) |
| 40 | 41 |
}, |
| 41 | 42 |
} |
| 42 |
- if cli.proto != "unix" {
|
|
| 43 |
- tr.TLSClientConfig = cli.tlsConfig |
|
| 44 |
- } |
|
| 45 | 43 |
return &http.Client{Transport: tr}
|
| 46 | 44 |
} |
| 47 | 45 |
|
| 48 |
-func (cli *DockerCli) dial() (net.Conn, error) {
|
|
| 49 |
- if cli.tlsConfig != nil && cli.proto != "unix" {
|
|
| 50 |
- return tls.Dial(cli.proto, cli.addr, cli.tlsConfig) |
|
| 51 |
- } |
|
| 52 |
- return net.Dial(cli.proto, cli.addr) |
|
| 53 |
-} |
|
| 54 |
- |
|
| 55 | 46 |
func (cli *DockerCli) call(method, path string, data interface{}, passAuthInfo bool) (io.ReadCloser, int, error) {
|
| 56 | 47 |
params := bytes.NewBuffer(nil) |
| 57 | 48 |
if data != nil {
|
| ... | ... |
@@ -69,9 +56,6 @@ func (cli *DockerCli) call(method, path string, data interface{}, passAuthInfo b
|
| 69 | 69 |
} |
| 70 | 70 |
} |
| 71 | 71 |
} |
| 72 |
- // fixme: refactor client to support redirect |
|
| 73 |
- re := regexp.MustCompile("/+")
|
|
| 74 |
- path = re.ReplaceAllString(path, "/") |
|
| 75 | 72 |
|
| 76 | 73 |
req, err := http.NewRequest(method, fmt.Sprintf("http://v%s%s", api.APIVERSION, path), params)
|
| 77 | 74 |
if err != nil {
|
| ... | ... |
@@ -134,10 +118,6 @@ func (cli *DockerCli) streamHelper(method, path string, setRawTerminal bool, in |
| 134 | 134 |
in = bytes.NewReader([]byte{})
|
| 135 | 135 |
} |
| 136 | 136 |
|
| 137 |
- // fixme: refactor client to support redirect |
|
| 138 |
- re := regexp.MustCompile("/+")
|
|
| 139 |
- path = re.ReplaceAllString(path, "/") |
|
| 140 |
- |
|
| 141 | 137 |
req, err := http.NewRequest(method, fmt.Sprintf("http://v%s%s", api.APIVERSION, path), in)
|
| 142 | 138 |
if err != nil {
|
| 143 | 139 |
return err |
| ... | ... |
@@ -189,118 +169,6 @@ func (cli *DockerCli) streamHelper(method, path string, setRawTerminal bool, in |
| 189 | 189 |
return nil |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 |
-func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer) error {
|
|
| 193 |
- defer func() {
|
|
| 194 |
- if started != nil {
|
|
| 195 |
- close(started) |
|
| 196 |
- } |
|
| 197 |
- }() |
|
| 198 |
- // fixme: refactor client to support redirect |
|
| 199 |
- re := regexp.MustCompile("/+")
|
|
| 200 |
- path = re.ReplaceAllString(path, "/") |
|
| 201 |
- |
|
| 202 |
- req, err := http.NewRequest(method, fmt.Sprintf("/v%s%s", api.APIVERSION, path), nil)
|
|
| 203 |
- if err != nil {
|
|
| 204 |
- return err |
|
| 205 |
- } |
|
| 206 |
- req.Header.Set("User-Agent", "Docker-Client/"+dockerversion.VERSION)
|
|
| 207 |
- req.Header.Set("Content-Type", "plain/text")
|
|
| 208 |
- req.Host = cli.addr |
|
| 209 |
- |
|
| 210 |
- dial, err := cli.dial() |
|
| 211 |
- if err != nil {
|
|
| 212 |
- if strings.Contains(err.Error(), "connection refused") {
|
|
| 213 |
- return fmt.Errorf("Cannot connect to the Docker daemon. Is 'docker -d' running on this host?")
|
|
| 214 |
- } |
|
| 215 |
- return err |
|
| 216 |
- } |
|
| 217 |
- clientconn := httputil.NewClientConn(dial, nil) |
|
| 218 |
- defer clientconn.Close() |
|
| 219 |
- |
|
| 220 |
- // Server hijacks the connection, error 'connection closed' expected |
|
| 221 |
- clientconn.Do(req) |
|
| 222 |
- |
|
| 223 |
- rwc, br := clientconn.Hijack() |
|
| 224 |
- defer rwc.Close() |
|
| 225 |
- |
|
| 226 |
- if started != nil {
|
|
| 227 |
- started <- rwc |
|
| 228 |
- } |
|
| 229 |
- |
|
| 230 |
- var receiveStdout chan error |
|
| 231 |
- |
|
| 232 |
- var oldState *term.State |
|
| 233 |
- |
|
| 234 |
- if in != nil && setRawTerminal && cli.isTerminal && os.Getenv("NORAW") == "" {
|
|
| 235 |
- oldState, err = term.SetRawTerminal(cli.terminalFd) |
|
| 236 |
- if err != nil {
|
|
| 237 |
- return err |
|
| 238 |
- } |
|
| 239 |
- defer term.RestoreTerminal(cli.terminalFd, oldState) |
|
| 240 |
- } |
|
| 241 |
- |
|
| 242 |
- if stdout != nil || stderr != nil {
|
|
| 243 |
- receiveStdout = utils.Go(func() (err error) {
|
|
| 244 |
- defer func() {
|
|
| 245 |
- if in != nil {
|
|
| 246 |
- if setRawTerminal && cli.isTerminal {
|
|
| 247 |
- term.RestoreTerminal(cli.terminalFd, oldState) |
|
| 248 |
- } |
|
| 249 |
- // For some reason this Close call blocks on darwin.. |
|
| 250 |
- // As the client exists right after, simply discard the close |
|
| 251 |
- // until we find a better solution. |
|
| 252 |
- if goruntime.GOOS != "darwin" {
|
|
| 253 |
- in.Close() |
|
| 254 |
- } |
|
| 255 |
- } |
|
| 256 |
- }() |
|
| 257 |
- |
|
| 258 |
- // When TTY is ON, use regular copy |
|
| 259 |
- if setRawTerminal {
|
|
| 260 |
- _, err = io.Copy(stdout, br) |
|
| 261 |
- } else {
|
|
| 262 |
- _, err = utils.StdCopy(stdout, stderr, br) |
|
| 263 |
- } |
|
| 264 |
- utils.Debugf("[hijack] End of stdout")
|
|
| 265 |
- return err |
|
| 266 |
- }) |
|
| 267 |
- } |
|
| 268 |
- |
|
| 269 |
- sendStdin := utils.Go(func() error {
|
|
| 270 |
- if in != nil {
|
|
| 271 |
- io.Copy(rwc, in) |
|
| 272 |
- utils.Debugf("[hijack] End of stdin")
|
|
| 273 |
- } |
|
| 274 |
- if tcpc, ok := rwc.(*net.TCPConn); ok {
|
|
| 275 |
- if err := tcpc.CloseWrite(); err != nil {
|
|
| 276 |
- utils.Debugf("Couldn't send EOF: %s\n", err)
|
|
| 277 |
- } |
|
| 278 |
- } else if unixc, ok := rwc.(*net.UnixConn); ok {
|
|
| 279 |
- if err := unixc.CloseWrite(); err != nil {
|
|
| 280 |
- utils.Debugf("Couldn't send EOF: %s\n", err)
|
|
| 281 |
- } |
|
| 282 |
- } |
|
| 283 |
- // Discard errors due to pipe interruption |
|
| 284 |
- return nil |
|
| 285 |
- }) |
|
| 286 |
- |
|
| 287 |
- if stdout != nil || stderr != nil {
|
|
| 288 |
- if err := <-receiveStdout; err != nil {
|
|
| 289 |
- utils.Debugf("Error receiveStdout: %s", err)
|
|
| 290 |
- return err |
|
| 291 |
- } |
|
| 292 |
- } |
|
| 293 |
- |
|
| 294 |
- if !cli.isTerminal {
|
|
| 295 |
- if err := <-sendStdin; err != nil {
|
|
| 296 |
- utils.Debugf("Error sendStdin: %s", err)
|
|
| 297 |
- return err |
|
| 298 |
- } |
|
| 299 |
- } |
|
| 300 |
- return nil |
|
| 301 |
- |
|
| 302 |
-} |
|
| 303 |
- |
|
| 304 | 192 |
func (cli *DockerCli) resizeTty(id string) {
|
| 305 | 193 |
height, width := cli.getTtySize() |
| 306 | 194 |
if height == 0 && width == 0 {
|