add recorder for serial handler
This commit is contained in:
44
handler/serial/conn.go
Normal file
44
handler/serial/conn.go
Normal 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)
|
||||||
|
|
||||||
|
}
|
@ -11,8 +11,10 @@ import (
|
|||||||
"github.com/go-gost/core/handler"
|
"github.com/go-gost/core/handler"
|
||||||
"github.com/go-gost/core/logger"
|
"github.com/go-gost/core/logger"
|
||||||
md "github.com/go-gost/core/metadata"
|
md "github.com/go-gost/core/metadata"
|
||||||
|
"github.com/go-gost/core/recorder"
|
||||||
xnet "github.com/go-gost/x/internal/net"
|
xnet "github.com/go-gost/x/internal/net"
|
||||||
serial_util "github.com/go-gost/x/internal/util/serial"
|
serial_util "github.com/go-gost/x/internal/util/serial"
|
||||||
|
xrecorder "github.com/go-gost/x/recorder"
|
||||||
"github.com/go-gost/x/registry"
|
"github.com/go-gost/x/registry"
|
||||||
goserial "github.com/tarm/serial"
|
goserial "github.com/tarm/serial"
|
||||||
)
|
)
|
||||||
@ -26,6 +28,7 @@ type serialHandler struct {
|
|||||||
router *chain.Router
|
router *chain.Router
|
||||||
md metadata
|
md metadata
|
||||||
options handler.Options
|
options handler.Options
|
||||||
|
recorder recorder.Recorder
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(opts ...handler.Option) handler.Handler {
|
func NewHandler(opts ...handler.Option) handler.Handler {
|
||||||
@ -48,6 +51,14 @@ func (h *serialHandler) Init(md md.Metadata) (err error) {
|
|||||||
if h.router == nil {
|
if h.router == nil {
|
||||||
h.router = chain.NewRouter(chain.LoggerRouterOption(h.options.Logger))
|
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
|
return
|
||||||
}
|
}
|
||||||
@ -83,7 +94,14 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
|
|||||||
"dst": target.Addr,
|
"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
|
// serial port
|
||||||
if _, _, err := net.SplitHostPort(target.Addr); err != nil {
|
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()
|
t := time.Now()
|
||||||
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
log.Infof("%s <-> %s", conn.LocalAddr(), target.Addr)
|
||||||
xnet.Transport(conn, cc)
|
xnet.Transport(conn, cc)
|
||||||
log.WithFields(map[string]any{
|
log.WithFields(map[string]any{
|
||||||
"duration": time.Since(t),
|
"duration": time.Since(t),
|
||||||
}).Infof("%s >-< %s", conn.RemoteAddr(), target.Addr)
|
}).Infof("%s >-< %s", conn.LocalAddr(), target.Addr)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -132,11 +150,11 @@ func (h *serialHandler) forwardSerial(ctx context.Context, conn net.Conn, target
|
|||||||
defer port.Close()
|
defer port.Close()
|
||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
log.Infof("%s <-> %s", conn.RemoteAddr(), target.Addr)
|
log.Infof("%s <-> %s", conn.LocalAddr(), target.Addr)
|
||||||
xnet.Transport(conn, port)
|
xnet.Transport(conn, port)
|
||||||
log.WithFields(map[string]any{
|
log.WithFields(map[string]any{
|
||||||
"duration": time.Since(t),
|
"duration": time.Since(t),
|
||||||
}).Infof("%s >-< %s", conn.RemoteAddr(), target.Addr)
|
}).Infof("%s >-< %s", conn.LocalAddr(), target.Addr)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ func NewConn(port io.ReadWriteCloser, addr net.Addr, cancel context.CancelFunc)
|
|||||||
return &conn{
|
return &conn{
|
||||||
port: port,
|
port: port,
|
||||||
laddr: addr,
|
laddr: addr,
|
||||||
raddr: addr,
|
raddr: &Addr{Port: "@"},
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
recorder/recorder.go
Normal file
5
recorder/recorder.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package recorder
|
||||||
|
|
||||||
|
const (
|
||||||
|
RecorderServiceHandlerSerial = "recorder.service.handler.serial"
|
||||||
|
)
|
Reference in New Issue
Block a user