Skip to content

Commit

Permalink
Add new flag option for resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuyoshi30 committed Nov 24, 2020
1 parent 04b67c9 commit af2ae17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions attacker/attacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Options struct {
HTTP2 bool
LocalAddr net.IPAddr
Buckets []time.Duration
Resolvers []string

Attacker Attacker
}
Expand Down
47 changes: 47 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -46,6 +47,7 @@ type cli struct {
localAddress string
noKeepAlive bool
buckets string
resolvers string

debug bool
version bool
Expand Down Expand Up @@ -84,6 +86,7 @@ func parseFlags(stdout, stderr io.Writer) (*cli, error) {
flagSet.StringVar(&c.localAddress, "local-addr", "0.0.0.0", "Local IP address.")
// TODO: Re-enable when making it capable of drawing histogram bar chart.
//flagSet.StringVar(&c.buckets, "buckets", "", "Histogram buckets; comma-separated list.")
flagSet.StringVar(&c.resolvers, "resolvers", "", "Custom DNS resolver addresses; comma-separated list.")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
Expand Down Expand Up @@ -186,6 +189,11 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
return nil, fmt.Errorf("wrong buckets format %w", err)
}

parsedResolvers, err := parseResolvers(c.resolvers)
if err != nil {
return nil, err
}

return &attacker.Options{
Rate: c.rate,
Duration: c.duration,
Expand All @@ -201,6 +209,7 @@ func (c *cli) makeOptions() (*attacker.Options, error) {
HTTP2: !c.noHTTP2,
LocalAddr: localAddr,
Buckets: parsedBuckets,
Resolvers: parsedResolvers,
}, nil
}

Expand Down Expand Up @@ -232,6 +241,44 @@ func parseBucketOptions(rawBuckets string) ([]time.Duration, error) {
return result, nil
}

func parseResolvers(addrs string) ([]string, error) {
if addrs == "" {
return nil, nil
}

stringAddrs := strings.Split(addrs, ",")
var result []string

for _, addr := range stringAddrs {
trimmedAddr := strings.TrimSpace(addr)

// if given address has no port, append "53"
if !strings.Contains(trimmedAddr, ":") {
trimmedAddr += ":53"
}

host, port, err := net.SplitHostPort(trimmedAddr)
if err != nil {
return nil, err
}

// validate port
_, err = strconv.ParseUint(port, 10, 16)
if err != nil {
return nil, fmt.Errorf("port of given address %q has a wrong format", addr)
}

// validate IP
if ip := net.ParseIP(host); ip == nil {
return nil, fmt.Errorf("given address %q has a wrong format", addr)
}

result = append(result, trimmedAddr)
}

return result, nil
}

// Makes a new file under the working directory only when debug use.
func setDebug(w io.Writer, debug bool) {
if !debug {
Expand Down

0 comments on commit af2ae17

Please sign in to comment.