-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathobject.go
More file actions
177 lines (149 loc) · 4.77 KB
/
object.go
File metadata and controls
177 lines (149 loc) · 4.77 KB
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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
"strings"
"time"
)
var proxy *httputil.ReverseProxy // The proxy object for redirecting object-storage requests
type storageResult struct {
Type string `json:"type"`
CreationDateTime time.Time `json:"creation_date_time"`
Submissions []string `json:"submissions"`
Source []string `json:"source"`
MD5 string `json:"md5"`
SHA1 string `json:"sha1"`
SHA256 string `json:"sha256"`
FileMime string `json:"file_mime"`
FileName []string `json:file_name"`
DomainFQDN string `json:"domain_fqdn"`
DomainTLD string `json:"domain_tld"`
DomainSubDomain string `json:"domain_sub_domain"`
IPAddress string `json:"ip_address"`
IPv6 bool `json:"ip_v6"`
EmailAddress string `json:"email_address"`
EmailLocalPart string `json:"email_local_part"`
EmailDomainPart string `json:"email_domain_part"`
EmailSubAddressing string `json:"email_sub_addressing"`
GenericIdentifier string `json:"generic_identifier"`
GenericType string `json:"generic_type"`
GenericDataRelAddress string `json:"generic_data_rel_address"`
}
type storageResponse struct {
ResponseCode int
Failure string
Result storageResult
}
type myTransport struct {
}
func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) {
// Forwards the given request to storage and executes auto-tasking, if successfull
// Since accessing the Form-values of the request changes the reader,
// which cannot be rewinded / seeked, an error would be thrown, if the
// request was forwarded with the reader at the wrong position.
// For this reason, the whole body is read and a new reader is created,
// which can be rewinded.
var err error
if request.ContentLength > 1024*1024*int64(conf.MaxUploadSize) {
respBody := ioutil.NopCloser(bytes.NewBufferString("Upload too large"))
resp := &http.Response{StatusCode: 413, Body: respBody}
respBody.Close()
log.Println("Upload too large")
return resp, nil
}
reqbuf := make([]byte, request.ContentLength)
_, err = io.ReadFull(request.Body, reqbuf)
if err != nil {
log.Printf("Error reading body!", err)
return nil, err
}
reader := bytes.NewReader(reqbuf)
reqrdr := ioutil.NopCloser(reader)
request.Body = reqrdr
defer func() {
request.Body.Close()
reqrdr.Close()
}()
request.ParseMultipartForm(1024 * 1024 * int64(conf.MaxUploadSize))
// Read the name and the source from the request, because they can not be
// reconstructed from storage's response.
name := request.FormValue("name")
source := request.FormValue("source")
username := request.FormValue("username")
password := request.FormValue("password")
*request.URL = storageURIStoreSample
// restore the reader for the body
reader.Seek(0, 0)
user, err := authenticate(username, password)
if err != nil {
return nil, err
}
form, _ := url.ParseQuery(request.URL.RawQuery)
form.Set("user_id", strconv.Itoa(user.Id))
request.URL.RawQuery = form.Encode()
// Do the proxy-request
trans := http.DefaultTransport.(*http.Transport)
trans.TLSClientConfig = &tls.Config{InsecureSkipVerify: conf.DisableStorageVerify}
response, err := http.DefaultTransport.RoundTrip(request)
if err != nil {
log.Printf("Error performing proxy-request!", err)
return nil, err
}
// Parse the response. If it was successful, execute automatic tasks
var resp storageResponse
buf := make([]byte, response.ContentLength)
_, err = io.ReadFull(response.Body, buf)
if err != nil {
log.Printf("Error reading body!", err)
return nil, err
}
rdr := ioutil.NopCloser(bytes.NewReader(buf))
defer func() {
rdr.Close()
response.Body.Close()
}()
json.Unmarshal(buf, &resp)
//log.Printf("%+v\n", resp)
if resp.ResponseCode == 0 {
log.Printf("\x1b[0;32mSuccessfully uploaded sample with SHA256: %s\x1b[0m", resp.Result.SHA256)
// Execute automatic tasks
for t := range conf.AutoTasks {
if strings.Contains(resp.Result.FileMime, t) {
autotasks := conf.AutoTasks[t]
if len(autotasks) != 0 {
task := TaskRequest{
PrimaryURI: resp.Result.SHA256,
SecondaryURI: "",
Filename: name,
Tasks: autotasks,
Tags: []string{},
Attempts: 0,
Source: source,
Download: true,
}
log.Printf("\x1b[0;33mAutomatically executing %+v\x1b[0m\n", task)
handleOwnTasks([]TaskRequest{task})
}
}
}
} else {
log.Printf("\x1b[0;31mUpload failed\x1b[0m\n")
if response.StatusCode == 200 {
response.StatusCode = 400 // bad request
}
}
// restore the reader for the body
response.Body = rdr
return response, err
}
func httpRequestIncomingSample(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
}