Example #1
0
 protected override async Task ClientStartListen(HttpConnection connection)
 {
     if (connection.NetworkStream == null && DualSettings.Certificate != null &&
         connection.NetworkClient != null
         )
     {
         var peaker = new StreamPeaker(connection.NetworkClient.GetStream());
         var mark   = peaker.FirstByte;
         if (mark != 0 && (mark < 32 || mark >= 127))
         {
             var ssl = new SslStream(peaker, false);
             connection.NetworkStream = ssl;
             ssl.AuthenticateAsServer(
                 serverCertificate:          DualSettings.Certificate,
                 clientCertificateRequired:  false,
                 enabledSslProtocols:        SslProtocols.None,
                 checkCertificateRevocation: true
                 );
             if (!ssl.IsAuthenticated)
             {
                 ssl.Dispose();
                 connection.NetworkClient.Close();
                 AllConnections.Remove(connection);
                 return;
             }
         }
         else
         {
             connection.NetworkStream = peaker;
         }
     }
     await base.ClientStartListen(connection).ConfigureAwait(false);
 }
Example #2
0
 /// <summary>
 /// 显式撤销连接对象
 /// </summary>
 public void Dispose()
 {
     if (IsConnected)
     {
         Disconnect();
     }
     AllConnections.Remove(this);
 }
Example #3
0
        /// <summary>
        /// Disconnect a client and remove it form the client list
        /// </summary>
        /// <param name="name">Client name</param>
        public void RemoveConnection(string name)
        {
            if (!AllConnections.ContainsKey(name))
            {
                return;
            }

            TConnection Conn = AllConnections[name];

            AllConnectionsList.Remove(Conn);

            Conn.IncomingData = null;
            Conn.OutgoingData = null;

            AllConnections.Remove(name);
        }
Example #4
0
        private void CleanupConnections(object sender)
        {
            for (int i = 0; i < AllConnectionsList.Count; i++)
            {
                var C = AllConnectionsList[i];
                if (C.ConnStats.HasTimedOut)
                {
                    if (C.ConnectionSocket.Connected)
                    {
                        C.ConnectionSocket.Disconnect(false);
                    }

                    AllConnectionsList.Remove(C);
                    AllConnections.Remove(C.Address);
                }
            }
        }
Example #5
0
        protected virtual void SecureClientConnected(TcpClient client)
        {
            if (SecureSettings.Certificate == null)
            {
                client.Close();
                return;
            }
            //prepare session
            var connection = new HttpConnection()
            {
                NetworkClient = client,
                Ip            = client.Client.RemoteEndPoint is IPEndPoint iPEndPoint
                    ? iPEndPoint.Address.ToString()
                    : client.Client.RemoteEndPoint?.ToString(),
            };

            AllConnections.Add(connection);
            //listen to connection
            _ = Task.Run(async() =>
            {
                //authentificate as server and establish ssl connection
                var stream = new SslStream(client.GetStream(), false);
                connection.NetworkStream = stream;
                stream.AuthenticateAsServer(
                    serverCertificate:          SecureSettings.Certificate,
                    clientCertificateRequired:  false,
                    enabledSslProtocols:        SslProtocols.None,
                    checkCertificateRevocation: true
                    );
                if (!stream.IsAuthenticated)
                {
                    stream.Dispose();
                    client.Close();
                    AllConnections.Remove(connection);
                    return;
                }

                await SafeClientStartListen(connection).ConfigureAwait(false);
            });
        }
Example #6
0
 /// <summary>
 /// 显式撤销连接对象
 /// </summary>
 public void Dispose()
 {
     Disconnect();
     AllConnections.Remove(this);
 }
Example #7
0
 public bool RemoveConnection(IConnection connection) => AllConnections.Remove(connection);