示例#1
0
        public static DuplexStream GetHandshakedDuplexStream(string address, bool allowHttp2Communication = true, bool useMock = false)
        {
            string selectedProtocol = null;

            var extensions = new[] { ExtensionType.Renegotiation, ExtensionType.ALPN };
            var useHandshake = ConfigurationManager.AppSettings["handshakeOptions"] != "no-handshake";

            var protocols = new List<string> { Protocols.Http1 };
            if (allowHttp2Communication)
            {
                protocols.Add(Protocols.Http2);
            }

            var options = UseSecurePort
                              ? new SecurityOptions(SecureProtocol.Tls1, extensions, protocols,
                                                    ConnectionEnd.Client)
                              : new SecurityOptions(SecureProtocol.None, extensions, protocols,
                                                    ConnectionEnd.Client);

            options.VerificationType = CredentialVerification.None;
            options.Certificate = Org.Mentalis.Security.Certificates.Certificate.CreateFromCerFile(@"certificate.pfx");
            options.Flags = SecurityFlags.Default;
            options.AllowedAlgorithms = SslAlgorithms.RSA_AES_256_SHA | SslAlgorithms.NULL_COMPRESSION;

            var sessionSocket = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream,
                                                ProtocolType.Tcp, options);

            using (var monitor = new ALPNExtensionMonitor())
            {
                monitor.OnProtocolSelected += (sender, args) => { selectedProtocol = args.SelectedProtocol; };

                var uri = new Uri(GetAddress() + address);

                sessionSocket.Connect(new DnsEndPoint(uri.Host, uri.Port), monitor);

                if (useHandshake)
                {
                    sessionSocket.MakeSecureHandshake(options);
                }
            }

            //SendSessionHeader(sessionSocket);

            return useMock ? new Mock<DuplexStream>(sessionSocket, true).Object : new DuplexStream(sessionSocket, true);
        }
 public ConfigurationAdapter(Cfg.Configuration configuration)
 {
     this.configuration = configuration;
 }