Feat: Add FallbackCommand for HTTP Strategy, refactor packages strategies (#175)

Add FallbackCommand for HTTP Strategy, refactor packages strategies, improve histories implementations.
This commit is contained in:
Bryan Nolen
2025-03-13 12:36:46 +05:30
committed by GitHub
parent 933f02911b
commit b062416c00
10 changed files with 378 additions and 251 deletions

View File

@ -0,0 +1,63 @@
package TCP
import (
"fmt"
"net"
"time"
"github.com/mariocandela/beelzebub/v3/parser"
"github.com/mariocandela/beelzebub/v3/tracer"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
type TCPStrategy struct {
}
func (tcpStrategy *TCPStrategy) Init(servConf parser.BeelzebubServiceConfiguration, tr tracer.Tracer) error {
listen, err := net.Listen("tcp", servConf.Address)
if err != nil {
log.Errorf("Error during init TCP Protocol: %s", err.Error())
return err
}
go func() {
for {
if conn, err := listen.Accept(); err == nil {
go func() {
conn.SetDeadline(time.Now().Add(time.Duration(servConf.DeadlineTimeoutSeconds) * time.Second))
conn.Write(fmt.Appendf([]byte{}, "%s\n", servConf.Banner))
buffer := make([]byte, 1024)
command := ""
if n, err := conn.Read(buffer); err == nil {
command = string(buffer[:n])
}
host, port, _ := net.SplitHostPort(conn.RemoteAddr().String())
tr.TraceEvent(tracer.Event{
Msg: "New TCP attempt",
Protocol: tracer.TCP.String(),
Command: command,
Status: tracer.Stateless.String(),
RemoteAddr: conn.RemoteAddr().String(),
SourceIp: host,
SourcePort: port,
ID: uuid.New().String(),
Description: servConf.Description,
})
conn.Close()
}()
}
}
}()
log.WithFields(log.Fields{
"port": servConf.Address,
"banner": servConf.Banner,
}).Infof("Init service %s", servConf.Protocol)
return nil
}