Skip to content

Commit dd06836

Browse files
committed
feat(Proxy): Enabled proxy on Unity 2018.11 or earlier if DisableSslVerification is set to true
1 parent f4a7c5e commit dd06836

File tree

3 files changed

+86
-21
lines changed

3 files changed

+86
-21
lines changed

Scripts/Connection/RESTConnector.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,23 @@ public Request()
260260
public bool DisableSslVerification
261261
{
262262
get { return disableSslVerification; }
263-
set { disableSslVerification = value; }
263+
set
264+
{
265+
#if UNITY_2018_2_12_OR_NEWER
266+
Log.Warning("WSConnector", "Please use Unity 2018.2.11 or earlier to disable ssl verification.")
267+
#else
268+
disableSslVerification = value;
269+
270+
if (disableSslVerification)
271+
{
272+
Network.useProxy = true;
273+
}
274+
else
275+
{
276+
Network.useProxy = false;
277+
}
278+
#endif
279+
}
264280
}
265281
#endregion
266282
}
@@ -323,12 +339,6 @@ public bool Send(Request request)
323339
throw new ArgumentNullException("request");
324340
}
325341

326-
#if !NETFX_CORE
327-
if (request.DisableSslVerification)
328-
{
329-
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
330-
}
331-
#endif
332342
_requests.Enqueue(request);
333343

334344
// if we are not already running a co-routine to send the Requests
@@ -529,6 +539,15 @@ private IEnumerator ProcessRequestQueue()
529539

530540
unityWebRequest.downloadHandler = new DownloadHandlerBuffer();
531541

542+
if (req.DisableSslVerification)
543+
{
544+
unityWebRequest.certificateHandler = new AcceptAllCertificates();
545+
}
546+
else
547+
{
548+
unityWebRequest.certificateHandler = null;
549+
}
550+
532551
#if UNITY_2017_2_OR_NEWER
533552
unityWebRequest.SendWebRequest();
534553
#else
@@ -647,10 +666,18 @@ private IEnumerator ProcessRequestQueue()
647666
unityWebRequest.Dispose();
648667
}
649668

650-
// reduce the connection count before we exit..
669+
// reduce the connection count before we exit.
651670
_activeConnections -= 1;
652671
yield break;
653672
}
654673
#endregion
655674
}
675+
676+
class AcceptAllCertificates : CertificateHandler
677+
{
678+
protected override bool ValidateCertificate(byte[] certificateData)
679+
{
680+
return true;
681+
}
682+
}
656683
}

Scripts/Connection/WSConnector.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Collections.Generic;
2525
using System.Security.Authentication;
2626
using System.Threading;
27+
using UnityEngine;
2728
#if !NETFX_CORE
2829
using UnitySDK.WebSocketSharp;
2930
#else
@@ -185,11 +186,27 @@ public Dictionary<string, string> Headers
185186
public bool DisableSslVerification
186187
{
187188
get { return disableSslVerification; }
188-
set { disableSslVerification = value; }
189+
set
190+
{
191+
#if UNITY_2018_2_12_OR_NEWER
192+
Log.Warning("WSConnector", "Please use Unity 2018.2.11 or earlier to disable ssl verification.")
193+
#else
194+
disableSslVerification = value;
195+
196+
if (disableSslVerification)
197+
{
198+
Network.useProxy = true;
199+
}
200+
else
201+
{
202+
Network.useProxy = false;
203+
}
204+
#endif
205+
}
189206
}
190-
#endregion
207+
#endregion
191208

192-
#region Private Data
209+
#region Private Data
193210
private ConnectionState _connectionState = ConnectionState.CLOSED;
194211
#if !NETFX_CORE
195212
private Thread _sendThread = null;
@@ -203,7 +220,7 @@ public bool DisableSslVerification
203220
private int _receiverRoutine = 0;
204221
private static readonly string https = "https://";
205222
private static readonly string wss = "wss://";
206-
#endregion
223+
#endregion
207224

208225
/// <summary>
209226
/// Helper function to convert a HTTP/HTTPS url into a WS/WSS URL.
@@ -315,7 +332,7 @@ public static WSConnector CreateConnector(Credentials credentials, string functi
315332
return connector;
316333
}
317334

318-
#region Public Functions
335+
#region Public Functions
319336
/// <summary>
320337
/// This function sends the given message object.
321338
/// </summary>
@@ -367,9 +384,9 @@ public void Close()
367384
// setting the state to closed will make the SendThread automatically exit.
368385
_connectionState = ConnectionState.CLOSED;
369386
}
370-
#endregion
387+
#endregion
371388

372-
#region Private Functions
389+
#region Private Functions
373390
private IEnumerator ProcessReceiveQueue()
374391
{
375392
while (_connectionState == ConnectionState.CONNECTED
@@ -400,9 +417,9 @@ private IEnumerator ProcessReceiveQueue()
400417
if (OnClose != null)
401418
OnClose(this);
402419
}
403-
#endregion
420+
#endregion
404421

405-
#region Threaded Functions
422+
#region Threaded Functions
406423
// NOTE: All functions in this region are operating in a background thread, do NOT call any Unity functions!
407424
#if !NETFX_CORE
408425
private void SendMessages()
@@ -428,9 +445,13 @@ private void SendMessages()
428445
return true;
429446
};
430447
}
448+
else
449+
{
450+
ws.SslConfiguration.ServerCertificateValidationCallback = null;
451+
}
431452
#if NET_4_6
432453
// Enable TLS 1.1 and TLS 1.2 if we are on .NET 4.x
433-
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
454+
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls | SslProtocols.None;
434455
#else
435456
// .NET 3.x does not support TLS 1.1 or TLS 1.2
436457
ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls;
@@ -609,6 +630,6 @@ private void WebSocket_MessageReceived(MessageWebSocket sender, MessageWebSocket
609630
}
610631
}
611632
#endif
612-
#endregion
633+
#endregion
613634
}
614635
}

Scripts/Utilities/Credentials.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System;
2222
using System.Collections.Generic;
2323
using System.Text;
24+
using UnityEngine;
2425
using UnityEngine.Networking;
2526

2627
namespace IBM.Watson.DeveloperCloud.Utilities
@@ -87,12 +88,28 @@ public IamTokenData TokenData
8788
private IamTokenData _tokenData = null;
8889
private bool disableSslVerification = false;
8990
/// <summary>
90-
/// Gets and sets the option to disable ssl verification
91+
/// Gets and sets the option to disable ssl verification for getting an IAM token.
9192
/// </summary>
9293
public bool DisableSslVerification
9394
{
9495
get { return disableSslVerification; }
95-
set { disableSslVerification = value; }
96+
set
97+
{
98+
#if UNITY_2018_2_12_OR_NEWER
99+
Log.Warning("WSConnector", "Please use Unity 2018.2.11 or earlier to disable ssl verification.")
100+
#else
101+
disableSslVerification = value;
102+
103+
if (disableSslVerification)
104+
{
105+
Network.useProxy = true;
106+
}
107+
else
108+
{
109+
Network.useProxy = false;
110+
}
111+
#endif
112+
}
96113
}
97114
#endregion
98115

0 commit comments

Comments
 (0)