NodeJs 代理连接

NodeJs Proxy Connection

本文关键字:连接 代理 NodeJs      更新时间:2023-09-26

为什么我无法使用用户名和密码与 SOCKS5 连接

用户名:password@ip:端口,我可以用HTTP,但不能用SOCKS5

done用户名:password@ip:port,我可以用HTTP,但不能用SOCKS5

袜子5 ; 用户名:password@ip:端口

Attempting connection to ws://ip:port
Connecting to: ws://ip:port
net.js:928
      throw new RangeError('port should be >= 0 and < 65536: ' + port);
      ^
RangeError: port should be >= 0 and < 65536: NaN
    at lookupAndConnect (net.js:928:13)
    at Socket.connect (net.js:905:5)
    at Socket.connect (net.js:868:37)

var WebSocket = require('ws');
var valid_player_pos = null;
var reconnect = false;
var suicide_targets = null;
var socket = require('socket.io-client')(config.feederServer);
socket.on('pos', function(data) {
    valid_player_pos = data;
    //console.log(data);
});
socket.on('cmd', function(data) {
    console.log(data);
    if (data.name == "split") {
        for (bot in bots) {
            bots[bot].client.split();
        }
    } else if (data.name == "eject") {
        for (bot in bots) {
            bots[bot].client.eject();
        }
    } else if (data.name == "connect_server") {
        if (data.ip == null) {
            return;
        }
        if (data.ip == "") {
            return;
        }
        for (bot in bots) {
            bots[bot].client.disconnect();
        }
        bots = {};
        game_server_ip = data.ip;
        console.log("client requested bots on: " + game_server_ip);
        setTimeout(function() {
            startFeederBotOnProxies();
        }, 1000);
    } else if(data.name == "reconnect_server") {
        reconnect = true;
        if (data.ip == null) {
            return;
        }
        if (data.ip == "") {
            return;
        }
        for (bot in bots) {
            bots[bot].client.disconnect();
        }
        bots = {};
        game_server_ip = data.ip;
        console.log("client requested bots on: " + game_server_ip);
    }
});
socket.on('force-login', function(data) {
    console.log(data);
    if (data == "server-booted-up") {
        return;
    }
    socket.emit("login", {
        "uuid": config.client_uuid,
        "type": "server"
    });
});
fs = require('fs');
var HttpsProxyAgent = require('https-proxy-agent');
var Socks = require('socks');

function getRandomLine(filename) {
    var fs = require('fs');
    var lines = fs.readFileSync(filename).toString().split("'n");
    line = lines[Math.floor(Math.random() * lines.length)];
    return line
}
//object of bots
var bots = {};
bot_count = 0;
var fs = require('fs');
var lines = fs.readFileSync(config.proxies).toString().split("'n");
var url = require('url');
var game_server_ip = null;
function createAgent(ip,type) {
    data = ip.split(":");
    return new Socks.Agent({
            proxy: {
                ipaddress: data[0],
                port: parseInt(data[1]),
                type: parseInt(type)
            }}
    );
}
var proxy_mode = "HTTP";
function startFeederBotOnProxies() {
    for (proxy_line in lines) {
        if(lines[proxy_line].trim() == "#HTTP"){
            proxy_mode = "HTTP";
        }else if(lines[proxy_line].trim() == "#SOCKS4"){
            proxy_mode = "SOCKS4";
        }else if(lines[proxy_line].trim() == "#SOCKS5"){
            proxy_mode = "SOCKS5";
        }
        if (lines[proxy_line][0] == "#" || lines[proxy_line].length < 3) {
            continue;
        }
        //usefull for testing single proxies
        if (process.argv[3] != null && proxy_line != process.argv[3]) {
            continue;
        }
        proxy = "http://" + lines[proxy_line];
        proxy_single = lines[proxy_line];
        console.log(proxy_mode + " ; " + proxy_single);
        try {
            var opts = url.parse(proxy);
            if (proxy != null) {
                if(proxy_mode=="HTTP"){
                    agent = HttpsProxyAgent(opts);
                }else if(proxy_mode=="SOCKS4"){
                    agent = createAgent(lines[proxy_line],4);
                }else if(proxy_mode=="SOCKS5"){
                    agent = createAgent(lines[proxy_line],5);
                }
            } else {
                var agent = null;
            }
            if (lines[proxy_line] == "NOPROXY") {
                agent = null;
            }
            console.log("Attempting connection to " + game_server_ip);
            for (i = 0; i < config.botsPerIp; i++) {
                if(bot_count<config.maxBots){
                    bot_count++;
                    bots[bot_count] = new FeederBot(bot_count, agent, bot_count, game_server_ip);
                }
            }
        } catch (e) {
            console.log('Error occured on startup: ' + e);
        }
    }
}
console.log("ogar-feeder-bot started! Join a game in Chrome with the Userscript installed.");
console.log("Press CTRL + C to stop this script.");

从所有的讨论中,我了解到您的创建代理存在问题。

所以使用这段代码并感到高兴(:

function createAgent(connectionString, type) {
    var type = parseInt(type || 5);
    var ipParts = connectionString.split('@'); // splitting user:pass@host:port
    var host, port, username, password;
    switch(ipParts.length) {
      case 3 : // somebody@somewhere.com:somepassword@proxy.com:1080
        var credentials = (ipParts[0]+'@'+ipParts[1]).split(':'); // yusolokuji@leeching.net:luquitas
        username = credentials[0]; // somebody@somewhere.com
        password = credentials[1]; // somepassword
        var hostParts = ipParts[2].split(':'); // proxy.com:1080
        host = hostParts[0];
        port = hostParts[1] || 1080;
        break;
      case 2 : // somebody:somepassword@proxy.com:1080
        var credentials = ipParts[0].split(':'); // somebody:somepassword
        username = credentials[0]; // somebody
        password = credentials[1]; // somepassword
        var hostParts = ipParts[1].split(':'); // proxy.com:1080
        host = hostParts[0];
        port = hostParts[1] || 1080;
        break;
      case 1 : // proxy.com:1080
        ipParts = ipParts[0].split(':');
        host = ipParts[0];
        port = ipParts[1];
        break;
    }
    var config = {
      proxy: {
        ipaddress: host,
        port: parseInt(port),
        type: type
      }
    };
    if(type == 5) {
      config.proxy.authentication = {
        username: username,
        password: password
      };
    }
    if(type == 4) {
      config.proxy.user_id = username;
    }
    return new Socks.Agent(config);
}