From 3e9fbfc86e2a63c52db96859452a0080481dd64f Mon Sep 17 00:00:00 2001 From: llaoj Date: Fri, 11 Nov 2022 17:06:22 +0800 Subject: [PATCH 1/7] operation log --- utils/log.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ webtty/webtty.go | 14 ++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 utils/log.go diff --git a/utils/log.go b/utils/log.go new file mode 100644 index 0000000..66ac4a3 --- /dev/null +++ b/utils/log.go @@ -0,0 +1,48 @@ +package utils + +func FormatOperationLog(line *[]byte) (log string) { + ascii := map[byte]string{ + 0: "", //NUL + 1: "SOH", + 2: "STX", + 3: "ETX", + 4: "EOT", + 5: "ENQ", + 6: "ACK", + 7: "BEL", + 8: "BS", + 9: "HT", + 10: "LF", + 11: "VT", + 12: "FF", + 13: "CR", + 14: "SO", + 15: "SI", + 16: "DLE", + 17: "DCI", + 18: "DC2", + 19: "DC3", + 20: "DC4", + 21: "NAK", + 22: "SYN", + 23: "TB", + 24: "CAN", + 25: "EM", + 26: "SUB", + 27: "ESC", + 28: "FS", + 29: "GS", + 30: "RS", + 31: "US", + 127: "DEL", + } + for _, word := range *line { + if value, ok := ascii[word]; ok { + log += value + continue + } + + log += string(word) + } + return +} diff --git a/webtty/webtty.go b/webtty/webtty.go index 90c8c31..8532b2b 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -4,6 +4,8 @@ import ( "context" "encoding/base64" "encoding/json" + "fmt" + "github.com/sorenisanerd/gotty/utils" "sync" "github.com/pkg/errors" @@ -88,13 +90,14 @@ func (wt *WebTTY) Run(ctx context.Context) error { go func() { errs <- func() error { buffer := make([]byte, wt.bufferSize) + lineBuffer := make([]byte, 1) for { n, err := wt.masterConn.Read(buffer) if err != nil { return ErrMasterClosed } - err = wt.handleMasterReadEvent(buffer[:n]) + err = wt.handleMasterReadEvent(buffer[:n], &lineBuffer) if err != nil { return err } @@ -163,7 +166,7 @@ func (wt *WebTTY) masterWrite(data []byte) error { return nil } -func (wt *WebTTY) handleMasterReadEvent(data []byte) error { +func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { if len(data) == 0 { return errors.New("unexpected zero length read from master") } @@ -184,6 +187,13 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte) error { return errors.Wrapf(err, "failed to decode received data") } + *line = append(*line, decodedBuffer[:n]...) + //fmt.Printf("master read: %v -> %v\n", decodedBuffer[:n], string(decodedBuffer[:n])) + if decodedBuffer[n-1] == 13 { + fmt.Printf("master read line: %v\n", utils.FormatOperationLog(line)) + *line = nil + } + _, err = wt.slave.Write(decodedBuffer[:n]) if err != nil { return errors.Wrapf(err, "failed to write received data to slave") From a7d7ea66294b30c3cdd8f32c5ab09655cd4a4877 Mon Sep 17 00:00:00 2001 From: llaoj Date: Sun, 13 Nov 2022 11:14:11 +0800 Subject: [PATCH 2/7] 1. introduct operation log feature in README.md 2. add args(json) in operation logs --- README.md | 30 ++++++++++++++++++++++++++++++ server/handlers.go | 1 + webtty/option.go | 8 ++++++++ webtty/webtty.go | 10 +++++++--- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 39404e8..1c7fbe0 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,36 @@ When you want to create a jailed environment for each client, you can use Docker $ gotty -w docker run -it --rm busybox ``` +## Operation log + +User's input in terminal can be found in logs. for example: + +if you run gotty like this: + +```shell +./gotty -w --permit-arguments ./test.sh +``` + +this is `test.sh`: + +```sh +# !/bin/bash + +echo "Welcome: $4" +kubectl -n $1 exec -it $2 -c $3 -- sh +``` + +visit `http://127.0.0.1:8080/?arg=without-istio&arg=sleep-7b6d569576-57sjq&arg=sleep&arg=21001713` and input your commands in shell, and you will see operation logs in stdout: + +``` +... +2022/11/13 10:48:12 [oplog] lsCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} +2022/11/13 10:48:14 [oplog] pwdCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} +... +``` + +Using the `[oplog]` flag, you can collect and store these logs persistently. All args are in the log, including the userID. + ## Development You can build a binary by simply running `make`. go1.16 is required. diff --git a/server/handlers.go b/server/handlers.go index 3393b22..2439e50 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -145,6 +145,7 @@ func (server *Server) processWSConn(ctx context.Context, conn *websocket.Conn, h opts := []webtty.Option{ webtty.WithWindowTitle(titleBuf.Bytes()), + webtty.WithArguments(params), } if server.options.PermitWrite { opts = append(opts, webtty.WithPermitWrite()) diff --git a/webtty/option.go b/webtty/option.go index 1618e89..5abc261 100644 --- a/webtty/option.go +++ b/webtty/option.go @@ -41,6 +41,14 @@ func WithWindowTitle(windowTitle []byte) Option { } } +// WithArguments sets the command line arguments that clients send +func WithArguments(arguments map[string][]string) Option { + return func(wt *WebTTY) error { + wt.arguments = arguments + return nil + } +} + // WithReconnect enables reconnection on the master side. func WithReconnect(timeInSeconds int) Option { return func(wt *WebTTY) error { diff --git a/webtty/webtty.go b/webtty/webtty.go index 8532b2b..9c940dc 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -4,8 +4,8 @@ import ( "context" "encoding/base64" "encoding/json" - "fmt" "github.com/sorenisanerd/gotty/utils" + "log" "sync" "github.com/pkg/errors" @@ -21,6 +21,7 @@ type WebTTY struct { slave Slave windowTitle []byte + arguments map[string][]string permitWrite bool columns int rows int @@ -188,9 +189,12 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { } *line = append(*line, decodedBuffer[:n]...) - //fmt.Printf("master read: %v -> %v\n", decodedBuffer[:n], string(decodedBuffer[:n])) if decodedBuffer[n-1] == 13 { - fmt.Printf("master read line: %v\n", utils.FormatOperationLog(line)) + argumentsByte, err := json.Marshal(wt.arguments) + if err != nil { + return errors.Wrapf(err, "failed to marshal arguments map") + } + log.Printf("[oplog] %s %s\n", utils.FormatOperationLog(line), string(argumentsByte)) *line = nil } From 73f0ca5a2f83bd2b1d7ff87456f59366b2d0f038 Mon Sep 17 00:00:00 2001 From: llaoj Date: Mon, 14 Nov 2022 08:53:57 +0800 Subject: [PATCH 3/7] writes log --- README.md | 10 +++++----- utils/log.go | 4 ++-- webtty/webtty.go | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1c7fbe0..0e0f869 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ When you want to create a jailed environment for each client, you can use Docker $ gotty -w docker run -it --rm busybox ``` -## Operation log +## Writes log User's input in terminal can be found in logs. for example: @@ -164,7 +164,7 @@ if you run gotty like this: this is `test.sh`: ```sh -# !/bin/bash +#!/bin/bash echo "Welcome: $4" kubectl -n $1 exec -it $2 -c $3 -- sh @@ -174,12 +174,12 @@ visit `http://127.0.0.1:8080/?arg=without-istio&arg=sleep-7b6d569576-57sjq&arg=s ``` ... -2022/11/13 10:48:12 [oplog] lsCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} -2022/11/13 10:48:14 [oplog] pwdCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} +2022/11/13 10:48:12 [wlog] lsCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} +2022/11/13 10:48:14 [wlog] pwdCR {"arg":["without-istio","sleep-7b6d569576-57sjq","sleep","21001713"]} ... ``` -Using the `[oplog]` flag, you can collect and store these logs persistently. All args are in the log, including the userID. +Using the `[wlog]` flag, you can collect and store these logs persistently. All args are in the log, including the userID. ## Development diff --git a/utils/log.go b/utils/log.go index 66ac4a3..8c57be2 100644 --- a/utils/log.go +++ b/utils/log.go @@ -1,8 +1,8 @@ package utils -func FormatOperationLog(line *[]byte) (log string) { +func FormatWriteLog(line *[]byte) (log string) { ascii := map[byte]string{ - 0: "", //NUL + 0: "NUL", 1: "SOH", 2: "STX", 3: "ETX", diff --git a/webtty/webtty.go b/webtty/webtty.go index 9c940dc..5f6193b 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -194,7 +194,8 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { if err != nil { return errors.Wrapf(err, "failed to marshal arguments map") } - log.Printf("[oplog] %s %s\n", utils.FormatOperationLog(line), string(argumentsByte)) + log.Printf("[wlog] %v\n", line) + log.Printf("[wlog] %s %s\n", utils.FormatWriteLog(line), string(argumentsByte)) *line = nil } From 1c4427af53c7e6e453f3e308585ecdacc3c300bb Mon Sep 17 00:00:00 2001 From: llaoj Date: Mon, 14 Nov 2022 09:32:24 +0800 Subject: [PATCH 4/7] writes log --- webtty/webtty.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webtty/webtty.go b/webtty/webtty.go index 5f6193b..0a6ba73 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -194,7 +194,7 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { if err != nil { return errors.Wrapf(err, "failed to marshal arguments map") } - log.Printf("[wlog] %v\n", line) + //log.Printf("[wlog] %v\n", line) log.Printf("[wlog] %s %s\n", utils.FormatWriteLog(line), string(argumentsByte)) *line = nil } From 70a264ed00c30eb42b1093c3109ee8f1f241556a Mon Sep 17 00:00:00 2001 From: llaoj Date: Mon, 14 Nov 2022 09:36:47 +0800 Subject: [PATCH 5/7] writes log --- utils/log.go | 2 +- webtty/webtty.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/log.go b/utils/log.go index 8c57be2..f981ad9 100644 --- a/utils/log.go +++ b/utils/log.go @@ -1,6 +1,6 @@ package utils -func FormatWriteLog(line *[]byte) (log string) { +func FormatWritesLog(line *[]byte) (log string) { ascii := map[byte]string{ 0: "NUL", 1: "SOH", diff --git a/webtty/webtty.go b/webtty/webtty.go index 0a6ba73..9a4e29a 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -195,7 +195,7 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { return errors.Wrapf(err, "failed to marshal arguments map") } //log.Printf("[wlog] %v\n", line) - log.Printf("[wlog] %s %s\n", utils.FormatWriteLog(line), string(argumentsByte)) + log.Printf("[wlog] %s %s\n", utils.FormatWritesLog(line), string(argumentsByte)) *line = nil } From 8a7fa9ecb9fcd7cbe1c7c2d9e0c96cf70e29cf7d Mon Sep 17 00:00:00 2001 From: llaoj Date: Mon, 14 Nov 2022 12:15:16 +0800 Subject: [PATCH 6/7] ascii to str --- utils/log.go | 65 +++++++++++++++++++++++++++++++++++++++++------- webtty/webtty.go | 14 +++++------ 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/utils/log.go b/utils/log.go index f981ad9..1491763 100644 --- a/utils/log.go +++ b/utils/log.go @@ -1,7 +1,35 @@ package utils -func FormatWritesLog(line *[]byte) (log string) { - ascii := map[byte]string{ +import "fmt" + +func FormatWritesLog(codes []byte, line *string) { + n := len(codes) + str := "" + exist := false + if n == 3 { + if str, exist = ASCIIGroupToStr(fmt.Sprintf("%X", codes)); exist { + *line += str + codes = nil + } + } + // sh line prefix + if n >= 6 { + if str, exist = ASCIIGroupToStr(fmt.Sprintf("%X", []byte{codes[0], codes[1], codes[n-1]})); exist { + *line += str + codes = nil + } + } + + if codes != nil { + str = ASCIIToStr(codes) + *line += str + } + + return +} + +func ASCIIToStr(codes []byte) string { + control := map[byte]string{ 0: "NUL", 1: "SOH", 2: "STX", @@ -36,13 +64,32 @@ func FormatWritesLog(line *[]byte) (log string) { 31: "US", 127: "DEL", } - for _, word := range *line { - if value, ok := ascii[word]; ok { - log += value - continue - } - log += string(word) + str := "" + for _, code := range codes { + if value, ok := control[code]; ok { + str += value + } else { + str += string(code) + } } - return + + return str +} + +func ASCIIGroupToStr(sum string) (string, bool) { + group := map[string]string{ + "1B5B41": "UP", + "1B5B42": "DOWN", + "1B5B43": "RIGHT", + "1B5B44": "LEFT", + // sh line prefix: codes[0]codes[1]codes[5] + // eg. "ESC[ 1;5 R" + "1B5B52": "", + } + if value, ok := group[sum]; ok { + return value, true + } + + return "", false } diff --git a/webtty/webtty.go b/webtty/webtty.go index 9a4e29a..56c7dcb 100644 --- a/webtty/webtty.go +++ b/webtty/webtty.go @@ -91,14 +91,14 @@ func (wt *WebTTY) Run(ctx context.Context) error { go func() { errs <- func() error { buffer := make([]byte, wt.bufferSize) - lineBuffer := make([]byte, 1) + line := "" for { n, err := wt.masterConn.Read(buffer) if err != nil { return ErrMasterClosed } - err = wt.handleMasterReadEvent(buffer[:n], &lineBuffer) + err = wt.handleMasterReadEvent(buffer[:n], &line) if err != nil { return err } @@ -167,7 +167,7 @@ func (wt *WebTTY) masterWrite(data []byte) error { return nil } -func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { +func (wt *WebTTY) handleMasterReadEvent(data []byte, line *string) error { if len(data) == 0 { return errors.New("unexpected zero length read from master") } @@ -188,15 +188,15 @@ func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error { return errors.Wrapf(err, "failed to decode received data") } - *line = append(*line, decodedBuffer[:n]...) + // log.Printf("[wlog] %v %X\n", decodedBuffer[:n], decodedBuffer[:n]) + utils.FormatWritesLog(decodedBuffer[:n], line) if decodedBuffer[n-1] == 13 { argumentsByte, err := json.Marshal(wt.arguments) if err != nil { return errors.Wrapf(err, "failed to marshal arguments map") } - //log.Printf("[wlog] %v\n", line) - log.Printf("[wlog] %s %s\n", utils.FormatWritesLog(line), string(argumentsByte)) - *line = nil + log.Printf("[wlog] %s %s\n", *line, string(argumentsByte)) + *line = "" } _, err = wt.slave.Write(decodedBuffer[:n]) From 8a407192cc0990b842c1b35cc5c6c28f04c2d2c7 Mon Sep 17 00:00:00 2001 From: llaoj Date: Tue, 15 Nov 2022 09:34:38 +0800 Subject: [PATCH 7/7] ascii to str --- utils/log.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/log.go b/utils/log.go index 1491763..675c4f3 100644 --- a/utils/log.go +++ b/utils/log.go @@ -12,7 +12,7 @@ func FormatWritesLog(codes []byte, line *string) { codes = nil } } - // sh line prefix + // sh control codes if n >= 6 { if str, exist = ASCIIGroupToStr(fmt.Sprintf("%X", []byte{codes[0], codes[1], codes[n-1]})); exist { *line += str @@ -62,6 +62,7 @@ func ASCIIToStr(codes []byte) string { 29: "GS", 30: "RS", 31: "US", + 32: "SPACE", 127: "DEL", } @@ -83,7 +84,7 @@ func ASCIIGroupToStr(sum string) (string, bool) { "1B5B42": "DOWN", "1B5B43": "RIGHT", "1B5B44": "LEFT", - // sh line prefix: codes[0]codes[1]codes[5] + // sh control codes: codes[0]codes[1]codes[5] // eg. "ESC[ 1;5 R" "1B5B52": "", }