/// <summary> /// Register the singleton ISession instance with the container once we can connect to the killrvideo schema. /// </summary> public async Task RegisterDseOnceAvailable(DryIoc.IContainer container) { IDseSession session = null; int attempts = 0; Logger.Information("Initializing connection to DSE Cluster..."); while (session == null) { try { Logger.Information("+ Reading node addresses from ETCD."); IEnumerable <string> hosts = await _serviceDiscovery .LookupServiceAsync(ConfigKeys.EtcdCassandraKey) .ConfigureAwait(false); // Create cluster builder with contact points var builder = DseCluster.Builder().AddContactPoints(hosts.Select(ToIpEndPoint)); // Authentication if (!string.IsNullOrEmpty(_kvConfig[ConfigKeys.DseUsername]) && !string.IsNullOrEmpty(_kvConfig[ConfigKeys.DsePassword])) { Logger.Information("+ Enable Authentication with user {user}", _kvConfig[ConfigKeys.DseUsername]); builder.WithAuthProvider( new DsePlainTextAuthProvider(_kvConfig[ConfigKeys.DseUsername], _kvConfig[ConfigKeys.DsePassword])); } else { Logger.Information("+ No Authentication"); } // SSL : To be tested (first try based on documentation) if (Boolean.Parse(_kvConfig[ConfigKeys.DseEnableSsl])) { String certPath = _kvConfig[ConfigKeys.DseSslCertPath]; String certPassword = _kvConfig[ConfigKeys.DseSslCertPassword]; if (string.IsNullOrEmpty(certPath)) { throw new ArgumentNullException("Cannot read SSL File " + certPath); } if (string.IsNullOrEmpty(certPath)) { throw new ArgumentNullException("Cannot read SSL Certificate password " + certPath); } Logger.Information("+ Setup SSL options with {certPath}", certPath); SSLOptions sslOptions = new SSLOptions(); X509Certificate2[] certs = new X509Certificate2[] { new X509Certificate2(ReadX509Certificate(certPath), certPassword) }; sslOptions.SetCertificateCollection(new X509CertificateCollection(certs)); sslOptions.SetRemoteCertValidationCallback((a1, a2, a3, a4) => true); //sslOptions.SetHostNameResolver((internalIPAddress) => { return "test_client"; }); builder.WithSSL(sslOptions); } else { Logger.Information("+ No SSL"); } // Query options var queryOptions = new QueryOptions(); queryOptions.SetConsistencyLevel(ConsistencyLevel.LocalQuorum); builder.WithQueryOptions(queryOptions); // Graph Options Logger.Information("+ Graph connection to {graphName}", _kvConfig[ConfigKeys.DseGraphName]); var graphOptions = new GraphOptions(); graphOptions.SetName(_kvConfig[ConfigKeys.DseGraphName]); graphOptions.SetReadTimeoutMillis(int.Parse(_kvConfig[ConfigKeys.DseGraphReadTimeout])); graphOptions.SetReadConsistencyLevel(ConsistencyLevel.One); graphOptions.SetWriteConsistencyLevel(ConsistencyLevel.One); builder.WithGraphOptions(graphOptions); // Cassandra session = builder.Build().Connect(_kvConfig[ConfigKeys.DseKeySpace]); Logger.Information("+ Session established to keyspace {keyspace}", _kvConfig[ConfigKeys.DseKeySpace]); } catch (Exception e) { attempts++; session = null; // Don't log exceptions until we've tried 6 times if (attempts >= int.Parse(_kvConfig[ConfigKeys.MaxRetry])) { Logger.Error(e, "Cannot connection to DSE after {max} attempts, exiting", _kvConfig[ConfigKeys.MaxRetry]); Environment.Exit(404); } } if (session != null) { continue; } Logger.Information("+ Attempt #{nb}/{max} failed.. trying in {delay} seconds, waiting Dse to Start", attempts, _kvConfig[ConfigKeys.MaxRetry], _kvConfig[ConfigKeys.RetryDelay]); await Task.Delay(int.Parse(_kvConfig[ConfigKeys.RetryDelay])).ConfigureAwait(false); } // Since session objects should be created once and then reused, register the instance with the container // which will register it as a singleton by default container.RegisterInstance(session); }
/// <summary> /// Select the SSL options /// </summary> private static void SelectSSLOptions() { int selectedOption; //Configure the server options //These will be applied for incoming connections Console.WriteLine("\nRequire connecting clients to provide certificate?\n1 - Yes (Connection will only be successful if client provides certificate.) \n2 - No (Client only requires certificate name to connect.)"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) { break; } Console.WriteLine("Invalid connection type choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); listenerSSLOptions = new SSLOptions(certificate, true, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); listenerSSLOptions = new SSLOptions(certificate, true, false); } else { throw new Exception("Unable to determine selected option."); } //Configure the connection options //These will be used when establishing outgoing connections Console.WriteLine("\nProvide certificate for outgoing connections?\n1 - Yes (Connection will only be successful if client and server certificate match.)\n2 - No (Client will accept any certificate from server.)"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) { break; } Console.WriteLine("Invalid connection type choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); connectionSSLOptions = new SSLOptions(certificate, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); connectionSSLOptions = new SSLOptions("networkcomms.net", true); } else { throw new Exception("Unable to determine selected option."); } //Select if the dataPadder will be enabled Console.WriteLine("\nWhen sending encrypted data" + " the quantity of traffic can give away a significant amount of information. To prevent this" + " traffic analysis attack we have included a data processor which if enabled ensures every packet sent" + " is of a fixed size. Do you want to enable this padding data processor? " + "\n1 - Yes\n2 - No"); while (true) { bool parseSucces = int.TryParse(Console.ReadKey(true).KeyChar.ToString(), out selectedOption); if (parseSucces && selectedOption <= 2) { break; } Console.WriteLine("Invalid choice. Please try again."); } if (selectedOption == 1) { Console.WriteLine(" ... selected yes."); sendingSendReceiveOptions = new SendReceiveOptions <ProtobufSerializer, DataPadder>(); DataPadder.AddPaddingOptions(sendingSendReceiveOptions.Options, 1024, DataPadder.DataPaddingType.Random, true); } else if (selectedOption == 2) { Console.WriteLine(" ... selected no."); sendingSendReceiveOptions = NetworkComms.DefaultSendReceiveOptions; } else { throw new Exception("Unable to determine selected option."); } }
/// <summary> /// Creates a new ProtocolOptions instance using the provided port and SSL context. /// </summary> /// <param name="port">the port to use for the binary protocol.</param> /// <param name="sslOptions">sslOptions the SSL options to use. Use null if SSL is not to be used.</param> public ProtocolOptions(int port, SSLOptions sslOptions) { _port = port; _sslOptions = sslOptions; }
public static void RunExample() { Console.WriteLine("Please select mode:"); Console.WriteLine("1 - Server (Listens for connections)"); Console.WriteLine("2 - Client (Creates connections to server)"); sendReceiveOptions = new SendReceiveOptions <NullSerializer>(); //RijndaelPSKEncrypter.AddPasswordToOptions(sendReceiveOptions.Options, "test"); //sendReceiveOptions.DataProcessors.Add(DPSManager.GetDataProcessor<RijndaelPSKEncrypter>()); //NetworkComms.ConnectionEstablishTimeoutMS = 600000; NetworkComms.ConnectionListenModeUseSync = true; X509Certificate cert = new X509Certificate2("testCertificate.pfx"); sslOptions = new SSLOptions(cert, true); //Read in user choice if (Console.ReadKey(true).Key == ConsoleKey.D1) { serverMode = true; } else { serverMode = false; } UDPConnection.DefaultUDPOptions = UDPOptions.None; IPAddress localIPAddress = IPAddress.Parse("::1"); packetTypeStr = "Unmanaged"; if (serverMode) { //NetworkComms.DOSProtection.Enabled = true; //NetworkComms.DisableLogging(); //Listen for connections int totalNumberOfListenPorts = 500; int portDivisor = 0; if ((mode & TestMode.TCP_Managed) == TestMode.TCP_Managed) { portDivisor++; } if ((mode & TestMode.TCP_Unmanaged) == TestMode.TCP_Unmanaged) { portDivisor++; } if ((mode & TestMode.UDP_Managed) == TestMode.UDP_Managed) { portDivisor++; } if ((mode & TestMode.UDP_Unmanaged) == TestMode.UDP_Unmanaged) { portDivisor++; } if ((mode & TestMode.TCPSSL_Managed) == TestMode.TCPSSL_Managed) { portDivisor++; } List <EndPoint> localIPEndPoints = new List <EndPoint>(); List <ConnectionListenerBase> listeners = new List <ConnectionListenerBase>(); for (int i = 0; i < totalNumberOfListenPorts / portDivisor; i++) { if ((mode & TestMode.TCP_Managed) == TestMode.TCP_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 10000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled)); } if ((mode & TestMode.TCP_Unmanaged) == TestMode.TCP_Unmanaged) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 20000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Disabled)); } if ((mode & TestMode.UDP_Managed) == TestMode.UDP_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 30000 + i)); listeners.Add(new UDPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled, UDPConnection.DefaultUDPOptions)); } if ((mode & TestMode.UDP_Unmanaged) == TestMode.UDP_Unmanaged) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 40000 + i)); listeners.Add(new UDPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Disabled, UDPConnection.DefaultUDPOptions)); } if ((mode & TestMode.TCPSSL_Managed) == TestMode.TCPSSL_Managed) { localIPEndPoints.Add(new IPEndPoint(localIPAddress, 50000 + i)); listeners.Add(new TCPConnectionListener(sendReceiveOptions, ApplicationLayerProtocolStatus.Enabled, sslOptions)); } } Connection.StartListening(listeners, localIPEndPoints, true); object locker = new object(); int messageCount = 0; long totalBytesReceived = 0; int tcpFragmentationConcatCount = 0; int connectionEstablishCount = 0; int connectionCloseCount = 0; //List<string> packetSequenceNumbers = new List<string>(); NetworkComms.AppendGlobalIncomingPacketHandler <byte[]>(packetTypeStr, (header, connection, data) => { lock (locker) { //long seqNumber = header.GetOption(PacketHeaderLongItems.PacketSequenceNumber); //packetSequenceNumbers.Add(connection.ToString() + "," + seqNumber); //Increment a global counter messageCount++; if (data.Length != testDataSize) { tcpFragmentationConcatCount++; } totalBytesReceived += data.Length; } }, sendReceiveOptions); //Establish handler NetworkComms.AppendGlobalConnectionEstablishHandler((connection) => { lock (locker) connectionEstablishCount++; }); //Close handler NetworkComms.AppendGlobalConnectionCloseHandler((connection) => { lock (locker) connectionCloseCount++; }); //Save the ports list out to disk using (StreamWriter sw = new StreamWriter("TCPServerPorts.txt", false)) { List <EndPoint> localListenEndPoints = Connection.ExistingLocalListenEndPoints(ConnectionType.TCP); foreach (IPEndPoint endPoint in localListenEndPoints) { if (Connection.ExistingLocalListeners <TCPConnectionListener>(endPoint)[0].ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Enabled) { sw.WriteLine("T-" + endPoint.Address.ToString() + "-" + endPoint.Port); } else { sw.WriteLine("F-" + endPoint.Address.ToString() + "-" + endPoint.Port); } } } //Save the ports list out to disk using (StreamWriter sw = new StreamWriter("UDPServerPorts.txt", false)) { List <EndPoint> localListenEndPoints = Connection.ExistingLocalListenEndPoints(ConnectionType.UDP); foreach (IPEndPoint endPoint in localListenEndPoints) { if (Connection.ExistingLocalListeners <UDPConnectionListener>(endPoint)[0].ApplicationLayerProtocol == ApplicationLayerProtocolStatus.Enabled) { sw.WriteLine("T-" + endPoint.Address.ToString() + "-" + endPoint.Port); } else { sw.WriteLine("F-" + endPoint.Address.ToString() + "-" + endPoint.Port); } } } Console.WriteLine("\nSelected mode = {0}, UDPOptions = {1}", mode, UDPConnection.DefaultUDPOptions); Console.WriteLine("Connection close after send = {0}", (closeConnectionAfterSend? "TRUE" : "FALSE")); Console.WriteLine("\nListening for incoming connections on {0} ports. Press 'c' key to see message count.", totalNumberOfListenPorts); while (true) { ConsoleKeyInfo key = Console.ReadKey(true); if (key.KeyChar == 'c') { Console.WriteLine("#Handlers={0}, #Data={2}, #TCPFragConcat={1}, #Establish={3}, #Close={4}. Press 'c' to refresh message count, any other key to quit.", messageCount, tcpFragmentationConcatCount, totalBytesReceived / (double)testDataSize, connectionEstablishCount, connectionCloseCount); //using (StreamWriter sw = new StreamWriter("seqNumbers.txt", false)) //{ // List<string> copy = packetSequenceNumbers.ToList(); // foreach (string line in copy) // sw.WriteLine(line); //} } else { break; } } } else { //NetworkComms.DisableLogging(); //Load server port list string[] tcpServerPortList = File.ReadAllLines("TCPServerPorts.txt"); TCPServerEndPoints = new Dictionary <IPEndPoint, ApplicationLayerProtocolStatus>(); foreach (string current in tcpServerPortList) { TCPServerEndPoints.Add(new IPEndPoint(IPAddress.Parse(current.Split('-')[1]), int.Parse(current.Split('-')[2])), (current.Substring(0, 1) == "T" ? ApplicationLayerProtocolStatus.Enabled : ApplicationLayerProtocolStatus.Disabled)); } TCPServerEndPointsKeys = TCPServerEndPoints.Keys.ToList(); string[] udpServerPortList = File.ReadAllLines("UDPServerPorts.txt"); UDPServerEndPoints = new Dictionary <IPEndPoint, ApplicationLayerProtocolStatus>(); foreach (string current in udpServerPortList) { UDPServerEndPoints.Add(new IPEndPoint(IPAddress.Parse(current.Split('-')[1]), int.Parse(current.Split('-')[2])), (current.Substring(0, 1) == "T" ? ApplicationLayerProtocolStatus.Enabled : ApplicationLayerProtocolStatus.Disabled)); } //UDPConnectionListener udpListener = new UDPConnectionListener(sendReceiveOptions, // ((UDPConnection.DefaultUDPOptions & UDPOptions.Handshake) != UDPOptions.Handshake ? ApplicationLayerProtocolStatus.Disabled : ApplicationLayerProtocolStatus.Enabled), // UDPConnection.DefaultUDPOptions); //Connection.StartListening(udpListener, new IPEndPoint(localIPAddress, 10010), true); Console.WriteLine("Listening for connections on:"); foreach (System.Net.IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.UDP)) { Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); } UDPServerEndPointsKeys = UDPServerEndPoints.Keys.ToList(); Console.WriteLine("\nLoaded {0} TCP & {1} UDP server ports. Press any key to start the hammer!", TCPServerEndPoints.Count, UDPServerEndPointsKeys.Count); Console.ReadKey(true); Console.WriteLine("It's hammer time ..."); clientHammerData = new byte[testDataSize]; //Lets start by making as many connections as possible, go to absolute maximum performance ParallelOptions options = new ParallelOptions(); options.MaxDegreeOfParallelism = 8; Stopwatch timer = new Stopwatch(); timer.Start(); Parallel.For(0, connectionHammerExecCount, options, ConnectionHammer); timer.Stop(); Console.WriteLine("\nCompleted {0} connections in {1} secs. {2}ms per connection. {3} exceptions. Press any key to quit.", connectionHammerExecCount * connectionsPerHammer, (timer.ElapsedMilliseconds / 1000.0).ToString("0.00"), ((double)timer.ElapsedMilliseconds / (connectionHammerExecCount * connectionsPerHammer)).ToString("0.00"), exceptionCount); Console.ReadKey(true); } NetworkComms.Shutdown(); }
/// <summary> /// Creates a new instance of TcpSocket using the endpoint and options provided. /// </summary> public TcpSocket(IConnectionEndPoint endPoint, SocketOptions options, SSLOptions sslOptions) { EndPoint = endPoint; Options = options; SSLOptions = sslOptions; }
public void CassandraAny_Ssl() { CcmCluster testCluster = GetTestCcmClusterForAuthTests(); var sslOptions = new SSLOptions() .SetRemoteCertValidationCallback((s, cert, chain, policyErrors) => { if (policyErrors == SslPolicyErrors.RemoteCertificateChainErrors && chain.ChainStatus.Length == 1 && chain.ChainStatus[0].Status == X509ChainStatusFlags.UntrustedRoot) { //self issued return true; } return policyErrors == SslPolicyErrors.None; }); using (var cluster = Cluster .Builder() .AddContactPoint(testCluster.InitialContactPoint) .WithCredentials("cassandra", "cassandra") .WithSSL(sslOptions) .Build()) { var session = cluster.Connect(); var rs = session.Execute("SELECT * FROM system.schema_keyspaces"); Assert.Greater(rs.Count(), 0); } }
public static void RunExample() { NetworkComms.ConnectionEstablishTimeoutMS = 600000; //Create a suitable certificate if it does not exist if (!File.Exists("testCertificate.pfx")) { CertificateDetails details = new CertificateDetails("CN=networkcomms.net", DateTime.Now, DateTime.Now.AddYears(1)); SSLTools.CreateSelfSignedCertificatePFX(details, "testCertificate.pfx"); } //Load the certificate X509Certificate cert = new X509Certificate2("testCertificate.pfx"); IPAddress localIPAddress = IPAddress.Parse("::1"); Console.WriteLine("Please select mode:"); Console.WriteLine("1 - Server (Listens for connections)"); Console.WriteLine("2 - Client (Creates connections to server)"); //Read in user choice if (Console.ReadKey(true).Key == ConsoleKey.D1) { serverMode = true; } else { serverMode = false; } if (serverMode) { NetworkComms.AppendGlobalIncomingPacketHandler <byte[]>("Data", (header, connection, data) => { Console.WriteLine("Received data (" + data.Length + ") from " + connection.ToString()); }); //Establish handler NetworkComms.AppendGlobalConnectionEstablishHandler((connection) => { Console.WriteLine("Connection established - " + connection); }); //Close handler NetworkComms.AppendGlobalConnectionCloseHandler((connection) => { Console.WriteLine("Connection closed - " + connection); }); SSLOptions sslOptions = new SSLOptions(cert, true, true); TCPConnectionListener listener = new TCPConnectionListener(NetworkComms.DefaultSendReceiveOptions, ApplicationLayerProtocolStatus.Enabled, sslOptions); Connection.StartListening(listener, new IPEndPoint(localIPAddress, 10000), true); Console.WriteLine("\nListening for TCP (SSL) messages on:"); foreach (IPEndPoint localEndPoint in Connection.ExistingLocalListenEndPoints(ConnectionType.TCP)) { Console.WriteLine("{0}:{1}", localEndPoint.Address, localEndPoint.Port); } Console.WriteLine("\nPress any key to quit."); ConsoleKeyInfo key = Console.ReadKey(true); } else { ConnectionInfo serverInfo = new ConnectionInfo(new IPEndPoint(localIPAddress, 10000)); SSLOptions sslOptions = new SSLOptions("networkcomms.net", true); //SSLOptions sslOptions = new SSLOptions(cert, true); TCPConnection conn = TCPConnection.GetConnection(serverInfo, NetworkComms.DefaultSendReceiveOptions, sslOptions); conn.SendObject("Data", sendArray); Console.WriteLine("Sent data to server."); Console.WriteLine("\nClient complete. Press any key to quit."); Console.ReadKey(true); } NetworkComms.Shutdown(); }