public void PrintSecurityInfo() { if (this.NetStream is SslStream) { SslUtils.PrintSslInfo(this.NetStream as SslStream, tcpConnectionLogger); } else { tcpConnectionLogger("Connection is not using SSL/TLS"); } }
public event EventHandler GameOver; //might be used for client when other player suddenly ends game #region Constructor /// <summary> /// Creates all necessary variables and threads for game connection, requires connected <see cref="System.Net.Sockets.TcpClient"/>. /// </summary> /// <param name="client">connected <see cref="System.Net.Sockets.TcpClient"/></param> /// <param name="isClient">true if used on the client side - clients send <see cref="OperationType.CONNECTION_TEST"/> packets to server</param> /// <param name="logger">method to log messages in this object</param> /// <param name="printDebugInfo">prints debug info to console if <see langword="true"/></param> /// <param name="useSSL">if <see langword="true"/> uses <see cref="SslStream"/> instead of bare <see cref="NetworkStream"/>, defaults to <see langword="false"/></param> /// <param name="sslCertificatePath"> specifies path to .cer file containing servers certificate</param> public TcpConnection(TcpClient client, bool isClient, Logger logger, bool printDebugInfo = true, bool useSSL = false, string sslCertificatePath = null) { this.TcpClient = client; try { tcpConnectionLogger = logger; debug = printDebugInfo; if (useSSL) { if (!isClient && !SslUtils.IsAdministrator()) { throw new NotAdministratorException("You need to run server application as local administartor if you want to use SSL!"); } if (!isClient) { serverCertificateObject = SslUtils.LoadServerCertificate(sslCertificatePath, printDebugInfo, logger); SslStream sslStream = new SslStream(client.GetStream(), false); sslStream.AuthenticateAsServer(serverCertificateObject); this.NetStream = sslStream; } else { PublicKeys.SetUsedPublicKey(PublicKeys.SERVER_CERTIFICATE_PUBLIC_KEY_STRING_SERVER); //modify this in order to change location of server application SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(SslUtils.ValidateServerCertificateNoImport), null); sslStream.AuthenticateAsClient(PublicKeys.PublicKeysToServerName[PublicKeys.USED_PUBLIC_KEY]); this.NetStream = sslStream; } } else { this.NetStream = client.GetStream(); } IPEndPoint ipData = client.Client.RemoteEndPoint as IPEndPoint; this.RemoteIpAddress = ipData.Address.ToString(); this.RemotePortNumber = ipData.Port; this.serializer = new BinaryFormatter(); //BinaryFormatter ALWAYS uses little endian this.messageReceivedEvent = new AutoResetEvent(false); this.connectionEndedEvent = new AutoResetEvent(false); alreadyDisconnected = false; keepReceiving = true; RemotePlannedDisconnect = false; receiver = new Thread(new ThreadStart(DoReceiving)); receiver.Start(); if (isClient) { keepTestingConnection = true; connectionTester = new Thread(new ThreadStart(DoTestConnection)); connectionTester.Start(); } } catch (AuthenticationException) { client.Close(); throw; } catch (IOException) { client.Close(); throw; } catch (Exception) { client.Close(); throw; } }