Ejemplo n.º 1
0
        public static async Task ConnectionHandler(Socket sock)
        {
            TcpConnection conn = new TcpConnection()
            {
                Socket = sock
            };

            byte[] hello = new TcpSHello(0).Build();
            using (NetworkStream str = new NetworkStream(sock))
            {
                if (SslManager.Certificate == null)
                {
                    conn.Stream = str;
                    str.Write(hello, 0, hello.Length);
                    // Insecure Mode
                    while (true)
                    {
                        Packet pack = str.NextPacket(conn);
                        CommsManager.Execute(pack);
                    }
                }
                else
                {
                    using (SslStream str2 = new SslStream(str))
                    {
                        conn.Stream = str2;
                        SslServerAuthenticationOptions ao = new SslServerAuthenticationOptions()
                        {
                            ServerCertificate                   = SslManager.Certificate,
                            ClientCertificateRequired           = true,
                            RemoteCertificateValidationCallback = RemoteCertificateValidationCallback,
                            CertificateRevocationCheckMode      = X509RevocationMode.NoCheck,
                            EnabledSslProtocols                 = SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13
                        };
                        await str2.AuthenticateAsServerAsync(ao);

                        if (!str2.IsMutuallyAuthenticated && SslManager.CACertificate != null)
                        {
                            Log.Warning("Connection {IP} failed to authenticate in time", sock.RemoteEndPoint.ToString());
                            sock.Close();
                            return;
                        }
                        else
                        {
                            Log.Information("{Client} established a connection successfully", str2.RemoteCertificate?.Subject);
                        }
                        str2.Write(hello, 0, hello.Length);

                        while (true)
                        {
                            Packet pack = str2.NextPacket(conn);
                            CommsManager.Execute(pack);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private static async Task UdpLoop()
        {
            Log.Information("UDP server started");
            while (true)
            {
                var udprr = await _client.ReceiveAsync();

                Packet pack = null;
                try
                {
                    pack = new Packet(udprr.Buffer, udprr.RemoteEndPoint);
                    CommsManager.Execute(pack);
                }
                catch (Exception e)
                {
                    Log.Warning(e, "Error while parsing a packet. Possibly corrupted in transport");
                }
            }
            // ReSharper disable once FunctionNeverReturns
        }