這兩天須要安裝一個Linux虛擬機,接下來要經過 yum 來安裝須要的軟件。node
因單位的網絡須要經過代理才能訪問外網,公司代理服務器:proxy.xxxx.com,給yum增長了proxy設置:linux
# vi /etc/yum.conf
添加下面的內容:
proxy=http://user:password@proxy.xxxx.com:8080
發現linux沒法訪問代理服務器,Linux虛擬機只能訪問宿主host所在的網段,添加了路由,網關等都沒效果(知道方法的請回復下)服務器
索性本身搭一個proxy,來作透明轉發,用nodejs來寫吧,用stream.pipe()透傳就OK,關鍵是傳遞給proxy服務器的認證信息如何傳過去。網絡
var http = require('http');
var proxy = http.createServer(function(request, response) {
var options = {
host: 'proxy.xxxx.com', // 這裏是代理服務器
port: 8080, // 這裏是代理服務器端口
path: request.url,
method: request.method,
headers: {
// 若是代理服務器須要認證
'Proxy-Authentication': 'Base ' + new Buffer('user:password').toString('base64') // 替換爲代理服務器用戶名和密碼
}
};
var req = http.request(options, function(req, res) {
res.pipe(response); // 這個pipe很喜歡
console.log(req.url);
}).end();
}).listen(8080);
這就寫好了,啓動 node proxy 後,用curl測試一下:curl
# curl -x localhost:8080 www.baidu.com
工做正常,虛擬機宿主:10.66.220.146,修改下 yum.conf測試
# vi yum.conf
proxy=http://10.66.220.146:8080/
我能夠在Linux裏面 yum install了,That's very good.ui