forked from danmia/pcapdaemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postdata.go
183 lines (162 loc) · 5.32 KB
/
postdata.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
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/textproto"
"strconv"
"strings"
"time"
// amamzon stuff
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func newBufferUploadRequest(uri string, params map[string]string, paramName string, buf bytes.Buffer, filename string) (*http.Request, error) {
var err error
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
if err != nil {
return nil, err
}
mh := make(textproto.MIMEHeader)
mh.Set("Content-Type", "application/octet-stream")
mh.Set("Content-Disposition", "form-data; name=\"file\"; filename=\""+filename+"\"")
part, err := writer.CreatePart(mh)
if nil != err {
panic(err.Error())
}
_, err = io.Copy(part, &buf)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
myReq, myErr := http.NewRequest("POST", uri, body)
myReq.Header.Add("Content-Type", "multipart/form-data; boundary="+writer.Boundary())
if myErr != nil {
log.Fatal(myErr)
}
return myReq, myErr
}
func postBufferCloudshark(scheme string, host string, port int, token string, timeout int, buf bytes.Buffer, filename string, tags string) {
var url string
extraParams := map[string]string{
"additional_tags": tags,
}
if port != 80 && port != 443 {
url = scheme + "://" + host + ":" + strconv.Itoa(port) + "/api/v1/" + token + "/upload"
} else {
url = scheme + "://" + host + "/api/v1/" + token + "/upload"
}
request, err := newBufferUploadRequest(url, extraParams, "file", buf, filename)
if err != nil {
log.Printf("newBufferUpload returned: %s\n", err)
fmt.Printf("newBufferUpload returned: %s\n", err)
return
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: tr,
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := client.Do(request)
if err != nil {
log.Printf("Error making request: %s\n", err)
fmt.Printf("Error making request: %s\n", err)
return
}
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
log.Printf("Error: can't read from body: %s\n", err)
fmt.Printf("Error: can't read from body: %s\n", err)
}
resp.Body.Close()
if resp.StatusCode == 200 {
var succ CsSuccess
err := json.Unmarshal(body.Bytes(), &succ)
if err != nil {
fmt.Printf("Unable to parse json error: %s\n", err)
log.Printf("Unable to parse json error: %s\n", err)
}
log.Printf("Successfully uploaded filename: %s with id: %s\n", succ.Filename, succ.ID)
fmt.Printf("Successfully uploaded filename: %s with id: %s\n", succ.Filename, succ.ID)
} else {
var fail CsFail
err := json.Unmarshal(body.Bytes(), &fail)
if err != nil {
fmt.Printf("Unable to parse json error: %s\n", err)
log.Printf("Unable to parse json error: %s\n", err)
}
fmt.Printf("Error code: %d\n", resp.StatusCode)
log.Printf("Error code: %d\n", resp.StatusCode)
fmt.Printf("Headers: %s\n", resp.Header)
log.Printf("Headers: %s\n", resp.Header)
fmt.Printf("Params: %s\n", extraParams)
log.Printf("Params: %s\n", extraParams)
log.Printf("\nFull Body:\n%s\n", body.Bytes())
for _, ex := range fail.Exceptions {
log.Printf("Exception: %s\n", ex)
fmt.Printf("Exception: %s\n", ex)
}
}
}
func postS3(config aws.Config, bucket string, buf bytes.Buffer, filename string, tags string, folder string, acl string, enc bool) {
s3sess, err := session.NewSession(&config)
if err != nil {
// Handle Session creation error
log.Println("Error creating S3 session: ", err)
return
}
s3client := s3.New(s3sess)
fileBytes := bytes.NewReader(buf.Bytes()) // convert to io.ReadSeeker type
fileType := http.DetectContentType(buf.Bytes())
path := "/" + folder + "/" + filename
params := &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
ACL: aws.String(acl),
Body: fileBytes,
ContentLength: aws.Int64(int64(len(buf.Bytes()))),
ContentType: aws.String(fileType),
Metadata: map[string]*string{
"tags": aws.String(tags),
},
}
// Set the encryption if true
if enc {
params.ServerSideEncryption = aws.String("AES256")
}
result, err := s3client.PutObject(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
// Generic AWS Error with Code, Message, and original error (if any)
fmt.Println("S3 Error: ", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
log.Println("S3 Error: ", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A service error occurred
fmt.Println("S3 Error: ", reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
log.Println("S3 Error: ", reqErr.Code(), reqErr.Message(), reqErr.StatusCode(), reqErr.RequestID())
}
} else {
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
log.Println("S3 Error: ", err.Error())
}
} else {
log.Println("S3 Upload successful: ", filename, " ", strings.TrimSpace(awsutil.StringValue(result)))
fmt.Println("S3 Upload successful: ", filename, " ", strings.TrimSpace(awsutil.StringValue(result)))
}
}