diff --git a/route/handler.go b/route/handler.go index 93d42b5..3c69aaa 100644 --- a/route/handler.go +++ b/route/handler.go @@ -322,10 +322,9 @@ func (server *Server) handleLogs(c *gin.Context) { } if err := tty.Run(ctx); err != nil { - if err != webtty.ErrMasterClosed && err != webtty.ErrMasterClosed { + if err != webtty.ErrMasterClosed && err != webtty.ErrSlaveClosed { log.Errorf("failed to run webtty: %s", err) } - return } } diff --git a/route/slave_wrapper.go b/route/slave_wrapper.go index 3f71a3c..859bba2 100644 --- a/route/slave_wrapper.go +++ b/route/slave_wrapper.go @@ -1,6 +1,7 @@ package route import ( + "bytes" "io" "github.com/yudai/gotty/webtty" @@ -44,9 +45,27 @@ func newSlave(rc io.ReadCloser) webtty.Slave { if err != nil { return } - pw.Write(bs[:n]) - if bs[n-1] == 10 { - pw.Write([]byte{13}) + if len(bs) <= 1 { + continue + } + + if bs[n-2] == 13 { // \r\n + pw.Write(bs[:n]) + continue + } + + // only \n or a long log string containes \n + s, e := 0, 0 + for e < n { + x := bytes.IndexByte(bs[e:n], 10) + if x == -1 { + break + } + s = e + e += x + pw.Write(bs[s:e]) + pw.Write([]byte{13, 10}) + e++ // skip this \n } } }()