Skip to content

TLS 1.0 support for .NET 3.x on Unity 2018.2+ #443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 54 additions & 53 deletions Scripts/Connection/WSConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
using IBM.Watson.DeveloperCloud.Utilities;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
#if UNITY_2018_2_OR_NEWER
using System.Security.Authentication;
#endif

using System.Threading;
#if !NETFX_CORE
using UnitySDK.WebSocketSharp;
#else
Expand Down Expand Up @@ -193,6 +190,8 @@ public Dictionary<string, string> Headers {
private AutoResetEvent _receiveEvent = new AutoResetEvent(false);
private Queue<Message> _receiveQueue = new Queue<Message>();
private int _receiverRoutine = 0;
private static readonly string https = "https://";
private static readonly string wss = "wss://";
#endregion

/// <summary>
Expand All @@ -203,78 +202,76 @@ public Dictionary<string, string> Headers {
public static string FixupURL(string URL)
{
#if UNITY_2018_2_OR_NEWER
#if NET_4_6
// Use standard endpoints since 2018.2 supports TLS 1.2
if (URL.StartsWith("http://stream."))
{
URL = URL.Replace("http://stream.", "ws://stream.");
}
else if (URL.StartsWith("https://stream."))
if (URL.StartsWith("https://stream."))
{
URL = URL.Replace("https://stream.", "wss://stream.");
}

// TLS 1.0 endpoint
else if (URL.StartsWith("http://stream-tls10."))
{
URL = URL.Replace("http://stream-tls10.", "ws://stream.");
}
// TLS 1.0 endpoint - Do not change this to TLS 1.2 endpoint since
// users may need to use the TLS 1.0 endpoint because of different
// platforms.
else if (URL.StartsWith("https://stream-tls10."))
{
URL = URL.Replace("https://stream-tls10.", "wss://stream.");
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
}

// Germany
else if (URL.StartsWith("http://gateway-fra."))
{
URL = URL.Replace("http://gateway-fra.", "ws://stream-fra.");
}
else if (URL.StartsWith("https://gateway-fra."))
{
URL = URL.Replace("https://gateway-fra.", "wss://stream-fra.");
}

// US East
else if (URL.StartsWith("http://gateway-wdc."))
{
URL = URL.Replace("http://gateway-wdc.", "ws://gateway-wdc.");
}
else if (URL.StartsWith("https://gateway-wdc."))
{
URL = URL.Replace("https://gateway-wdc.", "wss://gateway-wdc.");
}


// Sydney
else if (URL.StartsWith("http://gateway-syd."))
{
URL = URL.Replace("http://gateway-syd.", "ws://gateway-syd.");
}
else if (URL.StartsWith("https://gateway-syd."))
{
URL = URL.Replace("https://gateway-syd.", "wss://gateway-syd.");
}

else
{
Log.Warning("WSConnector", "No case for URL for wss://. Leaving URL unchanged.");
URL = URL.Replace(https, wss);
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
}
#else
// Use TLS 1.0 endpoint if user is on .NET 3.5. US South is the
// only region that supports this endpoint.
if (URL.StartsWith("https://stream."))
{
URL = URL.Replace("https://stream.", "wss://stream-tls10.");
}
else if (URL.StartsWith("https://stream-tls10."))
{
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
}
else
{
URL = URL.Replace(https, wss);
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
Log.Warning("WSConnector", "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version).");
}
#endif
#else
// Redirect to TLS 1.0 endpoints.
// Note frankfurt endpoint does not support TLS 1.0.
if (URL.StartsWith("http://stream."))
URL = URL.Replace("http://stream.", "ws://stream-tls10.");
else if (URL.StartsWith("https://stream."))
// Use TLS 1.0 endpoint if user is on .NET 3.5 or 4.6 if using Unity 2018.1 or older.
// US South is the only region that supports this endpoint.
if (URL.StartsWith("https://stream."))
{
URL = URL.Replace("https://stream.", "wss://stream-tls10.");
else if (URL.StartsWith("http://stream-tls10."))
URL = URL.Replace("http://stream-tls10.", "ws://stream-tls10.");
}
else if (URL.StartsWith("https://stream-tls10."))
{
URL = URL.Replace("https://stream-tls10.", "wss://stream-tls10.");
else if (URL.StartsWith("http://stream-fra."))
URL = URL.Replace("http://stream-fra.", "ws://stream-fra.");
else if (URL.StartsWith("https://stream-fra."))
URL = URL.Replace("https://stream-fra.", "wss://stream-fra.");
}
else
{
URL = URL.Replace(https, wss);
Log.Warning("WSConnector", "No case for URL for wss://. Replacing https:// with wss://.");
Log.Warning("WSConnector", "Streaming with TLS 1.0 is only available in US South. Please create your Speech to Text instance in US South. Alternatviely, use Unity 2018.2 with .NET 4.x Scripting Runtime Version enabled (File > Build Settings > Player Settings > Other Settings > Scripting Runtime Version).");
}
#endif

return URL;
}

Expand Down Expand Up @@ -307,7 +304,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
return connector;
}

#region Public Functions
#region Public Functions
/// <summary>
/// This function sends the given message object.
/// </summary>
Expand Down Expand Up @@ -359,9 +356,9 @@ public void Close()
// setting the state to closed will make the SendThread automatically exit.
_connectionState = ConnectionState.CLOSED;
}
#endregion
#endregion

#region Private Functions
#region Private Functions
private IEnumerator ProcessReceiveQueue()
{
while (_connectionState == ConnectionState.CONNECTED
Expand Down Expand Up @@ -392,10 +389,10 @@ private IEnumerator ProcessReceiveQueue()
if (OnClose != null)
OnClose(this);
}
#endregion
#endregion

#region Threaded Functions
// NOTE: ALl functions in this region are operating in a background thread, do NOT call any Unity functions!
#region Threaded Functions
// NOTE: All functions in this region are operating in a background thread, do NOT call any Unity functions!
#if !NETFX_CORE
private void SendMessages()
{
Expand All @@ -412,8 +409,12 @@ private void SendMessages()
ws.OnClose += OnWSClose;
ws.OnError += OnWSError;
ws.OnMessage += OnWSMessage;
#if UNITY_2018_2_OR_NEWER
#if NET_4_6
// Enable TLS 1.1 and TLS 1.2 if we are on .NET 4.x
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
#else
// .NET 3.x does not support TLS 1.1 or TLS 1.2
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls;
#endif
ws.Connect();

Expand Down Expand Up @@ -589,6 +590,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
}
}
#endif
#endregion
#endregion
}
}
3 changes: 0 additions & 3 deletions Scripts/UnitTests/TestDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class TestDiscovery : UnitTest
private bool _deleteConfigurationTested = false;
private bool _isEnvironmentReady = false;
private bool _deleteUserDataTested = false;
private bool _readyToContinue = false;

private bool _listCredentialsTested = false;
private bool _createCredentialsTested = false;
Expand Down Expand Up @@ -332,7 +331,6 @@ public override IEnumerator RunTest()
while (!_isEnvironmentReady)
yield return null;

_readyToContinue = false;
// Delete Collection
Log.Debug("TestDiscovery.RunTest()", "Attempting to delete collection {0}", _createdCollectionId);
if (!_discovery.DeleteCollection(OnDeleteCollection, OnFail, _environmentId, _createdCollectionId))
Expand All @@ -345,7 +343,6 @@ public override IEnumerator RunTest()
while (!_isEnvironmentReady)
yield return null;

_readyToContinue = false;
// Delete Configuration
Log.Debug("TestDiscovery.RunTest()", "Attempting to delete configuration {0}", _createdConfigurationID);
if (!_discovery.DeleteConfiguration(OnDeleteConfiguration, OnFail, _environmentId, _createdConfigurationID))
Expand Down