http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 9453|回复: 4

[Scoket] Socket代理方面的问题,socks5代理

[复制链接]
发表于 2013-10-17 12:31:19 | 显示全部楼层 |阅读模式
网上找的一个类  大家看哈可行否
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;

  5. /*
  6. * zahmed
  7. * Date 23 Jan 2004
  8. * Socks 5 RFC is available at http://www.faqs.org/rfcs/rfc1928.html.
  9. */
  10. namespace ConProxy
  11. {
  12.     public class ConnectionException : ApplicationException
  13.     {
  14.         public ConnectionException(string message)
  15.             : base(message)
  16.         {
  17.         }
  18.     }

  19.     /// <summary>
  20.     /// Provides sock5 functionality to clients (Connect only).
  21.     /// </summary>
  22.     public class SocksProxy
  23.     {

  24.         private SocksProxy() { }

  25.         #region ErrorMessages
  26.         private static string[] errorMsgs = {
  27.           "Operation completed successfully.",
  28.           "General SOCKS server failure.",
  29.           "Connection not allowed by ruleset.",
  30.           "Network unreachable.",
  31.           "Host unreachable.",
  32.           "Connection refused.",
  33.           "TTL expired.",
  34.           "Command not supported.",
  35.           "Address type not supported.",
  36.           "Unknown error."
  37.          };
  38.         #endregion

  39.         /// <summary>
  40.         /// Socks5代理
  41.         /// </summary>
  42.         /// <param name="proxyAdress">代理服务器地址</param>
  43.         /// <param name="proxyPort">代理服务器端口</param>
  44.         /// <param name="destAddress">目标服务器地址</param>
  45.         /// <param name="destPort">目标服务器端口</param>
  46.         /// <param name="userName">用户名</param>
  47.         /// <param name="password">密码</param>
  48.         /// <returns></returns>
  49.         public static Socket ConnectToSocks5Proxy(string proxyAdress, ushort proxyPort,
  50.             string destAddress, ushort destPort,
  51.             string userName, string password)
  52.         {
  53.             IPAddress destIP = null;
  54.             IPAddress proxyIP = null;
  55.             byte[] request = new byte[257];
  56.             byte[] response = new byte[257];
  57.             ushort nIndex;

  58.             try
  59.             {
  60.                 proxyIP = IPAddress.Parse(proxyAdress);
  61.             }
  62.             catch (FormatException)
  63.             { // get the IP address
  64.                 proxyIP = Dns.GetHostAddresses(proxyAdress)[0];
  65.             }

  66.             // Parse destAddress (assume it in string dotted format "212.116.65.112" )
  67.             try
  68.             {
  69.                 destIP = IPAddress.Parse(destAddress);
  70.             }
  71.             catch (FormatException)
  72.             {
  73.                 // wrong assumption its in domain name format "www.microsoft.com"
  74.             }

  75.             IPEndPoint proxyEndPoint = new IPEndPoint(proxyIP, proxyPort);

  76.             // open a TCP connection to SOCKS server...
  77.             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  78.             s.Connect(proxyEndPoint);

  79.             nIndex = 0;
  80.             request[nIndex++] = 0x05; // Version 5.
  81.             request[nIndex++] = 0x02; // 2 Authentication methods are in packet...
  82.             request[nIndex++] = 0x00; // NO AUTHENTICATION REQUIRED
  83.             request[nIndex++] = 0x02; // USERNAME/PASSWORD


  84.             // Send the authentication negotiation request...
  85.             s.Send(request, nIndex, SocketFlags.None);

  86.             // Receive 2 byte response...
  87.             int nGot = s.Receive(response, 2, SocketFlags.None);
  88.             if (nGot != 2)
  89.                 throw new ConnectionException("Bad response received from proxy server.");

  90.             if (response[1] == 0xFF)
  91.             { // No authentication method was accepted close the socket.
  92.                 s.Close();
  93.                 throw new ConnectionException("None of the authentication method was accepted by proxy server.");
  94.             }

  95.             byte[] rawBytes;

  96.             if (/*response[1]==0x02*/true)
  97.             {//Username/Password Authentication protocol
  98.                 nIndex = 0;
  99.                 request[nIndex++] = 0x05; // Version 5.

  100.                 // add user name
  101.                 request[nIndex++] = (byte)userName.Length;
  102.                 rawBytes = Encoding.Default.GetBytes(userName);
  103.                 rawBytes.CopyTo(request, nIndex);
  104.                 nIndex += (ushort)rawBytes.Length;

  105.                 // add password
  106.                 request[nIndex++] = (byte)password.Length;
  107.                 rawBytes = Encoding.Default.GetBytes(password);
  108.                 rawBytes.CopyTo(request, nIndex);
  109.                 nIndex += (ushort)rawBytes.Length;

  110.                 // Send the Username/Password request
  111.                 s.Send(request, nIndex, SocketFlags.None);
  112.                 // Receive 2 byte response...
  113.                 nGot = s.Receive(response, 2, SocketFlags.None);
  114.                 if (nGot != 2)
  115.                     throw new ConnectionException("Bad response received from proxy server.");
  116.                 if (response[1] != 0x00)
  117.                     throw new ConnectionException("Bad Usernaem/Password.");
  118.             }
  119.             // This version only supports connect command.
  120.             // UDP and Bind are not supported.

  121.             // Send connect request now...
  122.             nIndex = 0;
  123.             request[nIndex++] = 0x05; // version 5.
  124.             request[nIndex++] = 0x01; // command = connect.
  125.             request[nIndex++] = 0x00; // Reserve = must be 0x00

  126.             if (destIP != null)
  127.             {// Destination adress in an IP.
  128.                 switch (destIP.AddressFamily)
  129.                 {
  130.                     case AddressFamily.InterNetwork:
  131.                         // Address is IPV4 format
  132.                         request[nIndex++] = 0x01;
  133.                         rawBytes = destIP.GetAddressBytes();
  134.                         rawBytes.CopyTo(request, nIndex);
  135.                         nIndex += (ushort)rawBytes.Length;
  136.                         break;
  137.                     case AddressFamily.InterNetworkV6:
  138.                         // Address is IPV6 format
  139.                         request[nIndex++] = 0x04;
  140.                         rawBytes = destIP.GetAddressBytes();
  141.                         rawBytes.CopyTo(request, nIndex);
  142.                         nIndex += (ushort)rawBytes.Length;
  143.                         break;
  144.                 }
  145.             }
  146.             else
  147.             {// Dest. address is domain name.
  148.                 request[nIndex++] = 0x03; // Address is full-qualified domain name.
  149.                 request[nIndex++] = Convert.ToByte(destAddress.Length); // length of address.
  150.                 rawBytes = Encoding.Default.GetBytes(destAddress);
  151.                 rawBytes.CopyTo(request, nIndex);
  152.                 nIndex += (ushort)rawBytes.Length;
  153.             }

  154.             // using big-edian byte order
  155.             byte[] portBytes = BitConverter.GetBytes(destPort);
  156.             for (int i = portBytes.Length - 1; i >= 0; i--)
  157.                 request[nIndex++] = portBytes[i];

  158.             // send connect request.
  159.             s.Send(request, nIndex, SocketFlags.None);
  160.             s.Receive(response); // Get variable length response...
  161.             if (response[1] != 0x00)
  162.                 throw new ConnectionException(errorMsgs[response[1]]);
  163.             // Success Connected...
  164.             return s;
  165.         }
  166.     }
  167. }
复制代码
我有几点疑问。
首先代理服务器 有的是只有ip跟端口  而有的则还需要用户名和密码, 怎么去判断服务器是否需要提供用户名和密码呢? 还有 那个if里面注释了改成true的是判断是否需要用户名跟密码的吗?
然后呢  这个代码整体上对于socks5代理有没有问题,为什么总是不行呢,到底是我在网上找的资源不行还是..
资源在这里找的  http://www.cz88.net/proxy/socks5_2.aspx




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-8-1 23:06:01 | 显示全部楼层
我也在找socks5代理的解决办法,有没有测试成功的代码。
发表于 2014-8-2 11:16:02 | 显示全部楼层
加油,我们都看好你哦。
发表于 2014-9-9 22:45:41 | 显示全部楼层
强烈支持楼主ing……
 楼主| 发表于 2014-9-12 18:27:05 | 显示全部楼层
copyright123 发表于 2014-8-1 23:06
我也在找socks5代理的解决办法,有没有测试成功的代码。

没有啊
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-26 15:52

© 2014-2021

快速回复 返回顶部 返回列表