2023-02-26 18:04:05 +01:00
|
|
|
package strategies
|
2022-07-03 12:39:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"beelzebub/parser"
|
|
|
|
"beelzebub/tracer"
|
2022-07-03 17:15:38 +02:00
|
|
|
"fmt"
|
2022-07-03 12:39:27 +02:00
|
|
|
"net"
|
|
|
|
"time"
|
2023-06-01 00:15:21 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2022-07-03 12:39:27 +02:00
|
|
|
)
|
|
|
|
|
2023-06-01 00:15:21 +02:00
|
|
|
type TCPStrategy struct {
|
2022-07-03 12:39:27 +02:00
|
|
|
}
|
|
|
|
|
2023-06-01 00:15:21 +02:00
|
|
|
func (tcpStrategy *TCPStrategy) Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tr tracer.Tracer) error {
|
2022-07-03 17:15:38 +02:00
|
|
|
listen, err := net.Listen("tcp", beelzebubServiceConfiguration.Address)
|
2022-07-03 12:39:27 +02:00
|
|
|
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 {
|
2022-07-03 17:15:38 +02:00
|
|
|
go func() {
|
|
|
|
conn.SetDeadline(time.Now().Add(time.Duration(beelzebubServiceConfiguration.DeadlineTimeoutSeconds) * time.Second))
|
|
|
|
conn.Write([]byte(fmt.Sprintf("%s\n", beelzebubServiceConfiguration.Banner)))
|
|
|
|
|
|
|
|
buffer := make([]byte, 1024)
|
|
|
|
command := ""
|
|
|
|
|
|
|
|
if n, err := conn.Read(buffer); err == nil {
|
|
|
|
command = string(buffer[:n])
|
|
|
|
}
|
|
|
|
|
|
|
|
tr.TraceEvent(tracer.Event{
|
|
|
|
Msg: "New TCP attempt",
|
|
|
|
Protocol: tracer.TCP.String(),
|
|
|
|
Command: command,
|
|
|
|
Status: tracer.Stateless.String(),
|
|
|
|
RemoteAddr: conn.RemoteAddr().String(),
|
|
|
|
ID: uuid.New().String(),
|
|
|
|
Description: beelzebubServiceConfiguration.Description,
|
|
|
|
})
|
|
|
|
conn.Close()
|
|
|
|
}()
|
2022-07-03 12:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"port": beelzebubServiceConfiguration.Address,
|
|
|
|
"banner": beelzebubServiceConfiguration.Banner,
|
|
|
|
}).Infof("Init service %s", beelzebubServiceConfiguration.Protocol)
|
|
|
|
return nil
|
|
|
|
}
|