fix(handler/serial): record-after-write ordering, nil Router guards, consistent ReadTimeout

- conn.go: Write() now writes first then records b[:n] (not len(b)),
  matching the Read() pattern and preventing phantom data on failures.
- handler.go: add nil Router check before h.options.Router.Dial()
  in Handle's non-hop path to return a clear error instead of panicking.
- handler.go: guard h.options.Router against nil in forwardSerial;
  restructure if/else so ReadTimeout is applied in both the direct
  OpenPort path and when the Router has no chain configured.
This commit is contained in:
ginuerzh
2026-06-04 23:19:15 +08:00
parent e45328d1bb
commit 6810e50c2c
2 changed files with 19 additions and 8 deletions
+8 -5
View File
@@ -40,8 +40,10 @@ func (c *recorderConn) Read(b []byte) (n int, err error) {
return return
} }
func (c *recorderConn) Write(b []byte) (int, error) { func (c *recorderConn) Write(b []byte) (n int, err error) {
if c.recorder.Recorder != nil { n, err = c.Conn.Write(b)
if n > 0 && c.recorder.Recorder != nil {
var buf bytes.Buffer var buf bytes.Buffer
if c.recorder.Options != nil && c.recorder.Options.Direction { if c.recorder.Options != nil && c.recorder.Options.Direction {
buf.WriteByte('<') buf.WriteByte('<')
@@ -53,11 +55,12 @@ func (c *recorderConn) Write(b []byte) (int, error) {
buf.WriteByte('\n') buf.WriteByte('\n')
} }
if c.recorder.Options != nil && c.recorder.Options.Hexdump { if c.recorder.Options != nil && c.recorder.Options.Hexdump {
buf.WriteString(hex.Dump(b)) buf.WriteString(hex.Dump(b[:n]))
} else { } else {
buf.Write(b) buf.Write(b[:n])
} }
c.recorder.Recorder.Record(context.Background(), buf.Bytes()) c.recorder.Recorder.Record(context.Background(), buf.Bytes())
} }
return c.Conn.Write(b)
return
} }
+11 -3
View File
@@ -93,6 +93,11 @@ func (h *serialHandler) Handle(ctx context.Context, conn net.Conn, opts ...handl
return h.forwardSerial(ctx, conn, target, log) return h.forwardSerial(ctx, conn, target, log)
} }
if h.options.Router == nil {
err := errors.New("router not available")
log.Error(err)
return err
}
cc, err := h.options.Router.Dial(ctx, "tcp", "@") cc, err := h.options.Router.Dial(ctx, "tcp", "@")
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@@ -118,9 +123,12 @@ func (h *serialHandler) forwardSerial(ctx context.Context, conn net.Conn, target
cfg := serial.ParseConfigFromAddr(conn.LocalAddr().String()) cfg := serial.ParseConfigFromAddr(conn.LocalAddr().String())
cfg.Name = target.Addr cfg.Name = target.Addr
if opts := h.options.Router.Options(); opts != nil && opts.Chain != nil { if h.options.Router != nil {
port, err = h.options.Router.Dial(ctx, "serial", serial.AddrFromConfig(cfg)) if opts := h.options.Router.Options(); opts != nil && opts.Chain != nil {
} else { port, err = h.options.Router.Dial(ctx, "serial", serial.AddrFromConfig(cfg))
}
}
if port == nil && err == nil {
cfg.ReadTimeout = h.md.timeout cfg.ReadTimeout = h.md.timeout
port, err = serial.OpenPort(cfg) port, err = serial.OpenPort(cfg)
} }