使用 Tor 作为代理

发布于 2024-08-16 06:11:29 字数 517 浏览 10 评论 0原文

我正在尝试使用 Tor-Server 作为 HttpWebRequest 中的代理,我的代码如下所示:

HttpWebRequest request;
HttpWebResponse response;

request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Proxy = new WebProxy("127.0.0.1:9051");

response = (HttpWebResponse)request.GetResponse();
response.Close();

它与“普通”代理完美配合,但使用 Tor 我在调用

GetResponse 时遇到异常() 且状态 = ServerProtocolViolation。该消息是(德语...):Message =“Der Server hat eine Protokollverletzung ausgeführt..Section=ResponseStatusLine”

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

HttpWebRequest request;
HttpWebResponse response;

request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
request.Proxy = new WebProxy("127.0.0.1:9051");

response = (HttpWebResponse)request.GetResponse();
response.Close();

it works perfect with "normal" proxies but with Tor I'm getting Exceptions while calling

GetResponse() with Status = ServerProtocolViolation. The message is (in German...):Message = "Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淑女气质 2024-08-23 06:11:30

如果您安装并运行 privoxy 您可以执行

request.Proxy = new WebProxy("127.0.0.1:8118"); // default privoxy port

以下操作,这将使您能够使用 tor 发出请求

If you have privoxy installed and running you can do

request.Proxy = new WebProxy("127.0.0.1:8118"); // default privoxy port

Which will enable you to make requests using tor

开始看清了 2024-08-23 06:11:30

Tor 不是 HTTP 代理。这是一个 SOCKS 代理。您可以使用支持 SOCKS 转发的 HTTP 代理(如 Privoxy)并通过代码连接到该代理。

Tor is not an HTTP proxy. It's a SOCKS proxy. You can use an HTTP proxy that supports forwarding on SOCKS (like Privoxy) and connect to that via code instead.

薄暮涼年 2024-08-23 06:11:30

是的,就像另一位海报所说,需要一个袜子客户端。一些库是 Starksoft ProxyProxySocketComponentSpace Socks代理sockscap 是一个拦截和重新路由winsock调用的工具,并且privoxy 是一个本地代理,可以通过袜子传输您的请求。几个不同的解决方案。

Yes like the other poster said, a socks client is needed. Some libraries are Starksoft Proxy, ProxySocket and ComponentSpace Socks Proxy. sockscap is a tool that intercepts and reroutes winsock calls, and privoxy is a local proxy that can tunnel your requests over socks. A couple different solutions.

手长情犹 2024-08-23 06:11:30

使用库“SocksWebProxy”。您可以将它与 WebClient 和 WebClient 一起使用。 WebRequest(只需将新的 SocksWebProxy 分配给 *.Proxy 属性)。无需 Privoxy 或类似服务将 http 流量转换为 tor。

https://github.com/Ogglas/SocksWebProxy

我还通过启用控件对其进行了一些扩展港口。以下是如何在不启动 Tor Browser Bundle 的情况下让 Tor 在后台运行,并且为了控制 Tor,我们可以使用 Telnet 或通过 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");
}

配置 Tor 的步骤:

  1. 将 torrc-defaults 复制到 tor.exe 所在的目录中。如果您使用 Tor 浏览器,默认目录为:“~\Tor Browser\Browser\TorBrowser\Data\Tor”
  2. 打开 cmd 提示符窗口
  3. chdir 到 tor.exe 所在目录。如果您使用 Tor 浏览器,默认目录为:“~\Tor Browser\Browser\TorBrowser\Tor\”
  4. 生成 Tor 控制端口访问的密码。 tor.exe --哈希密码“your_password_without_hyphens”|更多
  5. 将您的密码密码哈希添加到 ControlPort 9151 下的 torrc-defaults 中。它应如下所示:hashedControlPassword 16:3B7DA467B1C0D550602211995AE8D9352BF942AB04110B2552324B2507。如果您接受密码为“password”,您可以复制上面的字符串。
  6. 现在,一旦启动,您就可以通过 Telnet 访问 Tor 控制。现在代码可以运行,只需编辑 Tor 文件在程序中所在的路径即可。
    测试通过 Telnet 修改 Tor:
  7. 使用以下命令启动 tor:tor.exe -f .\torrc-defaults
  8. 打开另一个 cmd 提示符并输入:telnet localhost 9151
  9. 如果全部顺利的话你应该会看到一个全黑的屏幕。输入“autenticate “your_password_with_hyphens””如果一切顺利,您应该会看到“250 OK”。
  10. 输入“SIGNAL NEWNYM”,您将获得一条新路由,即新的 IP。如果一切顺利,您应该会看到“250 OK”。
  11. 输入“setevents circ”(电路事件)以启用控制台输出
  12. 输入“getinfo Circuit-status”以查看当前电路

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
你的心境我的脸 2024-08-23 06:11:30

您需要从袜子中“提取”流......

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Runtime.CompilerServices

Public Class Form1

    Sub Form1_Load() Handles Me.Load

        Dim Host As String = "google.com"

        Dim P As New SocksProxy("localhost", 64129) 'Set your socks proxy here
        Dim Stream As NetworkStream = P.GetStream(Host, 80)
        Dim buffer As Byte() = Download(Stream, Host, "")

        My.Computer.FileSystem.WriteAllBytes("C:\webpage.html", buffer, False)

        MsgBox("ok")
    End Sub

    Function Download(Stream As NetworkStream, Host As String, Resource As String) As Byte()

        Using writer = New StreamWriter(Stream)
            writer.Write(String.Format("GET /{2} HTTP/1.1{0}Host: {1}{0}{0}", vbCrLf, Host, Resource))
            writer.Flush()

            Dim byteList As New List(Of Byte)
            Dim bufferSize As Integer = 4096
            Dim buffer(bufferSize - 1) As Byte

            Do
                Dim bytesRead As Integer = Stream.Read(buffer, 0, bufferSize)
                byteList.AddRange(buffer.Take(bytesRead))
            Loop While Stream.DataAvailable

            Return byteList.ToArray
        End Using

    End Function
End Class


Public Class SocksProxy

    Private _SocksHost As String
    Private _SocksPort As Integer

    Sub New(SocksHost As String, SocksPort As Integer)
        _SocksHost = SocksHost
        _SocksPort = SocksPort
    End Sub

    Function GetStream(HostDest As String, PortDest As Short) As NetworkStream

        Dim client As TcpClient = New TcpClient()
        client.Connect(_SocksHost, _SocksPort)

        Dim stream As NetworkStream = client.GetStream()
        'Auth
        Dim buf = New Byte(299) {}
        buf(0) = &H5
        buf(1) = &H1
        buf(2) = &H0
        stream.Write(buf, 0, 3)

        ReadExactSize(stream, buf, 0, 2)
        If buf(0) <> &H5 Then
            Throw New IOException("Invalid Socks Version")
        End If
        If buf(1) = &HFF Then
            Throw New IOException("Socks Server does not support no-auth")
        End If
        If buf(1) <> &H0 Then
            Throw New Exception("Socks Server did choose bogus auth")
        End If

        buf(0) = &H5
        buf(1) = &H1
        buf(2) = &H0
        buf(3) = &H3
        Dim domain = Encoding.ASCII.GetBytes(HostDest)
        buf(4) = CByte(domain.Length)
        Array.Copy(domain, 0, buf, 5, domain.Length)
        Dim port = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(CShort(PortDest)))
        buf(5 + domain.Length) = port(0)
        buf(6 + domain.Length) = port(1)
        stream.Write(buf, 0, domain.Length + 7)


        ' Reply
        ReadExactSize(stream, buf, 0, 4)
        If buf(0) <> &H5 Then
            Throw New IOException("Invalid Socks Version")
        End If
        If buf(1) <> &H0 Then
            Throw New IOException(String.Format("Socks Error {0:X}", buf(1)))
        End If
        Dim rdest = String.Empty
        Select Case buf(3)
            Case &H1
                ' IPv4
                ReadExactSize(stream, buf, 0, 4)
                Dim v4 = BitConverter.ToUInt32(buf, 0)
                rdest = New IPAddress(v4).ToString()
                Exit Select
            Case &H3
                ' Domain name
                ReadExactSize(stream, buf, 0, 1)
                If buf(0) = &HFF Then
                    Throw New IOException("Invalid Domain Name")
                End If
                ReadExactSize(stream, buf, 1, buf(0))
                rdest = Encoding.ASCII.GetString(buf, 1, buf(0))
                Exit Select
            Case &H4
                ' IPv6
                Dim octets = New Byte(15) {}
                ReadExactSize(stream, octets, 0, 16)
                rdest = New IPAddress(octets).ToString()
                Exit Select
            Case Else
                Throw New IOException("Invalid Address type")
        End Select
        ReadExactSize(stream, buf, 0, 2)
        Dim rport = CUShort(IPAddress.NetworkToHostOrder(CShort(BitConverter.ToUInt16(buf, 0))))

        Return stream
    End Function

    Private Sub ReadExactSize(stream As NetworkStream, buffer As Byte(), offset As Integer, size As Integer)
        While size <> 0
            Dim read = stream.Read(buffer, offset, size)
            If read < 0 Then
                Throw New IOException("Premature end")
            End If
            size -= read
            offset += read
        End While
    End Sub

End Class

You need "extract" a stream from socks...

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Runtime.CompilerServices

Public Class Form1

    Sub Form1_Load() Handles Me.Load

        Dim Host As String = "google.com"

        Dim P As New SocksProxy("localhost", 64129) 'Set your socks proxy here
        Dim Stream As NetworkStream = P.GetStream(Host, 80)
        Dim buffer As Byte() = Download(Stream, Host, "")

        My.Computer.FileSystem.WriteAllBytes("C:\webpage.html", buffer, False)

        MsgBox("ok")
    End Sub

    Function Download(Stream As NetworkStream, Host As String, Resource As String) As Byte()

        Using writer = New StreamWriter(Stream)
            writer.Write(String.Format("GET /{2} HTTP/1.1{0}Host: {1}{0}{0}", vbCrLf, Host, Resource))
            writer.Flush()

            Dim byteList As New List(Of Byte)
            Dim bufferSize As Integer = 4096
            Dim buffer(bufferSize - 1) As Byte

            Do
                Dim bytesRead As Integer = Stream.Read(buffer, 0, bufferSize)
                byteList.AddRange(buffer.Take(bytesRead))
            Loop While Stream.DataAvailable

            Return byteList.ToArray
        End Using

    End Function
End Class


Public Class SocksProxy

    Private _SocksHost As String
    Private _SocksPort As Integer

    Sub New(SocksHost As String, SocksPort As Integer)
        _SocksHost = SocksHost
        _SocksPort = SocksPort
    End Sub

    Function GetStream(HostDest As String, PortDest As Short) As NetworkStream

        Dim client As TcpClient = New TcpClient()
        client.Connect(_SocksHost, _SocksPort)

        Dim stream As NetworkStream = client.GetStream()
        'Auth
        Dim buf = New Byte(299) {}
        buf(0) = &H5
        buf(1) = &H1
        buf(2) = &H0
        stream.Write(buf, 0, 3)

        ReadExactSize(stream, buf, 0, 2)
        If buf(0) <> &H5 Then
            Throw New IOException("Invalid Socks Version")
        End If
        If buf(1) = &HFF Then
            Throw New IOException("Socks Server does not support no-auth")
        End If
        If buf(1) <> &H0 Then
            Throw New Exception("Socks Server did choose bogus auth")
        End If

        buf(0) = &H5
        buf(1) = &H1
        buf(2) = &H0
        buf(3) = &H3
        Dim domain = Encoding.ASCII.GetBytes(HostDest)
        buf(4) = CByte(domain.Length)
        Array.Copy(domain, 0, buf, 5, domain.Length)
        Dim port = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(CShort(PortDest)))
        buf(5 + domain.Length) = port(0)
        buf(6 + domain.Length) = port(1)
        stream.Write(buf, 0, domain.Length + 7)


        ' Reply
        ReadExactSize(stream, buf, 0, 4)
        If buf(0) <> &H5 Then
            Throw New IOException("Invalid Socks Version")
        End If
        If buf(1) <> &H0 Then
            Throw New IOException(String.Format("Socks Error {0:X}", buf(1)))
        End If
        Dim rdest = String.Empty
        Select Case buf(3)
            Case &H1
                ' IPv4
                ReadExactSize(stream, buf, 0, 4)
                Dim v4 = BitConverter.ToUInt32(buf, 0)
                rdest = New IPAddress(v4).ToString()
                Exit Select
            Case &H3
                ' Domain name
                ReadExactSize(stream, buf, 0, 1)
                If buf(0) = &HFF Then
                    Throw New IOException("Invalid Domain Name")
                End If
                ReadExactSize(stream, buf, 1, buf(0))
                rdest = Encoding.ASCII.GetString(buf, 1, buf(0))
                Exit Select
            Case &H4
                ' IPv6
                Dim octets = New Byte(15) {}
                ReadExactSize(stream, octets, 0, 16)
                rdest = New IPAddress(octets).ToString()
                Exit Select
            Case Else
                Throw New IOException("Invalid Address type")
        End Select
        ReadExactSize(stream, buf, 0, 2)
        Dim rport = CUShort(IPAddress.NetworkToHostOrder(CShort(BitConverter.ToUInt16(buf, 0))))

        Return stream
    End Function

    Private Sub ReadExactSize(stream As NetworkStream, buffer As Byte(), offset As Integer, size As Integer)
        While size <> 0
            Dim read = stream.Read(buffer, offset, size)
            If read < 0 Then
                Throw New IOException("Premature end")
            End If
            size -= read
            offset += read
        End While
    End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文