0

現在利用可能なNodejsの最後のバージョン(v0.10.2)で「2つの証明書を使用してhttpsからhttpをプロキシする」ことを試みてきましたが、できませんでした。

私はこれを1つの証明書で行いましたが、うまくいきます:

var https = require("https"),
path = require("path"),
http = require("http"),
fs = require("fs"),
httpProxy = require("http-proxy"),
crypto = require("crypto");

//
// Proxy options
//
var options = {
https: {
  key: fs.readFileSync('ryans-key.pem'),
  cert: fs.readFileSync('ryans-cert.pem')
},
hostnameOnly: true,
router: {
  'foobar.com': '127.0.0.1:8005',
}
};

//
// Create a standalone HTTPS proxy server
//
httpProxy.createServer(options).listen(8002);

//
// Create the target HTTPS server
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('hello https\n');
  res.end();
}).listen(8005);

それをテストする それは完全に動作します:

curl -k https://foobar.com:8002 こんにちは https

ただし、2 つの仮想ホストと 2 つの異なる証明書を使用する必要がある場合に何かをしようとしましたが、それを機能させることができませんでした (これは私が試した構文の例です)。

var https = require("https"),
path = require("path"),
http = require("http"),
httpProxy = require("http-proxy"),
fs = require("fs"),
crypto = require("crypto");

//
// generic function to load the credentials context from disk
//
function getCredentialsContext () {
return crypto.createCredentials({
  key: fs.readFileSync('ryans-key.pem'),
  cert: fs.readFileSync('ryans-cert.pem')
}).context;
}

//
// A certificate per domain hash
//
var certs = {
  "foobar.com": getCredentialsContext(),
};

//
// Proxy options
//
var options = {
https: {
  SNICallback: function (hostname) {
  return certs[hostname];
}
},
hostnameOnly: true,
router: {
'foobar.com': '127.0.0.1:8005',
}
};

//
// Create a standalone HTTPS proxy server
//
httpProxy.createServer(options).listen(8002);

//
// Create the target HTTPS server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello https\n');
res.end();
}).listen(8005);

これらは、この最後の例のログです:

tls.js:1046
throw new Error('Missing PFX or certificate + private key.');
^
Error: Missing PFX or certificate + private key.
at Server (tls.js:1046:11)
at new Server (https.js:35:14)
at Object.exports.createServer (https.js:54:10)
at Object.exports.createServer (node_modules/http-proxy/lib/node-http-proxy.js:178:13)
at Object. (nodejs_test/ssl_test2.js:43:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

キーを認識していないようです。関数 crypto.createCredentials が原因でしょうか?

nodejs のドキュメントに記載されているように、キーと証明書を作成しました: http://nodejs.org/api/tls.html :

   openssl genrsa -out ryans-key.pem 1024
   openssl req -new -key ryans-key.pem -out ryans-csr.pem
   openssl x509 -req -in ryans-csr.pem -signkey ryans-key.pem -out ryans-cert.pem

誰でも私を助けることができますか?

前もって感謝します

4

1 に答える 1

2

サーバーが使用するデフォルトの証明書とキーがありません。通常の https プロキシの場合と同様に、オプション オブジェクトに証明書とキーのプロパティを追加する必要があります。

var options = {
    https: {
        SNICallback: function (hostname) {
            return certs[hostname];
        },
        key: fs.readFileSync('ryans-key.pem'),
        cert: fs.readFileSync('ryans-cert.pem'),
    },
    hostnameOnly: true,
    router: {
        'foobar.com': '127.0.0.1:8005',
    }
};
于 2013-07-19T15:15:52.927 に答える