Created
December 13, 2016 13:56
-
-
Save zdying/5830007c920105c1ed4ee5e01c052c7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void | |
function() { | |
'use strict'; | |
var http = require('http'), | |
url = require('url'), | |
net = require('net'); | |
function printError(err, msg, url) { | |
console.log('%s %s: %s', new Date().toLocaleTimeString(), msg, url, err); | |
} | |
// http server | |
var server = http.createServer(function onCliReq(cliReq, cliRes) { | |
console.log(cliReq.method, cliReq.url); | |
cliRes.end(`//TODO 请求 ${cliReq.url} 的内容再返回...`); | |
}).listen(8888); | |
var path = require('path'); | |
var fs = require('fs'); | |
var sslKey = path.resolve('/Users/zdy/Desktop/https/zdying.com.key'); | |
var sslCert = path.resolve('/Users/zdy/Desktop/https/zdying.com.crt'); | |
var option = { | |
key: fs.readFileSync(sslKey), | |
cert: fs.readFileSync(sslCert) | |
}; | |
// https server | |
var httpsServer = require('https').createServer(option, function onCliReq(cliReq, cliRes) { | |
// console.log('https::::', cliReq.method, cliReq.url, cliReq.headers.host); | |
// cliRes.write('正忙呢。。。。,不能帮你清秋:' + cliReq.headers.host + cliReq.url + ' ' + cliReq.path); | |
var host = cliReq.headers.host; | |
var port = 443; | |
var path = cliReq.url; | |
console.log('https request', { | |
hostname: host, | |
port: 443, | |
path: path, | |
method: cliReq.method | |
}); | |
// var abc = require('https').request({ | |
// hostname: cliReq.headers.host, | |
// port: 443, | |
// path: cliReq.path, | |
// method: cliReq.method | |
// }, function(res){ | |
// res.pipe(cliRes) | |
// }); | |
var abc = require('https').request({ | |
// hostname: '127.0.0.1', | |
hostname: 'chuquba.cc', | |
// port: 8800, | |
port: 443, | |
path: path, | |
method: cliReq.method | |
}, | |
function(res) { | |
res.on('data', function(data){ | |
console.log('data ==>', data.toString()); | |
}) | |
res.pipe(cliRes) | |
}); | |
cliReq.pipe(abc); | |
}).listen(10010); | |
server.on('clientError', function onCliErr(err, cliSoc) { | |
cliSoc.end(); | |
printError(err, 'cliErr', ''); | |
}); | |
httpsServer.on('error', function(err){ | |
console.log('https error...:', err.message); | |
}) | |
server.on('connect', function onCliConn(cliReq, cliSoc, cliHead) { | |
console.log('connect', cliReq.method, cliReq.url); | |
var urlObj = require('url').parse('https://' + cliReq.url); | |
var host = urlObj.hostname; | |
var port = urlObj.port || 443; | |
console.log('host, port =>', host, port); | |
if (host === 'zdying.com') { | |
host = '127.0.0.1'; | |
port = 10010; | |
}else if(host === 'zdying.com.cn'){ | |
host = '127.0.0.1' | |
}else if(host === 'chuquba.com'){ | |
host = '127.0.0.1' | |
} | |
var svrSoc = net.connect(port, host, | |
function() { | |
cliSoc.write('HTTP/1.0 200 Connection established\r\n\r\n'); | |
cliSoc.pipe(svrSoc); | |
}); | |
svrSoc.pipe(cliSoc); | |
}); | |
// server.on('connection', function onConn(cliSoc) { | |
// cliSoc.$agent = new http.aAgent({keepAlive: true}); | |
// cliSoc.$agent.on('error', err => console.log('agent:', err)); | |
// }); | |
console.log('http proxy server started on port 8888'); | |
console.log('https proxy server started on port 10010'); | |
} (); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment