using Tor as Proxy

前端 未结 5 1982
再見小時候
再見小時候 2020-11-28 04:31

I\'m trying to use Tor-Server as a proxy in HttpWebRequest, my code looks like this:

HttpWebRequest request;
HttpWebResponse response;

request          


        
5条回答
  •  一整个雨季
    2020-11-28 05:28

    Use the library "SocksWebProxy". You can use it 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

提交回复
热议问题