C# 使用Tor作为代理
我试图在C# 使用Tor作为代理,c#,proxy,httpwebrequest,tor,C#,Proxy,Httpwebrequest,Tor,我试图在HttpWebRequest中使用Tor Server作为代理,我的代码如下所示: HttpWebRequest request; HttpWebResponse response; request = (HttpWebRequest)WebRequest.Create("http://www.google.com"); request.Proxy = new WebProxy("127.0.0.1:9051"); response = (HttpWebResponse)reques
HttpWebRequest
中使用Tor Server作为代理,我的代码如下所示:
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,我在调用时会遇到异常
状态为ServerProtocolViolation的GetResponse()。消息是(德语…):message=“Der Server hat eine protokolleverletzung ausgeführt..Section=ResponseStatusLine”Tor不是HTTP代理。这是袜子代理。您可以使用支持SOCKS转发的HTTP代理(如Privoxy)并通过代码连接到该代理。是的,正如另一张海报所说,需要SOCKS客户端。有些图书馆是,和。是一个拦截和重新路由winsock调用的工具,是一个本地代理,可以通过socks对请求进行隧道传输。两种不同的解决方案。如果已安装并运行,则可以
request.Proxy = new WebProxy("127.0.0.1:8118"); // default privoxy port
这将使您能够使用tor发出请求,您需要从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
Imports System.IO
导入系统.Net
导入System.Net.Sockets
导入系统文本
导入System.Runtime.CompilerServices
公开课表格1
子表单1_Load()处理Me.Load
将主机设置为String=“google.com”
Dim P As New SocksProxy(“localhost”,64129)”在此处设置socks代理
Dim Stream As NetworkStream=P.GetStream(主机,80)
Dim缓冲区作为字节()=下载(流,主机“”)
My.Computer.FileSystem.writealBytes(“C:\webpage.html”,缓冲区,False)
MsgBox(“ok”)
端接头
函数下载(流作为网络流,主机作为字符串,资源作为字符串)作为字节()
使用writer=newstreamwriter(流)
Write.Write(String.Format(“GET/{2}HTTP/1.1{0}主机:{1}{0}{0}”,vbCrLf,主机,资源))
writer.Flush()
Dim byteList作为新列表(字节数)
Dim bufferSize为整数=4096
Dim缓冲区(缓冲区大小-1)作为字节
做
Dim bytesRead As Integer=Stream.Read(缓冲区,0,缓冲区大小)
byteList.AddRange(buffer.Take(bytesRead))
在Stream.database可用时循环
返回byteList.ToArray
终端使用
端函数
末级
公营袜子
Private\u SocksHost作为字符串
Private\u SocksPort作为整数
Sub New(SocksHost作为字符串,SocksPort作为整数)
_SocksHost=SocksHost
_短袜运动
端接头
函数GetStream(HostDest作为字符串,PortDest作为短字符串)作为NetworkStream
作为TcpClient的Dim客户端=新TcpClient()
客户端连接(\u SocksHost,\u SocksPort)
作为NetworkStream=client.GetStream()的Dim流
“啊
Dim buf=新字节(299){}
buf(0)=&H5
buf(1)=&H1
buf(2)=&H0
stream.Write(buf,0,3)
ReadExactSize(流,buf,0,2)
如果buf(0)和H5,则
抛出新IOException(“无效Socks版本”)
如果结束
如果buf(1)=&HFF,则
抛出新IOException(“Socks服务器不支持无身份验证”)
如果结束
如果buf(1)&H0,则
抛出新异常(“Socks服务器确实选择了伪身份验证”)
如果结束
buf(0)=&H5
buf(1)=&H1
buf(2)=&H0
buf(3)=&H3
Dim域=Encoding.ASCII.GetBytes(HostDest)
buf(4)=CByte(域长度)
数组.Copy(域,0,buf,5,域.Length)
Dim port=BitConverter.GetBytes(IPAddress.HostToNetworkOrder(CShort(PortDest)))
buf(5+域长度)=端口(0)
buf(6+域长度)=端口(1)
stream.Write(buf,0,domain.Length+7)
”“回答
ReadExactSize(流,buf,0,4)
如果buf(0)和H5,则
抛出新IOException(“无效Socks版本”)
如果结束
如果buf(1)&H0,则
抛出新IOException(String.Format(“Socks错误{0:X}”,buf(1)))
如果结束
Dim rdest=String.Empty
选择案例buf(3)
案例&H1
'IPv4
ReadExactSize(流,buf,0,4)
Dim v4=位转换器.ToUInt32(buf,0)
rdest=新的IPAddress(v4).ToString()
退出选择
案例&H3
'域名
ReadExactSize(流,buf,0,1)
如果buf(0)=&HFF,则
抛出新IOException(“无效域名”)
如果结束
ReadExactSize(流,buf,1,buf(0))
rdest=Encoding.ASCII.GetString(buf,1,buf(0))
退出选择
案例&H4
“IPv6
Dim八位字节=新字节(15){}
ReadExactSize(流,八位字节,0,16)
rdest=新的IP地址(八位字节)。ToString()
退出选择
其他情况
抛出新IOException(“无效地址类型”)
结束选择
ReadExactSize(流,buf,0,2)
Dim rport=CUShort(IPAddress.NetworkToHostOrder(CShort(BitConverter.ToUInt16(buf,0)))
回流
端函数
私有子ReadExactSize(流为NetworkStream,缓冲区为Byte(),偏移量为整数,大小为整数)
而尺寸为0
Dim read=stream.read(缓冲区、偏移量、大小)
如果读取<0,则
抛出新IOException(“过早结束”)
如果结束
大小-=读取
偏移量+=读取
结束时
端接头
末级
使用库“SocksWebProxy”。您可以将它与WebClient&WebRequest一起使用(只需将新的SocksWebProxy分配给*.Proxy属性)。无需Privoxy或类似服务将http流量转换为tor
我还通过启用控制端口对其进行了一些扩展。下面是如何让Tor在后台运行,而不启动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的步骤: