Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aghArdeshir opened this issue Jul 30, 2023 · 15 comments
Open

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

aghArdeshir opened this issue Jul 30, 2023 · 15 comments
Assignees

Comments

@aghArdeshir
Copy link

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.

@Fa1z-khan
Copy link

+1

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

@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

@talmobi talmobi self-assigned this Oct 17, 2023
@Fa1z-khan
Copy link

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

@Fa1z-khan
Copy link

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

@Fa1z-khan
Copy link

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

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

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

@Fa1z-khan
Copy link

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

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

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

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

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

@Fa1z-khan
Copy link

yep i did that first didnt work

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

@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

@talmobi
Copy link
Owner

talmobi commented Oct 17, 2023

@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

@Fa1z-khan
Copy link

i created my own solution which is working fine

@Fa1z-khan
Copy link

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;

@followdarko
Copy link

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
Projects
None yet
Development

No branches or pull requests

4 participants