node.js - How to check if a proxy works in JavaScript - Stack Overflow

I'm trying to do a proxy checker in JavaScript. I give input the proxy data and it checks if the p

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

Share Improve this question edited Dec 11, 2019 at 20:28 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 2, 2019 at 21:02 Linch1Linch1 1,4493 gold badges26 silver badges49 bronze badges 3
  • 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
Add a ment  | 

1 Answer 1

Reset to default 3 +50

Install 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);
        }

    });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745175774a4615184.html

相关推荐

  • node.js - How to check if a proxy works in JavaScript - Stack Overflow

    I'm trying to do a proxy checker in JavaScript. I give input the proxy data and it checks if the p

    5小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信