Skip to content

Commit

Permalink
fix: remove limit on blockSize(#53), format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Dec 25, 2021
1 parent fbb7719 commit c06b27f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 186 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func main() {
printUsage()
return
}
if runConfig.blockSize > 4194304 {
runConfig.blockSize = 524288
}
// if runConfig.blockSize > 4194304 {
// runConfig.blockSize = 524288
// }

var f []string
for _, v := range files {
Expand Down
170 changes: 170 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
183 changes: 0 additions & 183 deletions uploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ package main

import (
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/cheggaaa/pb/v3"
cmap "github.com/orcaman/concurrent-map"
Expand Down Expand Up @@ -229,48 +221,6 @@ func uploader(ch *chan *uploadPart, conf uploadConfig) {

}

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 hashBlock(buf []byte) string {
return fmt.Sprintf("%x", md5.Sum(buf))
}

func urlSafeEncode(enc string) string {
r := base64.StdEncoding.EncodeToString([]byte(enc))
r = strings.ReplaceAll(r, "+", "-")
r = strings.ReplaceAll(r, "/", "_")
return r
}

func finishUpload(config *initResp, info os.FileInfo, hashMap *cmap.ConcurrentMap, limit int64) error {
if runConfig.debugMode {
log.Println("finishing upload...")
Expand Down Expand Up @@ -435,136 +385,3 @@ func getUploadConfig(info os.FileInfo, config *prepareSendResp) (*initResp, erro
// return config, nil
return initResp, 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 getFileInfo(path string) (os.FileInfo, error) {
info, err := os.Stat(path)
if err != nil {
return nil, err
}
return info, nil
}

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 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
}
Loading

0 comments on commit c06b27f

Please sign in to comment.