diff --git a/README.md b/README.md index e69de29..9b85e89 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,47 @@ +# LOIC + +A DDOS tool that can be used to attack Kubernetes services and arbitrary URLs. + +## Installation + +Instructions on how to build and start LOIC. + +```bash +# Install dependencies +go mod tidy + +# Build LOIC +go build -o loic + +# Start LOIC +./loic start +``` + +## Usage + +Instructions on how to LOIC. + +```bash +# Start an attack +./loic start --target http://example.com -c 10 -d 1m -r 30s + +# Get the status of LOIC +./loic status + +# Start the Web UI +./loic web + +# Stop LOIC +./loic stop +``` + +## Contributing + +Guidelines for contributing to the project. + +1. Fork the repository. +2. Create a new branch (`git checkout -b feature-branch`). +3. Make your changes. +4. Commit your changes (`git commit -m 'Add some feature'`). +5. Push to the branch (`git push origin feature-branch`). +6. Open a pull request. diff --git a/cmd/start.go b/cmd/start.go index 39cd680..671874a 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -2,14 +2,15 @@ package cmd import ( "fmt" + "time" + "forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go/pkg/loic" "github.com/spf13/cobra" - "time" ) var startCmd = &cobra.Command{ Use: "start", - Short: "Startet einen Lasttest", + Short: "Start an attack", Run: func(cmd *cobra.Command, args []string) { targetURL, _ := cmd.Flags().GetString("target") concurrency, _ := cmd.Flags().GetInt("concurrency") @@ -17,6 +18,7 @@ var startCmd = &cobra.Command{ rampUp, _ := cmd.Flags().GetDuration("ramp-up") fmt.Printf("Starting test with target %s, concurrency %d, duration %v, ramp-up %v\n", targetURL, concurrency, duration, rampUp) loic.StartTest(targetURL, concurrency, duration, rampUp) + <-loic.TestDone }, } @@ -25,5 +27,6 @@ func init() { startCmd.Flags().IntP("concurrency", "c", 10, "Maximale Anzahl paralleler Anfragen") startCmd.Flags().DurationP("duration", "d", time.Minute, "Dauer des Tests") startCmd.Flags().DurationP("ramp-up", "r", 30*time.Second, "Zeit für schrittweise Erhöhung der Last") + startCmd.MarkFlagRequired("target") rootCmd.AddCommand(startCmd) } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ae9f1cd --- /dev/null +++ b/go.mod @@ -0,0 +1,48 @@ +module forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Daniel.Sy/loic-go + +go 1.24.1 + +require ( + github.com/gin-gonic/gin v1.10.0 + github.com/spf13/cobra v1.9.1 + go.opentelemetry.io/otel v1.35.0 + go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v1.35.0 + go.opentelemetry.io/otel/metric v1.35.0 + go.opentelemetry.io/otel/sdk v1.35.0 + go.opentelemetry.io/otel/sdk/metric v1.35.0 +) + +require ( + github.com/bytedance/sonic v1.13.2 // indirect + github.com/bytedance/sonic/loader v0.2.4 // indirect + github.com/cloudwego/base64x v0.1.5 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v1.0.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.25.0 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.10 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/spf13/pflag v1.0.6 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + go.opentelemetry.io/auto/sdk v1.1.0 // indirect + go.opentelemetry.io/otel/trace v1.35.0 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/crypto v0.36.0 // indirect + golang.org/x/net v0.37.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + google.golang.org/protobuf v1.36.6 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/pkg/loic/loic.go b/pkg/loic/loic.go index ece29f8..48142e5 100644 --- a/pkg/loic/loic.go +++ b/pkg/loic/loic.go @@ -2,18 +2,20 @@ package loic import ( "context" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/metric" "log" "net/http" "sync" "time" + + "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/metric" ) var meter = otel.Meter("loic") var requestsSent metric.Int64Counter var currentTest *Test var isRunning bool +var TestDone = make(chan struct{}) func init() { var err error @@ -24,6 +26,8 @@ func init() { if err != nil { log.Fatalf("failed to create counter: %v", err) } + isRunning = false + log.Println("LOIC initialized") } type Test struct { @@ -37,6 +41,11 @@ type Test struct { } func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp time.Duration) { + if isRunning { + log.Println("Test already running, skipping start") + return + } + log.Printf("Starting test with target: %s, concurrency: %d, duration: %v, rampUp: %v", targetURL, concurrency, duration, rampUp) ctx, cancel := context.WithCancel(context.Background()) currentTest := &Test{ TargetURL: targetURL, @@ -46,17 +55,24 @@ func StartTest(targetURL string, concurrency int, duration time.Duration, rampUp ctx: ctx, cancel: cancel, } - - go currentTest.run() + isRunning = true + log.Println("isRunning set to true, launching goroutine") + go func() { + currentTest.run() + close(TestDone) + }() } func StopTest() { if currentTest != nil && isRunning { + log.Println("Stopping test") currentTest.cancel() currentTest.wg.Wait() currentTest = nil isRunning = false - log.Println("Test stopped") + log.Println("Test stopped, isRunning set to false") + } else { + log.Println("No test running to stop") } } @@ -65,23 +81,41 @@ func IsTestRunning() bool { } func (t *Test) run() { - defer func() { isRunning = false }() - ticker := time.NewTicker(t.RampUpTime / time.Duration(t.Concurrency)) - defer ticker.Stop() - currentConcurrency := 1 - for i := 0; i < t.Concurrency; i++ { - select { - case <-ticker.C: - if currentConcurrency < t.Concurrency { - currentConcurrency++ - t.startWorker() + defer func() { + isRunning = false + log.Println("Test run completed, isRunning set to false") + }() + log.Println("Starting test run") + + // Start first worker immediately + if t.Concurrency > 0 { + log.Println("Starting worker 1") + t.startWorker() + currentConcurrency := 1 + if t.Concurrency > 1 { + for i := 1; i < t.Concurrency; i++ { + select { + case <-time.After(t.RampUpTime / time.Duration(t.Concurrency-1)): + currentConcurrency++ + log.Printf("Starting worker %d", currentConcurrency) + t.startWorker() + case <-t.ctx.Done(): + log.Println("Context cancelled during ramp-up, exiting run") + return + } } - case <-time.After(t.Duration): - t.cancel() - case <-t.ctx.Done(): - return } } + + // Wait for the duration or context cancellation + select { + case <-time.After(t.Duration): + log.Println("Duration reached, cancelling test") + t.cancel() + case <-t.ctx.Done(): + log.Println("Context cancelled, exiting run") + } + log.Println("Waiting for all workers to finish") t.wg.Wait() } @@ -89,17 +123,20 @@ func (t *Test) startWorker() { t.wg.Add(1) go func() { defer t.wg.Done() + log.Println("Worker started") for { select { case <-t.ctx.Done(): + log.Println("Worker stopped due to context cancellation") return default: resp, err := http.Get(t.TargetURL) if err != nil { - log.Println("Error:", err) + log.Println("Error in worker: %v", err) } else { resp.Body.Close() requestsSent.Add(t.ctx, 1) + log.Println("Request sent, counter incremented") } time.Sleep(time.Second / time.Duration(t.Concurrency)) }