日期:2014-05-16  浏览次数:20364 次

nodejs写的sslproxy捕获ssl明文数据
var net = require('net');

var sslproxyhost="192.168.7.19";//被代理的服务的IP
var sslproxyport=5004;//被代理的端口
var ssllistenport=5004;//代理端口

var tls = require('tls');
var fs = require('fs');

var options = {
  key: fs.readFileSync('ryans-key.pem'),
  cert: fs.readFileSync('ryans-cert.pem'),
};

var sslserver = tls.createServer(options, function(cleartextStream) {
	console.log('client connected',
			  cleartextStream.authorized ? 'authorized' : 'unauthorized');
	console.log('client disconnected');
	//cleartextStream.setEncoding('ascii');
	cleartextStream.on('data', function(data) {
		console.log('client send:');
		console.log(data);	
		console.log('' + data);
	});
	cleartextStream.on('end', function() {
		console.log('client disconnected');
	  //server.close();
	});
  
	var c_cleartextStream = tls.connect(sslproxyport,sslproxyhost, {}, function() {
	  console.log('server connected',
				  c_cleartextStream.authorized ? 'authorized' : 'unauthorized');
	});

	//c_cleartextStream.setEncoding('ascii');
	c_cleartextStream.on('data', function(data) {
		console.log('server recv:');
		console.log(data);
		buf = new Buffer(data.length);
		for (var i = 0; i < data.length ; i++) {
		 // buf[i] = data.charCodeAt(i);
		  buf[i]=data[i]&0x7f;
		}
		console.log("server recv2(cut even):");
		console.log(buf);
		console.log('' + buf);
	});

	c_cleartextStream.on('end', function() {
		console.log('server disconnected');
		//sslserver.close();
	});

	cleartextStream.pipe(c_cleartextStream);
	c_cleartextStream.pipe(cleartextStream);
//
});

sslserver.listen(ssllistenport, function() {
  console.log('server bound');
});