Passed
Pull Request — master (#41)
by Stefano
02:42
created

cmd.rawHeadersToHeaders   A

Complexity

Conditions 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
package cmd
2
3
import (
4
	"net/http"
5
	"net/url"
6
	"strings"
7
	"time"
8
9
	"github.com/pkg/errors"
10
	"github.com/spf13/cobra"
11
	"github.com/stefanoj3/dirstalk/pkg/scan"
12
)
13
14
func scanConfigFromCmd(cmd *cobra.Command) (*scan.Config, error) {
15
	c := &scan.Config{}
16
17
	var err error
18
19
	c.DictionaryPath = cmd.Flag(flagDictionary).Value.String()
20
21
	c.HTTPMethods, err = cmd.Flags().GetStringSlice(flagHTTPMethods)
22
	if err != nil {
23
		return nil, errors.Wrap(err, "failed to read http methods flag")
24
	}
25
26
	c.Threads, err = cmd.Flags().GetInt(flagThreads)
27
	if err != nil {
28
		return nil, errors.Wrap(err, "failed to read threads flag")
29
	}
30
31
	c.TimeoutInMilliseconds, err = cmd.Flags().GetInt(flagHTTPTimeout)
32
	if err != nil {
33
		return nil, errors.Wrap(err, "failed to read http-timeout flag")
34
	}
35
36
	c.ScanDepth, err = cmd.Flags().GetInt(flagScanDepth)
37
	if err != nil {
38
		return nil, errors.Wrap(err, "failed to read http-timeout flag")
39
	}
40
41
	socks5Host := cmd.Flag(flagSocks5Host).Value.String()
42
	if len(socks5Host) > 0 {
43
		c.Socks5Url, err = url.Parse("socks5://" + socks5Host)
44
		if err != nil {
45
			return nil, errors.Wrap(err, "invalid value for "+flagSocks5Host)
46
		}
47
	}
48
49
	c.UserAgent = cmd.Flag(flagUserAgent).Value.String()
50
51
	c.UseCookieJar, err = cmd.Flags().GetBool(flagCookieJar)
52
	if err != nil {
53
		return nil, errors.Wrap(err, "cookie jar flag is invalid")
54
	}
55
56
	rawCookies, err := cmd.Flags().GetStringArray(flagCookie)
57
	if err != nil {
58
		return nil, errors.Wrap(err, "failed to read cookies flag")
59
	}
60
61
	c.Cookies, err = rawCookiesToCookies(rawCookies)
62
	if err != nil {
63
		return nil, errors.Wrap(err, "failed to convert rawCookies to objects")
64
	}
65
66
	rawHeaders, err := cmd.Flags().GetStringArray(flagHeader)
67
	if err != nil {
68
		return nil, errors.Wrap(err, "failed to read cookies flag")
69
	}
70
71
	c.Headers, err = rawHeadersToHeaders(rawHeaders)
72
	if err != nil {
73
		return nil, errors.Wrap(err, "failed to convert rawHeaders")
74
	}
75
76
	return c, nil
77
}
78
79
func rawHeadersToHeaders(rawHeaders []string) (map[string]string, error) {
80
	headers := make(map[string]string, len(rawHeaders)*2)
81
82
	for _, rawHeader := range rawHeaders {
83
		parts := strings.Split(rawHeader, ":")
84
		if len(parts) != 2 {
85
			return nil, errors.Errorf("header is in invalid format: %s", rawHeader)
86
		}
87
88
		headers[parts[0]] = parts[1]
89
	}
90
91
	return headers, nil
92
}
93
94
func rawCookiesToCookies(rawCookies []string) ([]*http.Cookie, error) {
95
	cookies := make([]*http.Cookie, 0, len(rawCookies))
96
97
	for _, rawCookie := range rawCookies {
98
		parts := strings.Split(rawCookie, "=")
99
		if len(parts) != 2 {
100
			return nil, errors.Errorf("cookie format is invalid: %s", rawCookie)
101
		}
102
103
		cookies = append(
104
			cookies,
105
			&http.Cookie{
106
				Name:    parts[0],
107
				Value:   parts[1],
108
				Expires: time.Now().AddDate(0, 0, 2),
109
			},
110
		)
111
	}
112
113
	return cookies, nil
114
}
115