Skip to content

Commit 15a7641

Browse files
committed
fix: share & log
1 parent fb45851 commit 15a7641

File tree

9 files changed

+169
-140
lines changed

9 files changed

+169
-140
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ image:
4343
push-image:
4444
docker push $(IMAGE)
4545

46-
4746
.PHONY: push-develop
4847
push-develop:
4948
docker tag $(IMAGE) $(IMAGE):develop

container/docker/slave.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"time"
55

66
apiTypes "github.com/docker/docker/api/types"
7+
"github.com/sirupsen/logrus"
78
)
89

910
// execInjector implement webtty.Slave
@@ -35,13 +36,12 @@ func (enj *execInjector) Read(p []byte) (n int, err error) {
3536
}
3637

3738
func (enj *execInjector) Write(p []byte) (n int, err error) {
38-
// logrus.Debugf("input: %v\n", p)
39+
logrus.Debugf("input: %v\n", p)
3940
return enj.hResp.Conn.Write(p)
4041
}
4142

4243
func (enj *execInjector) Exit() error {
43-
enj.Write([]byte{3}) // ^C
44-
enj.Write([]byte{4}) // ^D
44+
enj.Write([]byte{3, 13, 4, 13}) // ^C, ^D, enter
4545
close(enj.activeChan)
4646
return enj.hResp.Conn.Close()
4747
}

route/exec.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ import (
1515
"github.com/yudai/gotty/webtty"
1616
)
1717

18+
func (server *Server) handleExecRedirect(c *gin.Context) {
19+
containerID := c.Param("cid")
20+
execID := server.setContainerID(containerID)
21+
c.Redirect(302, "/exec/"+execID)
22+
}
23+
1824
func (server *Server) handleExec(c *gin.Context, counter *counter) {
19-
execID := c.Param("id")
25+
execID := c.Param("eid")
2026
containerID, ok := server.getContainerID(execID)
2127
if !ok {
22-
log.Errorf("exec id %s not found", execID)
28+
c.String(http.StatusBadRequest, fmt.Sprintf("exec id %s not found", execID))
2329
return
2430
}
2531

@@ -98,7 +104,7 @@ func (server *Server) processTTY(ctx context.Context, execID string, timeoutCanc
98104
if err != nil {
99105
return err
100106
}
101-
log.Debugf("exec container: %s, params: %s", container.ID, arguments)
107+
log.Debugf("exec container: %s, params: %s", container.ID[:7], arguments)
102108

103109
q, err := parseQuery(strings.TrimSpace(arguments))
104110
if err != nil {
@@ -115,7 +121,12 @@ func (server *Server) processTTY(ctx context.Context, execID string, timeoutCanc
115121
if err != nil {
116122
return fmt.Errorf("exec container error: %s", err)
117123
}
118-
defer containerTTY.Exit()
124+
defer func() {
125+
log.Infof("container %s exit", container.ID[:7])
126+
if err := containerTTY.Exit(); err != nil {
127+
log.Warnf("exit container err: %s", err)
128+
}
129+
}()
119130

120131
// handle timeout
121132
tout := server.options.IdleTime
@@ -162,7 +173,9 @@ func (server *Server) processTTY(ctx context.Context, execID string, timeoutCanc
162173
defer func() {
163174
// if master dead, all slaves dead
164175
server.m.Lock()
176+
masterTTY.Close()
165177
delete(server.masters, execID)
178+
delete(server.execs, execID)
166179
server.m.Unlock()
167180
}()
168181

@@ -174,7 +187,7 @@ func (server *Server) processTTY(ctx context.Context, execID string, timeoutCanc
174187
})
175188
}
176189

177-
log.Infof("new web tty for container: %s", container.ID)
190+
log.Infof("new web tty for container: %s", container.ID[:7])
178191
wrapper := &wsWrapper{conn}
179192
tty, err := webtty.New(wrapper, masterTTY, opts...)
180193
if err != nil {

route/handler.go

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,15 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"net/http"
87
"net/url"
98

109
"github.com/gin-gonic/gin"
1110
"github.com/gorilla/websocket"
1211
log "github.com/sirupsen/logrus"
13-
"github.com/yudai/gotty/webtty"
1412

1513
"github.com/wrfly/container-web-tty/types"
16-
"github.com/wrfly/container-web-tty/util"
1714
)
1815

19-
func (server *Server) handleExecRedirect(c *gin.Context) {
20-
containerID := c.Param("id")
21-
execID := server.setContainerID(containerID)
22-
c.Redirect(302, "/exec/"+execID)
23-
}
24-
25-
func (server *Server) handleWSIndex(c *gin.Context) {
26-
execID := c.Param("id")
27-
containerID, ok := server.getContainerID(execID)
28-
if !ok {
29-
log.Errorf("exec id %s not found", execID)
30-
return
31-
}
32-
cInfo := server.containerCli.GetInfo(c.Request.Context(), containerID)
33-
titleVars := server.titleVariables(
34-
[]string{"server"},
35-
map[string]map[string]interface{}{
36-
"server": map[string]interface{}{
37-
"containerName": cInfo.Name,
38-
},
39-
},
40-
)
41-
42-
titleBuf := new(bytes.Buffer)
43-
err := titleTemplate.Execute(titleBuf, titleVars)
44-
if err != nil {
45-
c.Error(err)
46-
}
47-
48-
indexVars := map[string]interface{}{
49-
"title": titleBuf.String(),
50-
}
51-
52-
indexBuf := new(bytes.Buffer)
53-
err = indexTemplate.Execute(indexBuf, indexVars)
54-
if err != nil {
55-
c.Error(err)
56-
}
57-
58-
c.Writer.Write(indexBuf.Bytes())
59-
}
60-
6116
func (server *Server) handleAuthToken(c *gin.Context) {
6217
c.Header("Content-Type", "application/javascript")
6318
// @TODO hashing?
@@ -148,79 +103,6 @@ func (server *Server) handleRestartContainer(c *gin.Context) {
148103
server.handleContainerActions(c, "restart")
149104
}
150105

151-
func (server *Server) handleLogs(c *gin.Context) {
152-
ctx := c.Request.Context()
153-
154-
conn, err := server.upgrader.Upgrade(c.Writer, c.Request, nil)
155-
if err != nil {
156-
c.String(http.StatusInternalServerError, "server error: %s", err)
157-
return
158-
}
159-
defer conn.Close()
160-
161-
initArg, err := server.readInitMessage(conn)
162-
if err != nil {
163-
c.String(http.StatusBadRequest, "read init message error: %s", err)
164-
return
165-
}
166-
167-
q, err := parseQuery(initArg)
168-
if err != nil {
169-
c.String(http.StatusBadRequest, err.Error())
170-
return
171-
}
172-
follow := true
173-
if v := q.Get("follow"); v != "1" && v != "" {
174-
follow = false
175-
}
176-
tail := "10"
177-
if v := q.Get("tail"); v != "" {
178-
tail = v
179-
}
180-
opts := types.LogOptions{
181-
ID: c.Param("id"),
182-
Follow: follow,
183-
Tail: tail,
184-
}
185-
186-
container := server.containerCli.GetInfo(ctx, opts.ID)
187-
188-
log.Debugf("get logs of container: %s", container.ID)
189-
logsReadCloser, err := server.containerCli.Logs(ctx, opts)
190-
if err != nil {
191-
c.String(http.StatusInternalServerError, "get logs error: %s", err)
192-
return
193-
}
194-
defer logsReadCloser.Close()
195-
196-
titleBuf, err := server.makeTitleBuff(container)
197-
if err != nil {
198-
c.String(http.StatusInternalServerError, "failed to fill window title template: %s", err)
199-
return
200-
}
201-
202-
tty, err := webtty.New(
203-
&wsWrapper{conn},
204-
newSlave(util.NopRWCloser(logsReadCloser), false),
205-
[]webtty.Option{
206-
webtty.WithWindowTitle(titleBuf),
207-
webtty.WithPermitWrite(), // can type "enter"
208-
}...,
209-
)
210-
if err != nil {
211-
c.String(http.StatusInternalServerError, "failed to create webtty: %s", err)
212-
return
213-
}
214-
215-
if err := tty.Run(ctx); err != nil {
216-
if err != webtty.ErrMasterClosed && err != webtty.ErrSlaveClosed {
217-
log.Errorf("failed to run webtty: %s", err)
218-
}
219-
}
220-
}
221-
222-
func (server *Server) terminalPage(c *gin.Context) { server.handleWSIndex(c) }
223-
224106
func (server *Server) makeTitleBuff(c types.Container) ([]byte, error) {
225107
location := "127.0.0.1"
226108
if c.LocServer != "" {

route/log.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package route
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gin-gonic/gin"
7+
log "github.com/sirupsen/logrus"
8+
"github.com/yudai/gotty/webtty"
9+
10+
"github.com/wrfly/container-web-tty/types"
11+
"github.com/wrfly/container-web-tty/util"
12+
)
13+
14+
func (server *Server) handleLogs(c *gin.Context) {
15+
ctx := c.Request.Context()
16+
17+
conn, err := server.upgrader.Upgrade(c.Writer, c.Request, nil)
18+
if err != nil {
19+
c.String(http.StatusInternalServerError, "server error: %s", err)
20+
return
21+
}
22+
defer conn.Close()
23+
24+
initArg, err := server.readInitMessage(conn)
25+
if err != nil {
26+
c.String(http.StatusBadRequest, "read init message error: %s", err)
27+
return
28+
}
29+
30+
q, err := parseQuery(initArg)
31+
if err != nil {
32+
c.String(http.StatusBadRequest, err.Error())
33+
return
34+
}
35+
follow := true
36+
if v := q.Get("follow"); v != "1" && v != "" {
37+
follow = false
38+
}
39+
tail := "10"
40+
if v := q.Get("tail"); v != "" {
41+
tail = v
42+
}
43+
opts := types.LogOptions{
44+
ID: c.Param("cid"),
45+
Follow: follow,
46+
Tail: tail,
47+
}
48+
49+
container := server.containerCli.GetInfo(ctx, opts.ID)
50+
51+
log.Debugf("get logs of container: %s", container.ID)
52+
logsReadCloser, err := server.containerCli.Logs(ctx, opts)
53+
if err != nil {
54+
c.String(http.StatusInternalServerError, "get logs error: %s", err)
55+
return
56+
}
57+
defer logsReadCloser.Close()
58+
59+
titleBuf, err := server.makeTitleBuff(container)
60+
if err != nil {
61+
c.String(http.StatusInternalServerError, "failed to fill window title template: %s", err)
62+
return
63+
}
64+
65+
tty, err := webtty.New(
66+
&wsWrapper{conn},
67+
newSlave(util.NopRWCloser(logsReadCloser), false),
68+
[]webtty.Option{
69+
webtty.WithWindowTitle(titleBuf),
70+
webtty.WithPermitWrite(), // can type "enter"
71+
}...,
72+
)
73+
if err != nil {
74+
c.String(http.StatusInternalServerError, "failed to create webtty: %s", err)
75+
return
76+
}
77+
78+
if err := tty.Run(ctx); err != nil {
79+
if err != webtty.ErrMasterClosed && err != webtty.ErrSlaveClosed {
80+
log.Errorf("failed to run webtty: %s", err)
81+
}
82+
}
83+
}

route/route.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,19 @@ func (server *Server) Run(ctx context.Context, options ...RunOption) error {
140140

141141
// exec
142142
counter := newCounter(server.options.IdleTime)
143-
router.GET("/e/:id/", server.handleExecRedirect) // containerID
144-
router.GET("/exec/:id/", server.terminalPage) // execID
145-
router.GET("/exec/:id/"+"ws", func(c *gin.Context) { server.handleExec(c, counter) })
143+
router.GET("/e/:cid/", server.handleExecRedirect) // containerID
144+
router.GET("/exec/:eid/", server.handleWSIndex) // execID
145+
router.GET("/exec/:eid/"+"ws", func(c *gin.Context) { server.handleExec(c, counter) })
146146

147147
if server.options.EnableShare {
148148
// share screen
149-
router.GET("/share/:id/", server.terminalPage)
150-
router.GET("/share/:id/ws", func(c *gin.Context) { server.handleShare(c) })
149+
router.GET("/share/:eid/", server.handleWSIndex)
150+
router.GET("/share/:eid/ws", func(c *gin.Context) { server.handleShare(c) })
151151
}
152152

153153
// logs
154-
router.GET("/logs/:id/", server.terminalPage)
155-
router.GET("/logs/:id/"+"ws", func(c *gin.Context) { server.handleLogs(c) })
154+
router.GET("/logs/:cid/", server.handleWSIndex)
155+
router.GET("/logs/:cid/"+"ws", func(c *gin.Context) { server.handleLogs(c) })
156156

157157
ctl := server.options.Control
158158
if ctl.Enable {

route/share.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package route
22

33
import (
44
"fmt"
5+
"net/http"
56

67
"github.com/gin-gonic/gin"
78
"github.com/gorilla/websocket"
@@ -16,7 +17,7 @@ func (server *Server) handleShare(c *gin.Context) {
1617
masterTTY, ok := server.masters[execID]
1718
server.m.RUnlock()
1819
if !ok || masterTTY == nil {
19-
log.Error("share terminal error, master not found")
20+
c.String(http.StatusBadRequest, "share terminal error, master not found")
2021
return
2122
}
2223

@@ -70,7 +71,8 @@ func (server *Server) processShare(c *gin.Context, execID string, masterTTY *typ
7071
return
7172
}
7273

73-
if err := tty.Run(ctx); err != nil && err != webtty.ErrMasterClosed {
74+
err = tty.Run(ctx)
75+
if err != nil && err != webtty.ErrMasterClosed {
7476
e := fmt.Sprintf("failed to run webtty: %s", err)
7577
log.Error(e)
7678
}

0 commit comments

Comments
 (0)