public override string ToString() { var sb = StringBuilderCache.Allocate(); sb.AppendFormat("{0}:{1}", Host, Port); var args = new List <string>(); if (Client != null) { args.Add("Client=" + Client); } if (Password != null) { args.Add("Password="******"Db=" + Db); } if (Ssl) { args.Add("Ssl=true"); } if (SslProtocols != null) { args.Add("SslProtocols=" + SslProtocols.ToString()); } if (ConnectTimeout != RedisConfig.DefaultConnectTimeout) { args.Add("ConnectTimeout=" + ConnectTimeout); } if (SendTimeout != RedisConfig.DefaultSendTimeout) { args.Add("SendTimeout=" + SendTimeout); } if (ReceiveTimeout != RedisConfig.DefaultReceiveTimeout) { args.Add("ReceiveTimeout=" + ReceiveTimeout); } if (RetryTimeout != RedisConfig.DefaultRetryTimeout) { args.Add("RetryTimeout=" + RetryTimeout); } if (IdleTimeOutSecs != RedisConfig.DefaultIdleTimeOutSecs) { args.Add("IdleTimeOutSecs=" + IdleTimeOutSecs); } if (NamespacePrefix != null) { args.Add("NamespacePrefix=" + NamespacePrefix.UrlEncode()); } if (args.Count > 0) { sb.Append("?").Append(string.Join("&", args)); } return(StringBuilderCache.ReturnAndFree(sb)); }
/// <summary> /// Returns the effective configuration string for this configuration /// with the option to include or exclude the password from the string. /// </summary> /// <param name="includePassword">Whether to include the password.</param> public string ToString(bool includePassword) { var sb = new StringBuilder(); foreach (var endpoint in EndPoints) { Append(sb, Format.ToString(endpoint)); } Append(sb, OptionKeys.ClientName, ClientName); Append(sb, OptionKeys.ServiceName, ServiceName); Append(sb, OptionKeys.KeepAlive, keepAlive); Append(sb, OptionKeys.SyncTimeout, syncTimeout); Append(sb, OptionKeys.AsyncTimeout, asyncTimeout); Append(sb, OptionKeys.AllowAdmin, allowAdmin); Append(sb, OptionKeys.Version, defaultVersion); Append(sb, OptionKeys.ConnectTimeout, connectTimeout); Append(sb, OptionKeys.Password, (includePassword || string.IsNullOrEmpty(Password)) ? Password : "******"); Append(sb, OptionKeys.TieBreaker, tieBreaker); Append(sb, OptionKeys.WriteBuffer, writeBuffer); Append(sb, OptionKeys.Ssl, ssl); Append(sb, OptionKeys.SslProtocols, SslProtocols?.ToString().Replace(',', '|')); Append(sb, OptionKeys.SslHost, sslHost); Append(sb, OptionKeys.HighPrioritySocketThreads, highPrioritySocketThreads); Append(sb, OptionKeys.ConfigChannel, configChannel); Append(sb, OptionKeys.AbortOnConnectFail, abortOnConnectFail); Append(sb, OptionKeys.ResolveDns, resolveDns); Append(sb, OptionKeys.ChannelPrefix, (string)ChannelPrefix); Append(sb, OptionKeys.ConnectRetry, connectRetry); Append(sb, OptionKeys.Proxy, proxy); Append(sb, OptionKeys.ConfigCheckSeconds, configCheckSeconds); Append(sb, OptionKeys.ResponseTimeout, responseTimeout); Append(sb, OptionKeys.DefaultDatabase, DefaultDatabase); commandMap?.AppendDeltas(sb); return(sb.ToString()); }
public static SSLType GetSSLType(this SslProtocols spe) { Log.Start(); #if DEBUG Log.Debug("(GetSSLType) SSL Protocol Value: {0}", spe.ToString()); #endif #if !DEBUG && VERBOSE Log.Verbose("(GetSSLType) SSL Protocol Value: {0}", ste.ToString()); #endif switch (spe) { case SslProtocols.Ssl3 | SslProtocols.Tls: return(SSLType.AuthTLSv10); case SslProtocols.Ssl3 | SslProtocols.Tls11: return(SSLType.AuthTLSv11); case SslProtocols.Ssl3 | SslProtocols.Tls12: return(SSLType.AuthTLSv12); case SslProtocols.Ssl3: return(SSLType.AuthSSL); case SslProtocols.Ssl2: return(SSLType.ImplicitSSL); default: case SslProtocols.None: return(SSLType.Unsecure); } }
/// <summary> /// Generates valid C# code for this connection profile. /// </summary> /// <returns></returns> public string ToCode() { var sb = new StringBuilder(); sb.AppendLine("// add this above your namespace declaration"); sb.AppendLine("using FluentFTP;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Net;"); sb.AppendLine("using System.Security.Authentication;"); sb.AppendLine(); sb.AppendLine("// add this to create and configure the FTP client"); sb.AppendLine("var client = new FtpClient();"); sb.AppendLine("client.Host = " + Host.EscapeStringLiteral() + ";"); sb.AppendLine("client.Credentials = new NetworkCredential(" + Credentials.UserName.EscapeStringLiteral() + ", " + Credentials.Password.EscapeStringLiteral() + ");"); sb.Append("client.EncryptionMode = FtpEncryptionMode."); sb.Append(Encryption.ToString()); sb.AppendLine(";"); sb.Append("client.SslProtocols = SslProtocols."); sb.Append(Protocols.ToString()); sb.AppendLine(";"); sb.Append("client.DataConnectionType = FtpDataConnectionType."); sb.Append(DataConnection.ToString()); sb.AppendLine(";"); sb.Append("client.Encoding = "); // Fix #468 - Invalid code generated: Encoding = System.Text.UTF8Encoding+UTF8EncodingSealed var encoding = Encoding.ToString(); sb.Append(encoding.Contains("+") ? encoding.Substring(0, encoding.IndexOf('+')) : encoding); sb.AppendLine(";"); if (Encryption != FtpEncryptionMode.None) { sb.AppendLine("// if you want to accept any certificate then set ValidateAnyCertificate=true and delete the following event handler"); sb.AppendLine("client.ValidateCertificate += new FtpSslValidation(delegate (FtpClient control, FtpSslValidationEventArgs e) {"); sb.AppendLine(" // add your logic to test if the SSL certificate is valid (see the FAQ for examples)"); sb.AppendLine(" e.Accept = true;"); sb.AppendLine("});"); } sb.AppendLine("client.Connect();"); return(sb.ToString()); }
public static string GetSslProtocolsString(this SslProtocols prot) { Log.Start(); #if DEBUG Log.Debug("(GetSslProtocolsString) Protocol Value: {0}", prot.ToString()); #endif #if !DEBUG && VERBOSE Log.Verbose("(GetSslProtocolsString) Protocol Value: {0}", prot.ToString()); #endif var mes = ""; switch (prot) { case SslProtocols.None: return("None"); case SslProtocols.Ssl2: return("Implicit SSL"); case SslProtocols.Ssl3: return("Auth SSL"); case SslProtocols.Tls: return("Auth TLS v1.0"); case SslProtocols.Tls11: return("Auth TLS v1.1"); case SslProtocols.Tls12: return("Auth TLS v1.2"); case SslProtocols.Default: return("Unknown"); } return(mes); }
public static string ToString(SslProtocols value) { if (value == DontPassAnything) { return("DontPassAnything"); } if (value == Tls12) { return("Tls12"); } if (value == Tls11) { return("Tls11s"); } return(value.ToString()); }
/// <summary> /// Generates valid C# code for this connection profile. /// </summary> /// <returns></returns> public string ToCode() { var sb = new StringBuilder(); sb.AppendLine("// add this above your namespace declaration"); sb.AppendLine("using FluentFTP;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Net;"); sb.AppendLine("using System.Security.Authentication;"); sb.AppendLine(); sb.AppendLine("// add this to create and configure the FTP client"); sb.AppendLine("var client = new FtpClient();"); sb.AppendLine("client.Host = " + Host.EscapeStringLiteral() + ";"); sb.AppendLine("client.Credentials = new NetworkCredential(" + Credentials.UserName.EscapeStringLiteral() + ", " + Credentials.Password.EscapeStringLiteral() + ");"); sb.Append("client.EncryptionMode = FtpEncryptionMode."); sb.Append(Encryption.ToString()); sb.AppendLine(";"); sb.Append("client.SslProtocols = SslProtocols."); sb.Append(Protocols.ToString()); sb.AppendLine(";"); sb.Append("client.DataConnectionType = FtpDataConnectionType."); sb.Append(DataConnection.ToString()); sb.AppendLine(";"); sb.Append("client.Encoding = "); sb.Append(Encoding.ToString()); sb.AppendLine(";"); if (Encryption != FtpEncryptionMode.None) { sb.AppendLine("client.ValidateCertificate += new FtpSslValidation(delegate (FtpClient control, FtpSslValidationEventArgs e) {"); sb.AppendLine(" // add your logic to test if the SSL certificate is valid (see the FAQ for examples)"); sb.AppendLine(" e.Accept = true;"); sb.AppendLine("});"); } sb.AppendLine("client.Connect();"); return(sb.ToString()); }
private static string SslProtocolToString(SslProtocols protocol) { switch (protocol) { case SslProtocols.Tls: return("TLS v1.0"); case SslProtocols.Tls11: return("TLS v1.1"); case SslProtocols.Tls12: return("TLS v1.2"); default: return(protocol.ToString()); } }
private SSLContext GetSSLContext() { string protocol; if (SslProtocols == SslProtocols.Tls11) { protocol = "TLSv1.1"; } else if (SslProtocols == SslProtocols.Tls || SslProtocols == SslProtocols.Tls12) { protocol = "TLSv1.2"; } else { throw new IOException("unsupported ssl protocol: " + SslProtocols.ToString()); } SSLContext ctx = SSLContext.GetInstance(protocol); ctx.Init(keyManagers, trustManagers, null); return(ctx); }
/// <summary> /// Generates valid C# code for this connection profile. /// </summary> /// <returns></returns> public string ToCode() { var sb = new StringBuilder(); sb.AppendLine("// add this above your namespace declaration"); sb.AppendLine("using FluentFTP;"); sb.AppendLine("using System.Text;"); sb.AppendLine("using System.Net;"); sb.AppendLine("using System.Security.Authentication;"); sb.AppendLine(); sb.AppendLine("// add this to create and configure the FTP client"); sb.AppendLine("var client = new FtpClient();"); // use LoadProfile rather than setting each property manually // this also allows us to use the high level properties like Timeout without // setting each Timeout individually sb.AppendLine("client.LoadProfile(new FtpProfile {"); sb.AppendLine(" Host = " + Host.EscapeStringLiteral() + ","); sb.AppendLine(" Credentials = new NetworkCredential(" + Credentials.UserName.EscapeStringLiteral() + ", " + Credentials.Password.EscapeStringLiteral() + "),"); sb.AppendLine(" Encryption = FtpEncryptionMode." + Encryption.ToString() + ","); sb.AppendLine(" Protocols = SslProtocols." + Protocols.ToString() + ","); sb.AppendLine(" DataConnection = FtpDataConnectionType." + DataConnection.ToString() + ","); // Fix #468 - Invalid code generated: Encoding = System.Text.UTF8Encoding+UTF8EncodingSealed var encoding = Encoding.ToString(); if (encoding.Contains("+")) { sb.AppendLine(" Encoding = " + encoding.Substring(0, encoding.IndexOf('+')) + ","); } else { sb.AppendLine(" Encoding = " + encoding + ","); } // Required for #533 - Auto detect Azure servers and use working settings if (Timeout != 0) { sb.AppendLine(" Timeout = " + Timeout + ","); } if (SocketPollInterval != 0) { sb.AppendLine(" SocketPollInterval = " + SocketPollInterval + ","); } if (RetryAttempts != 0) { sb.AppendLine(" RetryAttempts = " + RetryAttempts + ","); } sb.AppendLine("});"); if (Encryption != FtpEncryptionMode.None) { sb.AppendLine("// if you want to accept any certificate then set ValidateAnyCertificate=true and delete the following event handler"); sb.AppendLine("client.ValidateCertificate += new FtpSslValidation(delegate (FtpClient control, FtpSslValidationEventArgs e) {"); sb.AppendLine(" // add your logic to test if the SSL certificate is valid (see the FAQ for examples)"); sb.AppendLine(" e.Accept = true;"); sb.AppendLine("});"); } sb.AppendLine("client.Connect();"); return(sb.ToString()); }
static string ProtocolToString(SslProtocols?protocol) { return((protocol?.ToString() ?? "null").Replace(", ", "-")); }
public void SetVersion(SslProtocols version) { m_conversationModel.Version = version.ToString(); }
void RunOneClientConnection(string name, TcpClient client, X509Certificate cert, SslProtocols protocols) { bool doSsl = true; Console.WriteLine($"{name}: Connected!"); client.NoDelay = true; try { //NetworkStream networkStream = client.GetStream(); Stream stream = client.GetStream(); string replyword = name + "-???"; // THIS WILL BE OVER-WRITTEN if (doSsl) { SslStream ssl = new SslStream(stream); ssl.AuthenticateAsServer(cert, false, protocols, false); stream = ssl; replyword = ssl.SslProtocol.ToString(); } byte[] buffer = new byte[10]; int i; Console.WriteLine($"{name}: READ STRING"); string data = ""; while (!data.Contains("\r\n") && (i = stream.Read(buffer, 0, buffer.Length)) != 0) { data = data + System.Text.Encoding.ASCII.GetString(buffer, 0, i); Console.WriteLine($"GOT PARTIAL STRING: {data}"); } Console.WriteLine($"{name}: GOT STRING: {data}"); string reply = $"HTTP/1.0 200 OK\r\n\r\n{replyword} SuperSimpleSocketTlsVersionServer TLS {protocols.ToString()}"; Console.WriteLine($"{name}: REPLY: {reply}"); var replyBuffer = System.Text.Encoding.ASCII.GetBytes(reply); stream.Write(replyBuffer, 0, replyBuffer.Length); stream.Close(); // closes the networkstream and the underlying sslstream as needed client.Close(); // closes the socket, too } catch (Exception e) { Console.WriteLine($"{name}: SOCKET ERROR: Exception: {e.Message}"); client.Close(); } Console.WriteLine($"{name}: Closing Connection"); }
/// <summary> /// 使用HttpWebRequest 以GET方式取得資料 - By Sam /// </summary> /// <param name="url"></param> /// <param name="nc"></param> /// <param name="urlencode">是否要將參數以UrlEncode編碼</param> /// <returns></returns> public string http_web_request_data_by_get_IgnoreServerCertificate(string url, System.Collections.Specialized.NameValueCollection nc = null, bool urlencode = false) { var parameters = new System.Text.StringBuilder(); string strCallTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); string mykey = string.Empty; string myvalue = string.Empty; try { //準備將NameValueCollection轉換成NameValue字串 if (nc != null) { foreach (string key in nc.Keys) { mykey = (true == urlencode) ? HttpUtility.UrlEncode(key) : key; myvalue = (true == urlencode) ? HttpUtility.UrlEncode(nc[key]) : nc[key]; parameters.AppendFormat("{0}={1}&", mykey, myvalue); } if (parameters.Length >= 1) { parameters.Length -= 1; //去掉最後的「&」號 } //把完整網址兜出來 url += "?" + parameters.ToString(); } //ASP.NET解決停用SSL 3.0、TLS 1.0的方案 //For Net Framework 4.0 (必需安裝.net framework 4.5) //參考:http://slashlook.com/articles_20180604.html if (cbxTLSVersion.SelectedItem.ToString() == "TLS1.1") { System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType) 768; } else if (cbxTLSVersion.SelectedItem.ToString() == "TLS1.2") { System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType) 3072; } else if (cbxTLSVersion.SelectedItem.ToString() == "SSL3/TLS1.0/TLS1.1/TLS1.2") { System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType) 768 | (System.Net.SecurityProtocolType) 3072 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; } else //Default { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls; } var securityProtocolVersion = System.Net.ServicePointManager.SecurityProtocol; lblTLSVersion.Text = "Client Support TLS Version: " + securityProtocolVersion.ToString(); //.net Framework 4.5 才有支援 Tls 1.2 //設定 HTTPS 連線時,不要理會憑證的有效性問題,參考自:https://eric0806.blogspot.tw/2011/05/webrequest.html ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; //建立WebRequest元件 System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); //lblDesc.Text = string.Format("The HttpHeaders are Name Value {0}", myRequest.Headers); myRequest.Method = "GET"; //myRequest.ContentType = "application/json"; myRequest.Accept = "application/json"; myRequest.KeepAlive = false; // 不要keep connection , 調整support protocol 在測試才看的出來重新連線後選擇的 protocol //讀取資料 System.Net.HttpWebResponse wr = (System.Net.HttpWebResponse)myRequest.GetResponse(); System.IO.Stream resStream = wr.GetResponseStream(); System.IO.StreamReader sr = new System.IO.StreamReader(resStream); System.Text.StringBuilder output = new System.Text.StringBuilder(); //lblDesc.Text += string.Format("The Response Headers are Name Value {0}", wr.Headers); //What TLS Version used SslProtocols ssp = ExtractSslProtocol(resStream); lblTLSVersion.Text += " Negotiate Protocols = " + ssp.ToString(); output.Append(sr.ReadToEnd()); sr.Close(); return(output.ToString()); } catch (Exception ex) { throw ex; } }