public BridgeSslStreamConnector(BridgeSslStreamOptions options, CancellationToken token = default(CancellationToken)) { Options = options ?? throw new ArgumentNullException(nameof(options)); CancelToken = token; SetupDisposables(); }
private SslStream GetSslStream(TcpClient tcpClient, BridgeSslStreamOptions options) { // create the server validation policy to be used by the SSL stream var serverValidationPolicy = options.ServerValidationPolicy; var serverValidationCallback = new RemoteCertificateValidationCallback((o, cert, chain, policy) => { return(serverValidationPolicy(cert, chain, policy)); }); // create the local client certificate selection policy to be used by the SSL Stream var localCertificateSelectionPolicy = options.LocalCertificateSelectionPolicy; var localSelectionCallback = new LocalCertificateSelectionCallback((o, host, localCerts, remoteCerts, acceptableUsers) => { return(localCertificateSelectionPolicy(host, localCerts, remoteCerts, acceptableUsers)); }); // open SslStream using TCP Client Stream to ride on var closeInnerStreamOnOuterStreamClose = false; var stream = new SslStream( tcpClient.GetStream(), closeInnerStreamOnOuterStreamClose, serverValidationCallback, localSelectionCallback, EncryptionPolicy.RequireEncryption ); // set the read and write timeouts on the ssl stream stream.ReadTimeout = options.ReadTimeout; stream.WriteTimeout = options.WriteTimeout; // enssure disposables gets added compositeDisposable.Add(stream); return(stream); }
public static async Task RunMe() { // get the devices on my network var devices = await new LutronDiscovery().DiscoverAllLutronDevices(); // find the first one (there is only one at my house) var firstDevice = devices.FirstOrDefault(); if (firstDevice == null) { throw new Exception("Where is my bridge pro?"); } IPAddress bridgeAddress = firstDevice.IPAddress; var cancelToken = new CancellationToken(); var extraction = new CertificateExtraction(); var options = new BridgeSslStreamOptions(bridgeAddress) { Logging = new ConsoleLogging(), LocalCertificateSelectionPolicy = (a1, a2, a3, a4) => extraction.KeyedCertificate, }; using (var connector = new BridgeSslStreamConnector(options, cancelToken)) { var zone1 = new TestZone(); bool isConnected = await connector.Connect(); connector.Responses.Subscribe((response) => { var r = response; }); var result = await connector.Ping(); await Task.Delay(2000); var result2 = await connector.GetDevices(); await Task.Delay(2000); connector.SendGetScenes(); await Task.Delay(2000); connector.SendGetZoneStatus(zone1); await Task.Delay(2000); connector.SetZoneLevel(zone1, 50); await Task.Delay(2000); connector.TurnOffZone(zone1); await Task.Delay(2000); connector.TurnOnZone(zone1); } }
public BridgeSslStreamConnector(IPAddress bridgeAddress, X509Certificate privateKeyCertificate, CancellationToken token = default(CancellationToken)) { var ipAddress = bridgeAddress ?? throw new ArgumentNullException(nameof(bridgeAddress)); Options = new BridgeSslStreamOptions(ipAddress) { LocalCertificateSelectionPolicy = (a1, a2, a3, a4) => privateKeyCertificate, }; CancelToken = token; SetupDisposables(); }