Esempio n. 1
0
        //Start receiving
        internal override void StartReceiving(ISocketState state, int offset = 0)
        {
            try
            {
                state.SslStream = _sslStream;

                if (offset > 0)
                {
                    state.UnhandledBytes = state.Buffer;
                }

                if (state.Buffer.Length < state.BufferSize)
                {
                    state.ChangeBuffer(new byte[state.BufferSize]);
                    if (offset > 0)
                    {
                        Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                    }
                }

                _mreRead.WaitOne();
                _mreRead.Reset();
                state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
Esempio n. 2
0
        private async Task <bool> Authenticate(ISocketState state)
        {
            try
            {
                SslProtocols protocol = SslProtocols.Tls12;

                switch (_tlsProtocol)
                {
                case TlsProtocol.Tls10:
                    protocol = SslProtocols.Tls;
                    break;

                case TlsProtocol.Tls11:
                    protocol = SslProtocols.Tls11;
                    break;

                case TlsProtocol.Tls12:
                    protocol = SslProtocols.Tls12;
                    break;
                }

                await state.SslStream.AuthenticateAsServerAsync(_serverCertificate, true, protocol, false);

                if (!state.SslStream.IsEncrypted)
                {
                    throw new Exception("Stream from client " + state.Id + " is not encrypted.");
                }

                if (!state.SslStream.IsAuthenticated)
                {
                    throw new Exception("Stream from client " + state.Id + " not authenticated.");
                }

                return(true);
            }
            catch (IOException ex)
            {
                switch (ex.Message)
                {
                case "Authentication failed because the remote party has closed the transport stream.":
                case "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.":
                    throw new IOException("IOException " + state.Id + " closed the connection.");

                case "The handshake failed due to an unexpected packet format.":
                    throw new IOException("IOException " + state.Id + " disconnected, invalid handshake.");

                default:
                    throw new IOException(ex.Message, ex);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
 public FileHandlerState(ISocketState state, SocketClient client, ServerListener listener) : base(state, client, listener)
 {
     if (client == null)
     {
         _tempPath = listener.TempPath;
     }
     if (listener == null)
     {
         _tempPath = client.TempPath;
     }
 }
        //Check if the server should allow the client that is attempting to connect.
        internal bool IsConnectionAllowed(ISocketState state)
        {
            if (WhiteList.Count > 0)
            {
                return(CheckWhitelist(state.RemoteIPv4) || CheckWhitelist(state.RemoteIPv6));
            }

            if (BlackList.Count > 0)
            {
                return(!CheckBlacklist(state.RemoteIPv4) && !CheckBlacklist(state.RemoteIPv6));
            }

            return(true);
        }
        //Start receiving
        internal override void StartReceiving(ISocketState state, int offset = 0)
        {
            if (offset > 0)
            {
                state.UnhandledBytes = state.Buffer;
            }

            if (state.Buffer.Length < state.BufferSize)
            {
                state.ChangeBuffer(new byte[state.BufferSize]);
                if (offset > 0)
                {
                    Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                }
            }

            state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, this.ReceiveCallback, state);
        }
Esempio n. 6
0
        private async Task <bool> Authenticate(ISocketState state)
        {
            try
            {
                SslProtocols protocol = SslProtocols.Tls12;

                switch (_tlsProtocol)
                {
                case TlsProtocol.Tls10:
                    protocol = SslProtocols.Tls;
                    break;

                case TlsProtocol.Tls11:
                    protocol = SslProtocols.Tls11;
                    break;

                case TlsProtocol.Tls12:
                    protocol = SslProtocols.Tls12;
                    break;
                }

                await state.SslStream.AuthenticateAsServerAsync(_serverCertificate, true, protocol, false);

                if (!state.SslStream.IsEncrypted)
                {
                    throw new Exception("Stream from client " + state.Id + " is not encrypted.");
                }

                if (!state.SslStream.IsAuthenticated)
                {
                    throw new Exception("Stream from client " + state.Id + " not authenticated.");
                }

                return(true);
            }
            catch (IOException ex)
            {
                throw new IOException(ex.Message, ex);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        protected SocketStateState(ISocketState state, SocketClient client, ServerListener listener)
        {
            State  = state;
            Client = client;
            Server = listener;

            if (client == null)
            {
                Encrypter        = Server.MessageEncryption;
                FileCompressor   = Server.FileCompressor;
                FolderCompressor = Server.FolderCompressor;
            }

            if (Server == null)
            {
                Encrypter        = client.MessageEncryption;
                FileCompressor   = client.FileCompressor;
                FolderCompressor = client.FolderCompressor;
            }
        }
 public MessageHasBeenReceivedState(ISocketState state, SocketClient client, ServerListener listener) : base(state, client, listener)
 {
 }
 public InitialHandlerState(ISocketState state, SocketClient client, ServerListener listener) : base(state, client, listener)
 {
 }
Esempio n. 10
0
 internal Message(byte[] bytes, MessageType type, ISocketState state)
 {
     MessageType  = type;
     MessageBytes = bytes;
     SocketState  = state;
 }
 public FileHasBeenReceivedState(ISocketState state, TcpClient client, ServerListener listener, string tempFilePath) : base(state, client, listener)
 {
     _tempFilePath = tempFilePath;
 }
 public FileHandlerState(ISocketState state, TcpClient client, ServerListener listener) : base(state, client, listener)
 {
 }