Last active
December 11, 2020 03:35
-
-
Save jerryan999/e1187d68d928152904ca948f25c758e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification. | |
const puppeteer = require("puppeteer"); | |
const crypto = require('crypto'); | |
const { proxyRequest } = require('puppeteer-proxy'); | |
// xun proxy | |
let orderno = 'ZF2020121058569meja6'; | |
let secret = ''; | |
// 代理服务器 | |
function generate_sign(orderno, secret, timestamp) { | |
let plantext = 'orderno=' + orderno + ',secret=' + secret + ',timestamp=' + timestamp; | |
let md5 = crypto.createHash('md5'); | |
md5.update(plantext); | |
let sign = md5.digest('hex'); | |
sign = sign.toUpperCase(); | |
return sign | |
} | |
async function get_page(url) { | |
const browser = await puppeteer.launch({ | |
headless: false, | |
ignoreHTTPSErrors: true, | |
// args: ['--proxy-server=forward.xdaili.cn:80'] | |
}); | |
const page = await browser.newPage(); | |
// construct proxy-authorization | |
let timestamp = parseInt(new Date().getTime() / 1000); | |
let sign = generate_sign(orderno, secret, timestamp) | |
var sign_str = 'sign=' + sign + '&orderno=' + orderno + "×tamp=" + timestamp | |
// 1. Enable request/ response interception | |
await page.setRequestInterception(true); | |
page.on('request', async(request) => { | |
const headers = request.headers(); | |
headers['Proxy-Authorization'] = sign_str; | |
// request.continue({ headers }); | |
console.log(headers) | |
await proxyRequest({ | |
page, | |
proxyUrl: 'http://forward.xdaili.cn:80', | |
request, | |
}); | |
}); | |
// page.once('request', async (request) => { | |
// await pageProxy.proxyRequest({ | |
// request, | |
// proxyUrl: 'http://forward.xdaili.cn:80', | |
// }); | |
// }); | |
// 2. Intercept request | |
// page.once('request', async (request) => { | |
// // 3. Make request using Node.js | |
// const response = await got(request.url(), { | |
// // HTTP proxy. | |
// agent: new HttpProxyAgent('http://forward.xdaili.cn:80'), | |
// body: request.postData(), | |
// headers: request.headers(), | |
// method: request.method(), | |
// retry: 0, | |
// throwHttpErrors: false, | |
// }); | |
// // 4. Return response to Chrome | |
// await request.respond({ | |
// body: response.body, | |
// headers: response.headers, | |
// status: response.statusCode, | |
// }); | |
// }); | |
// await page.setExtraHTTPHeaders({ | |
// 'Proxy-Authorization':sign_str, | |
// }) | |
try { | |
await page.goto(url) | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
get_page("http://test.abuyun.com/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment