-
Notifications
You must be signed in to change notification settings - Fork 56
/
request.go
170 lines (162 loc) · 4.29 KB
/
request.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"strings"
"time"
)
func addHeaders(req *http.Request) *http.Request {
req.Header.Set("Referer", "https://cowtransfer.com/")
req.Header.Set("User-Agent", "Chrome/80.0.3987.149 CowTransfer-Uploader")
req.Header.Set("Origin", "https://cowtransfer.com/")
req.Header.Set("Cookie", fmt.Sprintf("%scf-cs-k-20181214=%d;", req.Header.Get("Cookie"), time.Now().UnixNano()))
return req
}
func addTk(req *http.Request) {
ck := runConfig.token
if runConfig.authCode != "" {
ck = fmt.Sprintf("%s; cow-auth-token=%s", runConfig.token, runConfig.authCode)
}
req.Header.Set("cookie", ck)
req.Header.Set("authorization", runConfig.authCode)
}
func blockPut(postURL string, buf []byte, token string) (string, error) {
data := new(bytes.Buffer)
data.Write(buf)
body, err := newRequest(postURL, data, token, "PUT")
if err != nil {
if runConfig.debugMode {
log.Printf("block upload failed (retrying)")
}
return "", err
}
var rBody upResp
if err := json.Unmarshal(body, &rBody); err != nil {
if runConfig.debugMode {
log.Printf("block upload failed (retrying)")
}
return "", err
}
if runConfig.hashCheck {
if hashBlock(buf) != rBody.MD5 {
if runConfig.debugMode {
log.Printf("block hashcheck failed (retrying)")
}
return "", fmt.Errorf("block hashcheck failed")
}
if runConfig.debugMode {
log.Printf("hash check: %s == %s", hashBlock(buf), rBody.MD5)
}
}
return rBody.Etag, nil
}
func newRequest(link string, postBody io.Reader, upToken string, action string) ([]byte, error) {
if runConfig.debugMode {
log.Printf("endpoint: %s", link)
}
client := http.Client{Timeout: time.Duration(runConfig.interval) * time.Second}
req, err := http.NewRequest(action, link, postBody)
if err != nil {
if runConfig.debugMode {
log.Printf("build request returns error: %v", err)
}
return nil, err
}
req.Header.Set("referer", "https://cowtransfer.com/")
req.Header.Set("Content-Type", "application/octet-stream")
req.Header.Set("Authorization", "UpToken "+upToken)
if runConfig.debugMode {
log.Println(req.Header)
}
resp, err := client.Do(req)
if err != nil {
if runConfig.debugMode {
log.Printf("do request returns error: %v", err)
}
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if runConfig.debugMode {
log.Printf("read response returns: %v", err)
}
return nil, err
}
_ = resp.Body.Close()
if runConfig.debugMode {
if len(body) < 1024 {
log.Printf("returns: %v", string(body))
}
}
return body, nil
}
func newMultipartRequest(url string, params map[string]string, retry int) ([]byte, error) {
if runConfig.debugMode {
log.Printf("retrying: %v", retry)
log.Printf("postBody: %v", params)
log.Printf("endpoint: %s", url)
}
client := http.Client{Timeout: time.Duration(runConfig.interval) * time.Second}
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
for key, val := range params {
_ = writer.WriteField(key, val)
}
_ = writer.Close()
req, err := http.NewRequest("POST", url, buf)
if err != nil {
if runConfig.debugMode {
log.Printf("build request returns error: %v", err)
}
if retry > 3 {
return nil, err
}
return newMultipartRequest(url, params, retry+1)
}
req.Header.Set("content-type", fmt.Sprintf("multipart/form-data;boundary=%s", writer.Boundary()))
req.Header.Set("referer", "https://cowtransfer.com/")
addTk(req)
if runConfig.debugMode {
log.Println(req.Header)
}
resp, err := client.Do(addHeaders(req))
if err != nil {
if runConfig.debugMode {
log.Printf("do request returns error: %v", err)
}
if retry > 3 {
return nil, err
}
return newMultipartRequest(url, params, retry+1)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
if runConfig.debugMode {
log.Printf("read response returns: %v", err)
}
if retry > 3 {
return nil, err
}
return newMultipartRequest(url, params, retry+1)
}
_ = resp.Body.Close()
if runConfig.debugMode {
log.Printf("returns: %v", string(body))
}
if s := resp.Header.Values("Set-Cookie"); len(s) != 0 && runConfig.token == "" {
for _, v := range s {
ck := strings.Split(v, ";")
runConfig.token += ck[0] + ";"
}
if runConfig.debugMode {
log.Printf("cookies set to: %v", runConfig.token)
}
}
return body, nil
}