Files
deceptifeed/internal/logmonitor/logmonitor.go
Ryan Smith c3ca87c7af Add logmonitor package for monitoring writes to log files
The Monitor type is an io.Writer that sends data to a channel. It is meant for use with the honeypot logging system. This will allow the threat feed to provide real-time monitoring of the logging system while simultaneously logging the data to disk.
2025-03-29 12:18:05 -07:00

35 lines
1.0 KiB
Go

package logmonitor
// Monitor is an io.Writer that sends bytes written to its Write method to an
// underlying byte channel. This allows other packages to receive the data from
// the channel. Writes are non-blocking. If there is no receiver, the data is
// silently discarded.
//
// Monitor does not implement io.Closer. Once initialized, it is meant to run
// for the duration of the program. If needed, manually close `Channel` when
// finished.
type Monitor struct {
Channel chan []byte
}
// New creates a new Monitor ready for I/O operations. The underlying `Channel`
// should have a receiver to capture and process the data.
func New() *Monitor {
channel := make(chan []byte, 2)
return &Monitor{
Channel: channel,
}
}
// Write sends the bytes from p to the underlying Monitor's channel. If there
// is no receiver for the channel, the data is silently discarded. Write always
// returns n = len(p) and err = nil.
func (m *Monitor) Write(p []byte) (n int, err error) {
select {
case m.Channel <- p:
return len(p), nil
default:
return len(p), nil
}
}