|
| 1 | +package route |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/gorilla/websocket" |
| 12 | + log "github.com/sirupsen/logrus" |
| 13 | + "github.com/wrfly/container-web-tty/audit" |
| 14 | + "github.com/wrfly/container-web-tty/types" |
| 15 | + "github.com/yudai/gotty/webtty" |
| 16 | +) |
| 17 | + |
| 18 | +func (server *Server) handleExec(c *gin.Context, counter *counter) { |
| 19 | + cInfo := server.containerCli.GetInfo(c.Request.Context(), c.Param("id")) |
| 20 | + server.generateHandleWS(c.Request.Context(), counter, cInfo). |
| 21 | + ServeHTTP(c.Writer, c.Request) |
| 22 | +} |
| 23 | + |
| 24 | +func (server *Server) generateHandleWS(ctx context.Context, counter *counter, container types.Container) http.HandlerFunc { |
| 25 | + return func(w http.ResponseWriter, r *http.Request) { |
| 26 | + if container.Shell == "" { |
| 27 | + log.Errorf("cannot find a valid shell in container [%s]", container.ID) |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + num := counter.add(1) |
| 32 | + closeReason := "unknown reason" |
| 33 | + |
| 34 | + defer func() { |
| 35 | + num := counter.done() |
| 36 | + if strings.Contains(closeReason, "error") { |
| 37 | + log.Errorf("Connection closed by %s: %s, connections: %d", |
| 38 | + closeReason, r.RemoteAddr, num) |
| 39 | + } |
| 40 | + log.Infof("Connection closed by %s: %s, connections: %d", |
| 41 | + closeReason, r.RemoteAddr, num) |
| 42 | + }() |
| 43 | + |
| 44 | + if int64(server.options.MaxConnection) != 0 { |
| 45 | + if num > server.options.MaxConnection { |
| 46 | + closeReason = "exceeding max number of connections" |
| 47 | + return |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + log.Infof("New client connected: %s, connections: %d", r.RemoteAddr, num) |
| 52 | + |
| 53 | + conn, err := server.upgrader.Upgrade(w, r, nil) |
| 54 | + if err != nil { |
| 55 | + closeReason = err.Error() |
| 56 | + return |
| 57 | + } |
| 58 | + defer conn.Close() |
| 59 | + |
| 60 | + cctx, timeoutCancel := context.WithCancel(ctx) |
| 61 | + defer timeoutCancel() |
| 62 | + |
| 63 | + err = server.processTTY(cctx, timeoutCancel, conn, container) |
| 64 | + switch err { |
| 65 | + case ctx.Err(): |
| 66 | + closeReason = "cancelation" |
| 67 | + case cctx.Err(): |
| 68 | + closeReason = "time out" |
| 69 | + case webtty.ErrSlaveClosed: |
| 70 | + closeReason = "backend closed" |
| 71 | + case webtty.ErrMasterClosed: |
| 72 | + closeReason = "tab closed" |
| 73 | + default: |
| 74 | + closeReason = fmt.Sprintf("an error: %s", err) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (server *Server) processTTY(ctx context.Context, timeoutCancel context.CancelFunc, |
| 80 | + conn *websocket.Conn, container types.Container) error { |
| 81 | + arguments, err := server.readInitMessage(conn) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + log.Debugf("exec container: %s, params: %s", container.ID, arguments) |
| 86 | + |
| 87 | + q, err := parseQuery(strings.TrimSpace(arguments)) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + container.Exec = types.ExecOptions{ |
| 92 | + Cmd: q.Get("cmd"), |
| 93 | + Env: q.Get("env"), |
| 94 | + User: q.Get("user"), |
| 95 | + Privileged: q.Get("p") != "", |
| 96 | + } |
| 97 | + |
| 98 | + containerTTY, err := server.containerCli.Exec(ctx, container) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("exec container error: %s", err) |
| 101 | + } |
| 102 | + defer containerTTY.Exit() |
| 103 | + |
| 104 | + // handle timeout |
| 105 | + tout := server.options.IdleTime |
| 106 | + if tout.Seconds() != 0 { |
| 107 | + go func() { |
| 108 | + timer := time.NewTimer(tout) |
| 109 | + activeChan := containerTTY.ActiveChan() |
| 110 | + for { |
| 111 | + select { |
| 112 | + case <-timer.C: |
| 113 | + timer.Stop() |
| 114 | + timeoutCancel() |
| 115 | + return |
| 116 | + case <-activeChan: |
| 117 | + // the connection is active, reset the timer |
| 118 | + timer.Reset(tout) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + }() |
| 123 | + } |
| 124 | + |
| 125 | + titleBuf, err := server.makeTitleBuff(container) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("failed to fill window title template: %s", err) |
| 128 | + } |
| 129 | + |
| 130 | + opts := []webtty.Option{ |
| 131 | + webtty.WithWindowTitle(titleBuf), |
| 132 | + webtty.WithPermitWrite(), |
| 133 | + // webtty.WithReconnect(10), // not work.... |
| 134 | + } |
| 135 | + |
| 136 | + wrapper := &wsWrapper{conn} |
| 137 | + masterTTY, err := types.NewMasterTTY(ctx, containerTTY, container.ID) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + server.mMux.Lock() |
| 142 | + if _, ok := server.masters[container.ID]; !ok { |
| 143 | + server.masters[container.ID] = masterTTY |
| 144 | + } |
| 145 | + server.mMux.Unlock() |
| 146 | + defer func() { |
| 147 | + server.mMux.Lock() |
| 148 | + delete(server.masters, container.ID) |
| 149 | + server.mMux.Unlock() |
| 150 | + }() |
| 151 | + |
| 152 | + if server.options.EnableAudit { |
| 153 | + go audit.LogTo(ctx, masterTTY.Fork(ctx, false), audit.LogOpts{ |
| 154 | + Dir: server.options.AuditLogDir, |
| 155 | + ContainerID: container.ID, |
| 156 | + ClientIP: conn.RemoteAddr().String(), |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + log.Infof("new web tty for container: %s", container.ID) |
| 161 | + tty, err := webtty.New(wrapper, masterTTY, opts...) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("failed to create webtty: %s", err) |
| 164 | + } |
| 165 | + |
| 166 | + return tty.Run(ctx) |
| 167 | +} |
0 commit comments