Esempio n. 1
0
        public static bool GetViaSocks(bool SocksVer5, string Uri, string Proxy, out string html, out double TimeSpan, int TimeOut)
        {
            html     = string.Empty;
            TimeSpan = 0;

            string _proxyUrl  = html_parser.Match(Proxy, "^.*?(?=:)");
            string _proxyPort = html_parser.Match(Proxy, @"(?<=:)\d*");

            string _host     = html_parser.Match(Uri.Replace("https", "http"), "(?<=http://).*?(?=(/|$))");
            string _hostPort = html_parser.Match(Uri, @"(?<=:)\d*");

            _hostPort = (_hostPort == string.Empty) ? "80" : _hostPort;

            DateTime start = DateTime.Now;

            try
            {
                Socket socket = Socks5Client.Connect(SocksVer5, _proxyUrl, int.Parse(_proxyPort), _host, int.Parse(_hostPort), null, null);
                socket.SendTimeout    = TimeOut;
                socket.ReceiveTimeout = TimeOut;
                string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0";
                byte[] request   = Encoding.ASCII.GetBytes(String.Format("GET / HTTP/1.1\r\nHost: {0}\r\nUser-Agent: {1}\r\n\r\n", _host, userAgent));
                socket.Send(request);
                byte[] buffer = new byte[2048];
                int    recv;

                while ((recv = socket.Receive(buffer, 2048, SocketFlags.None)) > 0)
                {
                    string response = Encoding.ASCII.GetString(buffer, 0, recv);
                    html += response;
                    if (!socket.Poll(1000 * 1000, SelectMode.SelectRead))
                    {
                        break;
                    }
                }
                socket.Close();

                TimeSpan = (DateTime.Now - start).TotalMilliseconds;
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Method connect to host via socks proxy
        /// </summary>
        /// <param name="socksVer5">True if ver 5, false if ver 4</param>
        /// <param name="socksAddress">adress of proxy</param>
        /// <param name="socksPort">port</param>
        /// <param name="destAddress">target url</param>
        /// <param name="destPort">port of dest url</param>
        /// <param name="username">login</param>
        /// <param name="password">password</param>
        /// <returns></returns>
        public static Socket Connect(bool socksVer5, string socksAddress, int socksPort, string destAddress, int destPort, string username, string password)
        {
            Socks5Client client = new Socks5Client(socksVer5, socksAddress, socksPort, destAddress, destPort, username, password);

            return(client.Connect());
        }