-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from robbinhan/goodrain
不设置代理的版本
- Loading branch information
Showing
1 changed file
with
17 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
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); | ||
}); | ||
} |