我需要设置一个 https 代理服务器(安全 Web 代理-http://dev.chromium.org/developers/design-documents/secure-web-proxy http://dev.chromium.org/developers/design-documents/secure-web-proxy)使用代理环境测试我的应用程序之一。
遵循的步骤:
1.借助http-proxy创建https代理服务器。代码,
var https = require('https'),
http = require('http'),
util = require('util'),
fs = require('fs'),
path = require('path'),
colors = require('colors'),
httpProxy = require('http-proxy'),
// fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures'),
httpsOpts = {
key: fs.readFileSync('server.key', 'utf8'),
cert: fs.readFileSync('server.crt', 'utf8')
};
//
// Create the target HTTPS server
//
https.createServer(httpsOpts, function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello https\n');
res.end();
}).listen(9010);
//
// Create the proxy server listening on port 8010
//
httpProxy.createServer({
ssl: httpsOpts,
target: 'https://localhost:9010',
secure: false
}).listen(8010);
console.log('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8010'.yellow);
console.log('https server '.blue + 'started '.green.bold + 'on port '.blue + '9010 '.yellow);
- 创建包含 https 代理服务器详细信息的 PAC 文件
function FindProxyForURL(url, host) {
return "HTTPS 127.0.0.1:8010";
}
- 并将我的应用程序中的 pac 文件指向使用代理环境测试应用程序,示例代码粘贴在此处。
var url = require('url');
var https = require('https');
var PacProxyAgent = require('pac-proxy-agent');
// URI to a PAC proxy file to use (the "pac+" prefix is stripped)
var proxy = 'pac+http://localhost:123/hosted/pac.pac';
//console.log('using PAC proxy proxy file at %j', proxy);
// HTTP endpoint for the proxy to connect to
var endpoint = 'https://nodejs.org/api/';
//console.log('attempting to GET %j', endpoint);
var opts = url.parse(endpoint);
// create an instance of the `PacProxyAgent` class with the PAC file location
var agent = new PacProxyAgent(proxy,opts);
opts.agent = agent;
https.get(opts, function (res) {
console.log('"response" event!', res.headers);
res.pipe(process.stdout);
});
但它不起作用,它会抛出错误
Chrome 行为:
I configured the PAC file in windows proxy settings
之后我没有收到任何网站的任何回复
例如:当我进入时https://gmail.com/ https://gmail.com/最终结果是
有什么建议么?