如何使用Tor控制协议,在C#中? [英] How to use Tor control protocol in C#?

查看:810
本文介绍了如何使用Tor控制协议,在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将命令发送到Tor的控制端口编程,使其刷新链。我一直没能找到在C#中的任何例子,我的解决方案不工作。请求超时。我有服务运行,而且我可以看到它监听控制端口。

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\r\n", client);
    if (authenticate.Equals("250"))
    {
        response = MakeTcpRequest("SIGNAL NEWNYM\r\n", 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?

编辑:

继汉斯的建议下,他现在已经删除了某些原因,我试图发送,而不是仅仅授权的n AUTHENTICATE \。现在我又回到了错误的Tor的:551无效引用的字符串你需要把密码在双引号。至少有一些进展。

Following Hans's suggestion, which he has now deleted for some reason, I tried to send "AUTHENTICATE\n" instead of just "AUTHENTICATE". Now I'm getting back an error from Tor: "551 Invalid quoted string. You need to put the password in double quotes." At least there's some progress.

我然后试图发送AUTHENTICATE \\\ñ,就像它要,但它超时,同时等待响应。

I then tried to send "AUTHENTICATE \"\"\n", like it wants to, but it times out while waiting for a response.

编辑:

该命令工作正常在Windows Telnet客户端。我甚至不具备加引号。无法弄清楚什么是错的。也许双引号不正确连接时,他们送codeD?

The command works fine in the Windows Telnet client. I don't even have to add the quotes. Can't figure out what's wrong. Maybe the double quotes aren't encoded correctly when they're sent?

推荐答案

当我送了鉴别命令,给StreamReader正在读取响应结束,但目前还没有结束,因为成功的流保持开放。所以,我把它改为只读在这种情况下,响应的第一行。

When I send the AUTHENTICATE command, the StreamReader is reading the response to the end, but there is no end because on success the stream is kept open. So I changed it to only read the first line of the response in this case.

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

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

        // Read response
        using (StreamReader streamReader = new StreamReader(client.GetStream()))
        {
            proxyResponse = readToEnd ? streamReader.ReadToEnd() : streamReader.ReadLine();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return proxyResponse;
}

这篇关于如何使用Tor控制协议,在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆