Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
orgrimarr committed Nov 16, 2020
1 parent 40e4a99 commit 77cfcbd
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 39 deletions.
47 changes: 37 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
[![Known Vulnerabilities](https://snyk.io/test/github/orgrimarr/node-wetransfert/badge.svg)](https://snyk.io/test/github/orgrimarr/node-wetransfert)

# Changelog
- 2.3.0
- Add proxy support
- 2.2.0
- Remove deprecated request-* libs and use node-fetch instead
- Fix wetransfer upload (send emails)
Expand All @@ -19,6 +21,23 @@
- Fix dependencies security issues
- Fix download (Thanks @cylwin). The upload part is still broken

# Table of content
- [Install](#Install)
- [Use custom proxy](#Use-custom-proxy)
- [Download](#Download-weTransfer-content-from-url)
- [From url](#Download-weTransfer-content-from-url)
- [From url by file ID](#Download-weTransfer-file-by-ID)
- [Pipe](#Download-weTransfer-content-from-url-pipe-response)
- [Get infos](#isValidWetransfertUrl)
- [Validate url](#isValidWetransfertUrl)
- [Get url detail](#Get-information-about-weTransfert-url)
- [Upload](#Upload)
- [Using payload wrapper](#Payload-Exemple)
- [Progress object](#Progress-object)
- [End object](#End-object)
- [Get share link](#Upload-without-email)
- [Known Bugs](#Known-Bugs)

# Install
```
npm install wetransfert --save
Expand All @@ -33,6 +52,10 @@ Tested in node 12.x
``` javascript
const { upload, download, getInfo, isValidWetransfertUrl } = require('wetransfert');
```

# Use custom proxy
- Add HTTP_PROXY or HTTPS_PROXY environement variable

# Download weTransfer content from url
### download(url, folder)
The function take a valid wetransfer url and a destination folder
Expand All @@ -58,6 +81,7 @@ download(myUrl, myDestinationFolder)
});
```


# Download weTransfer file by ID
### download(url, folder, fileIds)
- fileIds: An array of wetransfer file id
Expand All @@ -80,7 +104,8 @@ download(myUrl, myDestinationFolder, ['aaaaaaaaa'])

> /!\ If your transfer contain only one file, wetransfer does not zip the content. Be carefull when using the downloadPipe function. You can obtain all files information using the getInfo function.
# Download weTransfer content from url + pipe response (progress with callback)
# Download weTransfer content from url pipe response
- (progress with callback)
### downloadPipe(url)

This function take a valid wetransfer url. Like the classique download function, you can specify the file ids you want to download. downloadPipe(response.shortened_url, ["fileID"])
Expand All @@ -103,6 +128,16 @@ downloadPipe(response.shortened_url, null)
> /!\ If your transfer contain only one file, wetransfer does not zip the content. Be carefull when using the downloadPipe function. You can obtain all files information using the getInfo function.



# isValidWetransfertUrl

Return a NodeJS URL object if the url is valid.

If not, it return false



# Get information about weTransfert url

## Exemple
Expand Down Expand Up @@ -166,13 +201,6 @@ getInfo('myWeTransfertURL')
}
```

# isValidWetransfertUrl

Return a NodeJS URL object if the url is valid.

If not, it return false



# Upload
You can upload a total file size >= 2Gibibyte (2147483648 Byte)
Expand All @@ -183,8 +211,7 @@ upload('mailSender', ['receiverMail'], ['file1'], 'myMessage', 'ui_language', us

The upload function parameters :
- mailSender: A valid mail address of the sender
- receiverMail: An array of valid destination address
- file1: An array of valid file path you wan to transfer
- receiverMail: An array of valid destination addreEnd objectansfer
- myMessage: The message you want to send
- ui_language: The language of the wetransfer receiver. ex: en, fr
- username: Your wetransfer account username. /!\ username and mailSender email must be the same
Expand Down
9 changes: 7 additions & 2 deletions handler/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const path = require('path')
const fs = require('fs')
const stream = require('stream')
const util = require('util')
const utils = require('../utils/utils')

const streamPipeline = util.promisify(stream.pipeline)

Expand All @@ -30,7 +31,9 @@ exports.download = function (url = '', destPath = null, fileIds = null) {
? unzip.Extract({ path: destPath })
: fs.createWriteStream(path.join(destPath, weTransfertObject.content.items[0].name))

const response = await fetch(weTransfertObject.downloadURI)
const response = await fetch(weTransfertObject.downloadURI, {
agent: utils.getHttpAgent()
})
if (!response.ok) {
throw new Error(`Unexpected response ${response.status} ${response.statusText}`)
}
Expand Down Expand Up @@ -75,7 +78,9 @@ exports.downloadPipe = async function (url = '', fileIds = null, progressCallbac
}

const size = weTransfertObject.content.size
const response = await fetch(weTransfertObject.downloadURI)
const response = await fetch(weTransfertObject.downloadURI, {
agent: utils.getHttpAgent()
})
if (!response.ok) {
throw new Error(`Unexpected response ${response.status} ${response.statusText}`)
}
Expand Down
7 changes: 4 additions & 3 deletions handler/getInfo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fetch = require('node-fetch')
const debug = require('debug')("wetransfert:getinfos")

const { isValidWetransfertUrl, formatDownloadApiUri, waitAsync, getContentInfo } = require('../utils/utils')
const { isValidWetransfertUrl, formatDownloadApiUri, waitAsync, getContentInfo, getHttpAgent } = require('../utils/utils')


const getDownloadUri = async function (urlObj, sessionCookie, csrf, fileIds) {
Expand All @@ -12,12 +12,13 @@ const getDownloadUri = async function (urlObj, sessionCookie, csrf, fileIds) {
const result = await fetch(requestParams.uri, {
method: 'POST',
body: JSON.stringify(requestParams.body),
'headers': {
headers: {
'cookie': sessionCookie,
'Content-Type': 'application/json',
'x-csrf-token': csrf,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
}
},
agent: getHttpAgent()
})

if (result.status !== 200 && result.status !== 201) {
Expand Down
6 changes: 4 additions & 2 deletions handler/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ class Upload extends EventEmitter {
'x-csrf-token': this.csrfToken,
'Accept-Language': 'fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
}
},
agent: utils.getHttpAgent()
}
}
async apiRequest(method, url, body) {
Expand Down Expand Up @@ -513,7 +514,8 @@ class Upload extends EventEmitter {
'Accept': 'application/json'
},
form.getHeaders()
)
),
agent: utils.getHttpAgent()
}

const response = await fetch(endpoint, options)
Expand Down
62 changes: 52 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wetransfert",
"version": "2.2.0",
"version": "2.3.0",
"description": "Download/Upload wetransfer content with nodeJS - Unofficial API for wetransfer",
"keywords": [
"file transfer",
Expand Down Expand Up @@ -30,6 +30,7 @@
"cheerio": "^1.0.0-rc.3",
"debug": "^4.2.0",
"form-data": "^3.0.0",
"https-proxy-agent": "^5.0.0",
"node-fetch": "^2.6.1",
"p-map": "^1.2.0",
"snyk": "^1.426.0",
Expand Down
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ const testUploadLink = function(){
// testDownload()
// testDownloadPipe()
// testUploadLink()
testUpload().then(console.log).catch(console.error)
// testUpload().then(console.log).catch(console.error)

// getInfo("https://we.tl/t-BUr6nd2DAP")
// .then(response => {
// console.log(JSON.stringify(response, null, 2))
// })
// .catch(console.error)
getInfo("https://we.tl/t-BUr6nd2DAP")
.then(response => {
console.log(JSON.stringify(response, null, 2))
})
.catch(console.error)
24 changes: 19 additions & 5 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const urlUtils = require('url')
const fetch = require('node-fetch')
const debug = require('debug')('wetransfert:utils')
const cheerio = require('cheerio')
const HttpsProxyAgent = require('https-proxy-agent')
const https = require('https')

const wetransferEndpoint = "https://wetransfer.com/"
const apiVersion = "v4"
Expand All @@ -16,6 +18,14 @@ const _preloaded_transfer_Regex = /\_preloaded\_transfer\_/g
const removeVarDeclarationRegex = /var[\s]*\_preloaded\_transfer\_[\s]*=/g
const removeLastSemicolon = /(}\;\n)$/g

const getHttpAgent = function(){
const proxy = process.env.http_proxy || process.env.HTTP_PROXY || process.env.https_proxy || process.env.HTTPS_PROXY
if(proxy){
return new HttpsProxyAgent(proxy)
}
return https.globalAgent
}
exports.getHttpAgent = getHttpAgent

const extractVar = async function (text) {
const json = text.replace(removeVarDeclarationRegex, '').replace(removeLastSemicolon, '}')
Expand All @@ -39,7 +49,8 @@ const expandUrl = async function(shortUrl) {
const result = await fetch(shortUrl, {
method: "HEAD",
redirect: 'follow',
follow: 20
follow: 20,
agent: getHttpAgent()
})
const longUrl = result.url
debug(`expandUrl: "${shortUrl}" => "${longUrl}"`)
Expand Down Expand Up @@ -138,7 +149,9 @@ const getWetransferPageContent = async function(endpoint = wetransferEndpoint, c
endpoint = urlUtils.format(endpoint)
}

const options = {}
const options = {
agent: getHttpAgent()
}
if(cookies){
options.headers = {
'cookie': cookies,
Expand Down Expand Up @@ -189,12 +202,13 @@ exports.login = async function(user, password){
"password": password,
"remember_me": false
}),
'headers': {
headers: {
'cookie': loginSessionInfos.sessionCookie,
'Content-Type': 'application/json',
'x-csrf-token': loginSessionInfos.csrf,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'
}
},
agent: getHttpAgent()
})
if (!result.ok) {
const text = await result.text()
Expand All @@ -212,4 +226,4 @@ exports.login = async function(user, password){
csrf: infos.csrf,
data: data
}
}
}

0 comments on commit 77cfcbd

Please sign in to comment.