Passed
Pull Request — master (#40)
by Stefano
03:01
created

scan.buildClientFrom   B

Complexity

Conditions 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nop 1
dl 0
loc 28
rs 8.6166
c 0
b 0
f 0

1 Method

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