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

scan.StartScan   B

Complexity

Conditions 3

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 43
nop 4
dl 0
loc 58
rs 8.8478
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package scan
2
3
import (
4
	"fmt"
5
	"net/http"
6
	"net/url"
7
8
	"github.com/chuckpreslar/emission"
9
	"github.com/pkg/errors"
10
	"github.com/sirupsen/logrus"
11
	"github.com/stefanoj3/dirstalk/pkg/dictionary"
12
	"github.com/stefanoj3/dirstalk/pkg/scan/client"
13
)
14
15
// StartScan is a convenience method that wires together all the dependencies needed to start a scan
16
func StartScan(logger *logrus.Logger, eventManager *emission.Emitter, cnf *Config, u *url.URL) error {
17
	c, err := client.NewClientFromConfig(
18
		cnf.TimeoutInMilliseconds,
19
		cnf.Socks5Url,
20
		cnf.UserAgent,
21
		cnf.UseCookieJar,
22
		cnf.Cookies,
23
		cnf.Headers,
24
		u,
25
	)
26
	if err != nil {
27
		return errors.Wrap(err, "failed to build client")
28
	}
29
30
	s := NewScanner(
31
		c,
32
		eventManager,
33
		logger,
34
	)
35
36
	dict, err := dictionary.NewDictionaryFrom(cnf.DictionaryPath, c)
37
	if err != nil {
38
		return errors.Wrap(err, "failed to build dictionary")
39
	}
40
41
	r := NewReProcessor(eventManager, cnf.HTTPMethods, dict)
42
43
	eventManager.On(EventResultFound, r.ReProcess)
44
	eventManager.On(EventTargetProduced, s.AddTarget)
45
	eventManager.On(EventProducerFinished, s.Release)
46
47
	targetProducer := NewTargetProducer(
48
		eventManager,
49
		cnf.HTTPMethods,
50
		dict,
51
		cnf.ScanDepth,
52
	)
53
54
	go targetProducer.Run()
55
56
	logger.WithFields(logrus.Fields{
57
		"url":               u.String(),
58
		"threads":           cnf.Threads,
59
		"dictionary-length": len(dict),
60
		"scan-depth":        cnf.ScanDepth,
61
		"timeout":           cnf.TimeoutInMilliseconds,
62
		"socks5":            cnf.Socks5Url,
63
		"cookies":           strigifyCookies(cnf.Cookies),
64
		"cookie-jar":        cnf.UseCookieJar,
65
		"headers":           stringyfyHeaders(cnf.Headers),
66
		"user-agent":        cnf.UserAgent,
67
	}).Info("Starting scan")
68
69
	s.Scan(u, cnf.Threads)
70
71
	logger.Info("Finished scan")
72
73
	return nil
74
}
75
76
func strigifyCookies(cookies []*http.Cookie) string {
77
	result := ""
78
79
	for _, cookie := range cookies {
80
		result += fmt.Sprintf("{%s=%s}", cookie.Name, cookie.Value)
81
	}
82
83
	return result
84
}
85
86
func stringyfyHeaders(headers map[string]string) string {
87
	result := ""
88
89
	for name, value := range headers {
90
		result += fmt.Sprintf("{%s:%s}", name, value)
91
	}
92
93
	return result
94
}
95