forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hijack_conn.go
73 lines (58 loc) · 1.17 KB
/
hijack_conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package containerio
import (
"fmt"
"io"
"net"
"net/http"
)
func init() {
Register(func() Backend {
return &hijackConn{}
})
}
type hijackConn struct {
hijack http.Hijacker
conn net.Conn
closed bool
}
func createHijackConn() Backend {
return &hijackConn{}
}
func (h *hijackConn) Name() string {
return "hijack"
}
func (h *hijackConn) Init(opt *Option) error {
conn, _, err := opt.hijack.Hijack()
if err != nil {
return err
}
// set raw mode
conn.Write([]byte{})
if opt.hijackUpgrade {
fmt.Fprintf(conn, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else {
fmt.Fprintf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
}
h.hijack = opt.hijack
h.conn = conn
return nil
}
func (h *hijackConn) Out() io.Writer {
return h
}
func (h *hijackConn) In() io.Reader {
return h
}
func (h *hijackConn) Close() error {
if h.closed {
return nil
}
h.closed = true
return h.conn.Close()
}
func (h *hijackConn) Write(data []byte) (int, error) {
return h.conn.Write(data)
}
func (h *hijackConn) Read(p []byte) (int, error) {
return h.conn.Read(p)
}