Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

不设置代理的版本 #1

Merged
merged 1 commit into from
Oct 24, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,45 @@
var Agent = require('socks5-https-client/lib/Agent');
var request = require('request');
var http = require('http');
var url = require('url');
var socksHost = '127.0.0.1';
var socksPort = 7070;

http.createServer(function (req, res) {
var srvUrl = url.parse(req.url,true);
var q = srvUrl.query.url
var num = srvUrl.query.num || 10;
var srvUrl = url.parse(req.url,true);
var q = srvUrl.query.url
var num = srvUrl.query.num || 10;

/* Use the randomNum and the time to invalidate the Google cache and
fetch the latest articles.
*/
var randomNum = new Date().getTime() + Math.floor((Math.random() * 10000) + 1);
q = encodeURIComponent(q );
/* Use the randomNum and the time to invalidate the Google cache and
fetch the latest articles.
*/
var randomNum = new Date().getTime() + Math.floor((Math.random() * 10000) + 1);
q = encodeURIComponent(q);

parseFeed(q, num, function(err,feeds){
jsonFeeds = JSON.parse(feeds)
if (jsonFeeds.responseStatus != 200) {
parseFeed(q, num, function(err,feeds){
jsonFeeds = JSON.parse(feeds)
console.log('staus',jsonFeeds.responseStatus)
if (jsonFeeds.responseStatus != 200) {
parseFeed(q, num, function(err,feeds){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(feeds);
})
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(feeds);
}
})
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(feeds);
}
})
}).listen(9222);

console.log('Server running on port 9222.');


function parseFeed (q,num,callback) {
apiUrl = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='+ num+ '&q='+q;
console.log(apiUrl);
request({
headers: {
'Cache-Control': 'max-age=0'
},
timeout: 60000,
url: apiUrl,
strictSSL: true,
agentClass: Agent,
agentOptions: {
socksHost: socksHost,
socksPort: socksPort
}
}, function(err, res) {
// console.log(err,res.body);
callback(err,res.body);
callback(err,res.body);
});
}