refactoring: Tiny refactoring (#47)

* tiny refactoring name convention, and file name.

* added command line arguments files

* fix typo comment
This commit is contained in:
Mario Candela
2023-06-01 00:15:21 +02:00
committed by GitHub
parent 7d46c9544c
commit a7c69c8849
8 changed files with 38 additions and 27 deletions

View File

@ -0,0 +1,58 @@
package strategies
import (
"beelzebub/parser"
"beelzebub/tracer"
"fmt"
"net"
"time"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
type TCPStrategy struct {
}
func (tcpStrategy *TCPStrategy) Init(beelzebubServiceConfiguration parser.BeelzebubServiceConfiguration, tr tracer.Tracer) error {
listen, err := net.Listen("tcp", beelzebubServiceConfiguration.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(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()
}()
}
}
}()
log.WithFields(log.Fields{
"port": beelzebubServiceConfiguration.Address,
"banner": beelzebubServiceConfiguration.Banner,
}).Infof("Init service %s", beelzebubServiceConfiguration.Protocol)
return nil
}