6810e50c2c
- 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.
67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package serial
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/hex"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/go-gost/core/recorder"
|
|
)
|
|
|
|
type recorderConn struct {
|
|
net.Conn
|
|
recorder recorder.RecorderObject
|
|
}
|
|
|
|
func (c *recorderConn) Read(b []byte) (n int, err error) {
|
|
n, err = c.Conn.Read(b)
|
|
|
|
if n > 0 && c.recorder.Recorder != nil {
|
|
var buf bytes.Buffer
|
|
if c.recorder.Options != nil && c.recorder.Options.Direction {
|
|
buf.WriteByte('>')
|
|
}
|
|
if c.recorder.Options != nil && c.recorder.Options.TimestampFormat != "" {
|
|
buf.WriteString(time.Now().Format(c.recorder.Options.TimestampFormat))
|
|
}
|
|
if buf.Len() > 0 {
|
|
buf.WriteByte('\n')
|
|
}
|
|
if c.recorder.Options != nil && c.recorder.Options.Hexdump {
|
|
buf.WriteString(hex.Dump(b[:n]))
|
|
} else {
|
|
buf.Write(b[:n])
|
|
}
|
|
c.recorder.Recorder.Record(context.Background(), buf.Bytes())
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (c *recorderConn) Write(b []byte) (n int, err error) {
|
|
n, err = c.Conn.Write(b)
|
|
|
|
if n > 0 && c.recorder.Recorder != nil {
|
|
var buf bytes.Buffer
|
|
if c.recorder.Options != nil && c.recorder.Options.Direction {
|
|
buf.WriteByte('<')
|
|
}
|
|
if c.recorder.Options != nil && c.recorder.Options.TimestampFormat != "" {
|
|
buf.WriteString(time.Now().Format(c.recorder.Options.TimestampFormat))
|
|
}
|
|
if buf.Len() > 0 {
|
|
buf.WriteByte('\n')
|
|
}
|
|
if c.recorder.Options != nil && c.recorder.Options.Hexdump {
|
|
buf.WriteString(hex.Dump(b[:n]))
|
|
} else {
|
|
buf.Write(b[:n])
|
|
}
|
|
c.recorder.Recorder.Record(context.Background(), buf.Bytes())
|
|
}
|
|
|
|
return
|
|
}
|