-
Notifications
You must be signed in to change notification settings - Fork 2
/
image.go
229 lines (204 loc) · 4.47 KB
/
image.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package processor
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/gpestana/redonion/tor"
"github.com/xiam/exif"
"golang.org/x/net/html"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"strings"
"time"
)
type ImageProcessor struct {
name string
inChannel chan DataUnit
outChannel chan DataUnit
inputLength int
images []Image
tfUrl string
}
type Image struct {
RootUrl string
Url string
ProcessorName string
Exif map[string]string
Recon ReconResults
Errors []string
Timestamp time.Time
}
func NewImageProcessor(in chan DataUnit, out chan DataUnit, len int, cnf Config) ImageProcessor {
var tfUrl string
for _, c := range cnf.Processors {
if c.Type == "image" {
tfUrl = c.TFUrl
break
}
}
return ImageProcessor{
name: Name("image"),
inChannel: in,
outChannel: out,
inputLength: len,
tfUrl: tfUrl,
}
}
func (img Image) Json() ([]byte, error) {
json, err := json.Marshal(img)
return json, err
}
func (p ImageProcessor) Process() {
for j := 0; j < p.inputLength; j++ {
du := DataUnit{}
du = <-p.inChannel
imgUrls := images(du.Html)
for _, url := range imgUrls {
errs := []string{}
curl := canonicalUrl(du.Url, url)
imgData, err := tor.Get(curl)
if err != nil {
errs = append(errs, err.Error())
}
// TODO: refactor to struct method?
meta, err := metadata(imgData)
if err != nil {
errs = append(errs, err.Error())
}
recons, err := recognition(imgData, p.tfUrl)
if err != nil {
errs = append(errs, err.Error())
}
i := Image{
RootUrl: du.Url,
Url: curl,
ProcessorName: p.name,
Exif: meta,
Recon: recons,
Errors: errs,
Timestamp: time.Now(),
}
du.Outputs = append(du.Outputs, i)
}
p.outChannel <- du
}
}
func (p ImageProcessor) Name() string {
return p.name
}
func (p ImageProcessor) InChannel() chan DataUnit {
return p.inChannel
}
//gets all image urls from HTML
func images(b []byte) []string {
urls := []string{}
r := bytes.NewReader(b)
tz := html.NewTokenizer(r)
for {
tok := tz.Next()
switch {
case tok == html.ErrorToken:
return urls
case tok == html.StartTagToken:
t := tz.Token()
if t.Data == "img" {
for _, a := range t.Attr {
if a.Key == "src" {
urls = append(urls, a.Val)
}
}
}
case tok == html.SelfClosingTagToken:
t := tz.Token()
if t.Data == "img" {
for _, a := range t.Attr {
if a.Key == "src" {
urls = append(urls, a.Val)
}
}
}
}
}
}
func metadata(data []byte) (map[string]string, error) {
r := exif.New()
buf := bytes.NewBuffer(data)
_, err := io.Copy(r, buf)
if err.Error() != "Found EXIF header. OK to call Parse." {
return nil, err
}
err = r.Parse()
if err != nil {
return nil, err
}
return r.Tags, nil
}
type ReconResults struct {
Results []Recon
}
type Recon struct {
Label string
Probability float32
}
func recognition(d []byte, tfUrl string) (ReconResults, error) {
if tfUrl == "" {
return ReconResults{}, errors.New("Tensorflow URL is not defined")
}
data := bytes.NewBuffer(d)
var b bytes.Buffer
w := multipart.NewWriter(&b)
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name=image; filename="from-buffer"`))
fw, err := w.CreatePart(h)
if err != nil {
return ReconResults{}, err
}
if _, err = io.Copy(fw, data); err != nil {
return ReconResults{}, err
}
w.Close()
req, err := http.NewRequest("POST", tfUrl, &b)
if err != nil {
return ReconResults{}, err
}
req.Header.Set("Content-Type", w.FormDataContentType())
cli := &http.Client{}
r, err := cli.Do(req)
if err != nil {
return ReconResults{}, err
}
if r == nil {
return ReconResults{}, errors.New("Recon: HTTP response is empty")
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
type tfResults struct {
Filename string
Labels []Recon
Err string `json:"error"`
}
res := tfResults{}
err = json.Unmarshal(body, &res)
if err != nil {
return ReconResults{}, err
}
if recErr := res.Err; recErr != "" {
return ReconResults{}, errors.New("Recon: " + recErr)
}
recons := ReconResults{res.Labels}
return recons, nil
}
func canonicalUrl(b string, u string) string {
if strings.HasPrefix(u, "http") || strings.HasPrefix(u, "www") {
return u
}
b = strings.TrimSuffix(b, "/")
u = strings.TrimPrefix(u, ".")
u = strings.TrimPrefix(u, "/")
return b + "/" + u
}