Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Virgula0 committed Feb 21, 2025
1 parent 27c2b38 commit 79d4375
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 32 deletions.
1 change: 1 addition & 0 deletions client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.4

require (
github.com/gen2brain/raylib-go/raylib v0.0.0-20241228120719-d58ffe1a3a73
github.com/google/uuid v1.6.0
github.com/mandiant/gocat/v6 v6.1.0
github.com/mattn/go-sqlite3 v1.14.24
github.com/sirupsen/logrus v1.9.3
Expand Down
6 changes: 5 additions & 1 deletion client/internal/customerrors/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package customerrors

import "errors"
import (
"database/sql"
"errors"
)

var ErrFinalSending = errors.New("[CLIENT] Failed to send final status, retrying in ")
var ErrHcxToolsNotFound = errors.New("conversion was not successful, hcxtools output file not found")
var ErrNoRowsFound = sql.ErrNoRows
10 changes: 8 additions & 2 deletions client/internal/environment/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/Virgula0/progetto-dp/client/internal/constants"
"github.com/Virgula0/progetto-dp/client/internal/entities"
"github.com/Virgula0/progetto-dp/client/internal/repository"
"github.com/Virgula0/progetto-dp/client/internal/seed"
"github.com/Virgula0/progetto-dp/client/internal/usecase"
"github.com/Virgula0/progetto-dp/client/internal/utils"
"path/filepath"
Expand Down Expand Up @@ -31,7 +32,7 @@ var tables = []*Table{
},
}

func (db *Database) initDB() error {
func (db *Database) initDB(repo *repository.Repository) error {

var tableNames []string
for _, table := range tables {
Expand All @@ -51,6 +52,11 @@ func (db *Database) initDB() error {

go db.StartDBPinger()

// seed with known wordlist, comment this if you will delete rockyou.txt
if err := seed.LoadWordlist(repo); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -91,7 +97,7 @@ func InitEnvironment() (*Environment, ServiceHandler, error) {

repo := repository.NewRepository(db.DB) // init repository

if err := db.initDB(); err != nil {
if err := db.initDB(repo); err != nil {
return nil, ServiceHandler{}, err
}

Expand Down
18 changes: 15 additions & 3 deletions client/internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@ func NewRepository(db *sql.DB) *Repository {

// InsertWordlist creates a wordlist in the database
func (repo *Repository) InsertWordlist(wordlist *entities.Wordlist) error {
// User insert query
// Wordlist insert query
userQuery := fmt.Sprintf(
"INSERT INTO %s(uuid, uuid_user, client_uuid, wordlist_name, wordlist_hash, wordlist_size) VALUES(?,?,?,?,?,?)",
"INSERT INTO %s(uuid, uuid_user, client_uuid, wordlist_name, wordlist_hash, wordlist_size, wordlist_location_path) VALUES(?,?,?,?,?,?,?)",
entities.WordlistTableName,
)

_, err := repo.Exec(userQuery, wordlist.UUID, wordlist.UserUUID, wordlist.ClientUUID, wordlist.WordlistName, wordlist.WordlistHash, wordlist.WordlistSize)
_, err := repo.Exec(userQuery, wordlist.UUID, wordlist.UserUUID, wordlist.ClientUUID, wordlist.WordlistName, wordlist.WordlistHash, wordlist.WordlistSize, wordlist.WordlistLocationPath)
if err != nil {
return err
}

return nil
}

// GetWordlistByHash creates a wordlist in the database
func (repo *Repository) GetWordlistByHash(hash string) (*entities.Wordlist, error) {
var wordlist entities.Wordlist

query := fmt.Sprintf("SELECT * FROM %s WHERE wordlist_hash = ?", entities.WordlistTableName)

row := repo.QueryRow(query, hash)
err := row.Scan(&wordlist.UUID, &wordlist.UserUUID, &wordlist.ClientUUID, &wordlist.WordlistName, &wordlist.WordlistHash, &wordlist.WordlistSize, &wordlist.WordlistLocationPath)

return &wordlist, err
}
48 changes: 48 additions & 0 deletions client/internal/seed/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seed

import (
"fmt"
"github.com/Virgula0/progetto-dp/client/internal/constants"
"github.com/Virgula0/progetto-dp/client/internal/entities"
"github.com/Virgula0/progetto-dp/client/internal/repository"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)

type Seed struct {
Wordlist *entities.Wordlist
}

var Wordlist = &Seed{
Wordlist: &entities.Wordlist{
UUID: uuid.New().String(),
UserUUID: uuid.New().String(), // we don't use it so we can fake them just for seeding
ClientUUID: uuid.New().String(), // we don't use it so we can fake them just for seeding
WordlistName: "rockyou.txt",
WordlistHash: "9076652d8ae75ce713e23ab09e10d9ee", // this is important and what actually what is checked
WordlistSize: 139921497,
WordlistLocationPath: constants.WordlistPath,
},
}

func LoadWordlist(repo *repository.Repository) error {
return loadWordlist(repo)
}

func loadWordlist(repo *repository.Repository) error {

seeds := []*Seed{
Wordlist,
}

for _, ww := range seeds {

err := repo.InsertWordlist(ww.Wordlist)
if err != nil {
e := fmt.Errorf("failed to seed wordlist table: %v", err)
log.Error(e)
return e
}
}
return nil
}
4 changes: 4 additions & 0 deletions client/internal/usecase/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ func NewUsecase(repo *repository.Repository) *Usecase {
func (uc *Usecase) CreateWordlist(wordlist *entities.Wordlist) error {
return uc.repo.InsertWordlist(wordlist)
}

func (uc *Usecase) GetWordlistByHash(hash string) (*entities.Wordlist, error) {
return uc.repo.GetWordlistByHash(hash)
}
34 changes: 22 additions & 12 deletions client/internal/wordlisthandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package wordlisthandler

import (
"crypto/md5"
"errors"
"fmt"
"github.com/Virgula0/progetto-dp/client/internal/customerrors"
"io"
"io/fs"
"path/filepath"
Expand All @@ -25,22 +27,20 @@ const (
hiddenFilePrefix = "."
)

var serverList = make(map[string]string)

type Handler struct {
Handler *environment.ServiceHandler
Client *grpcclient.Client
}

func (h *Handler) WordlistSync() error {
func (h *Handler) WordlistSync() {
ticker := time.NewTicker(syncInterval)
defer ticker.Stop()

for {
log.Info("[CLIENT] Starting wordlist sync cycle")

if err := h.syncCycle(); err != nil {
return err
log.Errorf("[CLIENT] Error syncing wordlist: %v", err)
}
<-ticker.C
}
Expand Down Expand Up @@ -90,8 +90,6 @@ func (h *Handler) syncServerWordlists(response *pb.GetWordlistResponse) error {
if err := h.streamDownloadWordlist(wlEntity); err != nil {
return err
}

serverList[wordlistInfo.GetWordlistHash()] = wlEntity.WordlistName
}

return nil
Expand Down Expand Up @@ -143,15 +141,15 @@ func (h *Handler) uploadNewWordlist() error {
return nil
}

return h.processWordlistFile(path, serverList)
return h.processWordlistFile(path)
})
}

func shouldSkipFile(d fs.DirEntry) bool {
return d.IsDir() || strings.HasPrefix(d.Name(), hiddenFilePrefix)
}

func (h *Handler) processWordlistFile(path string, serverList map[string]string) error {
func (h *Handler) processWordlistFile(path string) error {
fileBytes, err := utils.ReadFileBytes(path)
if err != nil {
return fmt.Errorf("read file error: %w", err)
Expand All @@ -160,15 +158,27 @@ func (h *Handler) processWordlistFile(path string, serverList map[string]string)
fileName := filepath.Base(path)
fileHash := fmt.Sprintf(hashAlgorithm, md5.Sum(fileBytes))

if _, exists := serverList[fileHash]; exists {
log.Infof("[CLIENT] Skipping existing wordlist from the upload: %s (hash: %s)", fileName, fileHash)
return nil
// check wordlist existence in db, returns error if it already exists
_, err = h.Handler.Usecase.GetWordlistByHash(fileHash)
if err != nil && !errors.Is(err, customerrors.ErrNoRowsFound) {
return err
}

log.Infof("[CLIENT] Uploading new wordlist: %s (hash: %s)", fileName, fileHash)

// update server list
serverList[fileHash] = fileName
ww := &entities.Wordlist{
UserUUID: h.Client.EntityClient.UserUUID,
ClientUUID: h.Client.EntityClient.ClientUUID,
WordlistName: fileName,
WordlistHash: fileHash,
WordlistSize: len(fileBytes),
WordlistLocationPath: constants.WordlistPath,
}

if err := h.Handler.Usecase.CreateWordlist(ww); err != nil {
return err
}

return h.streamSendWordlist(fileName, fileBytes)
}
Expand Down
16 changes: 3 additions & 13 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,13 @@ func invokeClientStructInit(client *grpcclient.Client, info *pb.GetClientInfoRes
}
}

func wordlistSyncer(client *grpcclient.Client, handler *environment.ServiceHandler) error {
func wordlistSyncer(client *grpcclient.Client, handler *environment.ServiceHandler) {
syncer := wordlisthandler.Handler{
Handler: handler,
Client: client,
}

err := syncer.WordlistSync()
if err != nil {
return err
}

return nil
syncer.WordlistSync()
}

func main() {
Expand Down Expand Up @@ -142,12 +137,7 @@ func main() {
gocat := invokeClientStructInit(client, info)

// run wordlist syncer in background
go func() {
err = wordlistSyncer(client, &handler)
if err != nil {
log.Fatalf("[CLIENT] Sync cycle failed: %v", err.Error())
}
}()
go wordlistSyncer(client, &handler)

defer client.ClientCloser()

Expand Down
2 changes: 1 addition & 1 deletion server/backend/internal/grpcserver/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (s *ServerContext) ClientToServerWordlist(stream pb.HDSTemplateService_Clie

if err := s.Usecase.CreateWordlist(ww); err != nil {
if errors.Is(err, customErrors.ErrWordlistAlreadyPresent) {
log.Infof("[gRPC] %v", customErrors.ErrWordlistAlreadyPresent)
log.Warnf("[gRPC] %v", customErrors.ErrWordlistAlreadyPresent)
return nil
}
return err
Expand Down

0 comments on commit 79d4375

Please sign in to comment.