4

I'm trying to create web request using tor.it's giving error "Unable to connect to the remote server" inner exception "No connection could be made because the target machine actively refused it 127.0.0.1:8118"

tor browser working fine.

Code:

    static void Main(string[] args)
    {
        Process p = new Process();
        p.StartInfo = new ProcessStartInfo(@"C:\Users\king\Downloads\Tor Browser\App\tor.exe", "ControlPort 9151 CircuitBuildTimeout 10");
        p.Start();
        Thread.Sleep(5000);
        Regex regex = new Regex("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", RegexOptions.Multiline);

        do
        {       

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://whatismyipaddress.com/");
            request.Headers.Add("Cache-Control", "max-age=0");
            request.Proxy = new WebProxy("127.0.0.1:8118");
            request.KeepAlive = false;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {

                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    string contenu = reader.ReadToEnd();
                    Console.WriteLine(regex.Match(contenu).Groups[0].Value);
                }
            }
            Console.Write("en attente : continuez ?");
            string line = Console.ReadLine();
            if (line != "y")
                break;
        }
        while (true);

        p.Kill();
        Console.ReadLine();
    }
}

thank's

4

You are using port 8118, this is privoxy's port.

your code doesn't appear to run privoxy.

download privoxy, change its config to work with tor by uncommenting the

    forward-socks5             /     127.0.0.1:9050 .

line

run privoxy and tor at the same time

edit: the reason to run tor through privoxy is because c# does not work with socks proxies. and tor is a socks proxy, so it needs to be ran through a html proxy that runs through tor. (simplified description)

  • i have installed privoxy, it's running on 8118 port. uncommenting the forward-socks5 getting error "Privoxy was unable to socks5-forward your request localhost:9090 through 127.0.0.1: SOCKS5 request failed" – Nemi Chand Apr 23 '14 at 7:59
  • what is showing the error? visual studio, privoxy or tor? also i'm confused why it is trying to forward to 9090, shouldn't it be forwarding through 9051 or 9050, have you edited torrc? – puser Apr 23 '14 at 9:01
  • Also, why are you using request.keepalive = false? and why are you changing the request.headers – puser Apr 23 '14 at 9:32
  • Thats good to hear, out of interest, what did you change to fix it? – puser Apr 23 '14 at 11:40
  • no changes done in code,after some time it's automatically worked. i also wondered that – Nemi Chand Apr 23 '14 at 11:50
0

You can use the library "SocksWebProxy". It can be used with WebClient & WebRequest (Just assign a new SocksWebProxy to the *.Proxy attribute). No Need for Privoxy or similar service to translate http traffic to tor.

https://github.com/Ogglas/SocksWebProxy

I made some extensions to it as well by enabling the control port. Here is how you could have Tor running in the background without Tor Browser Bundle started and to control Tor we can use Telnet or send commands programmatically via Socket.

Socket server = null;

//Authenticate using control password
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9151);
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(endPoint);
server.Send(Encoding.ASCII.GetBytes("AUTHENTICATE \"your_password\"" + Environment.NewLine));
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data);
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);

//Request a new Identity
server.Send(Encoding.ASCII.GetBytes("SIGNAL NEWNYM" + Environment.NewLine));
data = new byte[1024];
receivedDataLength = server.Receive(data);
stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength);
if (!stringData.Contains("250"))
{
    Console.WriteLine("Unable to signal new user to server.");
    server.Shutdown(SocketShutdown.Both);
    server.Close();
}
else
{
    Console.WriteLine("SIGNAL NEWNYM sent successfully");
}

Steps to configure Tor:

  1. Copy torrc-defaults into the directory in which tor.exe is. Default directory if you are using Tor browser is: "~\Tor Browser\Browser\TorBrowser\Data\Tor"
  2. Open a cmd prompt window
  3. chdir to the directory where tor.exe is. Default directory if you are using Tor browser is: "~\Tor Browser\Browser\TorBrowser\Tor\"
  4. Generate a password for Tor control port access. tor.exe --hash-password “your_password_without_hyphens” | more
  5. Add your password password hash to torrc-defaults under ControlPort 9151. It should look something like this: hashedControlPassword 16:3B7DA467B1C0D550602211995AE8D9352BF942AB04110B2552324B2507. If you accept your password to be "password" you can copy the string above.
  6. You can now access Tor control via Telnet once it is started. Now the code can run, just edit the path to where your Tor files are located in the program. Test modifying Tor via Telnet:
  7. Start tor with the following command: tor.exe -f .\torrc-defaults
  8. Open up another cmd prompt and type: telnet localhost 9151
  9. If all goes well you should see a completely black screen. Type "autenticate “your_password_with_hyphens”" If all goes well you should see "250 OK".
  10. Type "SIGNAL NEWNYM" and you will get a new route, ergo new IP. If all goes well you should see "250 OK".
  11. Type "setevents circ" (circuit events) to enable console output
  12. Type "getinfo circuit-status" to see current circuits

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.