forked from stableproxy/puppeteer-page-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,61 @@ | ||
const lookup = async (page, lookupService = "https://api64.ipify.org?format=json", isJSON = true, timeout = 30000) => { | ||
const defaults = { | ||
url: "https://api64.ipify.org?format=json", | ||
json: true, | ||
timeout: 30000 | ||
}; | ||
|
||
const onLookupFail = (message) => {console.error(message)} | ||
const isOnLookupFailExposed = new WeakMap(); | ||
|
||
const lookup = async (page, lookupServiceUrl = defaults.url, isJSON = defaults.json, timeout = defaults.timeout) => { | ||
const doLookup = async () => { | ||
return await page.evaluate((lookupService, timeout, isJSON) => { | ||
// Wait for network to be idle before evaluating code in page context | ||
await page.waitForNetworkIdle(); | ||
return await page.evaluate((pageUrl, lookupServiceUrl, timeout, isJSON) => { | ||
return new Promise((resolve) => { | ||
const request = new XMLHttpRequest(); | ||
request.timeout = timeout; | ||
request.onload = () => { | ||
if (request.status >= 200 && request.status <= 299) { | ||
resolve(isJSON ? JSON.parse(request.responseText) : request.responseText); | ||
} else {resolve(onLookupFailed( | ||
`Request from ${window.location.href} to ` + | ||
`${lookupService} failed with status code ${request.status}` | ||
))} | ||
} else { | ||
// Print message to browser and NodeJS console | ||
const failMessage = | ||
`Lookup request from ${pageUrl} to ${lookupServiceUrl} ` + | ||
`failed with status code ${request.status}`; | ||
console.error(failMessage); | ||
$ppp_onLookupFail(failMessage); | ||
resolve(); | ||
} | ||
}; | ||
request.ontimeout = () => { | ||
// Print message to browser and NodeJS console | ||
const timeOutMessage = | ||
`Lookup request from ${pageUrl} to ${lookupServiceUrl} ` + | ||
`timed out at ${request.timeout} ms`; | ||
console.error(timeOutMessage); | ||
$ppp_onLookupFail(timeOutMessage); | ||
resolve(); | ||
}; | ||
request.ontimeout = (error) => {resolve(onLookupFailed( | ||
`Request from ${window.location.href} to ` + | ||
`${lookupService} timed out at ${request.timeout} ms` | ||
))}; | ||
request.open("GET", lookupService, true); | ||
request.open("GET", lookupServiceUrl, true); | ||
request.send(); | ||
}); | ||
}, lookupService, timeout, isJSON); | ||
}, page.url(), lookupServiceUrl, timeout, isJSON); | ||
}; | ||
try { | ||
await page.setBypassCSP(true); | ||
const functionName = "$ppp_on_lookup_failed"; | ||
if (!page._pageBindings.has(functionName)) { | ||
await page.exposeFunction(functionName, (failReason) => { | ||
console.error(failReason); return; | ||
}); | ||
// Expose function to log error on NodeJS side | ||
// Deal with already exposed error by explicitly keeping track of function exposure | ||
if (!isOnLookupFailExposed.get(page)) { | ||
await page.exposeFunction("$ppp_onLookupFail", onLookupFail); | ||
isOnLookupFailExposed.set(page, true); | ||
} | ||
// Stop keeping track of exposure if page is closed | ||
if (page.isClosed()) { | ||
isOnLookupFailExposed.delete(page); | ||
} | ||
await page.setBypassCSP(true); | ||
return await doLookup(); | ||
} catch(error) {console.error(error)} | ||
} catch(error) {console.log(error)} | ||
}; | ||
|
||
module.exports = lookup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ class CDP { | |
} | ||
} | ||
|
||
module.exports = CDP; | ||
module.exports = CDP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters