admin管理员组文章数量:1414614
I'm trying to do a proxy checker in JavaScript. I give input the proxy data and it checks if the proxy works.
For now I wrote this function using the request module of Node.js.
const request = require('request');
var checkProxy = function(id, ip, port, url, user, pass, callback) {
let proxy_url;
if (user){
if (user != ''){
proxy_url = `socks5://${user}:${pass}@${ip}:${port}`;
}
} else {
proxy_url = 'socks5://' + ip + ':' + port;
console.log(proxy_url)
}
var proxyRequest = request.defaults({
proxy: proxy_url,
});
proxyRequest({url: url, timeout: 120000}, function(err, res) {
var testText = 'content="Brum Brum ..."';
if( err ) {
callback(id, ip, port, false, -1, err);
} else if( res.statusCode != 200 ) {
callback(id, ip, port, false, res.statusCode, err);
} else if( !res.body ) {
callback(id, ip, port, false, res.statusCode, "regex problem" + options.regex + ".");
} else {
callback(id, ip, port, true, res.statusCode);
}
});
}
As callback I pass:
() => {console.log(id, ip, port, false, res.statusCode, err);}
But when I try to check an IP address it gives wrong results.
I took proxy from this site (proxy: 207.154.231.217:1080
) and checked it with the function, but in the callback console.log I got the current error:
ERROR: proxy num: 0, ip: 207.154.231.217, port: 1080, STATUS: -1, ERROR: Error: tunneling socket could not be established, cause=socket hang up
I read that this is for some sort of authentication required, but I don't understand why if I check it on this site, the sites tell me that the proxy works.
I'm using:
Ubuntu 19.10 (Eoan Ermine)
Node.js v.13.0.1
npm 6.12.0
I'm trying to do a proxy checker in JavaScript. I give input the proxy data and it checks if the proxy works.
For now I wrote this function using the request module of Node.js.
const request = require('request');
var checkProxy = function(id, ip, port, url, user, pass, callback) {
let proxy_url;
if (user){
if (user != ''){
proxy_url = `socks5://${user}:${pass}@${ip}:${port}`;
}
} else {
proxy_url = 'socks5://' + ip + ':' + port;
console.log(proxy_url)
}
var proxyRequest = request.defaults({
proxy: proxy_url,
});
proxyRequest({url: url, timeout: 120000}, function(err, res) {
var testText = 'content="Brum Brum ..."';
if( err ) {
callback(id, ip, port, false, -1, err);
} else if( res.statusCode != 200 ) {
callback(id, ip, port, false, res.statusCode, err);
} else if( !res.body ) {
callback(id, ip, port, false, res.statusCode, "regex problem" + options.regex + ".");
} else {
callback(id, ip, port, true, res.statusCode);
}
});
}
As callback I pass:
() => {console.log(id, ip, port, false, res.statusCode, err);}
But when I try to check an IP address it gives wrong results.
I took proxy from this site (proxy: 207.154.231.217:1080
) and checked it with the function, but in the callback console.log I got the current error:
ERROR: proxy num: 0, ip: 207.154.231.217, port: 1080, STATUS: -1, ERROR: Error: tunneling socket could not be established, cause=socket hang up
I read that this is for some sort of authentication required, but I don't understand why if I check it on this site, the sites tell me that the proxy works.
I'm using:
Ubuntu 19.10 (Eoan Ermine)
Node.js v.13.0.1
npm 6.12.0
-
What is
request.defaults
? Is this a nodejs application? – Bergi Commented Dec 2, 2019 at 21:20 - are you using windows? – Hùng Nguyễn Commented Dec 5, 2019 at 2:05
- No, i'm using ubunutu 19.10 – Linch1 Commented Dec 5, 2019 at 8:09
1 Answer
Reset to default 3 +50Install the socks5-http-client
module and run this code:
const request = require('request');
const Agent = require('socks5-http-client/lib/Agent');
var checkProxy = function (id, ip, port, url, user, pass, callback) {
var proxyRequest = request.defaults({
agentClass: Agent,
agentOptions: {
socksHost: ip,
socksPort: port,
socksUsername: user,
socksPassword: pass
}
});
proxyRequest({ url: url, timeout: 120000 }, function (err, res) {
var testText = 'content="Brum Brum ..."';
if (err) {
callback(id, ip, port, false, -1, err);
} else if (res.statusCode != 200) {
callback(id, ip, port, false, res.statusCode, err);
} else if (!res.body) {
callback(id, ip, port, false, res.statusCode, "regex problem" + options.regex + ".");
} else {
callback(id, ip, port, true, res.statusCode);
}
});
}
本文标签: nodejsHow to check if a proxy works in JavaScriptStack Overflow
版权声明:本文标题:node.js - How to check if a proxy works in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745175773a2646231.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论