8

我一直在寻找高低试图弄清楚这一点,但到目前为止我所看到的一切,人们只是告诉我使用其他方法。

顺便说一句,我的问题是我正在尝试使用 socks 5 代理通过 TcpClient 连接到服务器

我目前的设置是:

        Client = new TcpClient();
        Client.Connect(EndPoint);
        NetworkStream = Client.GetStream();
        Stream = new new BufferedStream(NetworkStream);
        Stream.Write...//Write Packet information etc

我不确定我是否错过了任何信息,所以如果我有,我会很乐意更新这个。

4

2 回答 2

11

我真的不认为 .Net 配备了 Socks5 支持或代理 TCP。

当然,有几个第三方实现(谷歌知道更多),但自己实现(部分)RFC 1928也很容易。

这是我刚刚一起破解的 Socks5 客户端示例。你真的很想清理它:p。只是进行身份验证协商、连接设置,最后是一个简单的 http 请求。

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;
                }
              }
            }
          }
        }
      }
    }
  }
}
于 2013-08-17T17:03:34.497 回答
3

我使用starksoft-aspen。它是免费和开源的。

在下面的示例中,TcpClient 在“CreateConnection()”方法中初始化,但您也可以自己初始化客户端,也可以全部异步进行。

using Starksoft.Aspen.Proxy;

    Socks5ProxyClient proxyClient = new Socks5ProxyClient(
        socks5Proxy.Host, socks5Proxy.Port, 
        socks5Proxy.Username, socks5Proxy.Password);

    TcpClient client = proxyClient.CreateConnection(destination.Host, 
        destination.Port);
    NetworkStream stream = client.GetStream();
于 2017-11-03T08:59:21.643 回答