在 C# 中使用 Tor 下载文件

Download file using Tor in C#

我想使用 Tor 下载文件。我发现的大多数解决方案都需要安装附加软件(例如 privoxy)并且 运行,但我不想一直安装附加软件 运行,即使我不使用我的程序也是如此。

所以我尝试了 Tor.NET 库,但我无法使用 Tor 获取它。这个例子不应该 return 我的 IP 地址,但它确实是:

ClientCreateParams createParams = new ClientCreateParams(@"D:\tor.exe", 9051);
Client client = Client.Create(createParams);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.icanhazip.com/");
request.Proxy = client.Proxy.WebProxy;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    var reader = new StreamReader(response.GetResponseStream());
    Console.WriteLine(reader.ReadToEnd());
}

Console.WriteLine("Press enter to exit...");
Console.ReadLine();

已经有很多关于这个的评论,但不幸的是图书馆的作者不再活跃了。

也许您知道我做错了什么(是否需要更多配置?)或者有其他使用 tor 下载文件的想法。

最后我用https://github.com/Ogglas/SocksWebProxy by @Ogglas用Tor下载了一个文件

该项目有一个不工作的例子(第一次启动它会无限等待 Tor 退出,但是当你再次启动程序时它可以通过你的第一次尝试使用 Tor 进程启动),所以我改了。

我创建了一个 Start() 方法来启动 Tor:

public async void Start(IProgress<int> progress)
{
    torProcess = new Process();
    torProcess.StartInfo.FileName = @"D:\...\tor.exe";
    torProcess.StartInfo.Arguments = @"-f ""D:\...\torrc""";
    torProcess.StartInfo.UseShellExecute = false;
    torProcess.StartInfo.RedirectStandardOutput = true;
    torProcess.StartInfo.CreateNoWindow = true;
    torProcess.Start();
    var reader = torProcess.StandardOutput;
    while (true)
    {
        var line = await reader.ReadLineAsync();
        if (line == null)
        {
            // EOF
            Environment.Exit(0);
        }
        // Get loading status
        foreach (Match m in Regex.Matches(line, @"Bootstrapped (\d+)%"))
        {
            progress.Report(Convert.ToInt32(m.Groups[1].Value));
        }

        if (line.Contains("100%: Done"))
        {
            // Tor loaded
            break;
        }
        if (line.Contains("Is Tor already running?"))
        {
            // Tor already running
            break;
        }
    }

    proxy = new SocksWebProxy(new ProxyConfig(
        //This is an internal http->socks proxy that runs in process
        IPAddress.Parse("127.0.0.1"),
        //This is the port your in process http->socks proxy will run on
        12345,
        //This could be an address to a local socks proxy (ex: Tor / Tor Browser, If Tor is running it will be on 127.0.0.1)
        IPAddress.Parse("127.0.0.1"),
        //This is the port that the socks proxy lives on (ex: Tor / Tor Browser, Tor is 9150)
        9150,
        //This Can be Socks4 or Socks5
        ProxyConfig.SocksVersion.Five
    ));
    progress.Report(100);
}

之后你可以使用类似这样的方式下载一些东西:

public static string DownloadString(string url)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Proxy = proxy;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        return new StreamReader(response.GetResponseStream()).ReadToEnd();
    }
}

并且当您退出程序时,您还应该终止 Tor 进程。

您按照 Tor 项目手册、命令行 HTTPTunnelPort,您发现 here:首先您必须启动一个

的 HTTP 隧道
Tor.exe --HTTPTunnelPort 4711

它在 127.0.0.1:4711 为您提供了一个 HTTP 隧道(另请参阅 here)。现在您可以连接到此代理:

WebProxy oWebProxy = new WebProxy (IPAddress.Loopback.ToString (), 4711);
WebClient oWebClient = new WebClient ();
oWebClient.Proxy = oWebProxy;
oWebClient.DownloadFile ("http://myUri", "myFilename");

使用Tor.exe请注意以下事项:

  • 如果端口已被使用 Tor.exe 将无法提供代理。它甚至不一定会通知您这次失败。
  • 确保没有人欺骗您的程序 Tor.exe 以便 Tor 为您提供此代理。因此,Tor.exe 应该位于文件系统中的安全位置。
  • 了解有关使用 Tor 的其他注意事项。

至少,您可能需要检查您的代理与本地互联网连接的 IP 地址是否不同。