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

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

问题描述

我正在尝试以编程方式向 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
", 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?

按照 Hans 的建议(由于某种原因他现在已将其删除),我尝试发送AUTHENTICATE "而不仅仅是AUTHENTICATE".现在我从 Tor 收到一个错误:551 无效的引号字符串.您需要将密码放在双引号中."至少有一些进展.

Following Hans's suggestion, which he has now deleted for some reason, I tried to send "AUTHENTICATE " 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 "" ", like it wants to, but it times out while waiting for a response.

该命令在 Windows Telnet 客户端中运行良好.我什至不必添加引号.无法弄清楚出了什么问题.也许双引号在发送时没有正确编码?

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?

推荐答案

当我发送 AUTHENTICATE 命令时,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;
}

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

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