-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathredonion.go
145 lines (122 loc) · 2.91 KB
/
redonion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"bufio"
"encoding/json"
"flag"
"github.com/gpestana/redonion/errors"
"github.com/gpestana/redonion/fetcher"
"github.com/gpestana/redonion/output"
"github.com/gpestana/redonion/processors"
"io/ioutil"
"log"
"os"
"strings"
)
func main() {
errs := []errors.Error{}
urls := flag.String("urls", "http://127.0.0.1", "list of addresses to scan (separated by comma)")
list := flag.String("list", "", "path for list of addresses to scan")
c := flag.String("config", "", "path for configuration file")
flag.Parse()
cnf, err := config(c)
procCnf, err := processor.ParseConfig(c)
if err != nil {
log.Fatal("Could not parse configuration file " + err.Error())
}
ulist, err := parseUrls(urls, list)
if err != nil {
log.Fatal(err)
}
imgChn := make(chan processor.DataUnit, len(ulist))
hiddenChn := make(chan processor.DataUnit, len(ulist))
chs := []chan processor.DataUnit{hiddenChn, imgChn}
outputChn := make(chan processor.DataUnit, len(ulist)*len(chs))
processors := []processor.Processor{
processor.NewHiddenProcessor(hiddenChn, outputChn, len(ulist)),
processor.NewImageProcessor(imgChn, outputChn, len(ulist), procCnf),
}
sizeResults := len(ulist) * len(chs)
outputs := []output.Output{
output.NewEs(cnf.Outputs, outputChn, sizeResults),
}
fetcher, err := fetcher.New(ulist, processors, &errs)
if err != nil {
log.Fatal(err)
}
fetcher.Start()
for _, p := range processors {
p.Process()
}
for _, o := range outputs {
o.Start()
}
// write outputs to stdout
for _, o := range outputs {
jres, err := o.Results()
if err != nil {
errs = append(errs, errors.Error{errors.OutputError, err.Error()})
} else {
os.Stdout.Write(jres)
}
}
closeChannels(chs, outputChn)
if len(errs) != 0 {
for _, e := range errs {
e.Print()
}
} else {
log.Println("No errors")
}
}
type processorsC struct {
Type string `json:"type"`
TFUrl string `json:"tensorflow_url"`
}
type Config struct {
Outputs []output.Conf `json:"outputs"`
Processors []processorsC `json:"processors"`
}
func config(p *string) (Config, error) {
cf, err := ioutil.ReadFile(*p)
if err != nil {
return Config{}, err
}
config := Config{}
err = json.Unmarshal([]byte(cf), &config)
if err != nil {
return Config{}, nil
}
return config, nil
}
func parseUrls(urlsIn *string, list *string) ([]string, error) {
var urls []string
if *list != "" {
urlsPath, err := parseListURL(*list)
if err != nil {
return nil, err
}
urls = urlsPath
} else {
urls = strings.Split(*urlsIn, ",")
}
return urls, nil
}
func parseListURL(p string) ([]string, error) {
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
var urls []string
s := bufio.NewScanner(f)
for s.Scan() {
urls = append(urls, s.Text())
}
return urls, s.Err()
}
func closeChannels(chs []chan processor.DataUnit, outputCh chan processor.DataUnit) {
for _, ch := range chs {
close(ch)
}
close(outputCh)
}