How to use Tor control protocol in C#?

Posted by Ed on Stack Overflow See other posts from Stack Overflow or by Ed
Published on 2010-04-30T14:40:08Z Indexed on 2010/04/30 17:37 UTC
Read the original article Hit count: 1011

Filed under:
|
|

I'm trying to send commands to the Tor control port programmatically to make it refresh the chain. I haven't been able to find any examples in C#, and my solution's not working. The request times out. I have the service running, and I can see it listening on the control port.

public string Refresh()
{
    TcpClient client = new TcpClient("localhost", 9051);
    string response = string.Empty;
    string authenticate = MakeTcpRequest("AUTHENTICATE", client);
    if (authenticate.Equals("250"))
        response = MakeTcpRequest("SIGNAL NEWNYM", client);
    client.Close();
    return response;
}

public string MakeTcpRequest(string message, TcpClient client)
{
    client.ReceiveTimeout = 20000;
    client.SendTimeout = 20000;
    string proxyResponse = string.Empty;

    try
    {
        // Send message
        StreamWriter streamWriter = new StreamWriter(client.GetStream());
        streamWriter.Write(message);
        streamWriter.Flush();

        // Read response
        StreamReader streamReader = new StreamReader(client.GetStream());
        proxyResponse = streamReader.ReadToEnd();
    }
    catch (Exception ex)
    {
        // Ignore
    }

    return proxyResponse;
}

Can anyone spot what I'm doing wrong?

© Stack Overflow or respective owner

Related posts about c#

Related posts about tor