Skip to content

Commit

Permalink
#9 Clean code, add validator outline
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed May 7, 2024
1 parent b450fec commit 81d1811
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
24 changes: 9 additions & 15 deletions internal/ddosml/ml.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
)

type RequestRepository interface {
CreateRequest(ctx context.Context, req *Request) error
type Validator interface {
ValidateRequest(ctx context.Context, req *http.Request) (bool, error)
}

type Request struct {
Expand All @@ -18,33 +18,27 @@ type Request struct {
// DDOS protection.
type DDOS struct {
// isEnabled is used in order to enable or disable the ddosml
isEnabled bool
repository RequestRepository
isEnabled bool
validator Validator
}

// NewDDOS creates new ddos ML analyzer used to
// analyze requests in order to find out if given
// request is ddos attack or not.
func NewDDOS(isEnabled bool, repository RequestRepository) *DDOS {
func NewDDOS(isEnabled bool, validator Validator) *DDOS {
return &DDOS{
isEnabled: isEnabled,
repository: repository,
isEnabled: isEnabled,
validator: validator,
}
}

// IsRequestSuspicious checks if given request is suspicious (and then saves it in the database in order to be
// used in future evaluations of this validator)
func (d *DDOS) IsRequestSuspicious(ctx context.Context, req *http.Request) (bool, error) {
ok, err := d.validateRequest(ctx, req)
ok, err := d.validator.ValidateRequest(ctx, req)
if err != nil {
return ok, fmt.Errorf("validate request: %w", err)
return ok, fmt.Errorf("validator validate request: %w", err)
}

return ok, nil
}

// validateRequest is used to validate if given request is ddos or not. The validation is based on the ML
// model, which decides based on normal user decisions from the UI.
func (d *DDOS) validateRequest(ctx context.Context, req *http.Request) (bool, error) {

}
15 changes: 13 additions & 2 deletions internal/ddosml/validator.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package ddosml

// Validator is a core of the ddosml, it validates
import "context"

type RequestRepository interface {
CreateRequest(ctx context.Context, req *Request) error
}

// MLBasedModelValidator is a core of the ddosml, it validates
// each request against trained model and from time to time,
// it's retraining the model against new data, and also it
// clears database.
type Validator struct {
type MLBasedModelValidator struct {
repository RequestRepository
}

func NewMlModelValidator(repository RequestRepository) *MLBasedModelValidator {
return &MLBasedModelValidator{repository: repository}
}

0 comments on commit 81d1811

Please sign in to comment.