Beispiel #1
0
        private void OnAuthenticate(Task result, ClientState state)
        {
            SslStream sslStream = (SslStream)state.Stream;

            try
            {
                result.GetAwaiter().GetResult();
                _log.WriteLine("Server authenticated to client with encryption cipher: {0} {1}-bit strength",
                               sslStream.CipherAlgorithm, sslStream.CipherStrength);

                // Start listening for data from the client connection.
                sslStream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
            }
            catch (AuthenticationException authEx)
            {
                _log.WriteLine(
                    "Server disconnecting from client during authentication.  No shared SSL/TLS algorithm. ({0})",
                    authEx);
                state.Dispose();
            }
            catch (Exception ex)
            {
                _log.WriteLine("Server disconnecting from client during authentication.  Exception: {0}",
                               ex.Message);
                state.Dispose();
            }
        }
Beispiel #2
0
        private void OnReceive(IAsyncResult result)
        {
            ClientState state = (ClientState)result.AsyncState;

            try
            {
                int bytesReceived = state.Stream.EndRead(result);
                if (bytesReceived == 0)
                {
                    state.Dispose();
                    return;
                }

                if (_receiveCallback != null)
                {
                    _receiveCallback(state.ReceiveBuffer, bytesReceived, state.Stream);
                }
                else
                {
                    // Echo back what we received
                    state.Stream.Write(state.ReceiveBuffer, 0, bytesReceived);
                }

                // Read more from client (asynchronous)
                state.Stream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
            }
            catch (IOException)
            {
                state.Dispose();
                return;
            }
            catch (SocketException)
            {
                state.Dispose();
                return;
            }
            catch (ObjectDisposedException)
            {
                state.Dispose();
                return;
            }
            catch (InvalidOperationException e)
            {
                throw new InvalidOperationException(
                          $"Exception in {nameof(DummyTcpServer)} created with stack: {_creationStack}",
                          e);
            }
        }
Beispiel #3
0
        private void OnAuthenticate(IAsyncResult result)
        {
            ClientState state     = (ClientState)result.AsyncState;
            SslStream   sslStream = (SslStream)state.Stream;

            try
            {
                sslStream.EndAuthenticateAsServer(result);
                _log.WriteLine("Server({0}) authenticated to client({1}) with encryption cipher: {2} {3}-bit strength",
                               state.TcpClient.Client.LocalEndPoint, state.TcpClient.Client.RemoteEndPoint,
                               sslStream.CipherAlgorithm, sslStream.CipherStrength);

                // Start listening for data from the client connection.
                sslStream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
            }
            catch (AuthenticationException authEx)
            {
                _log.WriteLine(
                    "Server({0}) disconnecting from client({1}) during authentication.  No shared SSL/TLS algorithm. ({2})",
                    state.TcpClient.Client.LocalEndPoint,
                    state.TcpClient.Client.RemoteEndPoint,
                    authEx);
            }
            catch (Exception ex)
            {
                _log.WriteLine("Server({0}) disconnecting from client({1}) during authentication.  Exception: {2}",
                               state.TcpClient.Client.LocalEndPoint, state.TcpClient.Client.RemoteEndPoint, ex.Message);
            }
            finally
            {
                state.Dispose();
            }
        }
Beispiel #4
0
        public void Dispose()
        {
            try {
                // this must be done before unloading plugins, or it can cause a race condition
                // due to rendering happening on another thread, where a plugin might receive
                // a render call after it has been disposed, which can crash if it attempts to
                // use any resources that it freed in its own Dispose method
                InterfaceManager?.Dispose();

                try {
                    PluginManager.UnloadPlugins();
                } catch (Exception ex) {
                    Log.Error(ex, "Plugin unload failed.");
                }

                Framework.Dispose();
                ClientState.Dispose();

                this.unloadSignal.Dispose();

                WinSock2.Dispose();

                SigScanner.Dispose();

                Data.Dispose();

                AntiDebug.Dispose();

                Log.Debug("Dalamud::Dispose OK!");
            } catch (Exception ex) {
                Log.Error(ex, "skjdgjjkodsfg");
            }
        }
Beispiel #5
0
        private void OnDestroy()
        {
            _socket?.Close();
            _socket = null;

            _state?.Dispose();
            _state = null;
        }
        private void OnReceive(IAsyncResult result)
        {
            ClientState state = (ClientState)result.AsyncState;

            try
            {
                int bytesReceived = state.Stream.EndRead(result);
                if (bytesReceived == 0)
                {
                    state.Dispose();
                    return;
                }

                if (_receiveCallback != null)
                {
                    _receiveCallback(state.ReceiveBuffer, bytesReceived, state.Stream);
                }
                else
                {
                    // Echo back what we received
                    state.Stream.Write(state.ReceiveBuffer, 0, bytesReceived);
                }

                // Read more from client (asynchronous)
                state.Stream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
            }
            catch (IOException)
            {
                state.Dispose();
                return;
            }
            catch (SocketException)
            {
                state.Dispose();
                return;
            }
            catch (ObjectDisposedException)
            {
                state.Dispose();
                return;
            }
        }
Beispiel #7
0
 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
 {
     Mubox.View.Client.ClientWindowCollection.Instance.Remove(this);
     ClientState.Dispose();
     try
     {
         if (timerStatusRefresh != null)
         {
             timerStatusRefresh.Stop();
             timerStatusRefresh = null;
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         Debug.WriteLine(ex.StackTrace);
     }
     base.OnClosing(e);
 }
Beispiel #8
0
        private void OnAccept(Task <TcpClient> result)
        {
            TcpClient client = null;

            // Accept current connection
            try
            {
                client = result.Result;
            }
            catch
            {
            }

            // If we have a connection, then process it
            if (client != null)
            {
                OnClientAccepted(client);

                ClientState state;

                // Start authentication for SSL?
                if (_useSsl)
                {
                    state = new ClientState(client, _sslEncryptionPolicy);
                    _log.WriteLine("Server: starting SSL authentication.");


                    SslStream        sslStream   = null;
                    X509Certificate2 certificate = Configuration.Certificates.GetServerCertificate();

                    try
                    {
                        sslStream = (SslStream)state.Stream;

                        _log.WriteLine("Server: attempting to open SslStream.");
                        sslStream.AuthenticateAsServerAsync(certificate, false, _sslProtocols, false).ContinueWith(t =>
                        {
                            certificate.Dispose();
                            OnAuthenticate(t, state);
                        }, TaskScheduler.Default);
                    }
                    catch (Exception ex)
                    {
                        _log.WriteLine("Server: Exception: {0}", ex);
                        certificate.Dispose();
                        state.Dispose(); // close connection to client
                    }
                }
                else
                {
                    state = new ClientState(client);

                    // Start listening for data from the client connection
                    try
                    {
                        state.Stream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
                    }
                    catch
                    {
                    }
                }
            }

            // Listen for more client connections
            try
            {
                _listener.AcceptTcpClientAsync().ContinueWith(t => OnAccept(t), TaskScheduler.Default);
            }
            catch
            {
            }
        }
Beispiel #9
0
        private void OnAuthenticate(Task result, ClientState state)
        {
            SslStream sslStream = (SslStream)state.Stream;

            try
            {
                result.GetAwaiter().GetResult();
                _log.WriteLine("Server authenticated to client with encryption cipher: {0} {1}-bit strength",
                    sslStream.CipherAlgorithm, sslStream.CipherStrength);

                // Start listening for data from the client connection.
                sslStream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
            }
            catch (AuthenticationException authEx)
            {
                _log.WriteLine(
                    "Server disconnecting from client during authentication.  No shared SSL/TLS algorithm. ({0})",
                    authEx);
            }
            catch (Exception ex)
            {
                _log.WriteLine("Server disconnecting from client during authentication.  Exception: {0}",
                    ex.Message);
            }
            finally
            {
                state.Dispose();
            }
        }
Beispiel #10
0
        private void OnAccept(Task<TcpClient> result)
        {
            TcpClient client = null;

            // Accept current connection
            try
            {
                client = result.Result;
            }
            catch
            {
            }

            // If we have a connection, then process it
            if (client != null)
            {
                OnClientAccepted(client);

                ClientState state;

                // Start authentication for SSL?
                if (_useSsl)
                {
                    state = new ClientState(client, _sslEncryptionPolicy);
                    _log.WriteLine("Server: starting SSL authentication.");


                    SslStream sslStream = null;
                    X509Certificate2 certificate = TestConfiguration.GetServerCertificate();

                    try
                    {
                        sslStream = (SslStream)state.Stream;

                        _log.WriteLine("Server: attempting to open SslStream.");
                        sslStream.AuthenticateAsServerAsync(certificate, false, _sslProtocols, false).ContinueWith(t => OnAuthenticate(t, state), TaskScheduler.Default);
                    }
                    catch (Exception ex)
                    {
                        _log.WriteLine("Server: Exception: {0}", ex);

                        state.Dispose(); // close connection to client
                    }
                }
                else
                {
                    state = new ClientState(client);

                    // Start listening for data from the client connection
                    try
                    {
                        state.Stream.BeginRead(state.ReceiveBuffer, 0, state.ReceiveBuffer.Length, OnReceive, state);
                    }
                    catch
                    {
                    }
                }
            }

            // Listen for more client connections
            try
            {
                _listener.AcceptTcpClientAsync().ContinueWith(t => OnAccept(t), TaskScheduler.Default);
            }
            catch
            {
            }
        }
Beispiel #11
0
        private void Read_Callback(IAsyncResult ar)
        {
            try
            {
                ClientState cs = (ClientState)ar.AsyncState;
                if (null == cs)
                {
                    return;
                }
                if (null == cs.client)
                {
                    return;
                }
                if (!cs.client.Connected)
                {
                    cs.Dispose();
                    try
                    {
                        cs.client.Close();
                        //cs.client.GetStream().Close();
                    }
                    catch { }
                    cs.client = null;
                    return;
                }
                if (cs.IsConnected == false)
                {
                    return;
                }

                var len = cs.client.GetStream().EndRead(ar);
                if (len > 0)
                {
                    if (null == cs.received)
                    {
                        cs.received = new byte[len];
                        cs.length   = len;
                    }
                    else
                    {
                        cs.length  += len;
                        cs.received = CustomConvert.expand(cs.received, cs.length);
                    }
                    Buffer.BlockCopy(cs.buffer, 0, cs.received, cs.length - len, len);
                    cs.client.GetStream().BeginRead(cs.buffer, 0, ClientState.SIZE, new AsyncCallback(Read_Callback), cs);
                    if (cs.length >= cs.received[2])
                    {
                        var buffer = new IridiumBuffer();
                        buffer.socket = null;
                        buffer.length = cs.length;
                        buffer.buffer = new byte[cs.length];
                        Buffer.BlockCopy(cs.received, 0, buffer.buffer, 0, cs.length);
                        cs.length   = 0;
                        cs.received = null;
                        _iridium.AddQueue(buffer);
                        try
                        {
                            cs.Dispose();
                            cs.client.Close();
                            //if (_iridium.ShowPackageInformation)
                            ShowHistory("MT operation complete, client disconnected.", true);
                        }
                        catch (Exception e)
                        {
                            ShowHistory("Handle Iridium package error: " + e.Message + Environment.NewLine + e.StackTrace, true);
                        }
                        finally
                        {
                            cs.client = null;
                            cs        = null;
                        }
                    }
                }
                else
                {
                    try
                    {
                        cs.Dispose();
                        cs.client.GetStream().Close();
                        cs.client.Close();
                    }
                    catch (Exception ee)
                    {
                        ShowHistory("Handle Iridium package error(len<=0): " + ee.Message + Environment.NewLine + ee.StackTrace, true);
                    }
                    finally
                    {
                        cs = null;
                    }
                }
            }
            catch (Exception error)
            { ShowHistory("Read_Callback error: " + error.Message + Environment.NewLine + error.StackTrace, true); }
        }
Beispiel #12
0
 public void Disconnect()
 {
     ClientState.Dispose();
     GlobalConnectionManifest.DeregisterClient(this);
 }