コード例 #1
0
        void StartCommsWithProxyAndImplant(NetworkStream stream)
        {
            var          timeoutd = false;
            IAsyncResult result   = null;
            var          buf      = new byte[1];

            try
            {
                var asyncBufferState = new AsyncBufferState()
                {
                    Buffer = new Byte[HEADERLIMIT], Stream = stream, RecvdData = new AutoResetEvent(false)
                };
                var dataRecvd = asyncBufferState.RecvdData;
                var ctr       = 0;
                while (!ShutdownRecieved)
                {
                    try
                    {
                        if (ShutdownRecieved)
                        {
                            return;
                        }

                        //Use peek to try and force an exception if the connection has closed
                        _tc.Client.Receive(buf, SocketFlags.Peek);
                        if (stream.CanRead && stream.DataAvailable)
                        {
                            result   = stream.BeginRead(asyncBufferState.Buffer, 0, HEADERLIMIT, ProxySocketReadCallback, asyncBufferState);
                            timeoutd = !dataRecvd.WaitOne((int)TOTALSOCKETTIMEOUT);
                            ctr      = 0;
                        }
                        else
                        {
                            TimeoutEvent.WaitOne(50);
                        }
                    }
                    catch (Exception ex)
                    {
                        ServerComms.LogError($"Connection to {_targetHost}:{_targetPort} has dropped: {ex.Message}");
                        ShutdownClient();
                        return;
                    }

                    if (ctr++ >= ((int)TOTALSOCKETTIMEOUT / 100))
                    {
                        stream.Close();
                        //Time out trying to read may as well shutdown the socket
                        ServerComms.LogError($"Connection closed to {_targetHost}:{_targetPort} after ({TOTALSOCKETTIMEOUT / 1000}s) idle. {_targetId}");
                        ShutdownClient();
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                ServerComms.LogError($"Connection to {_targetHost}:{_targetPort} has dropped: {ex.Message}");
                ShutdownClient();
            }
        }
コード例 #2
0
ファイル: SocksProxy.cs プロジェクト: y35uishere/SharpSocks
        void StartCommsWithProxyAndImplant(NetworkStream stream)
        {
            //Check the currently flag as we multiple threads can hit this
            //Interlocked uses an atomic. If there is a thread already reading just bang out here
            if ((Interlocked.CompareExchange(ref CurrentlyReading, 1, 0) == 1))
            {
                return;
            }

            var timeout    = 0;
            var timeoutCtr = 0;
            var wait       = new AutoResetEvent(false);

            //Shutdown has been recieved on another thread we outta here
            while (!ShutdownRecieved)
            {
                if (!stream.DataAvailable)
                {
                    if (1 == timeoutCtr)
                    {
                        timeout += TIMEBETWEENREADS;
                    }

                    TimeoutEvent.WaitOne(timeout);
                }
                else
                {
                    if (!stream.CanRead || !_tc.Connected)
                    {
                        ShutdownClient();
                        ServerComms.LogError($"Connection to {_targetHost}:{_targetPort} has dropped before data sent");
                        return;
                    }
                    try
                    {
                        //Quick check here just in case....
                        if (ShutdownRecieved)
                        {
                            return;
                        }
                        var asyncBufferState = new AsyncBufferState()
                        {
                            Stream = stream, RecvdData = wait
                        };
                        stream.BeginRead(asyncBufferState.Buffer, 0, HEADERLIMIT, ProxySocketReadCallback, asyncBufferState);
                        wait.WaitOne(-1);
                    }
                    catch (Exception ex)
                    {
                        ServerComms.LogError($"Connection to {_targetHost}:{_targetPort} has dropped: {ex.Message}");
                        ShutdownClient();
                    }
                }

                if (timeoutCtr > (TOTALSOCKETTIMEOUT / TIMEBETWEENREADS))
                {
                    //Time out trying to read may as well shutdown the socket
                    ServerComms.LogError($"Connection closed to {_targetHost}:{_targetPort} after ({TOTALSOCKETTIMEOUT / 1000}s) idle. {_targetId}");

                    ShutdownClient();
                    return;
                }
                timeoutCtr++;
            }
        }