Skip to content

Commit

Permalink
fix wetransfer(#30 #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikubill committed Mar 31, 2021
1 parent 4fb00b7 commit ce5804d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
61 changes: 45 additions & 16 deletions apis/public/wetransfer/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
)

var (
signDownload string
regexShorten = regexp.MustCompile("(https://)?we\\.tl/[a-zA-Z0-9\\-]{12}")
regex = regexp.MustCompile("(https://)?wetransfer\\.com/downloads/[a-z0-9]{46}/[a-z0-9]{6}")
regexList = regexp.MustCompile("{\"id.*}]}")
signDownload, safetyHash, blockID string
regexShorten = regexp.MustCompile("(https://)?we\\.tl/[a-zA-Z0-9\\-]{12}")
// regex2 = regexp.MustCompile("https://wetransfer.com/api/v4/transfers/1d0e80d8e4c05baddb6d0817acefaf9020210331043828/prepare-download")
regex = regexp.MustCompile("https?://wetransfer\\.com/downloads/([a-z0-9]{46})/([a-z0-9]{6})")
// regexList = regexp.MustCompile("{\"id.*}]}")
)

func (b weTransfer) LinkMatcher(v string) bool {
Expand All @@ -39,13 +40,28 @@ func (b weTransfer) download(v string, config apis.DownConfig) error {
fmt.Printf("fetching ticket..")
end := utils.DotTicker()

if !regexShorten.MatchString(v) && !regex.MatchString(v) {
if regexShorten.MatchString(v) {

req, err := http.NewRequest("HEAD", v, nil)
if err != nil {
return err
}
resp, err := http.DefaultTransport.RoundTrip(req)
v = resp.Header.Get("Location")
}

tk0 := regex.FindStringSubmatch(v)
if len(tk0) < 3 || !regex.MatchString(v) {
return fmt.Errorf("url is invaild")
}
blockID = string(tk0[1])
safetyHash = string(tk0[2])

if config.DebugMode {
log.Println("step 1/2 metadata")
log.Printf("link: %+v", v)
}

resp, err := client.Get(v)
if err != nil {
return err
Expand All @@ -60,6 +76,7 @@ func (b weTransfer) download(v string, config apis.DownConfig) error {
if len(tk) == 0 {
return fmt.Errorf("no csrf-token found")
}

ticket := requestTicket{
token: string(tk[1]),
cookies: "",
Expand All @@ -73,24 +90,36 @@ func (b weTransfer) download(v string, config apis.DownConfig) error {
log.Printf("ticket: %+v", ticket)
}
_ = resp.Body.Close()
dat := regexList.FindString(string(body))

signPreDownload := fmt.Sprintf("https://wetransfer.com/api/v4/transfers/%s/prepare-download", blockID)
data, _ := json.Marshal(map[string]interface{}{
"security_hash": safetyHash,
})
if config.DebugMode {
log.Printf("dst: %+v", dat)
}
var block configBlock
if err := json.Unmarshal([]byte(dat), &block); err != nil {
return err
log.Printf("tk: %+v", tk)
}
signDownload = fmt.Sprintf("https://wetransfer.com/api/v4/transfers/%s/download", block.ID)
resp0, err := newRequest(signPreDownload, string(data), requestConfig{
action: "POST",
debug: config.DebugMode,
retry: 0,
timeout: time.Duration(b.Config.interval) * time.Second,
modifier: addToken(ticket),
})

// var block configBlock
// if err := json.Unmarshal([]byte(resp0), &block); err != nil {
// return err
// }
signDownload = fmt.Sprintf("https://wetransfer.com/api/v4/transfers/%s/download", resp0.ID)

*end <- struct{}{}
fmt.Printf("ok\n")
if block.State != "downloadable" {
return fmt.Errorf("link state is not downloadable (state: %s)", block.State)
if resp0.State != "downloadable" {
return fmt.Errorf("link state is not downloadable (state: %s)", resp0.State)
}

for _, item := range block.Item {
config.Ticket = block.Hash
for _, item := range resp0.Item {
config.Ticket = resp0.Hash
err = b.downloadItem(item, ticket, config)
if err != nil {
fmt.Println(err)
Expand Down
11 changes: 8 additions & 3 deletions apis/public/wetransfer/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/cheggaaa/pb/v3"
"io"
"io/ioutil"
"log"
Expand All @@ -17,6 +16,8 @@ import (
"time"
"transfer/apis"
"transfer/utils"

"github.com/cheggaaa/pb/v3"
)

const (
Expand All @@ -29,7 +30,7 @@ const (
chunkSize = 15728640
)

var tokenRegex = regexp.MustCompile("csrf-token\" content=\"([a-zA-Z0-9+=/]{88})\"")
var tokenRegex = regexp.MustCompile("csrf-token\"\\scontent=\"([a-zA-Z0-9_=-]+)\"\\s/>?")

func (b *weTransfer) InitUpload(files []string, sizes []int64) error {
if b.Config.singleMode {
Expand Down Expand Up @@ -277,6 +278,9 @@ func (b *weTransfer) getTicket() (requestTicket, error) {
_ = resp.Body.Close()
tk := tokenRegex.FindSubmatch(body)
if apis.DebugMode {
// parsedBody := strings.TrimSpace(string(body))
// parsedBody = strings.Trim(parsedBody, "\n")
// log.Println("returns: ", parsedBody)
log.Println("returns: ", string(tk[0]), string(tk[1]))
}
if len(tk) == 0 {
Expand Down Expand Up @@ -404,6 +408,7 @@ func newRequest(link string, postBody string, config requestConfig) (*configBloc
func addToken(token requestTicket) func(req *http.Request) {
return func(req *http.Request) {
addHeaders(req)
req.Header.Set("x-requested-with", "XMLHttpRequest")
req.Header.Set("x-csrf-token", token.token)
req.Header.Set("cookie", token.cookies)
}
Expand All @@ -412,6 +417,6 @@ func addToken(token requestTicket) func(req *http.Request) {
func addHeaders(req *http.Request) {
req.Header.Set("Referer", "https://wetransfer.com/")
req.Header.Set("content-type", "application/json;charset=UTF-8")
req.Header.Set("User-Agent", "Chrome/80.0.3987.149 Wenshushu-Uploader")
req.Header.Set("User-Agent", "Chrome/80.0.3987.149 Wetransfer-Uploader")
req.Header.Set("Origin", "https://wetransfer.com/")
}

0 comments on commit ce5804d

Please sign in to comment.