Skip to content

Commit

Permalink
v1.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuadrix committed Feb 14, 2020
1 parent 7650c67 commit 7b6a003
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 89 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const useProxy = require('puppeteer-page-proxy');
await page2.goto(site);
})();
```
To remove a proxy set this way, simply pass a falsy value (e.g `null`) instead of the proxy;
```js
await useProxy(page, null);
```

#### Proxy per request:
```js
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Change log
### [1.2.3] - 2020-02-14
#### Changes
- Added ability to remove page-wide proxy
- Changed static classes to object literals for compability with Node.js **10.16.x** ([#6](https://github.com/Cuadrix/puppeteer-page-proxy/issues/6))
- Removed `src\util\` folder along with proxy-validator
### [1.2.2] - 2020-02-09
#### Patches
- Fixed code indentation.
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "puppeteer-page-proxy",
"description": "Additional Node.js module to use with 'puppeteer' for setting proxies per page basis.",
"version": "1.2.2",
"version": "1.2.3",
"author": "Cuadrix <[email protected]> (https://github.com/Cuadrix)",
"homepage": "https://github.com/Cuadrix/puppeteer-page-proxy",
"main": "./src/index.js",
Expand All @@ -22,10 +22,11 @@
],
"license": "MIT",
"dependencies": {
"got": "^10.5.2",
"got": "^10.5.5",
"http-proxy-agent": "^4.0.1",
"https-proxy-agent": "^5.0.0",
"socks-proxy-agent": "^5.0.0",
"tough-cookie": "^3.0.1"
"tough-cookie": "^3.0.1",
"type-dragoon": "^1.0.0"
}
}
11 changes: 5 additions & 6 deletions src/core/lookup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const enforceTypes = require("../util/type-enforcer");
const enforceTypes = require("type-dragoon");

module.exports = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => {
const lookup = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => {
/**/
enforceTypes(
[page, "object"], [lookupService, "string"], [isJSON, "boolean"], [timeout, "number"]
);
enforceTypes({object: page}, {string: lookupService}, {boolean: isJSON}, {number: timeout});
/**/
const XMLHttpRequest = async () => {
return await page.evaluate((lookupService, timeout, isJSON) => {
Expand Down Expand Up @@ -41,4 +39,5 @@ module.exports = async (page, lookupService = "https://api.ipify.org?format=json
return await XMLHttpRequest();
}
}
}
};
module.exports = lookup;
39 changes: 21 additions & 18 deletions src/core/page-proxy.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
const {setHeaders, setAgent, request} = require("../lib/request");
const cookies = require("../lib/cookies");
const enforceTypes = require("../util/type-enforcer");
const validateProxy = require("../util/proxy-validator");
const enforceTypes = require("type-dragoon");

module.exports = async (param, proxy) => {
const pageProxy = async (param, proxy) => {
/**/
enforceTypes(
[param, "object"], [proxy, "string"]
); validateProxy(proxy);
enforceTypes({object: param});
/**/
let page, req;
if (param.constructor.name === "Request") {
req = param;
}
else if (param.constructor.name === "Page") {
} else if (param.constructor.name === "Page") {
page = param;
await page.setRequestInterception(true);
} else {
throw new Error("@arg1: Not valid 'Page' or 'Request' object");
throw new Error("Not valid `Page` or `Request` object");
}
const $puppeteerPageProxyHandler = async req => {
if (req._interceptionHandled || !req._allowInterception) {
return;
}
const cookieJar = cookies.store(await cookies.get(
req._client._connection._url, req._frame._id
));
Expand All @@ -43,13 +36,23 @@ module.exports = async (param, proxy) => {
await req.abort();
}
};
const removeRequestListener = () => {
const listeners = page.listeners("request");
for (let i = 0; i < listeners.length; i++) {
if (listeners[i].name === "$puppeteerPageProxyHandler") {
page.removeListener("request", listeners[i]);
}
}
};
if (req) {
$puppeteerPageProxyHandler(req);
} else {
for (const listener of page.listeners("request")) {
if (listener.name === "$puppeteerPageProxyHandler") {
page.removeListener("request", listener);
}
}; page.on("request", $puppeteerPageProxyHandler);
removeRequestListener();
if (proxy) {
page.on("request", $puppeteerPageProxyHandler);
} else {
await page.setRequestInterception(false);
}
}
}
};
module.exports = pageProxy;
25 changes: 13 additions & 12 deletions src/lib/cdp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = class CDP {
static async Send(ws, command) {
const cdp = {
async _send(ws, command) {
ws.send(JSON.stringify(command));
return new Promise(resolve => {
ws.on("message", function handler(msg) {
Expand All @@ -10,10 +10,10 @@ module.exports = class CDP {
}
});
});
}
static Target = {
attachToTarget: async (ws, targetId) => {
const result = (await this.Send(ws, {
},
Target: {
async attachToTarget(ws, targetId) {
const result = (await cdp._send(ws, {
id: 1,
method: "Target.attachToTarget",
params: {
Expand All @@ -25,10 +25,10 @@ module.exports = class CDP {
return result.sessionId;
}
}
};
static Network = {
getCookies: async (ws, sessionId) => {
const result = (await this.Send(ws, {
},
Network: {
async getCookies(ws, sessionId) {
const result = (await cdp._send(ws, {
sessionId,
id: 2,
method: "Network.getCookies"
Expand All @@ -37,5 +37,6 @@ module.exports = class CDP {
return result.cookies;
}
}
};
}
}
};
module.exports = cdp;
15 changes: 8 additions & 7 deletions src/lib/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const WebSocket = require("ws");
const {CookieJar} = require("tough-cookie");
const {Target, Network} = require("./cdp");

module.exports = class Cookies {
static async get(endpoint, targetId) {
const cookies = {
async get(endpoint, targetId) {
const ws = new WebSocket(endpoint, {
perMessageDeflate: false,
maxPayload: 180 * 4096 // 0.73728Mb
Expand All @@ -12,8 +12,8 @@ module.exports = class Cookies {
/* Attach to target then get cookies */
const sessionId = await Target.attachToTarget(ws, targetId);
return await Network.getCookies(ws, sessionId);
};
static store(cookies) {
},
store(cookies) {
if (!cookies) {
return;
}
Expand All @@ -32,9 +32,10 @@ module.exports = class Cookies {
httpOnly: cookie.httpOnly,
sameSite: cookie.sameSite,
creation: new Date().toISOString(),
hostOnly: !cookie.domain.match(/^\./)
hostOnly: !(/^\./).test(cookie.domain)
};
})
});
};
};
}
};
module.exports = cookies;
17 changes: 9 additions & 8 deletions src/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const HttpProxyAgent = require("http-proxy-agent");
const HttpsProxyAgent = require("https-proxy-agent");
const SocksProxyAgent = require("socks-proxy-agent");

module.exports = class RequestCore {
static setHeaders(req) {
const request = {
setHeaders(req) {
const headers = {
...req.headers(),
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
Expand All @@ -20,8 +20,8 @@ module.exports = class RequestCore {
headers["sec-fetch-site"] = "same-origin";
}
return headers;
}
static setAgent(url, proxy) {
},
setAgent(url, proxy) {
if (proxy.startsWith("socks")) {
return new SocksProxyAgent(proxy);
}
Expand All @@ -30,17 +30,18 @@ module.exports = class RequestCore {
} else {
return new HttpProxyAgent(proxy);
}
}
static async request(url, options) {
},
async request(url, options) {
try {
const res = await got(url, options);
return {
status: res.statusCode,
headers: res.headers,
body: res.body,
};
} catch (error) {
} catch(error) {
throw new Error(error);
}
}
}
};
module.exports = request;
11 changes: 0 additions & 11 deletions src/util/proxy-validator.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/util/type-enforcer.js

This file was deleted.

0 comments on commit 7b6a003

Please sign in to comment.