package probeagent import ( "os" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire" ) // appendLine appends one validated line to path and syncs it. func appendLine(path, line string) error { f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o640) if err != nil { return err } if _, err := f.WriteString(line); err != nil { f.Close() return err } if err := f.Sync(); err != nil { f.Close() return err } return f.Close() } // applyCmds executes the commands above lastCmd, in order, and persists // lastCmd after each one. An invalid or unroutable command is counted and // skipped; it never stops the queue. A write failure stops (the same command // is delivered again later). func (p *probe) applyCmds(cmds []wire.Cmd) error { for _, c := range cmds { if c.Seq <= p.st.lastCmd() { continue } var path string switch c.Kind { case wire.CmdDecision: path = p.cfg.DecisionsLog case wire.CmdUnban: path = p.cfg.UnbanLog } if path == "" || !wire.ValidCmdLine(c.Kind, c.Line) { p.cmdErrors.Add(1) p.log.Warn("command ignored", "seq", c.Seq, "kind", c.Kind) } else if err := appendLine(path, c.Line); err != nil { p.cmdErrors.Add(1) p.log.Error("command write failed", "seq", c.Seq, "kind", c.Kind) return err } if err := p.st.setLastCmd(c.Seq); err != nil { return err } } return nil }