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)
}

View File

@ -11,8 +11,10 @@ import (
"github.com/go-gost/core/handler"
"github.com/go-gost/core/logger"
md "github.com/go-gost/core/metadata"
"github.com/go-gost/core/recorder"
xnet "github.com/go-gost/x/internal/net"
serial_util "github.com/go-gost/x/internal/util/serial"
xrecorder "github.com/go-gost/x/recorder"
"github.com/go-gost/x/registry"
goserial "github.com/tarm/serial"
)
@ -22,10 +24,11 @@ func init() {
}
type serialHandler struct {
hop chain.Hop
router *chain.Router
md metadata
options handler.Options
hop chain.Hop
router *chain.Router
md metadata
options handler.Options
recorder recorder.Recorder
}
func NewHandler(opts ...handler.Option) handler.Handler {
@ -48,6 +51,14 @@ func (h *serialHandler) Init(md md.Metadata) (err error) {
if h.router == nil {
h.router = chain.NewRouter(chain.LoggerRouterOption(h.options.Logger))
}
if opts := h.router.Options(); opts != nil {
for _, ro := range opts.Recorders {
if ro.Record == xrecorder.RecorderServiceHandlerSerial {
h.recorder = ro.Recorder
break
}
}
}
return
}
@ -83,7 +94,14 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
"dst": target.Addr,
})
log.Debugf("%s >> %s", conn.RemoteAddr(), target.Addr)
log.Debugf("%s >> %s", conn.LocalAddr(), target.Addr)
if h.recorder != nil {
conn = &recorderConn{
Conn: conn,
recorder: h.recorder,
}
}
// serial port
if _, _, err := net.SplitHostPort(target.Addr); err != nil {
@ -104,11 +122,11 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
}
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
log.Infof("%s <-> %s", conn.LocalAddr(), target.Addr)
xnet.Transport(conn, cc)
log.WithFields(map[string]any{
"duration": time.Since(t),
}).Infof("%s >-< %s", conn.RemoteAddr(), target.Addr)
}).Infof("%s >-< %s", conn.LocalAddr(), target.Addr)
return nil
}
@ -132,11 +150,11 @@ func (h *serialHandler) forwardSerial(ctx context.Context, conn net.Conn, target
defer port.Close()
t := time.Now()
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
log.Infof("%s <-> %s", conn.LocalAddr(), target.Addr)
xnet.Transport(conn, port)
log.WithFields(map[string]any{
"duration": time.Since(t),
}).Infof("%s >-< %s", conn.RemoteAddr(), target.Addr)
}).Infof("%s >-< %s", conn.LocalAddr(), target.Addr)
return nil
}

View File

@ -19,7 +19,7 @@ func NewConn(port io.ReadWriteCloser, addr net.Addr, cancel context.CancelFunc)
return &conn{
port: port,
laddr: addr,
raddr: addr,
raddr: &Addr{Port: "@"},
cancel: cancel,
}
}

5
recorder/recorder.go Normal file
View File

@ -0,0 +1,5 @@
package recorder
const (
RecorderServiceHandlerSerial = "recorder.service.handler.serial"
)