Skip to content

Commit

Permalink
design phase
Browse files Browse the repository at this point in the history
  • Loading branch information
evyatarmeged committed Oct 1, 2018
1 parent 3a3a3a5 commit 3f27751
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
26 changes: 10 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ const tough = require('tough-cookie');
const Cookie = tough.Cookie;


// Add cookie persistence - too bad for the hacky solution
// Adding cookie persistence to axios. Too bad for the hacky solution tbh
let cookieJar = new tough.CookieJar();
//

function setCookieJar(cookieJar) {
axios.interceptors.request.use(function (config) {
cookieJar.getCookies(config.url, function(err, cookies) {
config.headers.cookie = cookies.join('; ');
});
return config;
});

axios.interceptors.response.use(function (response) {
if (response.headers['set-cookie'] instanceof Array) {
cookies = response.headers['set-cookie'].forEach(function (c) {
Expand Down Expand Up @@ -45,7 +45,7 @@ function extractTimeoutFromHTML(htmlResponse) {
return match;
}

function isJSChallengeInRes(htmlResponse) {
function javascriptChallengeInResponse(htmlResponse) {
return htmlResponse.indexOf("jschl") > -1 && htmlResponse.indexOf("DDoS protection by Cloudflare") > -1
}

Expand All @@ -56,7 +56,7 @@ function getRequestHeaders(url, userAgent) {
headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
headers["Accept-Encoding"] = "gzip, deflate, br"
headers["User-Agent"] = userAgent

return headers
}

Expand All @@ -69,8 +69,6 @@ async function sendRequest(url, method=null, headers=null, userAgent=null) {
url: url,
headers: headers || getRequestHeaders(url, userAgent),
jar: cookieJar,
withCredentials: true,
maxRedirects: 5
});
} catch (err) {
// HTTP status of 503 means the request/session might have been flagged
Expand All @@ -89,26 +87,22 @@ async function sendRequest(url, method=null, headers=null, userAgent=null) {
// console.log(`no JS challenge in sight. Got ${res.code} HTTP status code`)
// })
// .catch(err => {
// if (err.response.status === 503 && isJSChallengeInRes(err.response.data)) {
// if (err.response.status === 503 && javascriptChallengeInResponse(err.response.data)) {
// console.log(`JS challenged detected for ${url}. Trying to solve`)
// solveJSChallenge()
// }
// })
// }

//

/*
Course of action #1: getPageAndSolve() - Facade of everything
Course of action #2: get Headers --> getPage --> isJSChallengeInRes(res) --> solveJSChallenge()
Finally ----> return Promise of (session & solved challenge) cookies or raise Error ;-)
* */

setCookieJar(cookieJar)
sendRequest("http://google.com")
.then(res => {
console.log(res.config.jar)
console.log(res)
console.log(cookieJar)
})
.catch(err => {
console.error(err)
})
// TODO: Add Incapsula ?
// TODO: Add Incapsula ?
28 changes: 27 additions & 1 deletion 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
Expand Up @@ -10,11 +10,12 @@
"license": "ISC",
"dependencies": {
"axios": "^0.18.0",
"axios-cookiejar-support": "^0.4.2",
"cloudscraper": "^1.5.0",
"html": "^1.0.0",
"jsdom": "^12.0.0",
"socks-proxy-agent": "^4.0.1",
"tough-cookie": "latest",
"tough-cookie": "^2.4.3",
"url-parse": "^1.4.3"
}
}

0 comments on commit 3f27751

Please sign in to comment.