个人博客地址,更多精彩内容
开启http | https服务
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 key = fs.readFileSync(`${__dirname}/ssl/test.com.key`, 'utf-8')
var cert = fs.readFileSync(`${__dirname}/ssl/test.com.pem`, 'utf-8')
var options = {
key: key,
cert: cert
};
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]);
var address = buf[0] === 22 ? httpsPort : httpPort;
var proxy = net.createConnection(address, function() {
proxy.write(buf);
socket.pipe(proxy).pipe(socket);
});
proxy.on('error', function(err) {
console.log(err);
});
});
socket.on('error', function(err) {
console.log(err);
});
}).listen(3344);
个人博客地址,更多精彩内容