-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (124 loc) · 3.05 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/gorilla/websocket"
"github.com/open-kingfisher/king-utils/common/log"
"io"
"io/ioutil"
"net/http"
)
var (
ctx context.Context
cli *client.Client
DockerEndpoint = "unix:///var/run/docker.sock"
)
func main() {
dockerClient()
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
err, _ := w.Write([]byte("king ok"))
log.Error("Response write error: ", err)
})
http.HandleFunc("/terminal", terminal)
log.Info("Starting king debug ...")
log.Fatal(http.ListenAndServe(":9091", nil))
}
func dockerClient() {
ctx = context.Background()
newCli, err := client.NewClient(DockerEndpoint, "", nil, nil)
cli = newCli
if err != nil {
log.Infof("Failed to get docker client: ", err)
return
}
cli.NegotiateAPIVersion(ctx)
}
var upgrade = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func terminal(w http.ResponseWriter, r *http.Request) {
// http协议升级为websocket
conn, err := upgrade.Upgrade(w, r, nil)
if err != nil {
log.Error(err)
return
}
defer conn.Close()
// 通过/etc/DEBUG_CONTAINER_ID获取容器ID
containerId, err := ioutil.ReadFile("/etc/DEBUG_CONTAINER_ID")
if err != nil {
log.Infof("Failed to get debug container Id: ", err)
return
}
// 执行exec,获取到容器终端的连接
log.Infof("Container Id: %s", containerId)
hr, err := exec(string(containerId), "/bin")
if err != nil {
log.Error("Failed to exec container: ", err)
return
}
// 关闭I/O流
defer hr.Close()
// 退出进程
defer func() {
hr.Conn.Write([]byte("exit\r"))
}()
go func() {
wsWriterCopy(hr.Conn, conn)
}()
wsReaderCopy(conn, hr.Conn)
}
func exec(container string, workdir string) (hr types.HijackedResponse, err error) {
// TODO cmd 应为环境变量里面的ENTRY_POINT值
// 执行/bin/bash命令
ir, err := cli.ContainerExecCreate(ctx, container, types.ExecConfig{
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Cmd: []string{"/bin/bash"},
Tty: true,
})
if err != nil {
log.Error("Failed to exec /bin/bash command: ", err)
return
}
// 附加到上面创建的/bin/bash进程中
hr, err = cli.ContainerExecAttach(ctx, ir.ID, types.ExecStartCheck{Detach: false, Tty: true})
if err != nil {
log.Error("Failed to attach /bin/bash: ", err)
return
}
return
}
func wsWriterCopy(reader io.Reader, writer *websocket.Conn) {
buf := make([]byte, 8192)
for {
nr, err := reader.Read(buf)
if nr > 0 {
err := writer.WriteMessage(websocket.BinaryMessage, buf[0:nr])
if err != nil {
log.Error("Failed to write message: ", err)
return
}
}
if err != nil {
log.Error("Failed to read buffer: ", err)
return
}
}
}
func wsReaderCopy(reader *websocket.Conn, writer io.Writer) {
for {
messageType, p, err := reader.ReadMessage()
if err != nil {
log.Error("Failed to read message: ", err)
return
}
if messageType == websocket.TextMessage {
writer.Write(p)
}
}
}