add recorder for serial handler

This commit is contained in:
ginuerzh
2023-09-18 09:43:01 +08:00
parent f2ff1aa45a
commit a623232cc1
4 changed files with 77 additions and 10 deletions

44
handler/serial/conn.go Normal file
View File

@ -0,0 +1,44 @@
package serial
import (
"bytes"
"context"
"encoding/hex"
"net"
"time"
"github.com/go-gost/core/recorder"
)
type recorderConn struct {
net.Conn
recorder recorder.Recorder
}
func (c *recorderConn) Read(b []byte) (n int, err error) {
n, err = c.Conn.Read(b)
if n > 0 && c.recorder != nil {
var buf bytes.Buffer
buf.WriteByte('>')
buf.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
buf.WriteByte('\n')
buf.WriteString(hex.Dump(b[:n]))
c.recorder.Record(context.Background(), buf.Bytes())
}
return
}
func (c *recorderConn) Write(b []byte) (int, error) {
if c.recorder != nil {
var buf bytes.Buffer
buf.WriteByte('<')
buf.WriteString(time.Now().Format("2006-01-02 15:04:05.000"))
buf.WriteByte('\n')
buf.WriteString(hex.Dump(b))
c.recorder.Record(context.Background(), buf.Bytes())
}
return c.Conn.Write(b)
}