通过代理C#TcpClient.Connect [英] C# TcpClient.Connect via a proxy
问题描述
我已经搜查高和低试图弄清楚这一点,但到目前为止,我所看到的一切,人们只是告诉我要使用其他方法。
I've searched high and low trying to figure this out, but everything I've seen so far, people are just telling me to use other methods.
有了这样的方式,我的问题是,我想用袜子通过TcpClient的连接到服务器5代理
With that out of the way, my issue is that I'm trying to connect to a server through a TcpClient using a socks 5 proxy
我目前的设置是:
Client = new TcpClient();
Client.Connect(EndPoint);
NetworkStream = Client.GetStream();
Stream = new new BufferedStream(NetworkStream);
Stream.Write...//Write Packet information etc
我不知道如果我错过了任何信息,所以出来,如果我有,我会愉快地更新这一点。
I'm not sure if I've missed any information out so if I have I'll happily update this.
推荐答案
我真的不认为.NET配备的Socks5支持或代理TCP。
I don't really think .Net comes equipped with Socks5 support, or proxied TCP.
有一对夫妇的第三方实现(谷歌知道更多),当然的,但它也很容易实现(部分) RFC 1928年的自己。
There are a couple of third-party implementations (google knows more), of course, but it's also pretty easy to implement (part of) RFC 1928 yourself.
下面是一个例子的Socks5客户端我砍死在一起。你真的想清理:对。 。只是做了AUTH协商,连接设置和最后一个简单的HTTP请求
Here is an example Socks5 client I just hacked together. You will really want to clean it up :p. Just does the auth negotiation, connection setup and finally a simple http request.
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace tcpsocks5
{
static class Program
{
static void ReadAll(this NetworkStream stream, byte[] buffer, int offset, int size)
{
while (size != 0) {
var read = stream.Read(buffer, offset, size);
if (read < 0) {
throw new IOException("Premature end");
}
size -= read;
offset += read;
}
}
static void Main(string[] args)
{
using (var client = new TcpClient()) {
client.Connect(ip, port); // Provide IP, Port yourself
using (var stream = client.GetStream()) {
// Auth
var buf = new byte[300];
buf[0] = 0x05; // Version
buf[1] = 0x01; // NMETHODS
buf[2] = 0x00; // No auth-method
stream.Write(buf, 0, 3);
stream.ReadAll(buf, 0, 2);
if (buf[0] != 0x05) {
throw new IOException("Invalid Socks Version");
}
if (buf[1] == 0xff) {
throw new IOException("Socks Server does not support no-auth");
}
if (buf[1] != 0x00) {
throw new Exception("Socks Server did choose bogus auth");
}
// Request
buf[0] = 0x05; // Version
buf[1] = 0x01; // Connect (TCP)
buf[2] = 0x00; // Reserved
buf[3] = 0x03; // Dest.Addr: Domain name
var domain = Encoding.ASCII.GetBytes("google.com");
buf[4] = (byte)domain.Length; // Domain name length (octet)
Array.Copy(domain, 0, buf, 5, domain.Length);
var port = BitConverter.GetBytes(
IPAddress.HostToNetworkOrder((short)80));
buf[5 + domain.Length] = port[0];
buf[6 + domain.Length] = port[1];
stream.Write(buf, 0, domain.Length + 7);
// Reply
stream.ReadAll(buf, 0, 4);
if (buf[0] != 0x05) {
throw new IOException("Invalid Socks Version");
}
if (buf[1] != 0x00) {
throw new IOException(string.Format("Socks Error {0:X}", buf[1]));
}
var rdest = string.Empty;
switch (buf[3]) {
case 0x01: // IPv4
stream.ReadAll(buf, 0, 4);
var v4 = BitConverter.ToUInt32(buf, 0);
rdest = new IPAddress(v4).ToString();
break;
case 0x03: // Domain name
stream.ReadAll(buf, 0, 1);
if (buf[0] == 0xff) {
throw new IOException("Invalid Domain Name");
}
stream.ReadAll(buf, 1, buf[0]);
rdest = Encoding.ASCII.GetString(buf, 1, buf[0]);
break;
case 0x04: // IPv6
var octets = new byte[16];
stream.ReadAll(octets, 0, 16);
rdest = new IPAddress(octets).ToString();
break;
default:
throw new IOException("Invalid Address type");
}
stream.ReadAll(buf, 0, 2);
var rport = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(buf, 0));
Console.WriteLine("Connected via {0}:{1}", rdest, rport);
// Make an HTTP request, aka. "do stuff ..."
using (var writer = new StreamWriter(stream)) {
writer.Write("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n");
writer.Flush();
using (var reader = new StreamReader(stream)) {
while (true) {
var line = reader.ReadLine();
if (string.IsNullOrEmpty(line)) {
break;
}
}
}
}
}
}
}
}
}
这篇关于通过代理C#TcpClient.Connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!