mirror of
https://github.com/sorenisanerd/gotty.git
synced 2024-11-23 04:44:26 +00:00
operation log
This commit is contained in:
parent
16c534e1a9
commit
3e9fbfc86e
48
utils/log.go
Normal file
48
utils/log.go
Normal file
@ -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
|
||||||
|
}
|
@ -4,6 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"github.com/sorenisanerd/gotty/utils"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -88,13 +90,14 @@ func (wt *WebTTY) Run(ctx context.Context) error {
|
|||||||
go func() {
|
go func() {
|
||||||
errs <- func() error {
|
errs <- func() error {
|
||||||
buffer := make([]byte, wt.bufferSize)
|
buffer := make([]byte, wt.bufferSize)
|
||||||
|
lineBuffer := make([]byte, 1)
|
||||||
for {
|
for {
|
||||||
n, err := wt.masterConn.Read(buffer)
|
n, err := wt.masterConn.Read(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrMasterClosed
|
return ErrMasterClosed
|
||||||
}
|
}
|
||||||
|
|
||||||
err = wt.handleMasterReadEvent(buffer[:n])
|
err = wt.handleMasterReadEvent(buffer[:n], &lineBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -163,7 +166,7 @@ func (wt *WebTTY) masterWrite(data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wt *WebTTY) handleMasterReadEvent(data []byte) error {
|
func (wt *WebTTY) handleMasterReadEvent(data []byte, line *[]byte) error {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return errors.New("unexpected zero length read from master")
|
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")
|
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])
|
_, err = wt.slave.Write(decodedBuffer[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to write received data to slave")
|
return errors.Wrapf(err, "failed to write received data to slave")
|
||||||
|
Loading…
Reference in New Issue
Block a user