标签:
var net = require(‘net‘); var http = require(‘http‘); var https = require(‘https‘); var fs = require(‘fs‘); var httpPort = 3345; var httpsPort = 3346; var server = http.createServer(function(req, res){ res.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); res.end(‘hello world!‘); }).listen(httpPort); var options = { key: fs.readFileSync(‘./cakey.pem‘), cert: fs.readFileSync(‘./cacert.pem‘) }; var sserver = https.createServer(options, function(req, res){ res.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); res.end(‘secured hello world‘); }).listen(httpsPort); net.createServer(function(socket){ socket.once(‘data‘, function(buf){ console.log(buf[0]); // https数据流的第一位是十六进制“16”,转换成十进制就是22 var address = buf[0] === 22 ? httpsPort : httpPort; //创建一个指向https或http服务器的链接 var proxy = net.createConnection(address, function() { proxy.write(buf); //反向代理的过程,tcp接受的数据交给代理链接,代理链接服务器端返回数据交由socket返回给客户端 socket.pipe(proxy).pipe(socket); }); proxy.on(‘error‘, function(err) { console.log(err); }); }); socket.on(‘error‘, function(err) { console.log(err); }); }).listen(3344);
摘录来源:http://www.cnblogs.com/dojo-lzz/p/5479870.html
标签:
原文地址:http://www.cnblogs.com/xuhang/p/5496604.html