Skip to content

Tor Control Port: Error: connect ECONNREFUSED ::1:9051 #75

Open

Description

@aghArdeshir

This library works perfect in case of using Tor, but for using Tor Control Port and to request a new torSession I get this error:

Error: connect ECONNREFUSED ::1:9051
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 9051
}

That being said, I tried it without and with setting a hashed password for the tor (according to README.md), and then I used tr.TorControlPort.password = '<my_pass>' to set the password.

In both cases, I received the same error when I tried:

tr.newTorSession(async function (err) {
  if (!err) {
    console.log('New Tor session created');
    await wait(5000);
    getIp();
  } else {
    console.log(err);
  }
});

node version: 18.16.0
tor-request: 3.1.0
Tor version: 0.4.7.13.

Activity

  1. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    +1

  2. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @aghArdeshir @FaizKEverestek

    Hello

    You have most likely configured your tor control port incorrectly and/or edited the wrong torrc file -- please check the README in this section: https://github.com/talmobi/tor-request#optional-configuring-tor-enabling-the-controlport

    When tor starts it will tell you which torrc file it is using, please make sure it's the correct one.

    e.g.
    Screenshot 2023-10-17 at 10 33 43

    I've tested with npm run test-control-port (and configuring the correct port and password in the test/test-control-port.js file beforehand that matches my configured torrc file) and it's working correctly.

    Screenshot_2023-10-17_at_10_38_47
  3. self-assigned this
    on Oct 17, 2023
  4. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    My torrc file :

    SocksPort 127.0.0.1:9050
    SocksPort 127.0.0.1:9052
    SocksPort 127.0.0.1:9053
    SocksPort 127.0.0.1:9054
    ControlPort 127.0.0.1:9051
    HashedControlPassword 16:D3F51F03E9A976FD60DC709D47A7AECCF051F0D0231144E4CD89F96844

    CLI output of tor running :
    tor -f torrc
    Oct 17 13:13:07.765 [notice] Tor 0.4.8.7 (git-bf5e234d44e73127) running on Windows 8 [or later] with Libevent 2.1.12-stable, OpenSSL 3.0.11, Zlib 1.3, Liblzma N/A, Libzstd N/A and Unknown N/A as libc.
    Oct 17 13:13:07.765 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://support.torproject.org/faq/staying-anonymous/
    Oct 17 13:13:07.796 [notice] Read configuration file "D:\Faiz\Backend\Test_puppeteer\tor\tor\torrc".
    Oct 17 13:13:07.796 [warn] Path for GeoIPFile () is relative and will resolve to D:\Faiz\Backend\Test_puppeteer\tor\tor<default>. Is this what you wanted?
    Oct 17 13:13:07.796 [warn] Path for GeoIPv6File () is relative and will resolve to D:\Faiz\Backend\Test_puppeteer\tor\tor<default>. Is this what you wanted?
    Oct 17 13:13:07.796 [notice] Opening Socks listener on 127.0.0.1:9050
    Oct 17 13:13:07.796 [notice] Opened Socks listener connection (ready) on 127.0.0.1:9050
    Oct 17 13:13:07.796 [notice] Opening Socks listener on 127.0.0.1:9052
    Oct 17 13:13:07.796 [notice] Opened Socks listener connection (ready) on 127.0.0.1:9052
    Oct 17 13:13:07.796 [notice] Opening Socks listener on 127.0.0.1:9053
    Oct 17 13:13:07.796 [notice] Opened Socks listener connection (ready) on 127.0.0.1:9053
    Oct 17 13:13:07.796 [notice] Opening Socks listener on 127.0.0.1:9054
    Oct 17 13:13:07.796 [notice] Opened Socks listener connection (ready) on 127.0.0.1:9054
    Oct 17 13:13:07.796 [notice] Opening Control listener on 127.0.0.1:9051
    Oct 17 13:13:07.796 [notice] Opened Control listener connection (ready) on 127.0.0.1:9051
    Oct 17 13:13:07.000 [notice] Bootstrapped 0% (starting): Starting
    Oct 17 13:13:09.000 [notice] Starting with guard context "default"
    Oct 17 13:13:10.000 [notice] Bootstrapped 5% (conn): Connecting to a relay
    Oct 17 13:13:10.000 [notice] Bootstrapped 10% (conn_done): Connected to a relay
    Oct 17 13:13:10.000 [notice] Bootstrapped 14% (handshake): Handshaking with a relay
    Oct 17 13:13:10.000 [notice] Bootstrapped 15% (handshake_done): Handshake with a relay done
    Oct 17 13:13:10.000 [notice] Bootstrapped 75% (enough_dirinfo): Loaded enough directory info to build circuits
    Oct 17 13:13:10.000 [notice] Bootstrapped 90% (ap_handshake_done): Handshake finished with a relay to build circuits
    Oct 17 13:13:10.000 [notice] Bootstrapped 95% (circuit_create): Establishing a Tor circuit
    Oct 17 13:13:11.000 [notice] Bootstrapped 100% (done): Done

    yes it's using the correct file

  5. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    The code i'm trying to run :

    var tr = require("tor-request");

    requestIP();

    tr.TorControlPort.password = "password";

    tr.setTorAddress("localhost", 9050);

    tr.newTorSession((err) => {
    console.log(err);
    requestIP();
    return;
    });
    //console.log (tr.TorControlPort)

    function requestIP() {
    tr.request("https://api.ipify.org", function (err, res, body) {
    if (!err && res.statusCode == 200) {
    console.log("Your public (through Tor) IP is: " + body);
    }
    });
    }

  6. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    it works perfectly with the tr.request()
    but has issues when it comes to tr.newTorSession()

  7. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @FaizKEverestek and what is the error you get? Error: connect ECONNREFUSED ::1:9051?

  8. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    yep

    PS D:\Faiz\Backend\Test_puppeteer> node request.js

    Error: connect ECONNREFUSED ::1:9051
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
    errno: -4078,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 9051
    }
    Your public (through Tor) IP is: 185.220.100.253

  9. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @FaizKEverestek try setting tr.TorControlPort.host = '::1' or tr.TorControlPort.host = '127.0.0.1'

  10. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @FaizKEverestek and maybe try just using the port number 9051 in the torrc file instead of 127.0.0.1:9051

  11. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    yep i did that first didnt work

  12. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @FaizKEverestek another way to reset the tor session is by restarting the tor client (which is helpful to know) -- this doesn't look like an issue with tor-request specifically

  13. talmobi commented on Oct 17, 2023

    @talmobi
    Owner

    @FaizKEverestek it seems like your tor ControlPort is running on 127.0.0.1:5051 while the library is trying to connect on ::1:5051 (which is the ipv6 version of the loopback address) -- I don't think they are equivalent but I'm not sure -- tr.TorControlPort.host = '127.0.0.1' looks like it had some hope of working but unsure

  14. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    i created my own solution which is working fine

  15. Fa1z-khan commented on Oct 17, 2023

    @Fa1z-khan

    const net = require("net");

    // Tor ControlPort configuration
    const torControlPortHost = "127.0.0.1"; // IPv4 address
    const torControlPortPort = 9051; // ControlPort port
    const torControlPortPassword = "password"; // Replace with your actual ControlPort password

    // Function to send commands to the Tor ControlPort
    function renewTorConnection() {
    const client = net.connect(
    {
    host: torControlPortHost,
    port: torControlPortPort,
    },
    () => {
    console.log("Connected to Tor ControlPort");

      // Send the commands to authenticate and request a new identity
      client.write(
        `authenticate "${torControlPortPassword}"\nsignal newnym\nquit\n`
      );
    }
    

    );

    client.on("data", (data) => {
    console.log("Received from Tor ControlPort:", data.toString());
    client.end();
    });

    client.on("end", () => {
    console.log("Disconnected from Tor ControlPort");
    });

    client.on("error", (err) => {
    console.error("Error connecting to Tor ControlPort:", err);
    });
    }

    // Export the function as renewTorConnection
    module.exports = renewTorConnection;

  16. followdarko commented on Apr 23, 2024

    @followdarko

    Faced with the same problem. Confirmed tr.TorControlPort.host = '127.0.0.1' solves issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions